Skip to content

Require types to be contextually canonical in AbstractionPatterns #30284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/swift/SIL/AbstractionPattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,10 @@ class AbstractionPattern {
TheKind = unsigned(kind);
OrigType = origType;
GenericSig = CanGenericSignature();
if (OrigType->hasTypeParameter())
if (OrigType->hasTypeParameter()) {
assert(OrigType == signature->getCanonicalTypeInContext(origType));
GenericSig = signature;
}
}

void initClangType(CanGenericSignature signature,
Expand Down
30 changes: 18 additions & 12 deletions lib/SIL/AbstractionPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ TypeConverter::getAbstractionPattern(AbstractStorageDecl *decl,

AbstractionPattern
TypeConverter::getAbstractionPattern(SubscriptDecl *decl, bool isNonObjC) {
auto type = decl->getElementInterfaceType()->getCanonicalType();
CanGenericSignature genericSig;
if (auto sig = decl->getGenericSignatureOfContext())
if (auto sig = decl->getGenericSignatureOfContext()) {
genericSig = sig.getCanonicalSignature();
return AbstractionPattern(genericSig,
decl->getElementInterfaceType()
->getCanonicalType());
type = sig->getCanonicalTypeInContext(type);
}
return AbstractionPattern(genericSig, type);
}

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

AbstractionPattern
TypeConverter::getAbstractionPattern(VarDecl *var, bool isNonObjC) {
CanGenericSignature genericSig;
if (auto sig = var->getDeclContext()->getGenericSignatureOfContext())
genericSig = sig.getCanonicalSignature();

CanType swiftType = var->getInterfaceType()
->getCanonicalType();

CanGenericSignature genericSig;
if (auto sig = var->getDeclContext()->getGenericSignatureOfContext()) {
genericSig = sig.getCanonicalSignature();
swiftType = genericSig->getCanonicalTypeInContext(swiftType);
}

if (isNonObjC)
return AbstractionPattern(genericSig, swiftType);

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

CanType type = decl->getArgumentInterfaceType()->getCanonicalType();

CanGenericSignature genericSig;
if (auto sig = decl->getParentEnum()->getGenericSignatureOfContext())
if (auto sig = decl->getParentEnum()->getGenericSignatureOfContext()) {
genericSig = sig.getCanonicalSignature();
return AbstractionPattern(genericSig,
decl->getArgumentInterfaceType()
->getCanonicalType());
type = genericSig->getCanonicalTypeInContext(type);
}

return AbstractionPattern(genericSig, type);
}

AbstractionPattern::EncodedForeignErrorInfo
Expand Down
4 changes: 4 additions & 0 deletions lib/SIL/SILFunctionType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ CanSILFunctionType SILFunctionType::getAutoDiffDerivativeFunctionType(
// Given a type, returns its formal SIL parameter info.
auto getTangentParameterInfoForOriginalResult =
[&](CanType tanType, ResultConvention origResConv) -> SILParameterInfo {
if (derivativeFnGenSig)
tanType = derivativeFnGenSig->getCanonicalTypeInContext(tanType);
AbstractionPattern pattern(derivativeFnGenSig, tanType);
auto &tl =
TC.getTypeLowering(pattern, tanType, TypeExpansionContext::minimal());
Expand All @@ -317,6 +319,8 @@ CanSILFunctionType SILFunctionType::getAutoDiffDerivativeFunctionType(
// Given a type, returns its formal SIL result info.
auto getTangentResultInfoForOriginalParameter =
[&](CanType tanType, ParameterConvention origParamConv) -> SILResultInfo {
if (derivativeFnGenSig)
tanType = derivativeFnGenSig->getCanonicalTypeInContext(tanType);
AbstractionPattern pattern(derivativeFnGenSig, tanType);
auto &tl =
TC.getTypeLowering(pattern, tanType, TypeExpansionContext::minimal());
Expand Down