Skip to content

[Sema] Disallow override of dynamic Self return with non-dynamic. #12495

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
Oct 19, 2017
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: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1885,6 +1885,9 @@ ERROR(override_ownership_mismatch,none,
"cannot override %select{strong|weak|unowned|unowned(unsafe)}0 property "
"with %select{strong|weak|unowned|unowned(unsafe)}1 property",
(/*Ownership*/unsigned, /*Ownership*/unsigned))
ERROR(override_dynamic_self_mismatch,none,
"cannot override a Self return type with a non-Self return type",
())
ERROR(override_class_declaration_in_extension,none,
"cannot override a non-dynamic class declaration from an extension",
())
Expand Down
13 changes: 13 additions & 0 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6086,6 +6086,19 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
}
}

// If a super method returns Self, and the subclass overrides it to
// instead return the subclass type, complain.
// This case gets this far because the type matching above specifically
// strips out dynamic self via replaceCovariantResultType(), and that
// is helpful in several cases - just not this one.
if (decl->getASTContext().isSwiftVersionAtLeast(5) &&
matchDecl->getInterfaceType()->hasDynamicSelfType() &&
!decl->getInterfaceType()->hasDynamicSelfType() &&
!classDecl->isFinal()) {
TC.diagnose(decl, diag::override_dynamic_self_mismatch);
TC.diagnose(matchDecl, diag::overridden_here);
}

// Check that the override has the required access level.
// Overrides have to be at least as accessible as what they
// override, except:
Expand Down
19 changes: 19 additions & 0 deletions test/Compatibility/self.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %target-typecheck-verify-swift -swift-version 4

// SR-695
// in version 4 and earlier all of these should build with no diagnostic
class Mario {
func getFriend() -> Self { return self }
func getEnemy() -> Mario { return self }
}
class SuperMario : Mario {
override func getFriend() -> SuperMario {
return SuperMario()
}
override func getEnemy() -> Self { return self }
}
final class FinalMario : Mario {
override func getFriend() -> FinalMario {
return FinalMario()
}
}
20 changes: 19 additions & 1 deletion test/type/self.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-typecheck-verify-swift
// RUN: %target-typecheck-verify-swift -swift-version 5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to test the old behavior in test/Compatibility/self.swift or similar, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do!


struct S0<T> {
func foo(_ other: Self) { } // expected-error{{'Self' is only available in a protocol or as the result of a method in a class; did you mean 'S0'?}}{{21-25=S0}}
Expand All @@ -25,3 +25,21 @@ extension X {
extension X.Inner {
func foo(_ other: Self) { } // expected-error{{'Self' is only available in a protocol or as the result of a method in a class; did you mean 'X.Inner'?}}{{21-25=X.Inner}}
}

// SR-695
class Mario {
func getFriend() -> Self { return self } // expected-note{{overridden declaration is here}}
func getEnemy() -> Mario { return self }
}
class SuperMario : Mario {
override func getFriend() -> SuperMario { // expected-error{{cannot override a Self return type with a non-Self return type}}
return SuperMario()
}
override func getEnemy() -> Self { return self }
}
final class FinalMario : Mario {
override func getFriend() -> FinalMario {
return FinalMario()
}
}