Skip to content

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

Closed
@nicolasstucki

Description

@nicolasstucki

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>.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions