From 5ead7bcc102294541c06b128edff601d26413cee Mon Sep 17 00:00:00 2001 From: JMD <56417208+StunxFS@users.noreply.github.com> Date: Wed, 15 Oct 2025 04:42:46 -0400 Subject: [PATCH] checker: disallow multiple left variables in if guards, that do not receive multireturn values (fix #24930) (#25507) --- vlib/v/checker/if.v | 5 +++ .../checker/tests/if_guard_variables_err.out | 43 +++++++++++-------- .../v/checker/tests/if_guard_variables_err.vv | 9 ++++ 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/vlib/v/checker/if.v b/vlib/v/checker/if.v index 720a6ded1..5a2d72204 100644 --- a/vlib/v/checker/if.v +++ b/vlib/v/checker/if.v @@ -201,6 +201,11 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type { } } } else { + if branch.cond.vars.len != 1 { + c.error('if guard expects a single variable, but got ${branch.cond.vars.len}', + branch.pos) + continue + } for _, var in branch.cond.vars { if var.name == '_' { continue diff --git a/vlib/v/checker/tests/if_guard_variables_err.out b/vlib/v/checker/tests/if_guard_variables_err.out index e30acdcca..df37f4ac5 100644 --- a/vlib/v/checker/tests/if_guard_variables_err.out +++ b/vlib/v/checker/tests/if_guard_variables_err.out @@ -1,21 +1,28 @@ -vlib/v/checker/tests/if_guard_variables_err.vv:10:2: error: if guard expects 3 variables, but got 1 - 8 | - 9 | fn main() { - 10 | if r1 := create() { +vlib/v/checker/tests/if_guard_variables_err.vv:14:2: error: if guard expects 3 variables, but got 1 + 12 | + 13 | fn main() { + 14 | if r1 := create() { | ~~~~~~~~~~~~~~~~~ - 11 | println(r1) - 12 | } -vlib/v/checker/tests/if_guard_variables_err.vv:14:2: error: if guard expects 3 variables, but got 4 - 12 | } - 13 | - 14 | if r1, r2, r3, r4 := create() { - | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 15 | println(r1) - 16 | println(r2) -vlib/v/checker/tests/if_guard_variables_err.vv:21:2: error: if guard expects 2 variables, but got 1 - 19 | } - 20 | - 21 | if x := maybe() { - | ~~~~~~~~~~~~~~~ - 22 | println('${x}') + 16 | } +vlib/v/checker/tests/if_guard_variables_err.vv:18:2: error: if guard expects 3 variables, but got 4 + 16 | } + 17 | + 18 | if r1, r2, r3, r4 := create() { + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 19 | println(r1) + 20 | println(r2) +vlib/v/checker/tests/if_guard_variables_err.vv:25:2: error: if guard expects 2 variables, but got 1 23 | } + 24 | + 25 | if x := maybe() { + | ~~~~~~~~~~~~~~~ + 26 | println('${x}') + 27 | } +vlib/v/checker/tests/if_guard_variables_err.vv:29:2: error: if guard expects a single variable, but got 2 + 27 | } + 28 | + 29 | if a, b := simple() { + | ~~~~~~~~~~~~~~~~~~~ + 30 | println(a) + 31 | println(b) diff --git a/vlib/v/checker/tests/if_guard_variables_err.vv b/vlib/v/checker/tests/if_guard_variables_err.vv index 3a63eb815..1d783d4e4 100644 --- a/vlib/v/checker/tests/if_guard_variables_err.vv +++ b/vlib/v/checker/tests/if_guard_variables_err.vv @@ -6,6 +6,10 @@ fn maybe() ?(int, int) { return 1, 2 } +fn simple() ?int { + return 1 +} + fn main() { if r1 := create() { println(r1) @@ -21,4 +25,9 @@ fn main() { if x := maybe() { println('${x}') } + + if a, b := simple() { + println(a) + println(b) + } } -- 2.39.5