Skip to content

Commit 5d65bf2

Browse files
mboveltgodzik
authored andcommitted
Do not copy symbols in BodyAnnotations
1 parent f11eac2 commit 5d65bf2

File tree

3 files changed

+25
-26
lines changed

3 files changed

+25
-26
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,19 +146,23 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
146146
ttm(tree)
147147

148148
/** Transforms the given annotation tree. */
149-
private def transformAnnot(annot: Tree)(using Context): Tree = {
149+
private def transformAnnotTree(annot: Tree)(using Context): Tree = {
150150
val saved = inJavaAnnot
151151
inJavaAnnot = annot.symbol.is(JavaDefined)
152152
if (inJavaAnnot) checkValidJavaAnnotation(annot)
153-
try transform(copySymbols(annot))
153+
try transform(annot)
154154
finally inJavaAnnot = saved
155155
}
156156

157157
private def transformAnnot(annot: Annotation)(using Context): Annotation =
158-
annot.derivedAnnotation(transformAnnot(annot.tree))
158+
val tree1 =
159+
annot match
160+
case _: BodyAnnotation => annot.tree
161+
case _ => copySymbols(annot.tree)
162+
annot.derivedAnnotation(transformAnnotTree(tree1))
159163

160164
/** Transforms all annotations in the given type. */
161-
private def transformAnnots(using Context) =
165+
private def transformAnnotsIn(using Context) =
162166
new TypeMap:
163167
def apply(tp: Type) = tp match
164168
case tp @ AnnotatedType(parent, annot) =>
@@ -460,7 +464,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
460464
Checking.checkRealizable(tree.tpt.tpe, tree.srcPos, "SAM type")
461465
super.transform(tree)
462466
case tree @ Annotated(annotated, annot) =>
463-
cpy.Annotated(tree)(transform(annotated), transformAnnot(annot))
467+
cpy.Annotated(tree)(transform(annotated), transformAnnotTree(annot))
464468
case tree: AppliedTypeTree =>
465469
if (tree.tpt.symbol == defn.andType)
466470
Checking.checkNonCyclicInherited(tree.tpe, tree.args.tpes, EmptyScope, tree.srcPos)
@@ -482,7 +486,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
482486
report.error(em"type ${alias.tpe} outside bounds $bounds", tree.srcPos)
483487
super.transform(tree)
484488
case tree: TypeTree =>
485-
tree.withType(transformAnnotsIn(tpe))
489+
tree.withType(transformAnnotsIn(tree.tpe))
486490
case Typed(Ident(nme.WILDCARD), _) =>
487491
withMode(Mode.Pattern)(super.transform(tree))
488492
// The added mode signals that bounds in a pattern need not

tests/pos/annot-body.scala

Lines changed: 15 additions & 0 deletions
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+
}

tests/pos/annot-i20272a.scala

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)