From c2bcfd8cd908265c358c60c4da137783b10a8549 Mon Sep 17 00:00:00 2001 From: metagn Date: Tue, 2 May 2023 12:13:38 +0300 Subject: [PATCH] cheap fix for #10853 + better tuple subscript error message (#21767) * cheap fix for #10853 * also better tuple subscript error message * weird --- compiler/lowerings.nim | 2 +- compiler/semexprs.nim | 5 ++++- tests/errmsgs/tassignunpack.nim | 3 +++ tests/errmsgs/ttupleindexoutofbounds.nim | 2 ++ tests/types/tassignemptytuple.nim | 2 +- 5 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 tests/errmsgs/tassignunpack.nim create mode 100644 tests/errmsgs/ttupleindexoutofbounds.nim diff --git a/compiler/lowerings.nim b/compiler/lowerings.nim index bd81773a82..d70c713a15 100644 --- a/compiler/lowerings.nim +++ b/compiler/lowerings.nim @@ -132,7 +132,7 @@ proc lowerTupleUnpackingForAsgn*(g: ModuleGraph; n: PNode; idgen: IdGenerator; o var vpart = newNodeI(nkIdentDefs, tempAsNode.info, 3) vpart[0] = tempAsNode - vpart[1] = newNodeI(nkEmpty, value.info) + vpart[1] = newNodeI(nkTupleClassTy, value.info) vpart[2] = value v.add vpart result.add(v) diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 930fd35163..a01466868b 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -1646,7 +1646,10 @@ proc semSubscript(c: PContext, n: PNode, flags: TExprFlags): PNode = {tyInt..tyInt64}: let idx = getOrdValue(n[1]) if idx >= 0 and idx < arr.len: n.typ = arr[toInt(idx)] - else: localError(c.config, n.info, "invalid index value for tuple subscript") + else: + localError(c.config, n.info, + "invalid index $1 in subscript for tuple of length $2" % + [$idx, $arr.len]) result = n else: result = nil diff --git a/tests/errmsgs/tassignunpack.nim b/tests/errmsgs/tassignunpack.nim new file mode 100644 index 0000000000..27413a42b6 --- /dev/null +++ b/tests/errmsgs/tassignunpack.nim @@ -0,0 +1,3 @@ +var a, b = 0 +(a, b) = 1 #[tt.Error + ^ type mismatch: got but expected 'tuple']# diff --git a/tests/errmsgs/ttupleindexoutofbounds.nim b/tests/errmsgs/ttupleindexoutofbounds.nim new file mode 100644 index 0000000000..ae634dddb9 --- /dev/null +++ b/tests/errmsgs/ttupleindexoutofbounds.nim @@ -0,0 +1,2 @@ +let a = (1, 2)[4] #[tt.Error + ^ invalid index 4 in subscript for tuple of length 2]# diff --git a/tests/types/tassignemptytuple.nim b/tests/types/tassignemptytuple.nim index 9d5a311baa..f3320dec7a 100644 --- a/tests/types/tassignemptytuple.nim +++ b/tests/types/tassignemptytuple.nim @@ -1,5 +1,5 @@ discard """ - errormsg: "cannot infer the type of the tuple" + errormsg: "invalid type: 'empty' in this context: '(seq[empty], (seq[empty], set[empty]))' for let" file: "tassignemptytuple.nim" line: 11 """