ast: use multi return optional for find_scope_and_var

pull/4161/head
joe-conigliaro 2020-03-31 15:39:14 +11:00
parent 71ca553190
commit 602bc06bee
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
2 changed files with 4 additions and 19 deletions
vlib/v

View File

@ -21,23 +21,13 @@ pub fn new_scope(parent &Scope, start_pos int) &Scope {
} }
} }
pub struct ScopeVar { pub fn (s &Scope) find_scope_and_var(name string) ?(&Scope,Var) {
pub:
scope &Scope
var Var
}
// pub fn (s &Scope) find_scope_and_var(name string) ?(&Scope,Var) {
pub fn (s &Scope) find_scope_and_var(name string) ?ScopeVar {
if name in s.vars { if name in s.vars {
// return s,s.vars[name] return s,s.vars[name]
return ScopeVar{
s,s.vars[name]}
} }
for sc := s; !isnil(sc.parent); sc = sc.parent { for sc := s; !isnil(sc.parent); sc = sc.parent {
if name in sc.vars { if name in sc.vars {
return ScopeVar{ return sc,sc.vars[name]
sc,sc.vars[name]}
} }
} }
return none return none

View File

@ -809,16 +809,11 @@ pub fn (c mut Checker) ident(ident mut ast.Ident) table.Type {
} }
start_scope := c.file.scope.innermost(ident.pos.pos) start_scope := c.file.scope.innermost(ident.pos.pos)
mut found := true mut found := true
mut var_scope := &ast.Scope(0) var_scope,var := start_scope.find_scope_and_var(ident.name) or {
mut var := ast.Var{}
// var_scope,var = start_scope.find_scope_and_var(ident.name) or {
mr := start_scope.find_scope_and_var(ident.name) or {
found = false found = false
c.error('not found: $ident.name - POS: $ident.pos.pos', ident.pos) c.error('not found: $ident.name - POS: $ident.pos.pos', ident.pos)
panic('') panic('')
} }
var_scope = mr.scope
var = mr.var
if found { if found {
// update the variable // update the variable
// we need to do this here instead of var_decl since some // we need to do this here instead of var_decl since some