Skip to content

Commit 7be53c2

Browse files
committed
Merge remote-tracking branch 'origin/master' into master-rebranch
2 parents 10f7b2d + 15a8619 commit 7be53c2

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

lib/AST/ASTContext.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4408,6 +4408,7 @@ GenericSignature *
44084408
ASTContext::getOverrideGenericSignature(const ValueDecl *base,
44094409
const ValueDecl *derived) {
44104410
auto baseGenericCtx = base->getAsGenericContext();
4411+
auto derivedGenericCtx = derived->getAsGenericContext();
44114412
auto &ctx = base->getASTContext();
44124413

44134414
if (!baseGenericCtx) {
@@ -4431,6 +4432,10 @@ ASTContext::getOverrideGenericSignature(const ValueDecl *base,
44314432
return nullptr;
44324433
}
44334434

4435+
if (!derivedGenericCtx || !derivedGenericCtx->isGeneric()) {
4436+
return nullptr;
4437+
}
4438+
44344439
if (derivedClass->getGenericSignature() == nullptr &&
44354440
!baseGenericCtx->isGeneric()) {
44364441
return nullptr;
@@ -4459,12 +4464,8 @@ ASTContext::getOverrideGenericSignature(const ValueDecl *base,
44594464
GenericSignatureBuilder builder(ctx);
44604465
builder.addGenericSignature(derivedClass->getGenericSignature());
44614466

4462-
if (auto derivedGenericCtx = derived->getAsGenericContext()) {
4463-
if (derivedGenericCtx->isGeneric()) {
4464-
for (auto param : *derivedGenericCtx->getGenericParams()) {
4465-
builder.addGenericParameter(param);
4466-
}
4467-
}
4467+
for (auto param : *derivedGenericCtx->getGenericParams()) {
4468+
builder.addGenericParameter(param);
44684469
}
44694470

44704471
auto source =

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,22 @@ bool swift::isOverrideBasedOnType(ValueDecl *decl, Type declTy,
171171
if (declIUOAttr != parentDeclIUOAttr)
172172
return false;
173173

174+
// If the generic signatures don't match, then return false because we don't
175+
// want to complain if an overridden method matches multiple superclass
176+
// methods which differ in generic signature.
177+
//
178+
// We can still succeed with a subtype match later in
179+
// OverrideMatcher::match().
180+
auto declGenericCtx = decl->getAsGenericContext();
181+
auto &ctx = decl->getASTContext();
182+
auto sig = ctx.getOverrideGenericSignature(parentDecl, decl);
183+
184+
if (sig && declGenericCtx &&
185+
declGenericCtx->getGenericSignature()->getCanonicalSignature() !=
186+
sig->getCanonicalSignature()) {
187+
return false;
188+
}
189+
174190
// If this is a constructor, let's compare only parameter types.
175191
if (isa<ConstructorDecl>(decl)) {
176192
// Within a protocol context, check for a failability mismatch.

test/attr/attr_override.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,3 +597,32 @@ class SR_4206_Derived<C: SR_4206_Container> : SR_4206_Base<C.Key> {
597597
typealias Key = C.Key
598598
override func foo(forKey key: Key) throws {} // Okay, no generic signature mismatch
599599
}
600+
601+
// SR-10198
602+
603+
class SR_10198_Base {
604+
func a<T>(_ val: T) -> String { return "not equatable" }
605+
func a<T: Equatable>(_ val: T) -> String { return "equatable" }
606+
}
607+
608+
class SR_10198_Derived: SR_10198_Base {
609+
override func a<T>(_ val: T) -> String { return super.a(val) } // okay
610+
override func a<T: Equatable>(_ val: T) -> String { return super.a(val) } // okay
611+
}
612+
613+
protocol SR_10198_Base_P {
614+
associatedtype Bar
615+
}
616+
617+
struct SR_10198_Base_S: SR_10198_Base_P {
618+
typealias Bar = Int
619+
}
620+
621+
class SR_10198_Base_1 {
622+
init<F: SR_10198_Base_P>(_ arg: F) where F.Bar == Int {}
623+
}
624+
625+
class SR_10198_Derived_1: SR_10198_Base_1 {
626+
init(_ arg1: Int) { super.init(SR_10198_Base_S()) } // okay, doesn't crash
627+
}
628+

0 commit comments

Comments
 (0)