Skip to content

Commit 79d1d68

Browse files
committed
Just replace the inner abstraction pattern when combining a subtype conversion
into a reabstraction.
1 parent 3e64f9d commit 79d1d68

File tree

3 files changed

+52
-65
lines changed

3 files changed

+52
-65
lines changed

lib/SILGen/Conversion.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,21 @@ class Conversion {
319319
return getReabstractionOutputLoweredType();
320320
}
321321

322+
/// Given that this conversion is not one of the specialized bridging
323+
/// conversion (i.e. it is either a reabstraction or a subtype conversion),
324+
/// rebuild it with the given source type.
325+
Conversion withSourceType(AbstractionPattern origSourceType,
326+
CanType sourceType,
327+
SILType loweredSourceTy) const;
328+
Conversion withSourceType(SILGenFunction &SGF, CanType sourceType) const;
329+
330+
/// Given that this conversion is not one of the specialized bridging
331+
/// conversion (i.e. it is either a reabstraction or a subtype conversion),
332+
/// rebuild it with the given result type.
333+
Conversion withResultType(AbstractionPattern origResultType,
334+
CanType sourceType,
335+
SILType loweredSourceTy) const;
336+
322337
ManagedValue emit(SILGenFunction &SGF, SILLocation loc,
323338
ManagedValue source, SGFContext ctxt) const;
324339

lib/SILGen/SILGenConvert.cpp

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,39 +1538,43 @@ static bool isMatchedAnyToAnyObjectConversion(CanType from, CanType to) {
15381538
return false;
15391539
}
15401540

1541-
static Conversion withNewInputType(const Conversion &conv,
1542-
AbstractionPattern origType,
1543-
CanType substType,
1544-
SILType loweredType) {
1545-
switch (conv.getKind()) {
1546-
case Conversion::Reabstract:
1547-
return Conversion::getReabstract(origType, substType, loweredType,
1548-
conv.getReabstractionOutputOrigType(),
1549-
conv.getReabstractionOutputSubstType(),
1550-
conv.getReabstractionOutputLoweredType());
1551-
case Conversion::Subtype:
1552-
return Conversion::getSubtype(substType, conv.getBridgingResultType(),
1553-
conv.getBridgingLoweredResultType());
1541+
Conversion
1542+
Conversion::withSourceType(SILGenFunction &SGF, CanType substType) const {
1543+
return withSourceType(AbstractionPattern(substType), substType,
1544+
SGF.getLoweredType(substType));
1545+
}
1546+
1547+
Conversion
1548+
Conversion::withSourceType(AbstractionPattern origType,
1549+
CanType substType, SILType loweredType) const {
1550+
switch (getKind()) {
1551+
case Reabstract:
1552+
return getReabstract(origType, substType, loweredType,
1553+
getReabstractionOutputOrigType(),
1554+
getReabstractionOutputSubstType(),
1555+
getReabstractionOutputLoweredType());
1556+
case Subtype:
1557+
return getSubtype(substType, getResultType(), getLoweredResultType());
15541558
default:
1555-
llvm_unreachable("shouldn't be trying to combine these kinds");
1559+
llvm_unreachable("operation not supported on specialized bridging "
1560+
"conversions");
15561561
}
15571562
}
15581563

1559-
static Conversion withNewOutputType(const Conversion &conv,
1560-
AbstractionPattern origType,
1561-
CanType substType,
1562-
SILType loweredType) {
1563-
switch (conv.getKind()) {
1564-
case Conversion::Reabstract:
1565-
return Conversion::getReabstract(conv.getReabstractionInputOrigType(),
1566-
conv.getReabstractionInputSubstType(),
1567-
conv.getReabstractionInputLoweredType(),
1568-
origType, substType, loweredType);
1569-
case Conversion::Subtype:
1570-
return Conversion::getSubtype(conv.getBridgingSourceType(),
1571-
substType, loweredType);
1564+
Conversion
1565+
Conversion::withResultType(AbstractionPattern origType,
1566+
CanType substType, SILType loweredType) const {
1567+
switch (getKind()) {
1568+
case Reabstract:
1569+
return getReabstract(getReabstractionInputOrigType(),
1570+
getReabstractionInputSubstType(),
1571+
getReabstractionInputLoweredType(),
1572+
origType, substType, loweredType);
1573+
case Subtype:
1574+
return getSubtype(getSourceType(), substType, loweredType);
15721575
default:
1573-
llvm_unreachable("shouldn't be trying to combine these kinds");
1576+
llvm_unreachable("operation not supported on specialized bridging "
1577+
"conversions");
15741578
}
15751579
}
15761580

@@ -1735,12 +1739,10 @@ salvageUncombinableConversion(SILGenFunction &SGF,
17351739

17361740
// Construct the new conversions with the new intermediate type.
17371741
return CombinedConversions(
1738-
withNewOutputType(inner, newIntermediateOrigType,
1739-
newIntermediateSubstType,
1740-
newIntermediateLoweredType),
1741-
withNewInputType(outer, newIntermediateOrigType,
1742-
newIntermediateSubstType,
1743-
newIntermediateLoweredType));
1742+
inner.withResultType(newIntermediateOrigType,
1743+
newIntermediateSubstType,
1744+
newIntermediateLoweredType),
1745+
outer.withSourceType(SGF, newIntermediateSubstType));
17441746
}
17451747
}
17461748

lib/SILGen/SILGenExpr.cpp

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2840,9 +2840,6 @@ wrappedValueAutoclosurePlaceholder(const AbstractClosureExpr *e) {
28402840
static std::optional<FunctionTypeInfo>
28412841
tryGetSpecializedClosureTypeFromContext(CanAnyFunctionType closureType,
28422842
const Conversion &conv) {
2843-
// NOTE: if you support new kinds of conversion here, make sure you can
2844-
// rewrite them in narrowClosureConvention below
2845-
28462843
if (conv.getKind() == Conversion::Reabstract) {
28472844
// We don't care about the input type here; we'll be emitting that
28482845
// based on the closure.
@@ -2868,33 +2865,6 @@ tryGetSpecializedClosureTypeFromContext(CanAnyFunctionType closureType,
28682865
return std::nullopt;
28692866
}
28702867

2871-
/// Given that tryGetSpecializedClosureTypeFromContext was able to return
2872-
/// specialized closure type information from the given contextual conversion,
2873-
/// construct a new conversion that starts from the given type, which is a
2874-
/// supertype of the previous closure type but a subtype of the final type.
2875-
/// The conversion should end with the same type.
2876-
static Conversion narrowClosureConversion(SILGenFunction &SGF,
2877-
CanAnyFunctionType newClosureType,
2878-
const Conversion &conv) {
2879-
if (conv.getKind() == Conversion::Reabstract) {
2880-
auto inputOrigType = conv.getReabstractionInputOrigType();
2881-
auto inputLoweredTy = SGF.getLoweredType(inputOrigType, newClosureType);
2882-
return Conversion::getReabstract(inputOrigType, newClosureType,
2883-
inputLoweredTy,
2884-
conv.getReabstractionOutputOrigType(),
2885-
conv.getReabstractionOutputSubstType(),
2886-
conv.getReabstractionOutputLoweredType());
2887-
}
2888-
2889-
if (conv.getKind() == Conversion::Subtype) {
2890-
return Conversion::getSubtype(newClosureType,
2891-
conv.getBridgingResultType(),
2892-
conv.getBridgingLoweredResultType());
2893-
}
2894-
2895-
llvm_unreachable("mismatch with tryGetSpecializedClosureTypeFromContext");
2896-
}
2897-
28982868
/// Whether the given abstraction pattern as an opaque thrown error.
28992869
static bool hasOpaqueThrownError(const AbstractionPattern &pattern) {
29002870
if (auto thrownPattern = pattern.getFunctionThrownErrorType())
@@ -3007,7 +2977,7 @@ RValueEmitter::tryEmitConvertedClosure(AbstractClosureExpr *e,
30072977
auto erasedResult = emitClosureReference(e, erasureInfo);
30082978

30092979
// Narrow the original conversion to start from the erased closure type.
3010-
auto convAfterErasure = narrowClosureConversion(SGF, erasedClosureType, conv);
2980+
auto convAfterErasure = conv.withSourceType(SGF, erasedClosureType);
30112981

30122982
// Apply the narrowed conversion.
30132983
return convAfterErasure.emit(SGF, e, erasedResult, SGFContext());

0 commit comments

Comments
 (0)