Skip to content

[Typechecker] Covariant subscript check must also take into consideration mismatched types #29828

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
merged 1 commit into from
Feb 15, 2020
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
3 changes: 2 additions & 1 deletion lib/Sema/TypeCheckDeclOverride.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,8 @@ bool OverrideMatcher::checkOverride(ValueDecl *baseDecl,
// Otherwise, if this is a subscript, validate that covariance is ok.
// If the parent is non-mutable, it's okay to be covariant.
auto parentSubscript = cast<SubscriptDecl>(baseDecl);
if (parentSubscript->supportsMutation()) {
if (parentSubscript->supportsMutation() &&
attempt != OverrideCheckingAttempt::MismatchedTypes) {
diags.diagnose(subscript, diag::override_mutable_covariant_subscript,
declTy, baseTy);
diags.diagnose(baseDecl, diag::subscript_override_here);
Expand Down
7 changes: 2 additions & 5 deletions test/attr/attr_override.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ class A {
}
}

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

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