Skip to content

Generic context parameters with default arguments crash - orphan parameter reference #23828

@GLI-RK0

Description

@GLI-RK0

Compiler version

3.7

Minimized code

When a class parameter is a lambda with context parameters, and one of the parameters is typed as a generic type, attempting to assign a default argument to that parameter crashes the compiler.

The following examples work

// Specific type, without default
class Foo(
   var f: (x: Int) ?=> Int
)
// Specific type, with default
class Foo(
   var f: (x: Int) ?=> Int = 1
)
// Generic type, without default
class Foo[I <: Int](
   var f: (x: I) ?=> Int
)

But the 4th variation crashes the compiler

// Generic type, with default
class Foo[I <: Int](
   var f: (x: I) ?=> Int = 1
)

Output

project.compile java.lang.AssertionError: assertion failed: orphan parameter reference: TypeParamRef(A)
    scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8)
    dotty.tools.dotc.core.tasty.TreePickler.pickleNewType(TreePickler.scala:318)
    dotty.tools.dotc.core.tasty.TreePickler.pickleType(TreePickler.scala:183)
    dotty.tools.dotc.core.tasty.TreePickler.pickleNewType$$anonfun$2(TreePickler.scala:204)
    scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
    scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
    scala.collection.immutable.List.foreach(List.scala:334)
    dotty.tools.dotc.core.tasty.TreePickler.pickleNewType(TreePickler.scala:204)
    dotty.tools.dotc.core.tasty.TreePickler.pickleType(TreePickler.scala:183)
    dotty.tools.dotc.core.tasty.TreePickler.pickleNewType(TreePickler.scala:270)
    dotty.tools.dotc.core.tasty.TreePickler.pickleType(TreePickler.scala:183)
    dotty.tools.dotc.core.tasty.TreePickler.pickleNewType(TreePickler.scala:286)
    dotty.tools.dotc.core.tasty.TreePickler.pickleType(TreePickler.scala:183)
    dotty.tools.dotc.core.tasty.TreePickler.pickleTree(TreePickler.scala:716)
    dotty.tools.dotc.core.tasty.TreePickler.pickleTpt(TreePickler.scala:352)
    dotty.tools.dotc.core.tasty.TreePickler.pickleDef(TreePickler.scala:382)
    dotty.tools.dotc.core.tasty.TreePickler.pickleTree(TreePickler.scala:662)
    dotty.tools.dotc.core.tasty.TreePickler.pickleStats$$anonfun$2(TreePickler.scala:423)
    scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
    scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
    scala.collection.immutable.List.foreach(List.scala:334)
    dotty.tools.dotc.core.tasty.TreePickler.pickleStats(TreePickler.scala:423)
    dotty.tools.dotc.core.tasty.TreePickler.pickleTree(TreePickler.scala:698)
    dotty.tools.dotc.core.tasty.TreePickler.pickleDef(TreePickler.scala:381)
    dotty.tools.dotc.core.tasty.TreePickler.pickleTree(TreePickler.scala:664)
    dotty.tools.dotc.core.tasty.TreePickler.pickleStats$$anonfun$2(TreePickler.scala:423)
    scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
    scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
    scala.collection.immutable.List.foreach(List.scala:334)
    dotty.tools.dotc.core.tasty.TreePickler.pickleStats(TreePickler.scala:423)
    dotty.tools.dotc.core.tasty.TreePickler.pickleTree(TreePickler.scala:714)
    dotty.tools.dotc.core.tasty.TreePickler.pickle$$anonfun$1(TreePickler.scala:938)
    scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
    scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
    scala.collection.immutable.List.foreach(List.scala:334)
    dotty.tools.dotc.core.tasty.TreePickler.pickle(TreePickler.scala:936)
    dotty.tools.dotc.transform.Pickler.run$$anonfun$1$$anonfun$1(Pickler.scala:306)
    scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
    scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
    scala.collection.immutable.List.foreach(List.scala:334)
    dotty.tools.dotc.transform.Pickler.run$$anonfun$1(Pickler.scala:279)
    scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
    scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
    scala.collection.immutable.List.foreach(List.scala:334)
    dotty.tools.dotc.transform.Pickler.run(Pickler.scala:278)
    dotty.tools.dotc.core.Phases$Phase.runOn$$anonfun$1(Phases.scala:383)
    scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
    scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
    scala.collection.immutable.List.foreach(List.scala:334)
    dotty.tools.dotc.core.Phases$Phase.runOn(Phases.scala:376)
    dotty.tools.dotc.transform.Pickler.runPhase$1(Pickler.scala:392)
    dotty.tools.dotc.transform.Pickler.runOn(Pickler.scala:398)
    dotty.tools.dotc.Run.runPhases$1$$anonfun$1(Run.scala:367)
    scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
    scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
    scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1324)
    dotty.tools.dotc.Run.runPhases$1(Run.scala:360)
    dotty.tools.dotc.Run.compileUnits$$anonfun$1$$anonfun$2(Run.scala:407)
    dotty.tools.dotc.Run.compileUnits$$anonfun$1$$anonfun$adapted$1(Run.scala:407)
    scala.Function0.apply$mcV$sp(Function0.scala:42)
    dotty.tools.dotc.Run.showProgress(Run.scala:469)
    dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:407)
    dotty.tools.dotc.Run.compileUnits$$anonfun$adapted$1(Run.scala:419)
    dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:69)
    dotty.tools.dotc.Run.compileUnits(Run.scala:419)
    dotty.tools.dotc.Run.compileSources(Run.scala:306)
    dotty.tools.dotc.Run.compile(Run.scala:291)
    dotty.tools.dotc.Driver.doCompile(Driver.scala:37)
    dotty.tools.xsbt.CompilerBridgeDriver.run(CompilerBridgeDriver.java:141)
    dotty.tools.xsbt.CompilerBridge.run(CompilerBridge.java:22)
    sbt.internal.inc.AnalyzingCompiler.compile(AnalyzingCompiler.scala:91)
    sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$7(MixedAnalyzingCompiler.scala:196)
    scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
    sbt.internal.inc.MixedAnalyzingCompiler.timed(MixedAnalyzingCompiler.scala:252)
    sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$4(MixedAnalyzingCompiler.scala:186)
    sbt.internal.inc.MixedAnalyzingCompiler.$anonfun$compile$4$adapted(MixedAnalyzingCompiler.scala:166)
    sbt.internal.inc.JarUtils$.withPreviousJar(JarUtils.scala:241)
    sbt.internal.inc.MixedAnalyzingCompiler.compileScala$1(MixedAnalyzingCompiler.scala:166)
    sbt.internal.inc.MixedAnalyzingCompiler.compile(MixedAnalyzingCompiler.scala:214)
    sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileInternal$1(IncrementalCompilerImpl.scala:542)
    sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileInternal$1$adapted(IncrementalCompilerImpl.scala:542)
    sbt.internal.inc.Incremental$.$anonfun$apply$3(Incremental.scala:182)
    sbt.internal.inc.Incremental$.$anonfun$apply$3$adapted(Incremental.scala:180)
    sbt.internal.inc.Incremental$$anon$2.run(Incremental.scala:458)
    sbt.internal.inc.IncrementalCommon$CycleState.next(IncrementalCommon.scala:117)
    sbt.internal.inc.IncrementalCommon$$anon$1.next(IncrementalCommon.scala:56)
    sbt.internal.inc.IncrementalCommon$$anon$1.next(IncrementalCommon.scala:52)
    sbt.internal.inc.IncrementalCommon.cycle(IncrementalCommon.scala:263)
    sbt.internal.inc.Incremental$.$anonfun$incrementalCompile$8(Incremental.scala:413)
    sbt.internal.inc.Incremental$.withClassfileManager(Incremental.scala:500)
    sbt.internal.inc.Incremental$.incrementalCompile(Incremental.scala:400)
    sbt.internal.inc.Incremental$.apply(Incremental.scala:208)
    sbt.internal.inc.IncrementalCompilerImpl.compileInternal(IncrementalCompilerImpl.scala:542)
    sbt.internal.inc.IncrementalCompilerImpl.$anonfun$compileIncrementally$1(IncrementalCompilerImpl.scala:496)
    sbt.internal.inc.IncrementalCompilerImpl.handleCompilationError(IncrementalCompilerImpl.scala:332)
    sbt.internal.inc.IncrementalCompilerImpl.compileIncrementally(IncrementalCompilerImpl.scala:433)
    sbt.internal.inc.IncrementalCompilerImpl.compile(IncrementalCompilerImpl.scala:137)
    mill.scalalib.worker.ZincWorkerImpl.compileInternal(ZincWorkerImpl.scala:649)
    mill.scalalib.worker.ZincWorkerImpl.$anonfun$compileMixed$1(ZincWorkerImpl.scala:444)
    mill.scalalib.worker.ZincWorkerImpl.$anonfun$withCompilers$2(ZincWorkerImpl.scala:478)
    mill.api.CachedFactory.withValue(CachedFactory.scala:39)
    mill.scalalib.worker.ZincWorkerImpl.withCompilers(ZincWorkerImpl.scala:477)
    mill.scalalib.worker.ZincWorkerImpl.compileMixed(ZincWorkerImpl.scala:443)
    mill.scalalib.ScalaModule.$anonfun$compile$2(ScalaModule.scala:266)
    mill.define.Task$TraverseCtx.evaluate(Task.scala:259)

It crashes on Scastie, you can see here: https://scastie.scala-lang.org/pngX8c7DS6uJMeF42kmHpQ

```Scala error when pickling type I error when pickling type (I) ?=> Int error when pickling type (x: I) ?=> Int error when pickling type ((x: I) ?=> Int) @uncheckedVariance error when pickling tree ((x: I) ?=> Int) @uncheckedVariance of class class dotty.tools.dotc.ast.Trees$InferredTypeTree error when pickling tree @ContextResultCount(1) def $lessinit$greater$default$1[I >: Nothing <: Int]: ((x: I) ?=> Int) @uncheckedVariance = (using x: I) => 1 of class class dotty.tools.dotc.ast.Trees$DefDef error when pickling tree () extends AnyRef() { this: Playground.Foo.type => private def writeReplace(): AnyRef = new scala.runtime.ModuleSerializationProxy(classOf[Playground.Foo.type]) @ContextResultCount(1) def $lessinit$greater$default$1[I >: Nothing <: Int]: ((x: I) ?=> Int) @uncheckedVariance = (using x: I) => 1 } of class class dotty.tools.dotc.ast.Trees$Template error when pickling tree final module class Foo() extends AnyRef() { this: Playground.Foo.type => private def writeReplace(): AnyRef = new scala.runtime.ModuleSerializationProxy(classOf[Playground.Foo.type]) @ContextResultCount(1) def $lessinit$greater$default$1[I >: Nothing <: Int]: ((x: I) ?=> Int) @uncheckedVariance = (using x: I) => 1 } of class class dotty.tools.dotc.ast.Trees$TypeDef error when pickling tree () extends Object(), com.olegych.scastie.api.runtime.ScastieApp { this: Playground.type => private def writeReplace(): AnyRef = new scala.runtime.ModuleSerializationProxy(classOf[Playground.type]) private[this] val instrumentationMap: scala.collection.mutable.Map[com.olegych.scastie.api.Position, com.olegych.scastie.api.Render] = _root_.scala.collection.mutable.Map.empty[ _root_.com.olegych.scastie.api.Position, _root_.com.olegych.scastie.api.Render] def instrumentations: List[com.olegych.scastie.api.Instrumentation] = Playground.instrumentationMap.toList.map[ com.olegych.scastie.api.Instrumentation](( x$1: (com.olegych.scastie.api.Position, com.olegych.scastie.api.Render)) => x$1 match { case Tuple2.unapply[com.olegych.scastie.api.Position, com.olegych.scastie.api.Render](pos @ _, r @ _) => _root_.com.olegych.scastie.api.Instrumentation.apply(pos, r) } ) class Foo[I >: Nothing <: Int](f: (x: I) ?=> Int) extends Object() { I <: Int var f: (x: I) ?=> Int def f_=(x$1: (x: I) ?=> Int): Unit } final lazy module val Foo: Playground.Foo = new Playground.Foo() final module class Foo() extends AnyRef() { this: Playground.Foo.type => private def writeReplace(): AnyRef = new scala.runtime.ModuleSerializationProxy(classOf[Playground.Foo.type]) @ContextResultCount(1) def $lessinit$greater$default$1[I >: Nothing <: Int] : ((x: I) ?=> Int) @uncheckedVariance = (using x: I) => 1 } } of class class dotty.tools.dotc.ast.Trees$Template error when pickling tree @sourcefile("src/main/scala/main.scala") final module class Playground() extends Object(), com.olegych.scastie.api.runtime.ScastieApp { this: Playground.type => private def writeReplace(): AnyRef = new scala.runtime.ModuleSerializationProxy(classOf[Playground.type]) private[this] val instrumentationMap: scala.collection.mutable.Map[com.olegych.scastie.api.Position, com.olegych.scastie.api.Render] = _root_.scala.collection.mutable.Map.empty[ _root_.com.olegych.scastie.api.Position, _root_.com.olegych.scastie.api.Render] def instrumentations: List[com.olegych.scastie.api.Instrumentation] = Playground.instrumentationMap.toList.map[ com.olegych.scastie.api.Instrumentation](( x$1: (com.olegych.scastie.api.Position, com.olegych.scastie.api.Render)) => x$1 match { case Tuple2.unapply[com.olegych.scastie.api.Position, com.olegych.scastie.api.Render](pos @ _, r @ _) => _root_.com.olegych.scastie.api.Instrumentation.apply(pos, r) } ) class Foo[I >: Nothing <: Int](f: (x: I) ?=> Int) extends Object() { I <: Int var f: (x: I) ?=> Int def f_=(x$1: (x: I) ?=> Int): Unit } final lazy module val Foo: Playground.Foo = new Playground.Foo() final module class Foo() extends AnyRef() { this: Playground.Foo.type => private def writeReplace(): AnyRef = new scala.runtime.ModuleSerializationProxy(classOf[Playground.Foo.type]) @ContextResultCount(1) def $lessinit$greater$default$1[I >: Nothing <: Int] : ((x: I) ?=> Int) @uncheckedVariance = (using x: I) => 1 } } of class class dotty.tools.dotc.ast.Trees$TypeDef error when pickling tree package { import _root_.com.olegych.scastie.api.runtime.* final lazy module val Playground: Playground = new Playground() @sourcefile("src/main/scala/main.scala") final module class Playground() extends Object(), com.olegych.scastie.api.runtime.ScastieApp { this: Playground.type => private def writeReplace(): AnyRef = new scala.runtime.ModuleSerializationProxy(classOf[Playground.type]) private[this] val instrumentationMap:
    scala.collection.mutable.Map[com.olegych.scastie.api.Position,
      com.olegych.scastie.api.Render]
  
 =
  _root_.scala.collection.mutable.Map.empty[
    _root_.com.olegych.scastie.api.Position,
    _root_.com.olegych.scastie.api.Render]
def instrumentations: List[com.olegych.scastie.api.Instrumentation] =
  Playground.instrumentationMap.toList.map[
    com.olegych.scastie.api.Instrumentation]((
    x$1: (com.olegych.scastie.api.Position, com.olegych.scastie.api.Render))
     =>
    x$1 match 
      {
        case 
          Tuple2.unapply[com.olegych.scastie.api.Position,
            com.olegych.scastie.api.Render](pos @ _, r @ _)
         =>
          _root_.com.olegych.scastie.api.Instrumentation.apply(pos, r)
      }
  )
class Foo[I >: Nothing <: Int](f: (x: I) ?=> Int) extends Object() {
  I <: Int
  var f: (x: I) ?=> Int
  def f_=(x$1: (x: I) ?=> Int): Unit
}
final lazy module val Foo: Playground.Foo = new Playground.Foo()
final module class Foo() extends AnyRef() { this: Playground.Foo.type =>
  private def writeReplace(): AnyRef =
    new scala.runtime.ModuleSerializationProxy(classOf[Playground.Foo.type])
  @ContextResultCount(1) def $lessinit$greater$default$1[I >: Nothing <: Int
    ]: ((x: I) ?=> Int) @uncheckedVariance = (using x: I) => 1
}

}
} of class class dotty.tools.dotc.ast.Trees$PackageDef

</details>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions