Skip to content

Commit be576c6

Browse files
committed
Require types to be contextually canonical in AbstractionPatterns.
Because violations of this might have made an AbstractionPattern incorrectly show up as abstract, it's possible that this will cause an ABI change. However, I haven't been able to find any examples where it does, and certainly there's no way we can promise to maintain the old behavior, especially since it's not done consistently.
1 parent b2edf20 commit be576c6

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

include/swift/SIL/AbstractionPattern.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,10 @@ class AbstractionPattern {
323323
TheKind = unsigned(kind);
324324
OrigType = origType;
325325
GenericSig = CanGenericSignature();
326-
if (OrigType->hasTypeParameter())
326+
if (OrigType->hasTypeParameter()) {
327+
assert(OrigType == signature->getCanonicalTypeInContext(origType));
327328
GenericSig = signature;
329+
}
328330
}
329331

330332
void initClangType(CanGenericSignature signature,

lib/SIL/AbstractionPattern.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ TypeConverter::getAbstractionPattern(AbstractStorageDecl *decl,
4646

4747
AbstractionPattern
4848
TypeConverter::getAbstractionPattern(SubscriptDecl *decl, bool isNonObjC) {
49+
auto type = decl->getElementInterfaceType()->getCanonicalType();
4950
CanGenericSignature genericSig;
50-
if (auto sig = decl->getGenericSignatureOfContext())
51+
if (auto sig = decl->getGenericSignatureOfContext()) {
5152
genericSig = sig.getCanonicalSignature();
52-
return AbstractionPattern(genericSig,
53-
decl->getElementInterfaceType()
54-
->getCanonicalType());
53+
type = sig->getCanonicalTypeInContext(type);
54+
}
55+
return AbstractionPattern(genericSig, type);
5556
}
5657

5758
static const clang::Type *getClangType(const clang::Decl *decl) {
@@ -76,13 +77,15 @@ static Bridgeability getClangDeclBridgeability(const clang::Decl *decl) {
7677

7778
AbstractionPattern
7879
TypeConverter::getAbstractionPattern(VarDecl *var, bool isNonObjC) {
79-
CanGenericSignature genericSig;
80-
if (auto sig = var->getDeclContext()->getGenericSignatureOfContext())
81-
genericSig = sig.getCanonicalSignature();
82-
8380
CanType swiftType = var->getInterfaceType()
8481
->getCanonicalType();
8582

83+
CanGenericSignature genericSig;
84+
if (auto sig = var->getDeclContext()->getGenericSignatureOfContext()) {
85+
genericSig = sig.getCanonicalSignature();
86+
swiftType = genericSig->getCanonicalTypeInContext(swiftType);
87+
}
88+
8689
if (isNonObjC)
8790
return AbstractionPattern(genericSig, swiftType);
8891

@@ -109,12 +112,15 @@ AbstractionPattern TypeConverter::getAbstractionPattern(EnumElementDecl *decl) {
109112
"Optional.Some does not have a unique abstraction pattern because "
110113
"optionals are re-abstracted");
111114

115+
CanType type = decl->getArgumentInterfaceType()->getCanonicalType();
116+
112117
CanGenericSignature genericSig;
113-
if (auto sig = decl->getParentEnum()->getGenericSignatureOfContext())
118+
if (auto sig = decl->getParentEnum()->getGenericSignatureOfContext()) {
114119
genericSig = sig.getCanonicalSignature();
115-
return AbstractionPattern(genericSig,
116-
decl->getArgumentInterfaceType()
117-
->getCanonicalType());
120+
type = genericSig->getCanonicalTypeInContext(type);
121+
}
122+
123+
return AbstractionPattern(genericSig, type);
118124
}
119125

120126
AbstractionPattern::EncodedForeignErrorInfo

lib/SIL/SILFunctionType.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ CanSILFunctionType SILFunctionType::getAutoDiffDerivativeFunctionType(
293293
// Given a type, returns its formal SIL parameter info.
294294
auto getTangentParameterInfoForOriginalResult =
295295
[&](CanType tanType, ResultConvention origResConv) -> SILParameterInfo {
296+
if (derivativeFnGenSig)
297+
tanType = derivativeFnGenSig->getCanonicalTypeInContext(tanType);
296298
AbstractionPattern pattern(derivativeFnGenSig, tanType);
297299
auto &tl =
298300
TC.getTypeLowering(pattern, tanType, TypeExpansionContext::minimal());
@@ -317,6 +319,8 @@ CanSILFunctionType SILFunctionType::getAutoDiffDerivativeFunctionType(
317319
// Given a type, returns its formal SIL result info.
318320
auto getTangentResultInfoForOriginalParameter =
319321
[&](CanType tanType, ParameterConvention origParamConv) -> SILResultInfo {
322+
if (derivativeFnGenSig)
323+
tanType = derivativeFnGenSig->getCanonicalTypeInContext(tanType);
320324
AbstractionPattern pattern(derivativeFnGenSig, tanType);
321325
auto &tl =
322326
TC.getTypeLowering(pattern, tanType, TypeExpansionContext::minimal());

0 commit comments

Comments
 (0)