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

cannot instantiate T, even when matching overload that doesn't require T exists #24688

Open
arnetheduck opened this issue Feb 14, 2025 · 5 comments · May be fixed by #24690
Open

cannot instantiate T, even when matching overload that doesn't require T exists #24688

arnetheduck opened this issue Feb 14, 2025 · 5 comments · May be fixed by #24690

Comments

@arnetheduck
Copy link
Contributor

Description

type
  R[T, E] = object
  A = object
  B = object

# Remove this overload to make it work
func err[T](_: type R[T, void]): R[T, void] = discard

func err(_: type R): R[A, B] = discard

echo R.err()

Nim Version

2.0, 2.2

Current Output

testit.nim(11, 7) template/generic instantiation of `err` from here
testit.nim(6, 10) Error: cannot instantiate: 'T'

Expected Output


Known Workarounds

No response

Additional Information

No response

@metagn
Copy link
Collaborator

metagn commented Feb 15, 2025

Shouldn't give this error with just the first overload either, it should fail to match

Edit: Indeed the overload wrongly matches

Should be noted this is in opposition to #5632

@arnetheduck
Copy link
Contributor Author

Should be noted this is in opposition

maybe can have both using a better-match resolution?

@arnetheduck
Copy link
Contributor Author

Should be noted this is in opposition to #5632

also, in this example, A is actually bound to a type unlike here where the binding fails .. anorther mechanism would be like SFINAE in C++, ie ignore generic resolutions that fail if some work

@metagn
Copy link
Collaborator

metagn commented Feb 15, 2025

maybe can have both using a better-match resolution?

The existing mechanism is that the compiler thinks a better match is one with a more specific type, which in this case is the first overload. Not sure how to work backwards on this.

@arnetheduck
Copy link
Contributor Author

The existing mechanism is that the compiler thinks a better match is one with a more specific type

sure, I meant among the "working" overloads, so it would pick the 5632 resolution in case both "could work" (indeed for being more specific)

Araq pushed a commit that referenced this issue Feb 19, 2025
Applies #24144 to the equivalent matches of generic types, and adds the
behavior to matches of generic invocations to generic invocations. Not
encountered in many cases so it's hard to come up with tests but an
example is the test code in #24688, the match to the generic body never
sets the inheritance penalty leaving it at -1, but the match to the
generic invocation sets it to 0 which matches worse, when it should set
it to -1 because the object does not participate in inheritance.
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