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

uninformative Nim 2.2/devel Error: cannot instantiate H [type declared in /tmp/y.nim(2, 6)], got: <M | M>, but expected: <K> error #24644

Closed
tersec opened this issue Jan 24, 2025 · 2 comments · Fixed by #24646
Assignees

Comments

@tersec
Copy link
Contributor

tersec commented Jan 24, 2025

Description

y.nim:

import "."/[g, u]
type H[K] = object
proc b(_: int) =  # slightly different, still not useful, error message if `b` generic
  proc r(): H[M] = discard
b(0)

g.nim:

type M* = int

u.nim:

type M* = bool

It's of course easy to spot here, but this is a real issue with a codebase of more than 100k LoC across various libraries.

There are of course ways to try to instantiate the object in a way which will trigger a useful error message, but this isn't one.

Nim Version

Nim Compiler Version 2.2.1 [Linux: amd64]
Compiled at 2025-01-24
Copyright (c) 2006-2025 by Andreas Rumpf

git hash: 12347eae74dbaa0e4dd99691e3a4509e8fe8b265
active boot switches: -d:release
Nim Compiler Version 2.3.1 [Linux: amd64]
Compiled at 2025-01-24
Copyright (c) 2006-2025 by Andreas Rumpf

git hash: d6d28a9c79b1f7d700458ee3aa56a38e1216bb41
active boot switches: -d:release

Current Output

/tmp/y.nim(4, 14) Error: cannot instantiate H [type declared in /tmp/y.nim(2, 6)]
got: <typedesc[M] | typedesc[M]>
but expected: <K>

Expected Output

An error message which helps identify the source of the error more precisely in a larger codebase

Known Workarounds

No response

Additional Information

No response

@metagn metagn self-assigned this Jan 24, 2025
@metagn
Copy link
Collaborator

metagn commented Jan 24, 2025

Since generic instantiation uses sigmatch it's hard to produce the expected ambiguous identifier error here but hopefully something like this would be satisfactory (the : typedesc[M] part could be omitted for type symbols):

/tmp/y.nim(4, 14) Error: cannot instantiate H [type declared in /tmp/y.nim(2, 6)]
got: <g.M: typedesc[M] | y.M: typedesc[M]>
but expected: <K>

This would fix this issue for ambiguous identifiers in normal call arguments too. These already add a message after the fact.

@tersec
Copy link
Contributor Author

tersec commented Jan 24, 2025

Sure, I agree that would be a reasonable and useful approach. The main thing is to get some hint/clue of what modules or namespaces are providing these symbols, indeed.

metagn added a commit to metagn/Nim that referenced this issue Jan 24, 2025
Araq pushed a commit that referenced this issue Jan 31, 2025
fixes #24644

Another option is to include the symbol names and owners in the type
listing as in #24645 but this is a bit verbose.
narimiran pushed a commit that referenced this issue Jan 31, 2025
fixes #24644

Another option is to include the symbol names and owners in the type
listing as in #24645 but this is a bit verbose.

(cherry picked from commit 0861dab)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants