Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fhackett opened this issue Mar 21, 2019 · 3 comments

Comments

@fhackett
Copy link
Contributor

fhackett commented Mar 21, 2019

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
@fhackett fhackett changed the title Constructing a Type[(A,B)] can sometimes crash Dotty's codegen Codegen crashes on implicit lookup of Type[T], where T is type member of variable from match case Mar 23, 2019
@fhackett
Copy link
Contributor Author

I've updated the issue above with a much more precise description - hopefully that helps.

@nicolasstucki
Copy link
Contributor

It looks like the issue comes from the _ of implicit val _ : Type[v.t] = v.tType, giving it any other name fixes it.

@nicolasstucki
Copy link
Contributor

Can be minimized to

object O {
  def foo = {
    type T
    implicit val _ : scala.quoted.Type[T] = ???
    '[List[T]]
    ()
  }
}

nicolasstucki added a commit that referenced this issue Jul 24, 2019
Fix #6142: remove unreasonable type change for _
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants