Skip to content

Commit 57b1348

Browse files
committed
Sema: Diagnose @_backDeploy attributes on initializers and deinitializers.
Applying the `@_backDeploy` attribute to an initializer currently causes a duplicate symbol error at link time so for now reject it on those declarations for clarity. Also, reject it on deinitializers as well since the attribute cannot have the desired effect on `deinit`. Resolves rdar://94450734
1 parent 18ca90e commit 57b1348

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
@@ -3626,6 +3626,12 @@ void AttributeChecker::checkBackDeployAttrs(ArrayRef<BackDeployAttr *> Attrs) {
36263626
}
36273627
}
36283628

3629+
if (isa<DestructorDecl>(D) || isa<ConstructorDecl>(D)) {
3630+
diagnoseAndRemoveAttr(Attr, diag::attr_invalid_on_decl_kind, Attr,
3631+
D->getDescriptiveKind());
3632+
continue;
3633+
}
3634+
36293635
auto AtLoc = Attr->AtLoc;
36303636
auto Platform = Attr->Platform;
36313637

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)