Skip to content

Commit 3c3cfe2

Browse files
committed
Fix owner of spices in class statements
Fixes #18358
1 parent 84099a7 commit 3c3cfe2

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

compiler/src/dotty/tools/dotc/quoted/PickledQuotes.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,18 @@ object PickledQuotes {
125125
val quotedType = evalHole.nn.apply(idx, reifyTypeHoleArgs(args))
126126
PickledQuotes.quotedTypeToTree(quotedType)
127127
}
128+
case tree @ Template(constr, parents, self, _) if tree.derived.isEmpty =>
129+
// Set local dummy as owner when we transform class statements
130+
val constr1 = transformSub(constr)
131+
val parents1 = transform(tree.parents)
132+
val self1 = transformSub(self)
133+
val stats1 = flatten(tree.body.mapConserve {
134+
case stat: DefTree => transform(stat)
135+
case stat =>
136+
val localDummy = tree.tpe.termSymbol
137+
transform(stat)(using ctx.withOwner(localDummy))
138+
})
139+
cpy.Template(tree)(constr1, parents1, Nil, self1, stats1)
128140
case tree =>
129141
if tree.isDef then
130142
tree.symbol.annotations = tree.symbol.annotations.map {

tests/pos-macros/i18358/Macro_1.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import scala.quoted.*
2+
inline def generateCode: Unit = ${ testLocalDummyOwner }
3+
def testLocalDummyOwner(using Quotes): Expr[Unit] = '{ trait E { $valInBlock } }
4+
def valInBlock(using Quotes): Expr[Unit] = '{ val x: Int = 2; () }

tests/pos-macros/i18358/Test_2.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def test = generateCode

0 commit comments

Comments
 (0)