Skip to content

Commit f9cc3f4

Browse files
committed
Just replace the inner abstraction pattern when combining a subtype conversion
into a reabstraction.
1 parent 0b5a803 commit f9cc3f4

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
@@ -1546,39 +1546,43 @@ static bool isMatchedAnyToAnyObjectConversion(CanType from, CanType to) {
15461546
return false;
15471547
}
15481548

1549-
static Conversion withNewInputType(const Conversion &conv,
1550-
AbstractionPattern origType,
1551-
CanType substType,
1552-
SILType loweredType) {
1553-
switch (conv.getKind()) {
1554-
case Conversion::Reabstract:
1555-
return Conversion::getReabstract(origType, substType, loweredType,
1556-
conv.getReabstractionOutputOrigType(),
1557-
conv.getReabstractionOutputSubstType(),
1558-
conv.getReabstractionOutputLoweredType());
1559-
case Conversion::Subtype:
1560-
return Conversion::getSubtype(substType, conv.getBridgingResultType(),
1561-
conv.getBridgingLoweredResultType());
1549+
Conversion
1550+
Conversion::withSourceType(SILGenFunction &SGF, CanType substType) const {
1551+
return withSourceType(AbstractionPattern(substType), substType,
1552+
SGF.getLoweredType(substType));
1553+
}
1554+
1555+
Conversion
1556+
Conversion::withSourceType(AbstractionPattern origType,
1557+
CanType substType, SILType loweredType) const {
1558+
switch (getKind()) {
1559+
case Reabstract:
1560+
return getReabstract(origType, substType, loweredType,
1561+
getReabstractionOutputOrigType(),
1562+
getReabstractionOutputSubstType(),
1563+
getReabstractionOutputLoweredType());
1564+
case Subtype:
1565+
return getSubtype(substType, getResultType(), getLoweredResultType());
15621566
default:
1563-
llvm_unreachable("shouldn't be trying to combine these kinds");
1567+
llvm_unreachable("operation not supported on specialized bridging "
1568+
"conversions");
15641569
}
15651570
}
15661571

1567-
static Conversion withNewOutputType(const Conversion &conv,
1568-
AbstractionPattern origType,
1569-
CanType substType,
1570-
SILType loweredType) {
1571-
switch (conv.getKind()) {
1572-
case Conversion::Reabstract:
1573-
return Conversion::getReabstract(conv.getReabstractionInputOrigType(),
1574-
conv.getReabstractionInputSubstType(),
1575-
conv.getReabstractionInputLoweredType(),
1576-
origType, substType, loweredType);
1577-
case Conversion::Subtype:
1578-
return Conversion::getSubtype(conv.getBridgingSourceType(),
1579-
substType, loweredType);
1572+
Conversion
1573+
Conversion::withResultType(AbstractionPattern origType,
1574+
CanType substType, SILType loweredType) const {
1575+
switch (getKind()) {
1576+
case Reabstract:
1577+
return getReabstract(getReabstractionInputOrigType(),
1578+
getReabstractionInputSubstType(),
1579+
getReabstractionInputLoweredType(),
1580+
origType, substType, loweredType);
1581+
case Subtype:
1582+
return getSubtype(getSourceType(), substType, loweredType);
15801583
default:
1581-
llvm_unreachable("shouldn't be trying to combine these kinds");
1584+
llvm_unreachable("operation not supported on specialized bridging "
1585+
"conversions");
15821586
}
15831587
}
15841588

@@ -1743,12 +1747,10 @@ salvageUncombinableConversion(SILGenFunction &SGF,
17431747

17441748
// Construct the new conversions with the new intermediate type.
17451749
return CombinedConversions(
1746-
withNewOutputType(inner, newIntermediateOrigType,
1747-
newIntermediateSubstType,
1748-
newIntermediateLoweredType),
1749-
withNewInputType(outer, newIntermediateOrigType,
1750-
newIntermediateSubstType,
1751-
newIntermediateLoweredType));
1750+
inner.withResultType(newIntermediateOrigType,
1751+
newIntermediateSubstType,
1752+
newIntermediateLoweredType),
1753+
outer.withSourceType(SGF, newIntermediateSubstType));
17521754
}
17531755
}
17541756

lib/SILGen/SILGenExpr.cpp

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2853,9 +2853,6 @@ wrappedValueAutoclosurePlaceholder(const AbstractClosureExpr *e) {
28532853
static std::optional<FunctionTypeInfo>
28542854
tryGetSpecializedClosureTypeFromContext(CanAnyFunctionType closureType,
28552855
const Conversion &conv) {
2856-
// NOTE: if you support new kinds of conversion here, make sure you can
2857-
// rewrite them in narrowClosureConvention below
2858-
28592856
if (conv.getKind() == Conversion::Reabstract) {
28602857
// We don't care about the input type here; we'll be emitting that
28612858
// based on the closure.
@@ -2881,33 +2878,6 @@ tryGetSpecializedClosureTypeFromContext(CanAnyFunctionType closureType,
28812878
return std::nullopt;
28822879
}
28832880

2884-
/// Given that tryGetSpecializedClosureTypeFromContext was able to return
2885-
/// specialized closure type information from the given contextual conversion,
2886-
/// construct a new conversion that starts from the given type, which is a
2887-
/// supertype of the previous closure type but a subtype of the final type.
2888-
/// The conversion should end with the same type.
2889-
static Conversion narrowClosureConversion(SILGenFunction &SGF,
2890-
CanAnyFunctionType newClosureType,
2891-
const Conversion &conv) {
2892-
if (conv.getKind() == Conversion::Reabstract) {
2893-
auto inputOrigType = conv.getReabstractionInputOrigType();
2894-
auto inputLoweredTy = SGF.getLoweredType(inputOrigType, newClosureType);
2895-
return Conversion::getReabstract(inputOrigType, newClosureType,
2896-
inputLoweredTy,
2897-
conv.getReabstractionOutputOrigType(),
2898-
conv.getReabstractionOutputSubstType(),
2899-
conv.getReabstractionOutputLoweredType());
2900-
}
2901-
2902-
if (conv.getKind() == Conversion::Subtype) {
2903-
return Conversion::getSubtype(newClosureType,
2904-
conv.getBridgingResultType(),
2905-
conv.getBridgingLoweredResultType());
2906-
}
2907-
2908-
llvm_unreachable("mismatch with tryGetSpecializedClosureTypeFromContext");
2909-
}
2910-
29112881
/// Whether the given abstraction pattern as an opaque thrown error.
29122882
static bool hasOpaqueThrownError(const AbstractionPattern &pattern) {
29132883
if (auto thrownPattern = pattern.getFunctionThrownErrorType())
@@ -3020,7 +2990,7 @@ RValueEmitter::tryEmitConvertedClosure(AbstractClosureExpr *e,
30202990
auto erasedResult = emitClosureReference(e, erasureInfo);
30212991

30222992
// Narrow the original conversion to start from the erased closure type.
3023-
auto convAfterErasure = narrowClosureConversion(SGF, erasedClosureType, conv);
2993+
auto convAfterErasure = conv.withSourceType(SGF, erasedClosureType);
30242994

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

0 commit comments

Comments
 (0)