diff --git a/include/swift/AST/DiagnosticsSema.def b/include/swift/AST/DiagnosticsSema.def index b31c4b067c72c..8eb65cf0dc4f0 100644 --- a/include/swift/AST/DiagnosticsSema.def +++ b/include/swift/AST/DiagnosticsSema.def @@ -1524,6 +1524,9 @@ WARNING(access_control_non_objc_open_member,none, ERROR(invalid_decl_attribute,none, "'%0' attribute cannot be applied to this declaration", (DeclAttribute)) +ERROR(attr_invalid_on_decl_kind,none, + "'%0' attribute cannot be applied to %1 declarations", + (DeclAttribute, DescriptiveDeclKind)) ERROR(invalid_decl_modifier,none, "%0 modifier cannot be applied to this declaration", (DeclAttribute)) ERROR(attribute_does_not_apply_to_type,none, diff --git a/lib/Sema/TypeCheckAttr.cpp b/lib/Sema/TypeCheckAttr.cpp index 2e50d65452457..6e21cb32e10b7 100644 --- a/lib/Sema/TypeCheckAttr.cpp +++ b/lib/Sema/TypeCheckAttr.cpp @@ -3626,6 +3626,12 @@ void AttributeChecker::checkBackDeployAttrs(ArrayRef Attrs) { } } + if (isa(D) || isa(D)) { + diagnoseAndRemoveAttr(Attr, diag::attr_invalid_on_decl_kind, Attr, + D->getDescriptiveKind()); + continue; + } + auto AtLoc = Attr->AtLoc; auto Platform = Attr->Platform; diff --git a/test/attr/attr_backDeploy.swift b/test/attr/attr_backDeploy.swift index d290de07360a0..a107b5630fdc3 100644 --- a/test/attr/attr_backDeploy.swift +++ b/test/attr/attr_backDeploy.swift @@ -128,6 +128,16 @@ extension TopLevelProtocol { @_backDeploy(before: macOS 12.0) // expected-error {{'@_backDeploy' attribute cannot be applied to this declaration}} public class CannotBackDeployClass {} +public final class CannotBackDeployClassInitDeinit { + @available(macOS 11.0, *) + @_backDeploy(before: macOS 12.0) // expected-error {{'@_backDeploy' attribute cannot be applied to initializer declarations}} + public init() {} + + @available(macOS 11.0, *) + @_backDeploy(before: macOS 12.0) // expected-error {{'@_backDeploy' attribute cannot be applied to deinitializer declarations}} + deinit {} +} + @_backDeploy(before: macOS 12.0) // expected-error {{'@_backDeploy' attribute cannot be applied to this declaration}} public struct CannotBackDeployStruct { @_backDeploy(before: macOS 12.0) // expected-error {{'@_backDeploy' must not be used on stored properties}}