Skip to content

Commit

Permalink
fixes #23180; fixes #19805; prohibits invalid tuple unpacking code in…
Browse files Browse the repository at this point in the history
… for loop (#23185)

fixes #23180
fixes #19805

(cherry picked from commit ab4278d)
  • Loading branch information
ringabout authored and narimiran committed Apr 20, 2024
1 parent fbd838b commit eda7db1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
5 changes: 4 additions & 1 deletion compiler/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,10 @@ proc semForVars(c: PContext, n: PNode; flags: TExprFlags): PNode =
if iterAfterVarLent.kind != tyTuple or n.len == 3:
if n.len == 3:
if n[0].kind == nkVarTuple:
if n[0].len-1 != iterAfterVarLent.len:
if iterAfterVarLent.kind != tyTuple:
return localErrorNode(c, n, n[0].info, errTupleUnpackingTupleExpected %
[typeToString(n[1].typ, preferDesc)])
elif n[0].len-1 != iterAfterVarLent.len:
return localErrorNode(c, n, n[0].info, errWrongNumberOfVariables)

for i in 0..<n[0].len-1:
Expand Down
4 changes: 2 additions & 2 deletions tests/errmsgs/t17460.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
discard """
cmd: "nim check $options $file"
errormsg: "wrong number of variables"
errormsg: "tuple expected for tuple unpacking, but got 'array[0..2, int]'"
"""

iterator xclusters*[T](a: openArray[T]; s: static[int]): array[s, T] {.inline.} =
Expand All @@ -16,4 +16,4 @@ proc m =
for (i, j, k) in xclusters([1, 2, 3, 4, 5], 3):
echo i, j, k

m()
m()

0 comments on commit eda7db1

Please sign in to comment.