Skip to content

Commit

Permalink
checker: fix closure in if guard, with multi_return (#19765)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Nov 4, 2023
1 parent 2ef49b8 commit 59ffee3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
12 changes: 11 additions & 1 deletion vlib/v/checker/fn.v
Expand Up @@ -487,7 +487,17 @@ fn (mut c Checker) anon_fn(mut node ast.AnonFn) ast.Type {
var.pos)
}
if parent_var.expr is ast.IfGuardExpr {
var.typ = parent_var.expr.expr_type.clear_flags(.option, .result)
sym := c.table.sym(parent_var.expr.expr_type)
if sym.info is ast.MultiReturn {
for i, v in parent_var.expr.vars {
if v.name == var.name {
var.typ = sym.info.types[i]
break
}
}
} else {
var.typ = parent_var.expr.expr_type.clear_flags(.option, .result)
}
} else {
var.typ = parent_var.typ
}
Expand Down
File renamed without changes.
16 changes: 16 additions & 0 deletions vlib/v/tests/closure_in_if_guard_2_test.v
@@ -0,0 +1,16 @@
fn get_info() ?(int, bool) {
return 45, true
}

fn test_closure_in_if_guard() {
mut ret := ''
if v1, v2 := get_info() {
func := fn [v1, v2] () string {
println(v1)
println(v2)
return '${v1}, ${v2}'
}
ret = func()
}
assert ret == '45, true'
}

0 comments on commit 59ffee3

Please sign in to comment.