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

Nothing inferred when unexpected #16323

Open
adampauls opened this issue Nov 12, 2022 · 4 comments
Open

Nothing inferred when unexpected #16323

adampauls opened this issue Nov 12, 2022 · 4 comments

Comments

@adampauls
Copy link
Contributor

adampauls commented Nov 12, 2022

Compiler version

3.2.1-RC2

Minimized code

class Foo {
  def member: String = this.toString
}
def method[T](c: T => Boolean): T = ???
val y: Foo => Boolean = _ => true
val x : String = "5"
method(y).member // error

Output

Found:    Nothing
Required: ?{ member: ? }
Note that implicit conversions were not tried because the result of an implicit conversion
must be more specific than ?{ member: <?> }

Expectation

Should compile. I understand that Nothing is inferrable here and that I should not expect .member to be able to constrain the type of T. Since Nothing <: Person I suppose it makes sense to infer the tightest type, but does it? Scala widens types in many places and I'm not sure there's any reason to ever want to infer Nothing here. This case is simple enough that I suspect it is intended behavior, but I'm still curious.

@adampauls adampauls added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Nov 12, 2022
@KacperFKorban KacperFKorban removed the stat:needs triage Every issue needs to have an "area" and "itype" label label Nov 12, 2022
@dwijnand dwijnand added the stat:needs triage Every issue needs to have an "area" and "itype" label label Nov 12, 2022
@dwijnand
Copy link
Member

I'm not sure. Is this a bug? Should T be instantiating to Foo?

@smarter
Copy link
Member

smarter commented Nov 12, 2022

We do have logic to avoid either uninstantiated type variables or type variables instantiated to Nothing in similar situations: https://github.com/lampepfl/dotty/pull/10205/files, so I think it's worth digging into why this doesn't trigger here.

@smarter
Copy link
Member

smarter commented Nov 12, 2022

Maybe T is instantiated early in interpolateTypeVars because it only appears in covariant positions (in which case we choose the most specific type), so we never reach couldInstantiateTypeVar. Not sure how to reconcile these two heuristics.

@odersky
Copy link
Contributor

odersky commented Nov 12, 2022

Yes, that's it. We interpolate before we have a chance to approximate by the upper bound in couldInstantiateTypeVar.

I think we can fix that by a adding a targeted special case in interpolateTypeVars.

@dwijnand dwijnand removed the stat:needs triage Every issue needs to have an "area" and "itype" label label Nov 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants