Skip to content

Commit a0d3542

Browse files
authored
[Typechecker] Covariant subscript check must also take into consideration that there may be a type mismatch (#29828)
1 parent c3be957 commit a0d3542

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,8 @@ bool OverrideMatcher::checkOverride(ValueDecl *baseDecl,
11571157
// Otherwise, if this is a subscript, validate that covariance is ok.
11581158
// If the parent is non-mutable, it's okay to be covariant.
11591159
auto parentSubscript = cast<SubscriptDecl>(baseDecl);
1160-
if (parentSubscript->supportsMutation()) {
1160+
if (parentSubscript->supportsMutation() &&
1161+
attempt != OverrideCheckingAttempt::MismatchedTypes) {
11611162
diags.diagnose(subscript, diag::override_mutable_covariant_subscript,
11621163
declTy, baseTy);
11631164
diags.diagnose(baseDecl, diag::subscript_override_here);

test/attr/attr_override.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ class A {
5959
}
6060
}
6161

62-
// FIXME(SR-10323): The second note is wrong; it should be "potential overridden class subscript 'subscript(_:)' here". This is a preexisting bug.
63-
class subscript (i: String) -> String { // expected-note{{overridden declaration is here}} expected-note{{attempt to override subscript here}}
62+
class subscript (i: String) -> String { // expected-note{{overridden declaration is here}} expected-note{{potential overridden class subscript 'subscript(_:)' here}}
6463
get {
6564
return "hello"
6665
}
@@ -144,8 +143,7 @@ class B : A {
144143
}
145144
}
146145

147-
// FIXME(SR-10323): This error is wrong; it should be "subscript does not override any subscript from its superclass". This is a preexisting bug.
148-
override class subscript (i: Int) -> String { // expected-error{{cannot override mutable subscript of type '(Int) -> String' with covariant type '(String) -> String'}}
146+
override class subscript (i: Int) -> String { // expected-error{{subscript does not override any subscript from its superclass}}
149147
get {
150148
return "hello"
151149
}
@@ -647,4 +645,3 @@ public extension SR_11740_Base where F: SR_11740_Q {
647645
extension SR_11740_Derived where F: SR_11740_P {
648646
public static func foo(_: A) {}
649647
}
650-

0 commit comments

Comments
 (0)