Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bogus error when the RHS of Table.[]= is not a compatible type #1439

Open
alaviss opened this issue Aug 30, 2024 · 4 comments
Open

bogus error when the RHS of Table.[]= is not a compatible type #1439

alaviss opened this issue Aug 30, 2024 · 4 comments
Labels
bug Something isn't working compiler/sem Related to semantic-analysis system of the compiler

Comments

@alaviss
Copy link
Contributor

alaviss commented Aug 30, 2024

Example

import std/tables

var o: Table[int, string]
o[0] = 0

Actual Output

test.nim(4, 2) Error: type mismatch: got <Table[system.int, system.string], int literal(0)>
but expected one of:
proc `[]`(s: string; i: BackwardsIndex): char
  first type mismatch at position: 0
proc `[]`(s: var string; i: BackwardsIndex): var char
  first type mismatch at position: 0
proc `[]`[A, B](t: OrderedTableRef[A, B]; key: A): var B
  first type mismatch at position: 0
proc `[]`[A, B](t: OrderedTable[A, B]; key: A): B
  first type mismatch at position: 0
proc `[]`[A, B](t: TableRef[A, B]; key: A): var B
  first type mismatch at position: 0
proc `[]`[A, B](t: Table[A, B]; key: A): B
  first type mismatch at position: 0
proc `[]`[A, B](t: var OrderedTable[A, B]; key: A): var B
  first type mismatch at position: 0
proc `[]`[A, B](t: var Table[A, B]; key: A): var B
  first type mismatch at position: 0
proc `[]`[A](t: CountTableRef[A]; key: A): int
  first type mismatch at position: 0
proc `[]`[A](t: CountTable[A]; key: A): int
  first type mismatch at position: 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: 0
proc `[]`[Idx, T](a: array[Idx, T]; i: BackwardsIndex): T
  first type mismatch at position: 0
proc `[]`[Idx, T](a: var array[Idx, T]; i: BackwardsIndex): var T
  first type mismatch at position: 0
proc `[]`[T, U: Ordinal](s: string; x: HSlice[T, U]): string
  first type mismatch at position: 0
proc `[]`[T; U, V: Ordinal](s: openArray[T]; x: HSlice[U, V]): seq[T]
  first type mismatch at position: 0
proc `[]`[T](s: openArray[T]; i: BackwardsIndex): T
  first type mismatch at position: 0
proc `[]`[T](s: var openArray[T]; i: BackwardsIndex): var T
  first type mismatch at position: 0
template `[]`(a: WideCStringObj; idx: int): Utf16Char
  first type mismatch at position: 0
template `[]`(s: string; i: int): char
  first type mismatch at position: 0

expression: `[]`(o, 0)

Expected Output

Something that says the last 0 is not a compatible type

@alaviss alaviss added the bug Something isn't working label Aug 30, 2024
@alaviss alaviss changed the title bogus error when the LHS of Tables.[]= is not a compatible type bogus error when the RHS of Tables.[]= is not a compatible type Aug 30, 2024
@alaviss alaviss changed the title bogus error when the RHS of Tables.[]= is not a compatible type bogus error when the RHS of Table.[]= is not a compatible type Aug 30, 2024
@zerbina zerbina added the compiler/sem Related to semantic-analysis system of the compiler label Aug 30, 2024
@zerbina
Copy link
Collaborator

zerbina commented Aug 30, 2024

A reduced reproducer:

type Type = object

proc `[]=`(a: Type, b, c: int) =
  discard

var x: Type
x[0] = 1.0

How to report a sensible error, I'm not sure yet (the necessary context to do so is missing), but I don't think [] overloads should be ignored in assignments, which is what's currently happening.

@saem
Copy link
Collaborator

saem commented Aug 30, 2024

Been staring at this for a bit, not sure how we'd improve the error beyond wording or formatting perhaps, but otherwise the core meaning of it would remain intact as we can't find a matching overload. 😬

@alaviss
Copy link
Contributor Author

alaviss commented Aug 30, 2024

For the particular error I posted, this part got me confused:

Error: type mismatch: got <Table[system.int, system.string], int literal(0)>
but expected one of:
proc `[]`[A, B](t: Table[A, B]; key: A): B
  first type mismatch at position: 0
proc `[]`[A, B](t: var Table[A, B]; key: A): var B
  first type mismatch at position: 0

It didn't even occur to me that the RHS was wrong...

@zerbina
Copy link
Collaborator

zerbina commented Aug 30, 2024

Been staring at this for a bit, not sure how we'd improve the error beyond wording or formatting perhaps, but otherwise the core meaning of it would remain intact as we can't find a matching overload. 😬

What would help is trying to actually try and use a [] overload. While the error says that no [] matched, this doesn't reflect reality.

At the moment, only the []= procedure is tried. If that fails, the compiler simply reports an (incomplete) "not found error":

of nkBracketExpr:
# a[i] = x
# --> `[]=`(a, i, x)
a = semSubscript(c, a, {efLValue})
if a.kind == nkCall:
case mode
of noOverloadedSubscript:
result = bracketNotFoundError(c, a)
else:
result = a
result[0].ident = getIdent(c.cache, "[]=")
result.add(n[1])
result = semExprNoType(c, result)

Beyond the incomplete error message, it also means that the following doesn't work:

type Object = object
  a: array[16, int]

proc `[]`(x: var Object, i: int): var int = x.a[i]

var o: Object
o[0] = 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler/sem Related to semantic-analysis system of the compiler
Projects
None yet
Development

No branches or pull requests

3 participants