Skip to content

Regression in type parameter inference in 2.13 #10819

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 Apr 9, 2018 · 3 comments
Closed

Regression in type parameter inference in 2.13 #10819

smarter opened this issue Apr 9, 2018 · 3 comments

Comments

@smarter
Copy link
Member

smarter commented Apr 9, 2018

When a non-Any upper bound is given to a type parameter, 2.13 instantiates the parameter to the upper bound even when a more precise type is known in some situations (unlike 2.12 and Dotty). Compare:

Welcome to Scala 2.12.4 (OpenJDK 64-Bit Server VM, Java 1.8.0_162).
Type in expressions for evaluation. Or try :help.

scala> def id0[T](x: T): T = x
id0: [T](x: T)T

scala> id0(if (1 == 1) 1 else 2)
res0: Int = 1

scala> def id[T <: AnyVal](x: T): T = x
id: [T <: AnyVal](x: T)T

scala> id(if (1 == 1) 1 else 2)
res1: Int = 1

And:

Welcome to Scala 2.13.0-M3 (OpenJDK 64-Bit Server VM, Java 1.8.0_162).
Type in expressions for evaluation. Or try :help.

scala> def id0[T](x: T): T = x
id0: [T](x: T)T

scala> id0(if (1 == 1) 1 else 2)
res0: Int = 1

scala> def id[T <: AnyVal](x: T): T = x
id: [T <: AnyVal](x: T)T

scala> id(if (1 == 1) 1 else 2)
res1: AnyVal = 1
@adriaanm
Copy link
Contributor

adriaanm commented Aug 8, 2018

Regressed with scala/scala#5310 (-Yliteral-types):

[info] Running scala.tools.nsc.MainGenericRunner -usejavacp
Welcome to Scala 2.13.0-local-2cdd67e (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_172).
Type in expressions for evaluation. Or try :help.

scala> def id[T <: AnyVal](x: T): T = x 
id: [T <: AnyVal](x: T)T

scala>  id(if (1 == 1) 1 else 2)
res0: Int = 1
> scala -Yliteral-types
[info] Running scala.tools.nsc.MainGenericRunner -usejavacp -Yliteral-types
Welcome to Scala 2.13.0-local-2cdd67e (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_172).
Type in expressions for evaluation. Or try :help.

scala> def id[T <: AnyVal](x: T): T = x 
id: [T <: AnyVal](x: T)T

scala>  id(if (1 == 1) 1 else 2)
res0: AnyVal = 1

@adriaanm
Copy link
Contributor

adriaanm commented Aug 8, 2018

Specifically, it's because of bounds propagation in TypeVar's deriveConstraint. I thought I had a PR that disabled that already... EDIT: scala/scala#6789

@adriaanm
Copy link
Contributor

adriaanm commented Aug 8, 2018

Note that this is sensitive to the expected type because typedIf assigns the expected type when it's fully defined. This is not in the spec -- instead, it's said to be the weak lub, as you'd imagine. (Same for match -- using the expected type is also not spec'ed)

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

3 participants