Type inference between 2.12 and 2.13

While I was doing a 2.12 to 2.13 migration, I found something odd with type inference in a for yield block. This piece of code compiles fine on 2.12.10 with all the scalac flags of sbt-tpolecat, but fails on 2.13.1

object Example {

  trait AError
  trait BError

  def doNothing: Unit = {
    val aEither: Either[AError, String] = Right("A")
    val bEither: Either[BError, String] = Right("B")

    val c = for {
      a <- aEither
      b <- bEither
    } yield a + b

    println(s"c=$c")
    ()
  }
}
a type was inferred to be `Object`; this may indicate a programming error.
[error]       a <- aEither
[error]            ^
[error] one error found

Was this type inference bug fixed in scala 2.13 and not backported to 2.12 OR is it a regression in 2.13?

Thanks
Anil

Well, the type of c is Either[Object,String] (in 2.12 and 2.13) so the error makes sense. If anything this looks like a bug that got fixed. I’m pretty sure some things got fixed in this area but @som-snytt probably knows more.

2 Likes

Thanks @Jasper-M, still waiting for @som-snytt that this is expected in 2.13 and intentionally not back-ported to 2.12

I don’t think Either is relevant here, and I don’t think type inference changed, either.

We can minimize as follows:

2.12 with -Xlint:

scala 2.12.10> class A; class B; List(new A, new B)
res0: List[Object] = List(A@5562c2c9, B@673c4f6e)

2.13 with -Xlint:

scala 2.13.1> class A; class B; List(new A, new B)
                                ^
              warning: a type was inferred to be `Object`; this may indicate a programming error.
res1: List[Object] = List(A@79316f3a, B@381d7219)

Note that it’s just a warning, not an error, and 2.13 doesn’t warn without -Xlint. I guess you must have -Werror (formerly known as -Xfatal-warnings) enabled.

The change was made in https://github.com/scala/scala/pull/6178 . I doubt there’s any special reason it was submitted against 2.13.x and not 2.12.x, and I doubt there’s any special reason why nobody has chosen to backport it.

2 Likes