From e6ee956845e34ae9e0117ec1d43e5871a66beaaf Mon Sep 17 00:00:00 2001 From: Nikolay Nikolov Date: Thu, 14 Mar 2024 19:35:25 +0200 Subject: [PATCH] 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 642ac0c1c31063ae966d8448c64c539203432e94) --------- Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com> --- compiler/semexprs.nim | 2 +- compiler/semmagic.nim | 2 +- tests/errmsgs/t10735.nim | 13 +++++---- tests/errmsgs/t22753.nim | 33 ++++++++++++++++++++++ tests/generics/tuninstantiated_failure.nim | 4 +-- 5 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 tests/errmsgs/t22753.nim diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 3999196154..594cb03b44 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -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 diff --git a/compiler/semmagic.nim b/compiler/semmagic.nim index 49c64086ca..2cb776829c 100644 --- a/compiler/semmagic.nim +++ b/compiler/semmagic.nim @@ -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``. diff --git a/tests/errmsgs/t10735.nim b/tests/errmsgs/t10735.nim index 307acac2d4..56e03d2d91 100644 --- a/tests/errmsgs/t10735.nim +++ b/tests/errmsgs/t10735.nim @@ -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 +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 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 """ diff --git a/tests/errmsgs/t22753.nim b/tests/errmsgs/t22753.nim new file mode 100644 index 0000000000..1c1ebd7642 --- /dev/null +++ b/tests/errmsgs/t22753.nim @@ -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 \ No newline at end of file diff --git a/tests/generics/tuninstantiated_failure.nim b/tests/generics/tuninstantiated_failure.nim index f09b115d65..f3d5b34b89 100644 --- a/tests/generics/tuninstantiated_failure.nim +++ b/tests/generics/tuninstantiated_failure.nim @@ -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)]#