@@ -2,7 +2,7 @@ package dotty.tools
2
2
package dotc
3
3
package transform
4
4
5
- import dotty .tools .dotc .ast .{Trees , tpd , untpd , desugar }
5
+ import dotty .tools .dotc .ast .{Trees , tpd , untpd , desugar , TreeTypeMap }
6
6
import scala .collection .mutable
7
7
import core .*
8
8
import dotty .tools .dotc .typer .Checking
@@ -16,7 +16,7 @@ import Symbols.*, NameOps.*
16
16
import ContextFunctionResults .annotateContextResults
17
17
import config .Printers .typr
18
18
import config .Feature
19
- import util .SrcPos
19
+ import util .{ SrcPos , Stats }
20
20
import reporting .*
21
21
import NameKinds .WildcardParamName
22
22
import cc .*
@@ -154,17 +154,39 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
154
154
case _ =>
155
155
case _ =>
156
156
157
+ /** Returns a copy of the given tree with all symbols fresh.
158
+ *
159
+ * Used to guarantee that no symbols are shared between trees in different
160
+ * annotations.
161
+ */
162
+ private def copySymbols (tree : Tree )(using Context ) =
163
+ Stats .trackTime(" Annotations copySymbols" ):
164
+ val ttm =
165
+ new TreeTypeMap :
166
+ override def withMappedSyms (syms : List [Symbol ]) =
167
+ withMappedSyms(syms, mapSymbols(syms, this , true ))
168
+ ttm(tree)
169
+
170
+ /** Transforms the given annotation tree. */
157
171
private def transformAnnot (annot : Tree )(using Context ): Tree = {
158
172
val saved = inJavaAnnot
159
173
inJavaAnnot = annot.symbol.is(JavaDefined )
160
174
if (inJavaAnnot) checkValidJavaAnnotation(annot)
161
- try transform(annot)
175
+ try transform(copySymbols( annot) )
162
176
finally inJavaAnnot = saved
163
177
}
164
178
165
179
private def transformAnnot (annot : Annotation )(using Context ): Annotation =
166
180
annot.derivedAnnotation(transformAnnot(annot.tree))
167
181
182
+ /** Transforms all annotations in the given type. */
183
+ private def transformAnnots (using Context ) =
184
+ new TypeMap :
185
+ def apply (tp : Type ) = tp match
186
+ case tp @ AnnotatedType (parent, annot) =>
187
+ tp.derivedAnnotatedType(mapOver(parent), transformAnnot(annot))
188
+ case _ => mapOver(tp)
189
+
168
190
private def processMemberDef (tree : Tree )(using Context ): tree.type = {
169
191
val sym = tree.symbol
170
192
Checking .checkValidOperator(sym)
@@ -524,11 +546,7 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
524
546
super .transform(tree)
525
547
case tree : TypeTree =>
526
548
val tpe = if tree.isInferred then CleanupRetains ()(tree.tpe) else tree.tpe
527
- tree.withType:
528
- tpe match
529
- case AnnotatedType (parent, annot) =>
530
- AnnotatedType (parent, transformAnnot(annot)) // TODO: Also map annotations embedded in type?
531
- case _ => tpe
549
+ tree.withType(transformAnnots(tpe))
532
550
case Typed (Ident (nme.WILDCARD ), _) =>
533
551
withMode(Mode .Pattern )(super .transform(tree))
534
552
// The added mode signals that bounds in a pattern need not
0 commit comments