Skip to content

Commit ca3c797

Browse files
committed
Do not copy symbols in BodyAnnotations
1 parent cb08b46 commit ca3c797

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

compiler/src/dotty/tools/dotc/transform/PostTyper.scala

+10-6
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,23 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
168168
ttm(tree)
169169

170170
/** Transforms the given annotation tree. */
171-
private def transformAnnot(annot: Tree)(using Context): Tree = {
171+
private def transformAnnotTree(annot: Tree)(using Context): Tree = {
172172
val saved = inJavaAnnot
173173
inJavaAnnot = annot.symbol.is(JavaDefined)
174174
if (inJavaAnnot) checkValidJavaAnnotation(annot)
175-
try transform(copySymbols(annot))
175+
try transform(annot)
176176
finally inJavaAnnot = saved
177177
}
178178

179179
private def transformAnnot(annot: Annotation)(using Context): Annotation =
180-
annot.derivedAnnotation(transformAnnot(annot.tree))
180+
val tree1 =
181+
annot match
182+
case _: BodyAnnotation => annot.tree
183+
case _ => copySymbols(annot.tree)
184+
annot.derivedAnnotation(transformAnnotTree(tree1))
181185

182186
/** Transforms all annotations in the given type. */
183-
private def transformAnnots(using Context) =
187+
private def transformAnnotsIn(using Context) =
184188
new TypeMap:
185189
def apply(tp: Type) = tp match
186190
case tp @ AnnotatedType(parent, annot) =>
@@ -523,7 +527,7 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
523527
Checking.checkRealizable(tree.tpt.tpe, tree.srcPos, "SAM type")
524528
super.transform(tree)
525529
case tree @ Annotated(annotated, annot) =>
526-
cpy.Annotated(tree)(transform(annotated), transformAnnot(annot))
530+
cpy.Annotated(tree)(transform(annotated), transformAnnotTree(annot))
527531
case tree: AppliedTypeTree =>
528532
if (tree.tpt.symbol == defn.andType)
529533
Checking.checkNonCyclicInherited(tree.tpe, tree.args.tpes, EmptyScope, tree.srcPos)
@@ -546,7 +550,7 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
546550
super.transform(tree)
547551
case tree: TypeTree =>
548552
val tpe = if tree.isInferred then CleanupRetains()(tree.tpe) else tree.tpe
549-
tree.withType(transformAnnots(tpe))
553+
tree.withType(transformAnnotsIn(tpe))
550554
case Typed(Ident(nme.WILDCARD), _) =>
551555
withMode(Mode.Pattern)(super.transform(tree))
552556
// The added mode signals that bounds in a pattern need not

tests/pos/annot-body.scala

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// This test checks that symbols in `BodyAnnotation` are not copied in
2+
// `transformAnnot` during `PostTyper`.
3+
4+
package json
5+
6+
trait Reads[A] {
7+
def reads(a: Any): A
8+
}
9+
10+
object JsMacroImpl {
11+
inline def reads[A]: Reads[A] =
12+
new Reads[A] { self =>
13+
def reads(a: Any) = ???
14+
}
15+
}

0 commit comments

Comments
 (0)