Skip to content

Exception when trying to run a class expression #6992

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
anatoliykmetyuk opened this issue Aug 5, 2019 · 3 comments
Closed

Exception when trying to run a class expression #6992

anatoliykmetyuk opened this issue Aug 5, 2019 · 3 comments

Comments

@anatoliykmetyuk
Copy link
Contributor

anatoliykmetyuk commented Aug 5, 2019

minimized code

Macro:

package playground

import scala.quoted._, scala.quoted.matching._
import delegate scala.quoted._
import scala.tasty._

delegate for Toolbox = Toolbox.make(getClass.getClassLoader)

object macros {
  inline def mcr(x: => Any): Any = ${mcrImpl('x)}

  class Foo { val x = 10 }

  def mcrImpl(body: Expr[Any]) given (ctx: QuoteContext): Expr[Any] = {
    import ctx.tasty._
    body match {
      case '{$x: Foo} => run(x).x.toExpr
    }
  }
}

Main:

package playground

import playground.macros._

val foo = new Foo
@main def main = println(mcr { foo })
Error
Error while emitting <quoted.Expr>
[error] -- Error: /Users/anatolii/Projects/dotty/playground/core/src/main/scala/playground/Main.scala:6:29
[error] 6 |@main def main = println(mcr { foo })
[error]   |                         ^^^^^^^^^^^
[error]   |Exception occurred while executing macro expansion.
[error]   |java.lang.AssertionError: assertion failed: Bad superClass for class Any: val <none>
[error]   |	at dotty.DottyPredef$.assertFail(DottyPredef.scala:16)
[error]   |	at dotty.tools.backend.jvm.BTypesFromSymbols.setClassInfo(BTypesFromSymbols.scala:66)
[error]   |	at dotty.tools.backend.jvm.BTypesFromSymbols.classBTypeFromSymbol$$anonfun$4(BTypesFromSymbols.scala:60)
[error]   |	at scala.collection.MapLike.getOrElse(MapLike.scala:131)
[error]   |	at scala.collection.MapLike.getOrElse$(MapLike.scala:129)
[error]   |	at scala.collection.AbstractMap.getOrElse(Map.scala:63)
[error]   |	at dotty.tools.backend.jvm.BTypesFromSymbols.classBTypeFromSymbol(BTypesFromSymbols.scala:61)
[error]   |	at dotty.tools.backend.jvm.BTypesFromSymbols.setClassInfo(BTypesFromSymbols.scala:77)
[error]   |	at dotty.tools.backend.jvm.BTypesFromSymbols.classBTypeFromSymbol$$anonfun$4(BTypesFromSymbols.scala:60)
[error]   |	at scala.collection.MapLike.getOrElse(MapLike.scala:131)
[error]   |	at scala.collection.MapLike.getOrElse$(MapLike.scala:129)
[error]   |	at scala.collection.AbstractMap.getOrElse(Map.scala:63)
[error]   |	at dotty.tools.backend.jvm.BTypesFromSymbols.classBTypeFromSymbol(BTypesFromSymbols.scala:61)
[error]   |	at dotty.tools.backend.jvm.BTypesFromSymbols.setClassInfo(BTypesFromSymbols.scala:77)
[error]   |	at dotty.tools.backend.jvm.BTypesFromSymbols.classBTypeFromSymbol$$anonfun$4(BTypesFromSymbols.scala:60)
[error]   |	at scala.collection.MapLike.getOrElse(MapLike.scala:131)
[error]   |	at scala.collection.MapLike.getOrElse$(MapLike.scala:129)
[error]   |	at scala.collection.AbstractMap.getOrElse(Map.scala:63)
[error]   |	at dotty.tools.backend.jvm.BTypesFromSymbols.classBTypeFromSymbol(BTypesFromSymbols.scala:61)
[error]   |	at dotty.tools.backend.jvm.BCodeHelpers$BCInnerClassGen.getClassBTypeAndRegisterInnerClass(BCodeHelpers.scala:238)
[error]   |	at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.getClassBTypeAndRegisterInnerClass(BCodeSkelBuilder.scala:43)
[error]   |	at dotty.tools.backend.jvm.DottyBackendInterface.dotty$tools$backend$jvm$DottyBackendInterface$$anon$9$$_$primitiveOrClassToBType$3$$anonfun$3(DottyBackendInterface.scala:899)
[error]   |	at scala.collection.MapLike.getOrElse(MapLike.scala:131)
[error]   |	at scala.collection.MapLike.getOrElse$(MapLike.scala:129)
[error]   |	at scala.collection.AbstractMap.getOrElse(Map.scala:63)
[error]   |	at dotty.tools.backend.jvm.DottyBackendInterface$$anon$9.primitiveOrClassToBType$1(DottyBackendInterface.scala:899)
[error]   |	at dotty.tools.backend.jvm.DottyBackendInterface$$anon$9.toTypeKind(DottyBackendInterface.scala:918)
[error]   |	at dotty.tools.backend.jvm.BCodeHelpers$BCInnerClassGen.toTypeKind(BCodeHelpers.scala:267)
[error]   |	at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.toTypeKind(BCodeSkelBuilder.scala:43)
[error]   |	at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.tpeTK(BCodeSkelBuilder.scala:75)
[error]   |	at dotty.tools.backend.jvm.BCodeBodyBuilder$PlainBodyBuilder.genTypeApply(BCodeBodyBuilder.scala:639)
[error]   |	at dotty.tools.backend.jvm.BCodeBodyBuilder$PlainBodyBuilder.genLoad(BCodeBodyBuilder.scala:430)
[error]   |	at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.emitNormalMethodBody$1(BCodeSkelBuilder.scala:597)
[error]   |	at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.genDefDef(BCodeSkelBuilder.scala:630)
[error]   |	at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.gen(BCodeSkelBuilder.scala:501)
[error]   |	at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.gen$$anonfun$1(BCodeSkelBuilder.scala:503)
[error]   |	at dotty.runtime.function.JProcedure1.apply(JProcedure1.java:15)
[error]   |	at dotty.runtime.function.JProcedure1.apply(JProcedure1.java:10)
[error]   |	at scala.collection.immutable.List.foreach(List.scala:392)
[error]   |	at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.gen(BCodeSkelBuilder.scala:503)
[error]   |	at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.genPlainClass(BCodeSkelBuilder.scala:109)
[error]   |	at dotty.tools.backend.jvm.GenBCodePipeline$Worker1.visit(GenBCode.scala:218)
[error]   |	at dotty.tools.backend.jvm.GenBCodePipeline$Worker1.run(GenBCode.scala:185)
[error]   |	at dotty.tools.backend.jvm.GenBCodePipeline.buildAndSendToDisk(GenBCode.scala:515)
[error]   |	at dotty.tools.backend.jvm.GenBCodePipeline.run(GenBCode.scala:481)
[error]   |	at dotty.tools.backend.jvm.GenBCode.run(GenBCode.scala:54)
[error]   |	at dotty.tools.dotc.core.Phases$Phase.runOn$$anonfun$1(Phases.scala:316)
[error]   |	at scala.collection.immutable.List.map(List.scala:286)
[error]   |	at dotty.tools.dotc.core.Phases$Phase.runOn(Phases.scala:318)
[error]   |	at dotty.tools.backend.jvm.GenBCode.runOn(GenBCode.scala:59)
[error]   |	at dotty.tools.dotc.Run.runPhases$4$$anonfun$4(Run.scala:158)
[error]   |	at dotty.runtime.function.JProcedure1.apply(JProcedure1.java:15)
[error]   |	at dotty.runtime.function.JProcedure1.apply(JProcedure1.java:10)
[error]   |	at scala.collection.IndexedSeqOptimized.foreach(IndexedSeqOptimized.scala:36)
[error]   |	at scala.collection.IndexedSeqOptimized.foreach$(IndexedSeqOptimized.scala:33)
[error]   |	at scala.collection.mutable.ArrayOps$ofRef.foreach(ArrayOps.scala:198)
[error]   |	at dotty.tools.dotc.Run.runPhases$5(Run.scala:170)
[error]   |	at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:178)
[error]   |	at dotty.runtime.function.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
[error]   |	at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:65)
[error]   |	at dotty.tools.dotc.Run.compileUnits(Run.scala:185)
[error]   |	at dotty.tools.dotc.Run.compileUnits(Run.scala:125)
[error]   |	at dotty.tools.dotc.quoted.QuoteCompiler$ExprRun.compileExpr(QuoteCompiler.scala:102)
[error]   |	at dotty.tools.dotc.quoted.QuoteDriver.run(QuoteDriver.scala:36)
[error]   |	at dotty.tools.dotc.quoted.ToolboxImpl$$anon$1.run(ToolboxImpl.scala:19)
[error]   |	at scala.quoted.package$.run(package.scala:21)
[error]   |	at playground.macros$.mcrImpl(macros.scala:17)
[error]   |
[error]   | This location is in code that was inlined at Main.scala:6
[error] one error found

expectation

I'd expect to be able to run any expression, given that there are mechanisms in place to ensure their self-consistency (phase consistency principle). If for some reason it is not possible to run expressions of classes, I'd expect a more user-friendly error.

@nicolasstucki
Copy link
Contributor

Here we have a scope extrusion of x, which should be rejected or have a better exception message. This is related to #4730.

anatoliykmetyuk added a commit to anatoliykmetyuk/utest that referenced this issue Aug 6, 2019
@anatoliykmetyuk
Copy link
Contributor Author

Do you think it is not possible to do run on some expressions? What's the cause?

@nicolasstucki
Copy link
Contributor

Because you are trying to precompile code and run that depends on code that has not be compiled yet. It is even worse if you depend on a locally defined value.

nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Aug 7, 2019
This heristic can detects and fails-fast when an Expr or Type is used with a different compiler instance.
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Aug 7, 2019
This heristic can detects and fails-fast when an Expr or Type is used with a different compiler instance.
liufengyun added a commit that referenced this issue Aug 19, 2019
Fix #4730,#6992: Detect scope extrusions in `quoted.run` and fail-fast
anatoliykmetyuk added a commit to anatoliykmetyuk/utest that referenced this issue Nov 8, 2019
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

2 participants