Skip to content

Commit c5b67b2

Browse files
committed
Don't add capture sets to types that cannot capture anything
1 parent ca3aa83 commit c5b67b2

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

compiler/src/dotty/tools/dotc/CaptureOps.scala

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ object CaptureOps:
5757
getBoxed(tp, enabled = false)
5858

5959
/** If this type appears as an expected type of a term, does it imply
60-
* that the term should be boxed? ^^^ Special treat Any?
60+
* that the term should be boxed?
61+
* ^^^ Special treat Any? - but the current status is more conservative in that
62+
* it counts free variables in expressions that have Any as expected type.
6163
*/
6264
def needsBox(using Context): Boolean = tp match
6365
case _: TypeVar => true
@@ -73,25 +75,15 @@ object CaptureOps:
7375
case _ => false
7476

7577
def canHaveInferredCapture(using Context): Boolean = tp match
76-
case tp: CapturingType =>
77-
false
78-
case tp: TypeRef =>
79-
if tp.symbol.isClass then
80-
!tp.symbol.isValueClass && tp.symbol != defn.AnyClass
81-
else
82-
tp.underlying.canHaveInferredCapture
78+
case tp: TypeRef if tp.symbol.isClass =>
79+
!tp.symbol.isValueClass && tp.symbol != defn.AnyClass
8380
case tp: TypeProxy =>
84-
tp.underlying.canHaveInferredCapture
81+
tp.superType.canHaveInferredCapture
8582
case tp: AndType =>
8683
tp.tp1.canHaveInferredCapture && tp.tp2.canHaveInferredCapture
8784
case tp: OrType =>
8885
tp.tp1.canHaveInferredCapture || tp.tp2.canHaveInferredCapture
8986
case _ =>
9087
false
9188

92-
def addCaptureVars(using Context): Type =
93-
if ctx.settings.Ycc.value && canHaveInferredCapture then
94-
CapturingType(tp, CaptureSet.Var()) // ^^^ go deep
95-
else
96-
tp
9789
end CaptureOps

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,14 @@ class CheckCaptures extends Recheck:
7272
case _ =>
7373
mapOver(t)
7474
def addVars(tp: Type): Type =
75-
val tp1 = tp match
76-
case tp @ AppliedType(tycon, args) =>
77-
tp.derivedAppliedType(tycon, args.map(addVars))
78-
case _ => // ^^^ go deeper in other types as well
79-
tp
80-
CapturingType(tp1, CaptureSet.Var())
75+
if tp.canHaveInferredCapture then
76+
val tp1 = tp match
77+
case tp @ AppliedType(tycon, args) =>
78+
tp.derivedAppliedType(tycon, args.map(addVars))
79+
case _ => // ^^^ go deeper in other types as well
80+
tp
81+
CapturingType(tp1, CaptureSet.Var())
82+
else tp
8183
tp match
8284
case tp: MethodOrPoly =>
8385
tp.derivedLambdaType(resType = reinfer(tp.resType))

0 commit comments

Comments
 (0)