Skip to content

Incorrect local optimisation cause run-time exceptions #3018

Closed
@liufengyun

Description

@liufengyun

Compile the following code with (do sbt dist/pack first if it doesn't exist):

dist/target/pack/bin/dotc -optimise space.scala
class Type
class Symbol

sealed trait Space
case object Empty extends Space
case class Typ(tp: Type, decomposed: Boolean) extends Space
case class Prod(tp: Type, unappTp: Type, unappSym: Symbol, params: List[Space], full: Boolean) extends Space
case class Or(spaces: List[Space]) extends Space

object Test {
  def simplify(space: Space, aggressive: Boolean = false): Space = space match {
    case Prod(tp, fun, sym, spaces, full) =>
      val sp = Prod(tp, fun, sym, spaces.map(simplify(_)), full)
      if (sp.params.contains(Empty)) Empty
      else sp
    case Or(spaces) => space
    case Typ(tp, _) => space
    case _ => space
  }

  def main(args: Array[String]): Unit = {
    simplify(Prod(new Type, new Type, new Symbol, Nil, false))
  }
}

However, running it gives the code dist/target/pack/bin/dotr Test generates a run-time exception:

Exception in thread "main" java.lang.ClassCastException: scala.collection.immutable.Nil$ cannot be cast to Type
	at Test$.simplify(Space3.scala:13)
	at Test$.main(Space3.scala:22)
	at Test.main(Space3.scala)

This issue is reproduced from #3011.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions