From 5ce9a9e55c8deba4a2d4da4401debf1302a4631e Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Tue, 2 Apr 2024 19:46:18 +0100 Subject: [PATCH] Improve error for duplicate declaration due to homograph. Fixes #875 --- src/names.c | 12 +++++++++++- test/parse/issue875.vhd | 19 +++++++++++++++++++ test/test_parse.c | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 test/parse/issue875.vhd diff --git a/src/names.c b/src/names.c index 4e99b053..8619577b 100644 --- a/src/names.c +++ b/src/names.c @@ -862,7 +862,17 @@ static symbol_t *make_visible(scope_t *s, ident_t name, tree_t decl, } else if (dd->origin == origin && !type_is_none(type)) { diag_t *d = diag_new(DIAG_ERROR, tree_loc(decl)); - diag_printf(d, "%s already declared in this region", type_pp(type)); + if (type_strict_eq(tree_type(dd->tree), type)) + diag_printf(d, "%s already declared in this region", + type_pp(type)); + else { + diag_printf(d, "homograph of %s already declared in this region", + type_pp(type)); + diag_hint(d, NULL, "only the base type is considered when " + "determining if two overloads have the same parameter " + "type profile"); + } + diag_hint(d, tree_loc(dd->tree), "previous declaration was here"); diag_hint(d, tree_loc(decl), "duplicate declaration"); diag_emit(d); diff --git a/test/parse/issue875.vhd b/test/parse/issue875.vhd new file mode 100644 index 00000000..75ba0d33 --- /dev/null +++ b/test/parse/issue875.vhd @@ -0,0 +1,19 @@ +package repro is + subtype instruction32_t is bit_vector(31 downto 0); + subtype opcode32_t is bit_vector(6 downto 0); + + function has_rd(op : opcode32_t) return boolean; + function has_rd(ins : instruction32_t) return boolean; +end; + +package body repro is + function has_rd(op : opcode32_t) return boolean is + begin + return false; + end function; + + function has_rd(ins : instruction32_t) return boolean is + begin + return true; + end function; +end package body; diff --git a/test/test_parse.c b/test/test_parse.c index 9eab3182..56d9c3e9 100644 --- a/test/test_parse.c +++ b/test/test_parse.c @@ -6254,6 +6254,38 @@ START_TEST(test_aggregate) } END_TEST +START_TEST(test_issue875) +{ + input_from_file(TESTDIR "/parse/issue875.vhd"); + + const error_t expect[] = { + { 6, "homograph of HAS_RD [INSTRUCTION32_T return BOOLEAN] already " + "declared in this region" }, + { 0, "duplicate declaration" }, + { 0, "only the base type is considered when determining" }, + { 0, "previous declaration was here" }, + { 9, "design unit depends on WORK.REPRO which was analysed " + "with errors" }, + { -1, NULL } + }; + expect_errors(expect); + + tree_t p = parse(); + fail_if(p == NULL); + fail_unless(tree_kind(p) == T_PACKAGE); + lib_put_error(lib_work(), p); + + tree_t b = parse(); + fail_if(b == NULL); + fail_unless(tree_kind(b) == T_PACK_BODY); + lib_put(lib_work(), b); + + fail_unless(parse() == NULL); + + check_expected_errors(); +} +END_TEST + Suite *get_parse_tests(void) { Suite *s = suite_create("parse"); @@ -6393,6 +6425,7 @@ Suite *get_parse_tests(void) tcase_add_test(tc_core, test_issue848); tcase_add_test(tc_core, test_issue870); tcase_add_test(tc_core, test_aggregate); + tcase_add_test(tc_core, test_issue875); suite_add_tcase(s, tc_core); return s; -- 2.39.2