ast: allow scope var shadowing

pull/4682/head
joe-conigliaro 2020-05-02 23:46:53 +10:00
parent 68ca8ab8a4
commit 483f0f408c
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
3 changed files with 7 additions and 13 deletions

View File

@ -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) {

View File

@ -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 {

View File

@ -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