Skip to content

Commit 43ca7c6

Browse files
committed
strip nested capturing types before box adaptation
1 parent f2bc25e commit 43ca7c6

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ extension (tp: Type)
121121
case _ =>
122122
tp
123123

124+
def isCapturingType(using Context): Boolean =
125+
tp match
126+
case CapturingType(_, _) => true
127+
case _ => false
128+
124129
extension (sym: Symbol)
125130

126131
/** Does this symbol allow results carrying the universal capability?

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -623,15 +623,19 @@ class CheckCaptures extends Recheck, SymTransformer:
623623
i"adapting $actual $arrow $expected"
624624

625625
def adapt(actual: Type, expected: Type, covariant: Boolean): Type = trace(adaptInfo(actual, expected, covariant), recheckr, show = true) {
626-
val actualTp = actual match
627-
case actual @ CapturingType(parent, cs) =>
628-
(parent, cs, actual.isBoxed)
626+
def destructCapturingType(tp: Type, reconstruct: Type => Type): ((Type, CaptureSet, Boolean), Type => Type) = tp match
627+
case tp @ CapturingType(parent, cs) =>
628+
if parent.isCapturingType then
629+
destructCapturingType(parent, res => reconstruct(tp.derivedCapturingType(res, cs)))
630+
else
631+
((parent, cs, tp.isBoxed), reconstruct)
629632
case actual =>
630-
(actual, CaptureSet(), false)
633+
((actual, CaptureSet(), false), reconstruct)
631634

635+
val (actualTp, recon) = destructCapturingType(actual, x => x)
632636
val (parent1, cs1, isBoxed1) = adaptCapturingType(actualTp, expected, covariant)
633637

634-
CapturingType(parent1, cs1, isBoxed1)
638+
recon(CapturingType(parent1, cs1, isBoxed1))
635639
}
636640

637641
def adaptCapturingType(actual: (Type, CaptureSet, Boolean),
@@ -652,7 +656,8 @@ class CheckCaptures extends Recheck, SymTransformer:
652656
(aargs1, ares1) =>
653657
rinfo.derivedLambdaType(paramInfos = aargs1, resType = ares1)
654658
.toFunctionType(isJava = false, alwaysDependent = true))
655-
case _ => (parent, cs)
659+
case _ =>
660+
(parent, cs)
656661
}
657662

658663
if needsAdaptation then

0 commit comments

Comments
 (0)