diff options
author | ringabout <43030857+ringabout@users.noreply.github.com> | 2024-01-13 21:09:34 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-13 14:09:34 +0100 |
commit | ab4278d2179639f19967431a7aa1be858046f7a7 (patch) | |
tree | 49271826ce4eea52eb707f3fb27b12f08def0f2a | |
parent | 8484abc2e498bd9738097c06362d442c615a8264 (diff) | |
download | Nim-ab4278d2179639f19967431a7aa1be858046f7a7.tar.gz |
fixes #23180; fixes #19805; prohibits invalid tuple unpacking code in for loop (#23185)
fixes #23180 fixes #19805
-rw-r--r-- | compiler/semstmts.nim | 5 | ||||
-rw-r--r-- | tests/errmsgs/t17460.nim | 4 |
2 files changed, 6 insertions, 3 deletions
diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 5a0968ba5..d62ad8305 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -930,7 +930,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: diff --git a/tests/errmsgs/t17460.nim b/tests/errmsgs/t17460.nim index e377bc48a..bb8e21198 100644 --- a/tests/errmsgs/t17460.nim +++ b/tests/errmsgs/t17460.nim @@ -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.} = @@ -16,4 +16,4 @@ proc m = for (i, j, k) in xclusters([1, 2, 3, 4, 5], 3): echo i, j, k -m() \ No newline at end of file +m() |