Skip to content

Commit

Permalink
checker: fix anon fn using global variable (fix #15004) (#15008)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Jul 10, 2022
1 parent 7d0a918 commit 64eab72
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
8 changes: 8 additions & 0 deletions vlib/v/checker/checker.v
Expand Up @@ -2771,6 +2771,14 @@ pub fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
}
if mut obj := c.file.global_scope.find(name) {
match mut obj {
ast.GlobalField {
node.kind = .global
node.info = ast.IdentVar{
typ: obj.typ
}
node.obj = obj
return obj.typ
}
ast.ConstField {
if !(obj.is_pub || obj.mod == c.mod || c.pref.is_test) {
c.error('constant `$obj.name` is private', node.pos)
Expand Down
@@ -0,0 +1 @@
123
16 changes: 16 additions & 0 deletions vlib/v/checker/tests/globals_run/global_var_in_anon_fn.vv
@@ -0,0 +1,16 @@
module main

__global (
number int
)

fn init() {
number = 123
}

fn main() {
f1 := fn() {
println(number)
}
f1()
}

0 comments on commit 64eab72

Please sign in to comment.