scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.83k stars 1.05k forks source link

scala.MatchError dotty.tools.dotc.typer.Namer$Completer.typeSig(Namer.scala:798) #19202

Closed akshaal closed 8 months ago

akshaal commented 10 months ago

Compiling a minimized example with scala 3.3.1:

class Test {
    def test(s: String): Unit = {
        if (s == null) {
            return
        }

        case class XXX()
    }
}

results in

scala.MatchError: TypeDef(XXX,Template(DefDef(<init>,List(List()),TypeTree,EmptyTree),List(Select(Select(Ident(_root_),scala),Product), Select(Select(Ident(_root_),scala),Serializable)),ValDef(_,EmptyTree,EmptyTree),List(DefDef(copy,List(List()),TypeTree,Apply(Select(New(TypeTree),<init>),List()))))) (of class dotty.tools.dotc.ast.Trees$TypeDef) while typechecking /tmp/a/test.scala
exception occurred while typechecking /tmp/a/test.scala

  exception occurred while compiling List(/tmp/a/test.scala)

  An unhandled exception was thrown in the compiler.
  Please file a crash report here:
  https://github.com/lampepfl/dotty/issues/new/choose

     while compiling: <no file>
        during phase: <no phase>
                mode: Mode(ImplicitsEnabled)
     library version: version 2.13.10
    compiler version: version 3.3.1
            settings: -classpath /home/user/opt/scala3/lib/scala-library-2.13.10.jar:/home/user/opt/scala3/lib/scala3-library_3-3.3.1.jar:/home/user/opt/scala3/lib/scala-asm-9.5.0-scala-1.jar:/home/user/opt/scala3/lib/compiler-interface-1.3.5.jar:/home/user/opt/scala3/lib/scala3-interfaces-3.3.1.jar:/home/user/opt/scala3/lib/scala3-compiler_3-3.3.1.jar:/home/user/opt/scala3/lib/tasty-core_3-3.3.1.jar:/home/user/opt/scala3/lib/scala3-staging_3-3.3.1.jar:/home/user/opt/scala3/lib/scala3-tasty-inspector_3-3.3.1.jar:/home/user/opt/scala3/lib/jline-reader-3.19.0.jar:/home/user/opt/scala3/lib/jline-terminal-3.19.0.jar:/home/user/opt/scala3/lib/jline-terminal-jna-3.19.0.jar:/home/user/opt/scala3/lib/jna-5.3.1.jar -d /tmp/scala3-scripting16613904917771917003

                tree: EmptyTree
       tree position: :<unknown>
           tree type: <notype>
              symbol: val <none>
           call site: package <root> in module class <root>

  == Source file context for tree position ==

Exception in thread "main" scala.MatchError: TypeDef(XXX,Template(DefDef(<init>,List(List()),TypeTree,EmptyTree),List(Select(Select(Ident(_root_),scala),Product), Select(Select(Ident(_root_),scala),Serializable)),ValDef(_,EmptyTree,EmptyTree),List(DefDef(copy,List(List()),TypeTree,Apply(Select(New(TypeTree),<init>),List()))))) (of class dotty.tools.dotc.ast.Trees$TypeDef)
    at dotty.tools.dotc.typer.Namer$Completer.typeSig(Namer.scala:798)
    at dotty.tools.dotc.typer.Namer$Completer.completeInCreationContext(Namer.scala:934)
    at dotty.tools.dotc.typer.Namer$Completer.complete(Namer.scala:814)
    at dotty.tools.dotc.core.SymDenotations$SymDenotation.completeFrom(SymDenotations.scala:174)
    at dotty.tools.dotc.core.Denotations$Denotation.completeInfo$1(Denotations.scala:187)
    at dotty.tools.dotc.core.Denotations$Denotation.info(Denotations.scala:189)
    at dotty.tools.dotc.core.SymDenotations$SymDenotation.ensureCompleted(SymDenotations.scala:393)
    at dotty.tools.dotc.typer.Typer.retrieveSym(Typer.scala:2989)
    at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:3014)
    at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3111)
    at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3184)
    at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3188)
    at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:3210)
    at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3256)
    at dotty.tools.dotc.typer.Typer.typedBlockStats(Typer.scala:1159)
    at dotty.tools.dotc.typer.Typer.typedBlock(Typer.scala:1163)
    at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3056)
    at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3112)
    at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3184)
    at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3188)
    at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:3300)
    at dotty.tools.dotc.typer.Typer.$anonfun$57(Typer.scala:2486)
    at dotty.tools.dotc.inlines.PrepareInlineable$.dropInlineIfError(PrepareInlineable.scala:243)
    at dotty.tools.dotc.typer.Typer.typedDefDef(Typer.scala:2486)
    at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:3024)
    at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3111)
    at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3184)
    at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3188)
    at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:3210)
    at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3256)
    at dotty.tools.dotc.typer.Typer.typedClassDef(Typer.scala:2669)
    at dotty.tools.dotc.typer.Typer.typedTypeOrClassDef$1(Typer.scala:3036)
    at dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:3040)
    at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3111)
    at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3184)
    at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3188)
    at dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:3210)
    at dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3256)
    at dotty.tools.dotc.typer.Typer.typedPackageDef(Typer.scala:2812)
    at dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:3081)
    at dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:3112)
    at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3184)
    at dotty.tools.dotc.typer.Typer.typed(Typer.scala:3188)
    at dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:3300)
    at dotty.tools.dotc.typer.TyperPhase.typeCheck$$anonfun$1(TyperPhase.scala:44)
    at dotty.tools.dotc.typer.TyperPhase.typeCheck$$anonfun$adapted$1(TyperPhase.scala:54)
    at scala.Function0.apply$mcV$sp(Function0.scala:42)
    at dotty.tools.dotc.core.Phases$Phase.monitor(Phases.scala:440)
    at dotty.tools.dotc.typer.TyperPhase.typeCheck(TyperPhase.scala:54)
    at dotty.tools.dotc.typer.TyperPhase.runOn$$anonfun$3(TyperPhase.scala:88)
    at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
    at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
    at scala.collection.immutable.List.foreach(List.scala:333)
    at dotty.tools.dotc.typer.TyperPhase.runOn(TyperPhase.scala:88)
    at dotty.tools.dotc.Run.runPhases$1$$anonfun$1(Run.scala:246)
    at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
    at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
    at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1321)
    at dotty.tools.dotc.Run.runPhases$1(Run.scala:262)
    at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:270)
    at dotty.tools.dotc.Run.compileUnits$$anonfun$adapted$1(Run.scala:279)
    at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:67)
    at dotty.tools.dotc.Run.compileUnits(Run.scala:279)
    at dotty.tools.dotc.Run.compileSources(Run.scala:194)
    at dotty.tools.dotc.Run.compile(Run.scala:179)
    at dotty.tools.dotc.Driver.doCompile(Driver.scala:37)
    at dotty.tools.scripting.ScriptingDriver.compileAndRun(ScriptingDriver.scala:22)
    at dotty.tools.scripting.Main$.process(Main.scala:45)
    at dotty.tools.MainGenericRunner$.run$1(MainGenericRunner.scala:250)
    at dotty.tools.MainGenericRunner$.process(MainGenericRunner.scala:270)
    at dotty.tools.MainGenericRunner$.main(MainGenericRunner.scala:281)
    at dotty.tools.MainGenericRunner.main(MainGenericRunner.scala)
mbovel commented 8 months ago

Still crashes as of 3.4.0-RC1-bin-20240112-c50f2ff-NIGHTLY-git-c50f2ff.

scala-center-bot commented 8 months ago

This issue was picked for the Issue Spree No. 41 of January 16th, 2024 which takes place in 2 days. @smarter, @jan-pieter, @nmcb will be working on it. If you have any insight into the issue or guidance on how to fix it, please leave it here.

jan-pieter commented 8 months ago

Some observations:

mbovel commented 8 months ago

It does also work if the case class is replaced by a def:

class Test {
    def test(s: String): Unit = {
        if (s == null) {
            return
        }

        def g() = ()
    }
}
mbovel commented 8 months ago

Compilation succeeds when wrapping the case class definition in a block with curly braces

Interesting. The difference there is that the case class and the result gets wrapped in a block:

sbt:scala3> scalac -Xprint:parser tests/pos/i19202.scala
[[syntax trees at end of                    parser]] // tests/pos/i19202.scala
package <empty> {
  class Test {
    def test(s: String): Unit =
      {
        if (s == null)
          {
            return
          }
        {
          case class XXX() {}
          <empty>
        }
      }
  }
}

Without curly braces:

sbt:scala3> scalac -Xprint:parser tests/pos/i19202.scala
[[syntax trees at end of                    parser]] // tests/pos/i19202.scala
package <empty> {
  class Test {
    def test(s: String): Unit =
      {
        if (s == null)
          {
            return
          }
        case class XXX() {}
        <empty>
      }
  }
}
...
mbovel commented 8 months ago

The exception is a MatchError thrown from typeSig when original is a TypeDef for which there is node case:

https://github.com/lampepfl/dotty/blob/2945fd178dfc7331e476d61d79d6bb44174263aa/compiler/src/dotty/tools/dotc/typer/Namer.scala#L793-L806

mbovel commented 8 months ago

@noti0na1 will join you to work on this issue.

jan-pieter commented 8 months ago

Turns out the match fails on a TypeDef. And indeed, when replacing the case class definition with type A = String we get the same error.