Skip to content

The example in the "Multi-Stage Programming" document does not compile #7897

Closed
@Atry

Description

@Atry

minimized code

The following example is simplified from https://dotty.epfl.ch/docs/reference/metaprogramming/staging.html

import scala.quoted._, staging._

given Toolbox = Toolbox.make(getClass.getClassLoader)

val f: Array[Int] => Int = run {
  val stagedSum: Expr[Array[Int] => Int] = '{ (arr: Array[Int]) => 6 }
  println(stagedSum.show)
  stagedSum
}

@main
def main = {
  f.apply(Array(1, 2, 3)) // Returns 6
}

https://scastie.scala-lang.org/4ZJ45FiQQLO9xZ0R2IFCSw

Stack trace
java.lang.ExceptionInInitializerError
	at main.main(main.scala:12)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at sbt.Run.invokeMain(Run.scala:115)
	at sbt.Run.execute$1(Run.scala:79)
	at sbt.Run.$anonfun$runWithLoader$4(Run.scala:92)
	at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:23)
	at sbt.util.InterfaceUtil$$anon$1.get(InterfaceUtil.scala:10)
	at sbt.TrapExit$App.run(TrapExit.scala:257)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassCastException: dotty.tools.dotc.core.Symbols$NoSymbol$ cannot be cast to dotty.tools.dotc.core.Symbols$ClassSymbol
	at dotty.tools.dotc.core.Symbols$Symbol.asClass(Symbols.scala:539)
	at dotty.tools.dotc.core.Definitions.UnitClass(Definitions.scala:453)
	at dotty.tools.dotc.core.Types$MethodicType.resultSignature(Types.scala:3006)
	at dotty.tools.dotc.core.Types$MethodOrPoly.resultSignature(Types.scala:3147)
	at dotty.tools.dotc.core.Types$MethodType.computeSignature(Types.scala:3306)
	at dotty.tools.dotc.core.Types$SignatureCachingType.signature(Types.scala:2995)
	at dotty.tools.dotc.core.Types$MethodOrPoly.signature(Types.scala:3147)
	at dotty.tools.dotc.core.Denotations$SingleDenotation.signature(Denotations.scala:739)
	at dotty.tools.dotc.core.Denotations$SingleDenotation.atSignature(Denotations.scala:771)
	at dotty.tools.dotc.core.Denotations$SingleDenotation.atSignature(Denotations.scala:769)
	at dotty.tools.dotc.core.tasty.TreeUnpickler.dotty$tools$dotc$core$tasty$TreeUnpickler$TreeReader$$_$accessibleDenot$1(TreeUnpickler.scala:1044)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.completeSelect$1(TreeUnpickler.scala:1026)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readSimpleTerm$1(TreeUnpickler.scala:1058)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readTerm(TreeUnpickler.scala:1212)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readLengthTerm$1(TreeUnpickler.scala:1090)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readTerm(TreeUnpickler.scala:1212)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.$anonfun$3(TreeUnpickler.scala:889)
	at dotty.tools.tasty.TastyReader.collectWhile(TastyReader.scala:137)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readTemplate(TreeUnpickler.scala:892)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readNewDef(TreeUnpickler.scala:818)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readIndexedDef(TreeUnpickler.scala:743)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$Completer.complete(TreeUnpickler.scala:119)
	at dotty.tools.dotc.core.SymDenotations$SymDenotation.completeFrom(SymDenotations.scala:258)
	at dotty.tools.dotc.core.Denotations$Denotation.completeInfo$1(Denotations.scala:185)
	at dotty.tools.dotc.core.Denotations$Denotation.info(Denotations.scala:187)
	at dotty.tools.dotc.core.Denotations$Denotation.completeInfo$1(Denotations.scala:185)
	at dotty.tools.dotc.core.Denotations$Denotation.info(Denotations.scala:187)
	at dotty.tools.dotc.core.SymDenotations$SymDenotation.ensureCompleted(SymDenotations.scala:398)
	at dotty.tools.dotc.core.SymDenotations$SymDenotation.flags(SymDenotations.scala:157)
	at dotty.tools.dotc.core.SymDenotations$ModuleCompleter.complete(SymDenotations.scala:2336)
	at dotty.tools.dotc.core.SymDenotations$SymDenotation.completeFrom(SymDenotations.scala:258)
	at dotty.tools.dotc.core.Denotations$Denotation.completeInfo$1(Denotations.scala:185)
	at dotty.tools.dotc.core.Denotations$Denotation.info(Denotations.scala:187)
	at dotty.tools.dotc.core.Denotations$Denotation.requiredClass(Denotations.scala:341)
	at dotty.tools.dotc.core.Definitions.InternalQuoted_QuoteTypeTagAnnot(Definitions.scala:688)
	at dotty.tools.dotc.core.quoted.PickledQuotes$$anon$2.transform(PickledQuotes.scala:70)
	at dotty.tools.dotc.ast.Trees$Instance$TreeMap.transform(Trees.scala:1291)
	at dotty.tools.dotc.core.quoted.PickledQuotes$$anon$2.transform(PickledQuotes.scala:73)
	at dotty.tools.dotc.core.quoted.PickledQuotes$.unpickleExpr(PickledQuotes.scala:76)
	at dotty.tools.dotc.tastyreflect.ReflectionCompilerInterface.unpickleExpr(ReflectionCompilerInterface.scala:38)
	at scala.runtime.quoted.Unpickler$.unpickleExpr$direct(Unpickler.scala:16)
	at main$package$.$init$$$anonfun$1(main.scala:7)
	at scala.quoted.staging.package$.run$$anonfun$1(staging.scala:19)
	at scala.quoted.staging.QuoteCompiler$QuotedFrontend.runOn$$anonfun$1(QuoteCompiler.scala:71)
	at scala.collection.immutable.List.flatMap(List.scala:265)
	at scala.quoted.staging.QuoteCompiler$QuotedFrontend.runOn(QuoteCompiler.scala:84)
	at dotty.tools.dotc.Run.runPhases$4$$anonfun$4(Run.scala:161)
	at dotty.runtime.function.JProcedure1.apply(JProcedure1.java:15)
	at dotty.runtime.function.JProcedure1.apply(JProcedure1.java:10)
	at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1323)
	at dotty.tools.dotc.Run.runPhases$5(Run.scala:171)
	at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:179)
	at dotty.runtime.function.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
	at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:65)
	at dotty.tools.dotc.Run.compileUnits(Run.scala:186)
	at dotty.tools.dotc.Run.compileUnits(Run.scala:128)
	at scala.quoted.staging.QuoteCompiler$ExprRun.compileExpr(QuoteCompiler.scala:104)
	at scala.quoted.staging.QuoteDriver.run(QuoteDriver.scala:37)
	at scala.quoted.staging.Toolbox$$anon$1.run(Toolbox.scala:36)
	at scala.quoted.staging.package$.run(staging.scala:19)
	at main$package$.<init>(main.scala:10)
	at main$package$.<clinit>(main.scala:4)
	... 12 more

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions