Skip to content

Commit

Permalink
Do not copy symbols in BodyAnnotations
Browse files Browse the repository at this point in the history
  • Loading branch information
mbovel committed Nov 22, 2024
1 parent cb08b46 commit ca3c797
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
16 changes: 10 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/PostTyper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -168,19 +168,23 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
ttm(tree)

/** Transforms the given annotation tree. */
private def transformAnnot(annot: Tree)(using Context): Tree = {
private def transformAnnotTree(annot: Tree)(using Context): Tree = {
val saved = inJavaAnnot
inJavaAnnot = annot.symbol.is(JavaDefined)
if (inJavaAnnot) checkValidJavaAnnotation(annot)
try transform(copySymbols(annot))
try transform(annot)
finally inJavaAnnot = saved
}

private def transformAnnot(annot: Annotation)(using Context): Annotation =
annot.derivedAnnotation(transformAnnot(annot.tree))
val tree1 =
annot match
case _: BodyAnnotation => annot.tree
case _ => copySymbols(annot.tree)
annot.derivedAnnotation(transformAnnotTree(tree1))

/** Transforms all annotations in the given type. */
private def transformAnnots(using Context) =
private def transformAnnotsIn(using Context) =
new TypeMap:
def apply(tp: Type) = tp match
case tp @ AnnotatedType(parent, annot) =>
Expand Down Expand Up @@ -523,7 +527,7 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
Checking.checkRealizable(tree.tpt.tpe, tree.srcPos, "SAM type")
super.transform(tree)
case tree @ Annotated(annotated, annot) =>
cpy.Annotated(tree)(transform(annotated), transformAnnot(annot))
cpy.Annotated(tree)(transform(annotated), transformAnnotTree(annot))
case tree: AppliedTypeTree =>
if (tree.tpt.symbol == defn.andType)
Checking.checkNonCyclicInherited(tree.tpe, tree.args.tpes, EmptyScope, tree.srcPos)
Expand All @@ -546,7 +550,7 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
super.transform(tree)
case tree: TypeTree =>
val tpe = if tree.isInferred then CleanupRetains()(tree.tpe) else tree.tpe
tree.withType(transformAnnots(tpe))
tree.withType(transformAnnotsIn(tpe))
case Typed(Ident(nme.WILDCARD), _) =>
withMode(Mode.Pattern)(super.transform(tree))
// The added mode signals that bounds in a pattern need not
Expand Down
15 changes: 15 additions & 0 deletions tests/pos/annot-body.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// This test checks that symbols in `BodyAnnotation` are not copied in
// `transformAnnot` during `PostTyper`.

package json

trait Reads[A] {
def reads(a: Any): A
}

object JsMacroImpl {
inline def reads[A]: Reads[A] =
new Reads[A] { self =>
def reads(a: Any) = ???
}
}

0 comments on commit ca3c797

Please sign in to comment.