fixes #22753; Nimsuggest segfault with invalid assignment to table (#22781) (#23314)

fixes #22753

## Future work
We should turn all the error nodes into nodes of a nkError kind, which
could be a industrious task. But perhaps we can add a special treatment
for error nodes to make the transition smooth.

(cherry picked from commit 642ac0c1c3)

---------

Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
This commit is contained in:
Nikolay Nikolov
2024-03-14 19:35:25 +02:00
committed by GitHub
parent afced7d9ab
commit e6ee956845
5 changed files with 45 additions and 9 deletions

View File

@@ -1803,7 +1803,7 @@ proc semAsgn(c: PContext, n: PNode; mode=asgnNormal): PNode =
result.add(n[1])
if mode == noOverloadedSubscript:
bracketNotFoundError(c, result)
return n
return errorNode(c, n)
else:
result = semExprNoType(c, result)
return result

View File

@@ -51,7 +51,7 @@ proc semArrGet(c: PContext; n: PNode; flags: TExprFlags): PNode =
x[0] = newIdentNode(getIdent(c.cache, "[]"), n.info)
bracketNotFoundError(c, x)
#localError(c.config, n.info, "could not resolve: " & $n)
result = n
result = errorNode(c, n)
proc semArrPut(c: PContext; n: PNode; flags: TExprFlags): PNode =
# rewrite `[]=`(a, i, x) back to ``a[i] = x``.

View File

@@ -1,10 +1,12 @@
discard """
cmd: "nim check $file"
errormsg: "selector must be of an ordinal type, float or string"
errormsg: "illformed AST: case buf[pos]"
nimout: '''
t10735.nim(38, 5) Error: 'let' symbol requires an initialization
t10735.nim(39, 10) Error: undeclared identifier: 'pos'
t10735.nim(39, 9) Error: type mismatch: got <cstring, >
t10735.nim(41, 5) Error: 'let' symbol requires an initialization
t10735.nim(42, 10) Error: undeclared identifier: 'pos'
t10735.nim(42, 10) Error: expression 'pos' has no type (or is ambiguous)
t10735.nim(42, 10) Error: expression 'pos' has no type (or is ambiguous)
t10735.nim(42, 9) Error: type mismatch: got <cstring, >
but expected one of:
proc `[]`(s: string; i: BackwardsIndex): char
first type mismatch at position: 0
@@ -30,7 +32,8 @@ template `[]`(s: string; i: int): char
first type mismatch at position: 0
expression: `[]`(buf, pos)
t10735.nim(39, 9) Error: selector must be of an ordinal type, float or string
t10735.nim(42, 9) Error: expression '' has no type (or is ambiguous)
t10735.nim(44, 3) Error: illformed AST: case buf[pos]
'''
joinable: false
"""

33
tests/errmsgs/t22753.nim Normal file
View File

@@ -0,0 +1,33 @@
discard """
cmd: "nim check --hints:off $file"
errormsg: "type mismatch"
nimoutFull: true
nimout: '''
t22753.nim(32, 13) Error: array expects two type parameters
t22753.nim(33, 1) Error: expression 'x' has no type (or is ambiguous)
t22753.nim(33, 1) Error: expression 'x' has no type (or is ambiguous)
t22753.nim(33, 2) Error: type mismatch: got <>
but expected one of:
proc `[]=`(s: var string; i: BackwardsIndex; x: char)
first type mismatch at position: 0
proc `[]=`[I: Ordinal; T, S](a: T; i: I; x: sink S)
first type mismatch at position: 0
proc `[]=`[Idx, T; U, V: Ordinal](a: var array[Idx, T]; x: HSlice[U, V];
b: openArray[T])
first type mismatch at position: 0
proc `[]=`[Idx, T](a: var array[Idx, T]; i: BackwardsIndex; x: T)
first type mismatch at position: 0
proc `[]=`[T, U: Ordinal](s: var string; x: HSlice[T, U]; b: string)
first type mismatch at position: 0
proc `[]=`[T; U, V: Ordinal](s: var seq[T]; x: HSlice[U, V]; b: openArray[T])
first type mismatch at position: 0
proc `[]=`[T](s: var openArray[T]; i: BackwardsIndex; x: T)
first type mismatch at position: 0
template `[]=`(s: string; i: int; val: char)
first type mismatch at position: 0
expression: `[]=`(x, 0, 9)
'''
"""
var x: array[3] # bug #22753
x[0] = 9

View File

@@ -11,6 +11,6 @@ func `[]`[T, K](x: var Test[T, K], idx: int): var Test[T, K] =
x
var b: Something
# Should give a type-mismatch since Something isn't a valid Test
# Should give an error since Something isn't a valid Test
b[0].name = "Test" #[tt.Error
^ type mismatch]#
^ expression '' has no type (or is ambiguous)]#