From 862d91ed0acb6897632c64a7e89c7951db3f8094 Mon Sep 17 00:00:00 2001 From: shove Date: Mon, 5 Sep 2022 23:58:30 +0800 Subject: [PATCH] checker: check for name conflicts between const and __global variables (fix #15668) (#15669) --- vlib/v/checker/checker.v | 3 +++ vlib/v/checker/tests/globals/name_conflict_with_const.out | 6 ++++++ vlib/v/checker/tests/globals/name_conflict_with_const.vv | 7 +++++++ 3 files changed, 16 insertions(+) create mode 100644 vlib/v/checker/tests/globals/name_conflict_with_const.out create mode 100644 vlib/v/checker/tests/globals/name_conflict_with_const.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 41a81f0af..1efda4a89 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1656,6 +1656,9 @@ fn (mut c Checker) global_decl(mut node ast.GlobalDecl) { if field.name in c.global_names { c.error('duplicate global `$field.name`', field.pos) } + if '${c.mod}.$field.name' in c.const_names { + c.error('duplicate global and const `$field.name`', field.pos) + } sym := c.table.sym(field.typ) if sym.kind == .placeholder { c.error('unknown type `$sym.name`', field.typ_pos) diff --git a/vlib/v/checker/tests/globals/name_conflict_with_const.out b/vlib/v/checker/tests/globals/name_conflict_with_const.out new file mode 100644 index 000000000..031a0400a --- /dev/null +++ b/vlib/v/checker/tests/globals/name_conflict_with_const.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/globals/name_conflict_with_const.vv:6:2: error: duplicate global and const `foo` + 4 | + 5 | __global ( + 6 | foo = 123 + | ~~~ + 7 | ) diff --git a/vlib/v/checker/tests/globals/name_conflict_with_const.vv b/vlib/v/checker/tests/globals/name_conflict_with_const.vv new file mode 100644 index 000000000..4fa5c4389 --- /dev/null +++ b/vlib/v/checker/tests/globals/name_conflict_with_const.vv @@ -0,0 +1,7 @@ +const ( + foo = 'abc' +) + +__global ( + foo = 123 +) -- 2.30.2