Skip to content

Commit e9e9cf3

Browse files
authored
checker: report clear error for comma in declaration assignment instead of panicking (#28145)
1 parent 2d6b899 commit e9e9cf3

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

vlib/v/checker/assign.v

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
158158
c.inside_recheck = old_recheck
159159
}
160160
for i, mut right in node.right {
161+
// early check: if right-side has more values than left-side for a declaration
162+
// (e.g. `x := a, b` — comma not valid in expression), produce a clear error
163+
// and exit before any out-of-bounds access on node.left
164+
if is_decl && i >= node.left.len {
165+
c.error('unexpected `,` in expression, use `;` or a new line to separate statements',
166+
right.pos())
167+
return
168+
}
161169
if right in [ast.ArrayInit, ast.CallExpr, ast.ComptimeCall, ast.DumpExpr, ast.IfExpr,
162170
ast.LockExpr, ast.MapInit, ast.MatchExpr, ast.ParExpr, ast.SelectorExpr, ast.StructInit] {
163171
if right in [ast.ArrayInit, ast.IfExpr, ast.MapInit, ast.MatchExpr, ast.StructInit]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
vlib/v/checker/tests/comma_in_decl_assign_err.vv:3:24: error: unexpected `,` in expression, use `;` or a new line to separate statements
2+
1 | fn main() {
3+
2 | a := [1, 2, 3]
4+
3 | mut b := a.clone(), a.clone()
5+
| ~~~~~~~
6+
4 | println(b)
7+
5 | }
8+
vlib/v/checker/tests/comma_in_decl_assign_err.vv:4:10: error: invalid variable `b`
9+
2 | a := [1, 2, 3]
10+
3 | mut b := a.clone(), a.clone()
11+
4 | println(b)
12+
| ^
13+
5 | }
14+
vlib/v/checker/tests/comma_in_decl_assign_err.vv:4:2: error: `println` can not print void expressions
15+
2 | a := [1, 2, 3]
16+
3 | mut b := a.clone(), a.clone()
17+
4 | println(b)
18+
| ~~~~~~~~~~
19+
5 | }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
a := [1, 2, 3]
3+
mut b := a.clone(), a.clone()
4+
println(b)
5+
}

0 commit comments

Comments
 (0)