scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
230 stars 21 forks source link

glb for deep structural types computes wrong results #10592

Open keikonakata opened 6 years ago

keikonakata commented 6 years ago

The following code snippet does not type check with scala 2.12.4 but it does type check with dotty

object O {
  type A = AnyRef { def M: AnyRef { def a : Int }}
  type B = AnyRef { def M: AnyRef { def b : Int }}

  class U[-X]

  def f(a: U[A], b: U[B]) = Seq(a, b)
}

The error message is

 found   : O.U[O.B]
    (which expands to)  O.U[AnyRef{def M: AnyRef{def b: Int}}]
 required: O.U[O.B with O.A]
    (which expands to)  O.U[AnyRef{def M: AnyRef{def b: Int}} with AnyRef{def M: AnyRef{def a: Int}}]
  def f(a: U[A], b: U[B]) = Seq(a, b)
                                   ^

Expanding type aliases does not help

  def g(
    a: U[AnyRef { def M: AnyRef { def a: Int }}],
    b: U[AnyRef { def M: AnyRef { def b: Int }}]) = Seq(a, b)

Now it complains

error: type mismatch;
 found   : O.U[AnyRef{def M: AnyRef{def a: Int}}]
 required: O.U[AnyRef{def M: AnyRef}]
    b: U[AnyRef { def M: AnyRef { def b: Int }}]) = Seq(a, b)
                                                        ^
error: type mismatch;
 found   : O.U[AnyRef{def M: AnyRef{def b: Int}}]
 required: O.U[AnyRef{def M: AnyRef}]
    b: U[AnyRef { def M: AnyRef { def b: Int }}]) = Seq(a, b)

The code type-checks with a type annotation

  type C = AnyRef { def M: AnyRef { def a: Int; def b : Int }}
  def h(a: U[A], b: U[B]) = Seq[U[C]](a, b)
keikonakata commented 6 years ago

I'll submit a PR shortly, proposing a fix for this issue.