diff --git a/lib/Sema/TypeCheckAttr.cpp b/lib/Sema/TypeCheckAttr.cpp index 45b5de3b8084b..39a81de865689 100644 --- a/lib/Sema/TypeCheckAttr.cpp +++ b/lib/Sema/TypeCheckAttr.cpp @@ -186,7 +186,8 @@ class AttributeEarlyChecker : public AttributeVisitor { // '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()); diff --git a/test/Compatibility/attr_final_protocol_extension.swift b/test/Compatibility/attr_final_protocol_extension.swift new file mode 100644 index 0000000000000..00e116446c158 --- /dev/null +++ b/test/Compatibility/attr_final_protocol_extension.swift @@ -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=}} +} diff --git a/test/attr/attr_final_protocol_extension.swift b/test/attr/attr_final_protocol_extension.swift new file mode 100644 index 0000000000000..cc1c37707aaa4 --- /dev/null +++ b/test/attr/attr_final_protocol_extension.swift @@ -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=}} +}