@@ -303,20 +303,19 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
303
303
if ! tree.symbol.is(Package ) then tree
304
304
else errorTree(tree, em " ${tree.symbol} cannot be used as a type " )
305
305
306
- // Cleans up retains annotations in inferred type trees. This is needed because
307
- // during the typer, it is infeasible to correctly infer the capture sets in most
308
- // cases, resulting ill-formed capture sets that could crash the pickler later on.
309
- // See #20035.
310
- private def cleanupRetainsAnnot (symbol : Symbol , tpt : Tree )(using Context ): Tree =
306
+ /** Make result types of ValDefs and DefDefs that override some other definitions
307
+ * declared types rather than InferredTypes. This is necessary since we otherwise
308
+ * clean retains annotations from such types. But for an overriding symbol the
309
+ * retains annotations come from the explicitly declared parent types, so should
310
+ * be kept.
311
+ */
312
+ private def makeOverrideTypeDeclared (symbol : Symbol , tpt : Tree )(using Context ): Tree =
311
313
tpt match
312
314
case tpt : InferredTypeTree
313
- if ! symbol.allOverriddenSymbols.hasNext =>
314
- // if there are overridden symbols, the annotation comes from an explicit type of the overridden symbol
315
- // and should be retained.
316
- val tm = new CleanupRetains
317
- val tpe1 = tm(tpt.tpe)
318
- tpt.withType(tpe1)
319
- case _ => tpt
315
+ if symbol.allOverriddenSymbols.hasNext =>
316
+ TypeTree (tpt.tpe, inferred = false ).withSpan(tpt.span).withAttachmentsFrom(tpt)
317
+ case _ =>
318
+ tpt
320
319
321
320
override def transform (tree : Tree )(using Context ): Tree =
322
321
try tree match {
@@ -432,7 +431,7 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
432
431
registerIfHasMacroAnnotations(tree)
433
432
checkErasedDef(tree)
434
433
Checking .checkPolyFunctionType(tree.tpt)
435
- val tree1 = cpy.ValDef (tree)(tpt = cleanupRetainsAnnot (tree.symbol, tree.tpt), rhs = normalizeErasedRhs(tree.rhs, tree.symbol))
434
+ val tree1 = cpy.ValDef (tree)(tpt = makeOverrideTypeDeclared (tree.symbol, tree.tpt), rhs = normalizeErasedRhs(tree.rhs, tree.symbol))
436
435
if tree1.removeAttachment(desugar.UntupledParam ).isDefined then
437
436
checkStableSelection(tree.rhs)
438
437
processValOrDefDef(super .transform(tree1))
@@ -441,7 +440,7 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
441
440
checkErasedDef(tree)
442
441
Checking .checkPolyFunctionType(tree.tpt)
443
442
annotateContextResults(tree)
444
- val tree1 = cpy.DefDef (tree)(tpt = cleanupRetainsAnnot (tree.symbol, tree.tpt), rhs = normalizeErasedRhs(tree.rhs, tree.symbol))
443
+ val tree1 = cpy.DefDef (tree)(tpt = makeOverrideTypeDeclared (tree.symbol, tree.tpt), rhs = normalizeErasedRhs(tree.rhs, tree.symbol))
445
444
processValOrDefDef(superAcc.wrapDefDef(tree1)(super .transform(tree1).asInstanceOf [DefDef ]))
446
445
case tree : TypeDef =>
447
446
registerIfHasMacroAnnotations(tree)
@@ -524,12 +523,12 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
524
523
report.error(em " type ${alias.tpe} outside bounds $bounds" , tree.srcPos)
525
524
super .transform(tree)
526
525
case tree : TypeTree =>
527
- tree.withType(
528
- tree.tpe match {
529
- case AnnotatedType ( tpe, annot) => AnnotatedType (tpe, transformAnnot(annot))
530
- case tpe => tpe
531
- }
532
- )
526
+ 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
533
532
case Typed (Ident (nme.WILDCARD ), _) =>
534
533
withMode(Mode .Pattern )(super .transform(tree))
535
534
// The added mode signals that bounds in a pattern need not
0 commit comments