From a75a12ec1e14764bd89c180d4d038f4b5096354c Mon Sep 17 00:00:00 2001 From: shove Date: Thu, 8 Sep 2022 00:06:45 +0800 Subject: [PATCH] checker: fix const variable type error when global variable with the same name exists (fix #15686) (#15689) --- vlib/v/checker/checker.v | 12 +++++++----- vlib/v/tests/const_and_global_with_same_name_test.v | 11 +++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 vlib/v/tests/const_and_global_with_same_name_test.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 1efda4a89a..2405698217 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2739,12 +2739,14 @@ pub fn (mut c Checker) ident(mut node ast.Ident) ast.Type { if mut obj := node.scope.find(node.name) { match mut obj { ast.GlobalField { - node.kind = .global - node.info = ast.IdentVar{ - typ: obj.typ + if node.mod == '' { + node.kind = .global + node.info = ast.IdentVar{ + typ: obj.typ + } + node.obj = obj + return obj.typ } - node.obj = obj - return obj.typ } ast.Var { // inside vweb tmpl ident positions are meaningless, use the position of the comptime call. diff --git a/vlib/v/tests/const_and_global_with_same_name_test.v b/vlib/v/tests/const_and_global_with_same_name_test.v new file mode 100644 index 0000000000..11533745fc --- /dev/null +++ b/vlib/v/tests/const_and_global_with_same_name_test.v @@ -0,0 +1,11 @@ +[has_globals] +module main + +import time + +__global seconds_per_minute = 'sixty' + +fn test_const_and_globals_with_the_same_name_in_different_modules_do_not_conflict() { + assert seconds_per_minute == 'sixty' + assert time.seconds_per_minute == 60 +}