Skip to content

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

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
Atry opened this issue Jan 6, 2020 · 8 comments
Closed

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

Atry opened this issue Jan 6, 2020 · 8 comments

Comments

@Atry
Copy link
Contributor

Atry commented Jan 6, 2020

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
@nicolasstucki
Copy link
Contributor

It does work on master and with https://github.com/lampepfl/dotty-staging.g8. This is probably an issue in Scastie. I noticed that Scastie is still using sbt-dotty 0.3.1 instead of 0.3.4.

@nicolasstucki
Copy link
Contributor

Update to 0.3.4 in scalacenter/scastie#454

nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jan 6, 2020
nicolasstucki added a commit that referenced this issue Jan 6, 2020
@julienrf
Copy link
Contributor

julienrf commented Jan 6, 2020

It still fails with sbt-dotty 0.3.4: https://scastie.scala-lang.org/BUGFYfWmStuS5I6Nlo034w

@nicolasstucki can you confirm that a Dotty project executed from within sbt does work?

@OlegYch
Copy link
Contributor

OlegYch commented Jan 6, 2020

this seems sbt related
with sbt 1.3.6 the code compiles, and runs fine but only with fork := true

with sbt 1.2.8 i get a compile error:
[error] -- [E007] Type Mismatch Error: D:\Distrib\Download\0\src\main\scala\main.scala:12:0
[error] 12 |@main
[error] |^^^^^
[error] |Found: main
[error] |Required: scala.annotation.Annotation
[error] one error found
[error] (Compile / compileIncremental) Compilation failed

@nicolasstucki
Copy link
Contributor

Then my guess is that we do not get the correct class path for the scala lib within the forked VM. I will investigate.

@OlegYch
Copy link
Contributor

OlegYch commented Jan 6, 2020

@nicolasstucki it works fine with fork, perhaps it's something to do with new layered classloaders in sbt 1.3, dunno

@nicolasstucki
Copy link
Contributor

The bug is most likely here.

@nicolasstucki
Copy link
Contributor

Indeed, the LayeredClassloader does not provide the path to the library (scala-library-2.13.1.jar)

nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jan 10, 2020
sbt 1.3 introduced the `sbt.internal.LayeredClassLoader` which is a `URLClassloader`
which layers the classpath into different Classloaders. To make sure we recover
`scala-library-XYZ.jar` we need to get the URLs from the parent classloaders as well.
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jan 10, 2020
* Make classpathFromClassloader non quadratic
* Use `java.class.path` as a fallback path
smarter pushed a commit to dotty-staging/dotty that referenced this issue Jan 13, 2020
smarter added a commit to dotty-staging/dotty that referenced this issue Jan 14, 2020
The tests ensure that we've fixed scala#7897.

Co-Authored-By: Guillaume Martres <[email protected]>
smarter added a commit to dotty-staging/dotty that referenced this issue Jan 14, 2020
The tests ensure that we've fixed scala#7897.

Co-Authored-By: Guillaume Martres <[email protected]>
smarter added a commit to dotty-staging/dotty that referenced this issue Jan 15, 2020
The tests ensure that we've fixed scala#7897.

Co-Authored-By: Guillaume Martres <[email protected]>
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

5 participants