Skip to content

Codegen crashes on implicit lookup of Type[T], where T is type member of variable from match case #6142

Closed
@fhackett

Description

@fhackett

Note: this is a New and Improved™ version of this issue after I discovered a much more precise description of the problem while trying to develop a temporary workaround. I edited the existing issue since it is still the exact same problem, hopefully that's ok.

// we need this to get something with a type member
// and a corresponding implicitly unreachable but known Type node
import scala.quoted._
trait Trait {
  type t
  val tType : Type[t]
}

object O {
  def fn[T](c : Trait) given QuoteContext : Unit = c match {
    case v => {
      implicit val _ : Type[v.t] = v.tType
      type VT = v.t
      val a = '[List[VT]]
      ()
    }
  }
}

This, as long as the '[...] is a non-trivial expression in terms of VT, crashes Dotty's codegen ('[VT] is fine, probably because it does not need to generate anything). This can be reproduced as long as v comes from a pattern match, and VT is derived from a type member of v. This includes implicit Type nodes generated by calling a function that takes Type[...VT...] as an implicit argument.

The crash can be avoided by explicitly writing out the type splices:

object O {
  def fn[T](c : Trait) : Unit = c match {
    case v => {
      implicit val _ : Type[v.t] = v.tType
      type VT = v.t
      val tp : Type[List[VT]] = '[List[${v.tType}]]
      ()
    }
  }
}

The error will look something like this:

java.util.NoSuchElementException: class Type
        at scala.collection.mutable.AnyRefMap$ExceptionDefault.apply(AnyRefMap.scala:437)
        at scala.collection.mutable.AnyRefMap$ExceptionDefault.apply(AnyRefMap.scala:436)
        at scala.collection.mutable.AnyRefMap.apply(AnyRefMap.scala:192)
        at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder$locals$.load(BCodeSkelBuilder.scala:396)
        at dotty.tools.backend.jvm.BCodeBodyBuilder$PlainBodyBuilder.genLoad(BCodeBodyBuilder.scala:392)
        at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.emitNormalMethodBody$1(BCodeSkelBuilder.scala:
597)
        at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.genDefDef(BCodeSkelBuilder.scala:630)
        at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.gen(BCodeSkelBuilder.scala:501)
        at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.gen$$anonfun$1(BCodeSkelBuilder.scala:503)
        at dotty.runtime.function.JProcedure1.apply(JProcedure1.java:15)
        at dotty.runtime.function.JProcedure1.apply(JProcedure1.java:10)
        at scala.collection.immutable.List.foreach(List.scala:392)
        at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.gen(BCodeSkelBuilder.scala:503)
        at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.genPlainClass(BCodeSkelBuilder.scala:109)
        at dotty.tools.backend.jvm.GenBCodePipeline$Worker1.visit(GenBCode.scala:213)
        at dotty.tools.backend.jvm.GenBCodePipeline$Worker1.run(GenBCode.scala:180)
        at dotty.tools.backend.jvm.GenBCodePipeline.buildAndSendToDisk(GenBCode.scala:510)
        at dotty.tools.backend.jvm.GenBCodePipeline.run(GenBCode.scala:476)
        at dotty.tools.backend.jvm.GenBCode.run(GenBCode.scala:54)
        at dotty.tools.dotc.core.Phases$Phase.runOn$$anonfun$1(Phases.scala:313)
        at scala.collection.immutable.List.map(List.scala:286)
        at dotty.tools.dotc.core.Phases$Phase.runOn(Phases.scala:315)
        at dotty.tools.backend.jvm.GenBCode.runOn(GenBCode.scala:59)
        at dotty.tools.dotc.Run.runPhases$4$$anonfun$4(Run.scala:158)
        at dotty.runtime.function.JProcedure1.apply(JProcedure1.java:15)
        at dotty.runtime.function.JProcedure1.apply(JProcedure1.java:10)
        at scala.collection.IndexedSeqOptimized.foreach(IndexedSeqOptimized.scala:36)
        at scala.collection.IndexedSeqOptimized.foreach$(IndexedSeqOptimized.scala:33)
        at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:198)
        at dotty.tools.dotc.Run.runPhases$5(Run.scala:170)
        at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:178)
        at dotty.runtime.function.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
        at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:102)
        at dotty.tools.dotc.Run.compileUnits(Run.scala:185)
        at dotty.tools.dotc.Run.compileSources(Run.scala:120)
        at dotty.tools.dotc.Run.compile(Run.scala:104)
        at dotty.tools.dotc.Driver.doCompile(Driver.scala:33)
        at dotty.tools.dotc.Driver.process(Driver.scala:170)
        at dotty.tools.dotc.Driver.process(Driver.scala:139)
        at dotty.tools.dotc.Driver.process(Driver.scala:151)
        at dotty.tools.dotc.Driver.main(Driver.scala:178)
        at dotty.tools.dotc.Main.main(Main.scala)
Error while emitting Polyparse.scala
class Type

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions