cheap fix for #10853 + better tuple subscript error message (#21767)

* cheap fix for #10853

* also better tuple subscript error message

* weird
This commit is contained in:
metagn
2023-05-02 12:13:38 +03:00
committed by GitHub
parent afc30ca879
commit c2bcfd8cd9
5 changed files with 11 additions and 3 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -0,0 +1,3 @@
var a, b = 0
(a, b) = 1 #[tt.Error
^ type mismatch: got <int literal(1)> but expected 'tuple']#

View File

@@ -0,0 +1,2 @@
let a = (1, 2)[4] #[tt.Error
^ invalid index 4 in subscript for tuple of length 2]#

View File

@@ -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
"""