From 483f0f408c2a3eba7b660440dfd81fd6ddfe3f30 Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Sat, 2 May 2020 23:46:53 +1000 Subject: [PATCH] ast: allow scope var shadowing --- vlib/v/ast/scope.v | 7 +------ vlib/v/parser/if.v | 10 +++++----- vlib/v/parser/parser.v | 3 +-- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/vlib/v/ast/scope.v b/vlib/v/ast/scope.v index 2158496376..40c085ee17 100644 --- a/vlib/v/ast/scope.v +++ b/vlib/v/ast/scope.v @@ -106,18 +106,13 @@ pub fn (s mut Scope) register(name string, obj ScopeObject) { if name == '_' { return } - if _ := s.find(name) { + if name in s.objects { // println('existing obect: $name') return } s.objects[name] = obj } -pub fn (s mut Scope) register_force(name string, obj ScopeObject) { - s.objects[name] = obj -} - - pub fn (s &Scope) outermost() &Scope { mut sc := s for !isnil(sc.parent) { diff --git a/vlib/v/parser/if.v b/vlib/v/parser/if.v index bfe7f8993f..6d3b448bf3 100644 --- a/vlib/v/parser/if.v +++ b/vlib/v/parser/if.v @@ -140,11 +140,11 @@ fn (mut p Parser) match_expr() ast.MatchExpr { if var_name != '' { // Register a shadow variable with the actual type // (this replaces the old `it`) - // TODO doesn't work right now - p.scope.register(var_name, ast.Var{ - name: var_name - typ: typ.to_ptr() - }) + // TODO doesn't work right now (fixed, uncomment when merging) + // p.scope.register(var_name, ast.Var{ + // name: var_name + // typ: typ.to_ptr() + // }) // println(var_name) } } else { diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index d593ccea16..658efeb4f4 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -757,8 +757,7 @@ fn (mut p Parser) index_expr(left ast.Expr) ast.IndexExpr { } fn (mut p Parser) scope_register_it() { - // force new 'it' even if it exists in parent scope - p.scope.register_force('it', ast.Var{ + p.scope.register('it', ast.Var{ name: 'it' pos: p.tok.position() is_used: true