mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: fix taking a closure of x, where x
may be coming from a non trivial parent scope, like for x in y {
or x,y := multi()
(fix #16141) (#16147)
This commit is contained in:
parent
a139bed785
commit
a23e06184d
@ -428,6 +428,7 @@ fn (mut c Checker) anon_fn(mut node ast.AnonFn) ast.Type {
|
||||
if var.typ.has_flag(.generic) {
|
||||
has_generic = true
|
||||
}
|
||||
node.decl.scope.update_var_type(var.name, var.typ)
|
||||
}
|
||||
c.stmts(node.decl.stmts)
|
||||
c.fn_decl(mut node.decl)
|
||||
|
@ -179,7 +179,7 @@ struct Command {
|
||||
|
||||
struct App {}
|
||||
|
||||
fn main() {
|
||||
fn test_larger_closure_parameters() {
|
||||
mut app := &App{}
|
||||
eprintln('app ptr: ${u64(app)}')
|
||||
f := fn [mut app] (cmd Command) u64 {
|
||||
@ -198,3 +198,28 @@ fn main() {
|
||||
println('> res: $res | sizeof Command: ${sizeof(Command)}')
|
||||
assert res == u64(app)
|
||||
}
|
||||
|
||||
fn test_closure_in_for_in_loop() {
|
||||
a := [2, 4, 6, 9]
|
||||
for v in a {
|
||||
func := fn [v] (msg string) string {
|
||||
return '$msg: $v'
|
||||
}
|
||||
res := func('hello')
|
||||
assert res == 'hello: $v'
|
||||
// dump(res)
|
||||
}
|
||||
}
|
||||
|
||||
fn ret_two() (int, int) {
|
||||
return 2, 5
|
||||
}
|
||||
|
||||
fn test_closure_over_variable_that_is_returned_from_a_multi_value_function() {
|
||||
one, two := ret_two()
|
||||
a := fn [one] () {
|
||||
println(one)
|
||||
}
|
||||
a()
|
||||
println(two)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user