Skip to content

When ExprType(T) has expected type Foo, we should add a constraint T <: Foo #2998

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

Closed
smarter opened this issue Aug 21, 2017 · 1 comment
Closed

Comments

@smarter
Copy link
Member

smarter commented Aug 21, 2017

Type inference should work the same for zero and one, but it doesn't, foo(1).zero has expected type 1 but this isn't added as a constraint, so we instantiate T to Int (because the only constraint is >: 1 and we widen when instantiating a singleton lower bound)

class Foo[T] {
  def zero: T = ???
  def one(): T = ???
}

object Test {
  def foo[T](x: T): Foo[T] = new Foo

  val a: 1 = foo(1).zero // Fails: Found: Int, required: Int(1)
  val b: 1 = foo(1).one() // OK
}
@odersky
Copy link
Contributor

odersky commented Jan 7, 2018

This one does not work either:

class Foo[T] {
  def zero: T = ???
  def one(): T = ???
  val two: T = ???
}

object Test {
  def foo[T](x: T): Foo[T] = new Foo

  val b: 1 = foo(1).one() // OK
  val a: 1 = foo(1).zero // Fails: Found: Int, required: Int(1)
  val c: 1 = foo(1).two  // Fails: Found: Int, required: Int(1)
}

So the problem applies to normal value types as well, ExprTypes are not special.

odersky added a commit to dotty-staging/dotty that referenced this issue Jan 7, 2018
When interpolating type variables we should use all available information
at this point, including the expected result type. One situation where this makes
a difference is if the expected type is a singleton type, because type variables
are instantiated to singleton types only if their upper bounds are singleton types.

Also fixes scala#2997.
odersky added a commit that referenced this issue Jan 12, 2018
Fix #2998: Constrain type from above before interpolating variables
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants