Skip to content

Emit error in Swift 4 mode for final protocol extensions #8890

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
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/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ class AttributeEarlyChecker : public AttributeVisitor<AttributeEarlyChecker> {
// 'final' only makes sense in the context of a class declaration.
// Reject it on global functions, protocols, structs, enums, etc.
if (!D->getDeclContext()->getAsClassOrClassExtensionContext()) {
if (D->getDeclContext()->getAsProtocolExtensionContext())
if (TC.Context.isSwiftVersion3() &&
D->getDeclContext()->getAsProtocolExtensionContext())
TC.diagnose(attr->getLocation(),
diag::protocol_extension_cannot_be_final)
.fixItRemove(attr->getRange());
Expand Down
8 changes: 8 additions & 0 deletions test/Compatibility/attr_final_protocol_extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: %target-typecheck-verify-swift -swift-version 3
// Generate a swift 3 compatible warning if final is specified in an extension

protocol P {}

extension P {
final func inExtension() {} // expected-warning{{functions in a protocol extension do not need to be marked with 'final'}} {{3-9=}}
}
7 changes: 7 additions & 0 deletions test/attr/attr_final_protocol_extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RUN: %target-typecheck-verify-swift -swift-version 4

protocol P {}

extension P {
final func inExtension() {} // expected-error {{only classes and class members may be marked with 'final'}} {{3-9=}}
}