parser: check assign expr with undefined variables of struct_init (#13495)

pull/13496/head
yuyi 2022-02-17 15:21:03 +08:00 committed by GitHub
parent 89b99ad4c3
commit 842fd7a27e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 2 deletions

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/assign_expr_undefined_err_l.vv:29:6: error: undefined variable: `poo`
27 | poo := Stru{
28 | a: 123
29 | i: poo.result()
| ~~~
30 | }
31 | println('$poo')

View File

@ -0,0 +1,32 @@
module main
struct Stru {
mut:
a int
i Iface2
}
interface Iface {
result() &Iface2
}
interface Iface2 {
result2() Iface
}
pub fn (s Stru)result() &Iface2 {
return &Stru{}
}
pub fn (s Stru)result2() Iface {
return &Stru{}
}
fn main() {
println('hello world')
poo := Stru{
a: 123
i: poo.result()
}
println('$poo')
}

View File

@ -18,7 +18,7 @@ fn (mut p Parser) assign_stmt() ast.Stmt {
return p.partial_assign_stmt(exprs, comments) return p.partial_assign_stmt(exprs, comments)
} }
const max_expr_level = 310 const max_expr_level = 100
fn (mut p Parser) check_undefined_variables(exprs []ast.Expr, val ast.Expr) ? { fn (mut p Parser) check_undefined_variables(exprs []ast.Expr, val ast.Expr) ? {
p.expr_level++ p.expr_level++
@ -85,6 +85,12 @@ fn (mut p Parser) check_undefined_variables(exprs []ast.Expr, val ast.Expr) ? {
p.check_undefined_variables(exprs, expr_) ? p.check_undefined_variables(exprs, expr_) ?
} }
} }
ast.StructInit {
fields := val.fields.clone()
for field in fields {
p.check_undefined_variables(exprs, field.expr) ?
}
}
else {} else {}
} }
} }

File diff suppressed because one or more lines are too long