Files
Nim/tests/errmsgs/t22753.nim
metagn 8752392838 implement setter fallback for subscripts (#24872)
follows up #24871

For subscript assignments, if an overload of `[]=`/`{}=` is not found,
the LHS checks for overloads of `[]`/`{}` as a fallback, similar to what
field setters do since #24871. This is accomplished by just compiling
the LHS if the assignment overloads fail. This has the side effect that
the error messages are different now, instead of displaying the
overloads of `[]=`/`{}=` that did not match, it will display the ones
for `[]`/`{}` instead. This could be fixed by checking for `efLValue`
when giving the error messages for `[]`/`{}` but this is not done here.

The code for `[]` subscripts is a little different because of the
`mArrGet`/`mArrPut` overloads that always match. If the `mArrPut`
overload matches without a builtin subscript behavior for the LHS then
it calls `semAsgn` again with `mode = noOverloadedSubscript`. Before
this meant "fail to compile" but now it means "try to compile the LHS as
normal", in both cases the overloads of `[]=` are not considered again.
2025-05-23 16:19:13 +02:00

60 lines
2.4 KiB
Nim

discard """
cmd: "nim check --hints:off $file"
action: "reject"
nimoutFull: true
nimout: '''
t22753.nim(58, 13) Error: array expects two type parameters
t22753.nim(59, 1) Error: expression 'x' has no type (or is ambiguous)
t22753.nim(59, 1) Error: expression 'x' has no type (or is ambiguous)
t22753.nim(59, 1) Error: expression 'x' has no type (or is ambiguous)
t22753.nim(59, 1) Error: expression 'x' has no type (or is ambiguous)
t22753.nim(59, 2) Error: type mismatch: got <>
but expected one of:
proc `[]`(s: string; i: BackwardsIndex): char
first type mismatch at position: 2
required type for i: BackwardsIndex
but expression '0' is of type: int literal(0)
proc `[]`(s: var string; i: BackwardsIndex): var char
first type mismatch at position: 2
required type for i: BackwardsIndex
but expression '0' is of type: int literal(0)
proc `[]`[I: Ordinal; T](a: T; i: I): T
first type mismatch at position: 0
proc `[]`[Idx, T; U, V: Ordinal](a: array[Idx, T]; x: HSlice[U, V]): seq[T]
first type mismatch at position: 2
required type for x: HSlice[[].U, [].V]
but expression '0' is of type: int literal(0)
proc `[]`[Idx, T](a: array[Idx, T]; i: BackwardsIndex): T
first type mismatch at position: 2
required type for i: BackwardsIndex
but expression '0' is of type: int literal(0)
proc `[]`[Idx, T](a: var array[Idx, T]; i: BackwardsIndex): var T
first type mismatch at position: 2
required type for i: BackwardsIndex
but expression '0' is of type: int literal(0)
proc `[]`[T, U: Ordinal](s: string; x: HSlice[T, U]): string
first type mismatch at position: 2
required type for x: HSlice[[].T, [].U]
but expression '0' is of type: int literal(0)
proc `[]`[T; U, V: Ordinal](s: openArray[T]; x: HSlice[U, V]): seq[T]
first type mismatch at position: 2
required type for x: HSlice[[].U, [].V]
but expression '0' is of type: int literal(0)
proc `[]`[T](s: openArray[T]; i: BackwardsIndex): T
first type mismatch at position: 2
required type for i: BackwardsIndex
but expression '0' is of type: int literal(0)
proc `[]`[T](s: var openArray[T]; i: BackwardsIndex): var T
first type mismatch at position: 2
required type for i: BackwardsIndex
but expression '0' is of type: int literal(0)
expression: x[0]
t22753.nim(59, 2) Error: expression '' has no type (or is ambiguous)
t22753.nim(59, 2) Error: '' cannot be assigned to
'''
"""
var x: array[3] # bug #22753
x[0] = 9