From a752292b54e22a8024b9d9162762edd2efa533b5 Mon Sep 17 00:00:00 2001 From: zinc Date: Wed, 14 Oct 2026 16:10:42 +0330 Subject: fndef: a second definition of a function in the same module is 'is defined twice' (appendix b.2.3) --- src/compiler/decl.zc | 4 ++++ tests/conformance/known.txt | 1 - tests/langerrors/fn_defined_twice.zc | 4 ++++ 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 tests/langerrors/fn_defined_twice.zc diff --git a/src/compiler/decl.zc b/src/compiler/decl.zc index 0870756..6794373 100644 --- a/src/compiler/decl.zc +++ b/src/compiler/decl.zc @@ -285,6 +285,10 @@ int fndef(int chk, int rty, int ret, int n) { if (rty || ntp) typed = 1; if (!dnm) dnm = n; s = lookup(n); + if (s >= ubase && skind[s] == K_FN && (sflag[s] & F_DEF) && !(sflag[s] & F_TPL) && !ginst && !ntp) { // appendix b.2.3: the same name in the same module + word qn = n; if (module_name) qn = module_symbol(n); + if (rt.eq_w(sname[s], qn)) serrn(n, "is defined twice"); + } if (s < 0 || s < ubase || skind[s] != K_FN || (sflag[s] & F_DEF)) { s = addsym(n, K_FN, 0); sflag[s] = 0; diff --git a/tests/conformance/known.txt b/tests/conformance/known.txt index 13ef43c..ff67f6b 100644 --- a/tests/conformance/known.txt +++ b/tests/conformance/known.txt @@ -5,7 +5,6 @@ 5-declarations/enum_member_names_earlier_b2.zc zy a appendix b.2.2 confirmed: enum members can't name earlier members 5-declarations/fn_prototype_after_definition_b4.zc zy a appendix b.2.4 confirmed: a prototype after the definition hides it 5-declarations/fn_prototype_unnamed_params_b13.zc zy a appendix b.2.13 confirmed (message now: doesn't match its earlier declaration) -5-declarations/fn_redefined_b3.zc zy a appendix b.2.3 confirmed: a plain redefinition silently shadows the first 5-declarations/global_float_init_b7.zc zy a appendix b.2.7 confirmed: float global initializer rejected 5-declarations/include_in_function_b9.zc zy a appendix b.2.9 confirmed: include in a body gives "return outside a function" 9-appendix-b/b2_05_ternary_int_float.zc zy a appendix b.2.5 confirmed diff --git a/tests/langerrors/fn_defined_twice.zc b/tests/langerrors/fn_defined_twice.zc new file mode 100644 index 0000000..7af57e8 --- /dev/null +++ b/tests/langerrors/fn_defined_twice.zc @@ -0,0 +1,4 @@ +// expect: f: is defined twice +int f() { return 1; } +int f() { return 2; } +fn main() { return f(); } -- cgit v1.2.3