Skip to content
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

Splices in statements of class definition have the wrong owner. #18358

Closed
nicolasstucki opened this issue Aug 8, 2023 · 0 comments · Fixed by #18359
Closed

Splices in statements of class definition have the wrong owner. #18358

nicolasstucki opened this issue Aug 8, 2023 · 0 comments · Fixed by #18359
Labels
area:metaprogramming:quotes Issues related to quotes and splices itype:bug

Comments

@nicolasstucki
Copy link
Contributor

nicolasstucki commented Aug 8, 2023

Compiler version

If you're not sure what version you're using, run print scalaVersion from sbt
(if you're running scalac manually, use scalac -version instead).

Minimized code

import scala.quoted.*
inline def generateCode: Unit = ${ testLocalDummyOwner }
def testLocalDummyOwner(using Quotes): Expr[Unit] = '{ trait E { $valInBlock } }
def valInBlock(using Quotes): Expr[Unit] = '{ val x: Int = 2; () }
def test = generateCode

Output

With -Xcheck-macros

1 |def test = generateCode
  |           ^^^^^^^^^^^^tstrapped / Test / testOnly 3s
  |Malformed tree was found while expanding macro with -Xcheck-macros.
  |               |The tree does not conform to the compiler's tree invariants.
  |               |
  |               |Macro was:
  |               |scala.quoted.runtime.Expr.splice[scala.Unit](((contextual$1: scala.quoted.Quotes) ?=> Macro_1$package.testLocalDummyOwner(contextual$1)))
  |               |
  |               |The macro returned:
  |               |{
  |  trait E() extends java.lang.Object {
  |    {
  |      val x: scala.Int = 2
  |      ()
  |    }
  |  }
  |  ()
  |}
  |               |
  |               |Error:
  |               |assertion failed: bad owner; value x has owner trait E, expected was value <local E>
  |owner chain = value x, trait E, method test, package object Test_2$package, package <empty>, package <root>, ctxOwners = value <local E>, value <local E>, value <local E>, value <local E>, trait E, trait E, method test, method test, method test, method test, method test, method test, method test, method test, package object Test_2$package, package <empty>, package <root>, package <root>, package <root>, package <root>, package <root>, package <root>, <none>, <none>, <none>, <none>, <none>
  |               |
  |stacktrace available when compiling with `-Ydebug`
  |               |
  |---------------------------------------------------------------------------------------------------------------------
  |Inline stack trace
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  |This location contains code that was inlined from Macro_1.scala:2
2 |inline def generateCode: Unit = ${ testLocalDummyOwner }
  |                                ^^^^^^^^^^^^^^^^^^^^^^^^
   ---------------------------------------------------------------------------------------------------------------------

It will also fail with -Ycheck:all.

Without checks

Unexpected tree in genLoad: DefDef(x,List(List()),TypeTree[TypeRef(ThisType(TypeRef(NoPrefix,module class scala)),class Int)],Literal(Constant(2)))/class dotty.tools.dotc.ast.Trees$DefDef at: [206..210..220]
java.lang.RuntimeException: Unexpected tree in genLoad: DefDef(x,List(List()),TypeTree[TypeRef(ThisType(TypeRef(NoPrefix,module class scala)),class Int)],Literal(Constant(2)))/class dotty.tools.dotc.ast.Trees$DefDef at: [206..210..220]
        at dotty.tools.backend.jvm.BCodeHelpers.abort(BCodeHelpers.scala:844)
        at dotty.tools.backend.jvm.BCodeHelpers.abort$(BCodeHelpers.scala:45)
        at dotty.tools.backend.jvm.CodeGen$Impl$.abort(CodeGen.scala:176)
        at dotty.tools.backend.jvm.BCodeBodyBuilder$PlainBodyBuilder.genLoadTo(BCodeBodyBuilder.scala:482)
        at dotty.tools.backend.jvm.BCodeBodyBuilder$PlainBodyBuilder.genLoad(BCodeBodyBuilder.scala:303)
        at dotty.tools.backend.jvm.BCodeBodyBuilder$PlainBodyBuilder.genStat(BCodeBodyBuilder.scala:117)
        at dotty.tools.backend.jvm.BCodeBodyBuilder$PlainBodyBuilder.genBlockTo$$anonfun$1(BCodeBodyBuilder.scala:1087)
        ...

Expectation

The owner of x should be the local dummy <local E>.

@nicolasstucki nicolasstucki added itype:bug area:metaprogramming:quotes Issues related to quotes and splices labels Aug 8, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Aug 8, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Aug 9, 2023
Kordyjan pushed a commit that referenced this issue Dec 1, 2023
Kordyjan pushed a commit that referenced this issue Dec 7, 2023
Kordyjan pushed a commit that referenced this issue Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:metaprogramming:quotes Issues related to quotes and splices itype:bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant