Skip to content

Commit 9580510

Browse files
authored
Merge pull request #28206 from slavapestov/skip-override-check-constrained-extension
Skip override checking for members of constrained extensions
2 parents 631305f + 736343f commit 9580510

File tree

4 files changed

+54
-29
lines changed

4 files changed

+54
-29
lines changed

lib/AST/ASTContext.cpp

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4354,42 +4354,28 @@ ASTContext::getOverrideGenericSignature(const ValueDecl *base,
43544354
auto derivedGenericCtx = derived->getAsGenericContext();
43554355
auto &ctx = base->getASTContext();
43564356

4357-
if (!baseGenericCtx) {
4357+
if (!baseGenericCtx || !derivedGenericCtx)
43584358
return nullptr;
4359-
}
43604359

43614360
auto baseClass = base->getDeclContext()->getSelfClassDecl();
4362-
4363-
if (!baseClass) {
4361+
if (!baseClass)
43644362
return nullptr;
4365-
}
43664363

43674364
auto derivedClass = derived->getDeclContext()->getSelfClassDecl();
4368-
auto baseClassSig = baseClass->getGenericSignature();
4369-
4370-
if (!derivedClass) {
4365+
if (!derivedClass)
43714366
return nullptr;
4372-
}
43734367

4374-
if (derivedClass->getSuperclass().isNull()) {
4368+
if (derivedClass->getSuperclass().isNull())
43754369
return nullptr;
4376-
}
43774370

4378-
if (!derivedGenericCtx || !derivedGenericCtx->isGeneric()) {
4371+
if (baseGenericCtx->getGenericSignature().isNull() ||
4372+
derivedGenericCtx->getGenericSignature().isNull())
43794373
return nullptr;
4380-
}
4381-
4382-
if (derivedClass->getGenericSignature().isNull() &&
4383-
!baseGenericCtx->isGeneric()) {
4384-
return nullptr;
4385-
}
43864374

4375+
auto baseClassSig = baseClass->getGenericSignature();
43874376
auto subMap = derivedClass->getSuperclass()->getContextSubstitutionMap(
43884377
derivedClass->getModuleContext(), baseClass);
43894378

4390-
if (baseGenericCtx->getGenericSignature().isNull()) {
4391-
return nullptr;
4392-
}
43934379
unsigned derivedDepth = 0;
43944380

43954381
auto key = OverrideSignatureKey(baseGenericCtx->getGenericSignature(),
@@ -4405,9 +4391,11 @@ ASTContext::getOverrideGenericSignature(const ValueDecl *base,
44054391
derivedDepth = derivedSig->getGenericParams().back()->getDepth() + 1;
44064392

44074393
SmallVector<GenericTypeParamType *, 2> addedGenericParams;
4408-
for (auto gp : *derivedGenericCtx->getGenericParams()) {
4409-
addedGenericParams.push_back(
4410-
gp->getDeclaredInterfaceType()->castTo<GenericTypeParamType>());
4394+
if (auto *gpList = derivedGenericCtx->getGenericParams()) {
4395+
for (auto gp : *gpList) {
4396+
addedGenericParams.push_back(
4397+
gp->getDeclaredInterfaceType()->castTo<GenericTypeParamType>());
4398+
}
44114399
}
44124400

44134401
unsigned baseDepth = 0;

lib/AST/Decl.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,16 +1221,21 @@ bool ExtensionDecl::hasValidParent() const {
12211221
}
12221222

12231223
bool ExtensionDecl::isConstrainedExtension() const {
1224-
// Non-generic extension.
1225-
if (!getGenericSignature())
1224+
auto nominal = getExtendedNominal();
1225+
if (!nominal)
12261226
return false;
12271227

1228-
auto nominal = getExtendedNominal();
1229-
assert(nominal);
1228+
auto typeSig = nominal->getGenericSignature();
1229+
if (!typeSig)
1230+
return false;
1231+
1232+
auto extSig = getGenericSignature();
1233+
if (!extSig)
1234+
return false;
12301235

12311236
// If the generic signature differs from that of the nominal type, it's a
12321237
// constrained extension.
1233-
return !getGenericSignature()->isEqual(nominal->getGenericSignature());
1238+
return !typeSig->isEqual(extSig);
12341239
}
12351240

12361241
bool ExtensionDecl::isEquivalentToExtendedContext() const {

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ static bool areOverrideCompatibleSimple(ValueDecl *decl,
234234
return false;
235235
}
236236

237+
// Ignore declarations that are defined inside constrained extensions.
238+
if (auto *ext = dyn_cast<ExtensionDecl>(parentDecl->getDeclContext()))
239+
if (ext->isConstrainedExtension())
240+
return false;
241+
237242
// The declarations must be of the same kind.
238243
if (decl->getKind() != parentDecl->getKind())
239244
return false;
@@ -1169,6 +1174,11 @@ bool swift::checkOverrides(ValueDecl *decl) {
11691174
// Otherwise, we have more checking to do.
11701175
}
11711176

1177+
// Members of constrained extensions are not considered to be overrides.
1178+
if (auto *ext = dyn_cast<ExtensionDecl>(decl->getDeclContext()))
1179+
if (ext->isConstrainedExtension())
1180+
return false;
1181+
11721182
// Accessor methods get overrides through their storage declaration, and
11731183
// all checking can be performed via that mechanism.
11741184
if (isa<AccessorDecl>(decl)) {

test/attr/attr_override.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,3 +626,25 @@ class SR_10198_Derived_1: SR_10198_Base_1 {
626626
init(_ arg1: Int) { super.init(SR_10198_Base_S()) } // okay, doesn't crash
627627
}
628628

629+
// SR-11740
630+
631+
public class SR_11740_Base<F, A> {}
632+
633+
public class SR_11740_Derived<F, A>
634+
: SR_11740_Base<SR_11740_Base<F, A>, A>,
635+
SR_11740_Q {}
636+
637+
public protocol SR_11740_P {}
638+
639+
public protocol SR_11740_Q: SR_11740_P {
640+
associatedtype A
641+
}
642+
643+
public extension SR_11740_Base where F: SR_11740_Q {
644+
static func foo(_: F.A) {}
645+
}
646+
647+
extension SR_11740_Derived where F: SR_11740_P {
648+
public static func foo(_: A) {}
649+
}
650+

0 commit comments

Comments
 (0)