@@ -19,6 +19,7 @@ import config.Feature
19
19
import util .SrcPos
20
20
import reporting .*
21
21
import NameKinds .WildcardParamName
22
+ import cc .*
22
23
23
24
object PostTyper {
24
25
val name : String = " posttyper"
@@ -279,6 +280,21 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
279
280
if ! tree.symbol.is(Package ) then tree
280
281
else errorTree(tree, em " ${tree.symbol} cannot be used as a type " )
281
282
283
+ // Cleans up retains annotations in inferred type trees. This is needed because
284
+ // during the typer, it is infeasible to correctly infer the capture sets in most
285
+ // cases, resulting ill-formed capture sets that could crash the pickler later on.
286
+ // See #20035.
287
+ private def cleanupRetainsAnnot (symbol : Symbol , tpt : Tree )(using Context ): Tree =
288
+ tpt match
289
+ case tpt : InferredTypeTree
290
+ if ! symbol.allOverriddenSymbols.hasNext =>
291
+ // if there are overridden symbols, the annotation comes from an explicit type of the overridden symbol
292
+ // and should be retained.
293
+ val tm = new CleanupRetains
294
+ val tpe1 = tm(tpt.tpe)
295
+ tpt.withType(tpe1)
296
+ case _ => tpt
297
+
282
298
override def transform (tree : Tree )(using Context ): Tree =
283
299
try tree match {
284
300
// TODO move CaseDef case lower: keep most probable trees first for performance
@@ -388,7 +404,7 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
388
404
registerIfHasMacroAnnotations(tree)
389
405
checkErasedDef(tree)
390
406
Checking .checkPolyFunctionType(tree.tpt)
391
- val tree1 = cpy.ValDef (tree)(rhs = normalizeErasedRhs(tree.rhs, tree.symbol))
407
+ val tree1 = cpy.ValDef (tree)(tpt = cleanupRetainsAnnot(tree.symbol, tree.tpt), rhs = normalizeErasedRhs(tree.rhs, tree.symbol))
392
408
if tree1.removeAttachment(desugar.UntupledParam ).isDefined then
393
409
checkStableSelection(tree.rhs)
394
410
processValOrDefDef(super .transform(tree1))
@@ -398,7 +414,7 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
398
414
checkErasedDef(tree)
399
415
Checking .checkPolyFunctionType(tree.tpt)
400
416
annotateContextResults(tree)
401
- val tree1 = cpy.DefDef (tree)(rhs = normalizeErasedRhs(tree.rhs, tree.symbol))
417
+ val tree1 = cpy.DefDef (tree)(tpt = cleanupRetainsAnnot(tree.symbol, tree.tpt), rhs = normalizeErasedRhs(tree.rhs, tree.symbol))
402
418
processValOrDefDef(superAcc.wrapDefDef(tree1)(super .transform(tree1).asInstanceOf [DefDef ]))
403
419
case tree : TypeDef =>
404
420
registerIfHasMacroAnnotations(tree)
0 commit comments