scala / bug

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

namespace blur results in unsoundness #3987

Closed scabug closed 13 years ago

scabug commented 13 years ago

The following program should not compile, but instead fails at runtime.

class Gox {
  object Zed { }
  class Zed  { }
}          

object Test {
  type GoxZed = t.Zed forSome { val t: Gox }

  def main(args: Array[String]): Unit = {
    val x = new Gox
    val y: GoxZed = x.Zed
  }
}
% scala29 Test
java.lang.ClassCastException: Gox$$Zed$$ cannot be cast to Gox$$Zed
 at Test$$.main(gox.scala:11)
 at Test.main(gox.scala)

The type of y is declared to be an instance of the class Zed, but the compiler allows the object Zed to be assigned to it. We can verify this is a result of type/term blurring by making a small change:

class Gox {
  object ZedZed { }
  class Zed  { }
}          

object Test {
  type GoxZed = t.Zed forSome { val t: Gox }

  def main(args: Array[String]): Unit = {
    val x = new Gox
    val y: GoxZed = x.ZedZed
  }
}

Now the compiler correctly fails it.

% scalac29 zed2.scala 
zed2.scala:11: error: type mismatch;
 found   : x.ZedZed.type (with underlying type object x.ZedZed)
 required: Test.GoxZed
    val y: GoxZed = x.ZedZed
                      ^
one error found
scabug commented 13 years ago

Imported From: https://issues.scala-lang.org/browse/SI-3987?orig=1 Reporter: @paulp

scabug commented 13 years ago

@odersky said: (In r24396) Closes #3987. Review by extempore.