diff --git a/vlib/v/checker/comptime.v b/vlib/v/checker/comptime.v index 0a07ab2aec..4dd5ec3af7 100644 --- a/vlib/v/checker/comptime.v +++ b/vlib/v/checker/comptime.v @@ -37,6 +37,7 @@ fn (mut c Checker) comptime_call(mut node ast.ComptimeCall) ast.Type { } mut c2 := new_checker(c.table, pref2) c2.check(node.vweb_tmpl) + mut caller_scope := c.fn_scope.innermost(node.pos.pos) mut i := 0 // tmp counter var for skipping first three tmpl vars for k, _ in c2.file.scope.children[0].objects { if i < 2 { @@ -44,10 +45,17 @@ fn (mut c Checker) comptime_call(mut node ast.ComptimeCall) ast.Type { i++ continue } - if k in c.fn_scope.objects && unsafe { c.fn_scope.objects[k] } is ast.Var { - mut vsc := unsafe { c.fn_scope.objects[k] } as ast.Var - vsc.is_used = true - c.fn_scope.objects[k] = vsc + tmpl_obj := unsafe { c2.file.scope.children[0].objects[k] } + if tmpl_obj is ast.Var { + if mut caller_var := caller_scope.find_var(tmpl_obj.name) { + // var is used in the tmpl so mark it as used in the caller + caller_var.is_used = true + // update props from the caller scope var to the tmpl scope var + c2.file.scope.children[0].objects[k] = ast.Var{ + ...(*caller_var) + pos: tmpl_obj.pos + } + } } } c.warnings << c2.warnings diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 2259088cd3..3b2ed301a7 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -3665,8 +3665,10 @@ fn (mut g Gen) ident(node ast.Ident) { g.write('${name}.val') return } - scope := g.file.scope.innermost(node.pos.pos) - if v := scope.find_var(node.name) { + // TODO: investigate why node.obj is pointing to outdated ScopeObject? + // v := node.obj + // if v is ast.Var { + if v := node.scope.find_var(node.name) { is_auto_heap = v.is_auto_heap && (!g.is_assign_lhs || g.assign_op != .decl_assign) if is_auto_heap { g.write('(*(') diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 8e596bbf47..9237cc16e8 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -117,9 +117,8 @@ fn (mut g Gen) comptime_call(mut node ast.ComptimeCall) { // try to see if we need to pass a pointer if node.left is ast.Ident { - scope := g.file.scope.innermost(node.pos.pos) - if v := scope.find_var(node.left.name) { - if m.params[0].typ.is_ptr() && !v.typ.is_ptr() { + if node.left.obj is ast.Var { + if m.params[0].typ.is_ptr() && !node.left.obj.typ.is_ptr() { g.write('&') } } diff --git a/vlib/v/parser/comptime.v b/vlib/v/parser/comptime.v index 02b2a2f0ac..5a6f5858f8 100644 --- a/vlib/v/parser/comptime.v +++ b/vlib/v/parser/comptime.v @@ -260,21 +260,15 @@ fn (mut p Parser) comptime_call() ast.ComptimeCall { file.path = tmpl_path // copy vars from current fn scope into vweb_tmpl scope for stmt in file.stmts { - if stmt is ast.FnDecl { + if mut stmt is ast.FnDecl { if stmt.name == 'main.vweb_tmpl_$tmp_fn_name' { - // mut tmpl_scope := file.scope.innermost(stmt.body_pos.pos) - mut tmpl_scope := stmt.scope for _, obj in p.scope.objects { - if obj is ast.Var { - mut v := obj - v.pos = stmt.body_pos - tmpl_scope.register(ast.Var{ - ...v + if mut obj is ast.Var { + stmt.scope.register(ast.Var{ + ...obj is_used: true + pos: stmt.body_pos }) - // set the controller action var to used - // if it's unused in the template it will warn - v.is_used = true } } break