Skip to content

Reused bindings in staged code #8178

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
liufengyun opened this issue Feb 3, 2020 · 0 comments · Fixed by #8184
Closed

Reused bindings in staged code #8178

liufengyun opened this issue Feb 3, 2020 · 0 comments · Fixed by #8184

Comments

@liufengyun
Copy link
Contributor

Run on scastie: https://scastie.scala-lang.org/aIv0YMfSSoCH5tuen4bUKg

minimized code

import scala.quoted._
import scala.quoted.staging._

def foo(n: Int, t: Expr[Int])(given QuoteContext): Expr[Int] =
  if (n == 0) t
  else '{ val a = ${Expr(n)}; ${foo(n - 1, 'a)} + $t  }

@main def main = {
  // make available the necessary toolbox for runtime code generation
  given Toolbox = Toolbox.make(getClass.getClassLoader)

  val f: Int = run { foo(2, Expr(5)) }

  println(f)
}

Crash output (click arrow to expand)

class dotty.tools.dotc.reporting.diagnostic.messages$Error at /tmp/scastie2283433749370612327/src/main/scala/main.scala:<162..184>: undefined: [cannot display due to dotty.tools.dotc.core.Symbols$NoSymbol$ cannot be cast to dotty.tools.dotc.core.Symbols$ClassSymbol, raw string = Select(Inlined(EmptyTree,List(),Ident(a)),+)] # 14345: TermRef(TermRef(NoPrefix,val a),+) at quotedFrontend

class dotty.tools.dotc.reporting.diagnostic.messages$Error at /tmp/scastie2283433749370612327/src/main/scala/main.scala:<162..184>: undefined: [cannot display due to dotty.tools.dotc.core.Symbols$NoSymbol$ cannot be cast to dotty.tools.dotc.core.Symbols$ClassSymbol, raw string = Select(Inlined(EmptyTree,List(),Ident(a)),+)] # 14345: TermRef(TermRef(NoPrefix,val a),+) at quotedFrontend
	at dotty.tools.dotc.reporting.Reporting.error(Reporter.scala:135)
	at dotty.tools.dotc.core.Contexts$Context.error(Contexts.scala:75)
	at dotty.tools.dotc.typer.ErrorReporting$.errorType(ErrorReporting.scala:29)
	at dotty.tools.dotc.typer.TypeAssigner.assignType(TypeAssigner.scala:414)
	at dotty.tools.dotc.typer.Typer.assignType(Typer.scala:83)
	at dotty.tools.dotc.ast.tpd$.Apply(tpd.scala:46)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readLengthTerm$1(TreeUnpickler.scala:1091)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readTerm(TreeUnpickler.scala:1212)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readLengthTerm$1(TreeUnpickler.scala:1104)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readTerm(TreeUnpickler.scala:1212)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readLengthTerm$1(TreeUnpickler.scala:1115)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readTerm(TreeUnpickler.scala:1212)
	at dotty.tools.dotc.core.tasty.TreeUnpickler.unpickle(TreeUnpickler.scala:107)
	at dotty.tools.dotc.core.tasty.DottyUnpickler.computeRootTrees(DottyUnpickler.scala:59)
	at dotty.tools.dotc.ast.tpd$TreeProvider.rootTrees(tpd.scala:1107)
	at dotty.tools.dotc.core.tasty.DottyUnpickler.rootTrees(DottyUnpickler.scala:41)
	at dotty.tools.dotc.ast.tpd$TreeProvider.tree(tpd.scala:1111)
	at dotty.tools.dotc.core.tasty.DottyUnpickler.tree(DottyUnpickler.scala:41)
	at dotty.tools.dotc.core.quoted.PickledQuotes$.unpickle(PickledQuotes.scala:131)
	at dotty.tools.dotc.core.quoted.PickledQuotes$.unpickleExpr(PickledQuotes.scala:66)
	at dotty.tools.dotc.tastyreflect.ReflectionCompilerInterface.unpickleExpr(ReflectionCompilerInterface.scala:38)
	at scala.runtime.quoted.Unpickler$.unpickleExpr$direct(Unpickler.scala:16)
	at main$package$.foo(main.scala:6)
	at main$package$.foo$$anonfun$5$2$$anonfun$1(main.scala:6)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readHole(TreeUnpickler.scala:1293)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readLengthTerm$1(TreeUnpickler.scala:1204)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readTerm(TreeUnpickler.scala:1212)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readLengthTerm$1(TreeUnpickler.scala:1115)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readTerm(TreeUnpickler.scala:1212)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.completeSelect$1(TreeUnpickler.scala:1024)
	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.readLengthTerm$1(TreeUnpickler.scala:1104)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readTerm(TreeUnpickler.scala:1212)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readLengthTerm$1(TreeUnpickler.scala:1115)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readTerm(TreeUnpickler.scala:1212)
	at dotty.tools.dotc.core.tasty.TreeUnpickler$TreeReader.readTerm(TreeUnpickler.scala:1006)
	at dotty.tools.dotc.core.tasty.TreeUnpickler.unpickle(TreeUnpickler.scala:107)
	at dotty.tools.dotc.core.tasty.DottyUnpickler.computeRootTrees(DottyUnpickler.scala:59)
	at dotty.tools.dotc.ast.tpd$TreeProvider.rootTrees(tpd.scala:1107)
	at dotty.tools.dotc.core.tasty.DottyUnpickler.rootTrees(DottyUnpickler.scala:41)
	at dotty.tools.dotc.ast.tpd$TreeProvider.tree(tpd.scala:1111)
	at dotty.tools.dotc.core.tasty.DottyUnpickler.tree(DottyUnpickler.scala:41)
	at dotty.tools.dotc.core.quoted.PickledQuotes$.unpickle(PickledQuotes.scala:131)
	at dotty.tools.dotc.core.quoted.PickledQuotes$.unpickleExpr(PickledQuotes.scala:66)
	at dotty.tools.dotc.tastyreflect.ReflectionCompilerInterface.unpickleExpr(ReflectionCompilerInterface.scala:38)
	at scala.runtime.quoted.Unpickler$.unpickleExpr$direct(Unpickler.scala:16)
	at main$package$.foo(main.scala:6)
	at main$package$.$anonfun$1(main.scala:12)
	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$.main(main.scala:12)
	at main.main(main.scala:8)
	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)
@liufengyun liufengyun changed the title Dynamic bindings in staged code Reused bindings in staged code Feb 3, 2020
@liufengyun liufengyun self-assigned this Feb 4, 2020
liufengyun added a commit to dotty-staging/dotty that referenced this issue Feb 4, 2020
Cannot reproduce on master
nicolasstucki added a commit that referenced this issue Feb 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant