Skip to content

Commit e63df1e

Browse files
authored
Merge pull request #59283 from tshortli/diagnose-back-deploy-on-init-deinit
Sema: Diagnose `@_backDeploy` attributes on initializers and deinitializers
2 parents 0fe2d7b + 6de3268 commit e63df1e

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,9 @@ WARNING(access_control_non_objc_open_member,none,
15241524

15251525
ERROR(invalid_decl_attribute,none,
15261526
"'%0' attribute cannot be applied to this declaration", (DeclAttribute))
1527+
ERROR(attr_invalid_on_decl_kind,none,
1528+
"'%0' attribute cannot be applied to %1 declarations",
1529+
(DeclAttribute, DescriptiveDeclKind))
15271530
ERROR(invalid_decl_modifier,none,
15281531
"%0 modifier cannot be applied to this declaration", (DeclAttribute))
15291532
ERROR(attribute_does_not_apply_to_type,none,

lib/Sema/TypeCheckAttr.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3662,6 +3662,12 @@ void AttributeChecker::checkBackDeployAttrs(ArrayRef<BackDeployAttr *> Attrs) {
36623662
}
36633663
}
36643664

3665+
if (isa<DestructorDecl>(D) || isa<ConstructorDecl>(D)) {
3666+
diagnoseAndRemoveAttr(Attr, diag::attr_invalid_on_decl_kind, Attr,
3667+
D->getDescriptiveKind());
3668+
continue;
3669+
}
3670+
36653671
auto AtLoc = Attr->AtLoc;
36663672
auto Platform = Attr->Platform;
36673673

test/attr/attr_backDeploy.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ extension TopLevelProtocol {
128128
@_backDeploy(before: macOS 12.0) // expected-error {{'@_backDeploy' attribute cannot be applied to this declaration}}
129129
public class CannotBackDeployClass {}
130130

131+
public final class CannotBackDeployClassInitDeinit {
132+
@available(macOS 11.0, *)
133+
@_backDeploy(before: macOS 12.0) // expected-error {{'@_backDeploy' attribute cannot be applied to initializer declarations}}
134+
public init() {}
135+
136+
@available(macOS 11.0, *)
137+
@_backDeploy(before: macOS 12.0) // expected-error {{'@_backDeploy' attribute cannot be applied to deinitializer declarations}}
138+
deinit {}
139+
}
140+
131141
@_backDeploy(before: macOS 12.0) // expected-error {{'@_backDeploy' attribute cannot be applied to this declaration}}
132142
public struct CannotBackDeployStruct {
133143
@_backDeploy(before: macOS 12.0) // expected-error {{'@_backDeploy' must not be used on stored properties}}

0 commit comments

Comments
 (0)