Skip to content

Commit e6e01a3

Browse files
committed
Cleanup retains annotations in PostTyper
1 parent f34ff5d commit e6e01a3

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

compiler/src/dotty/tools/dotc/cc/Setup.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ class Setup extends PreRecheck, SymTransformer, SetupAPI:
454454
case _ => false
455455

456456
def signatureChanges =
457-
tree.tpt.hasRememberedType && !sym.isConstructor || paramSignatureChanges
457+
(tree.tpt.hasRememberedType || tree.tpt.isInstanceOf[InferredTypeTree]) && !sym.isConstructor || paramSignatureChanges
458458

459459
// Replace an existing symbol info with inferred types where capture sets of
460460
// TypeParamRefs and TermParamRefs put in correspondence by BiTypeMaps with the

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import config.Feature
1919
import util.SrcPos
2020
import reporting.*
2121
import NameKinds.WildcardParamName
22+
import cc.*
2223

2324
object PostTyper {
2425
val name: String = "posttyper"
@@ -279,6 +280,17 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
279280
if !tree.symbol.is(Package) then tree
280281
else errorTree(tree, em"${tree.symbol} cannot be used as a type")
281282

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 if !symbol.allOverriddenSymbols.hasNext =>
290+
val tpe1 = cleanupRetains(tpt.tpe)
291+
tpt.withType(tpe1)
292+
case _ => tpt
293+
282294
override def transform(tree: Tree)(using Context): Tree =
283295
try tree match {
284296
// TODO move CaseDef case lower: keep most probable trees first for performance
@@ -388,7 +400,7 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
388400
registerIfHasMacroAnnotations(tree)
389401
checkErasedDef(tree)
390402
Checking.checkPolyFunctionType(tree.tpt)
391-
val tree1 = cpy.ValDef(tree)(rhs = normalizeErasedRhs(tree.rhs, tree.symbol))
403+
val tree1 = cpy.ValDef(tree)(tpt = cleanupRetainsAnnot(tree.symbol, tree.tpt), rhs = normalizeErasedRhs(tree.rhs, tree.symbol))
392404
if tree1.removeAttachment(desugar.UntupledParam).isDefined then
393405
checkStableSelection(tree.rhs)
394406
processValOrDefDef(super.transform(tree1))
@@ -398,7 +410,7 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
398410
checkErasedDef(tree)
399411
Checking.checkPolyFunctionType(tree.tpt)
400412
annotateContextResults(tree)
401-
val tree1 = cpy.DefDef(tree)(rhs = normalizeErasedRhs(tree.rhs, tree.symbol))
413+
val tree1 = cpy.DefDef(tree)(tpt = cleanupRetainsAnnot(tree.symbol, tree.tpt), rhs = normalizeErasedRhs(tree.rhs, tree.symbol))
402414
processValOrDefDef(superAcc.wrapDefDef(tree1)(super.transform(tree1).asInstanceOf[DefDef]))
403415
case tree: TypeDef =>
404416
registerIfHasMacroAnnotations(tree)

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,12 +2187,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
21872187
errorTree(tree, em"Something's wrong: missing original symbol for type tree")
21882188
}
21892189
case _ =>
2190-
val pt1 = cleanupRetains(pt)
2191-
// Cleans up retains annotations in inferred type trees. This is needed because
2192-
// during the typer, it is infeasible to correctly infer the capture sets in most
2193-
// cases, resulting ill-formed capture sets that could crash the pickler later on.
2194-
// See #20035.
2195-
completeTypeTree(InferredTypeTree(), pt1, tree)
2190+
completeTypeTree(InferredTypeTree(), pt, tree)
21962191

21972192
def typedInLambdaTypeTree(tree: untpd.InLambdaTypeTree, pt: Type)(using Context): Tree =
21982193
val tp =

tests/neg-custom-args/captures/byname.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
| Found: (x$0: Int) ->{cap2} Int
1010
| Required: (x$0: Int) -> Int
1111
|
12-
| Note that the expected type Int => Int
12+
| Note that the expected type Int -> Int
1313
| is the previously inferred result type of method test
1414
| which is also the type seen in separately compiled sources.
1515
| The new inferred type (x$0: Int) ->{cap2} Int

0 commit comments

Comments
 (0)