Skip to content

Commit e39c550

Browse files
committed
AST: Add support for @_backDeploy on computed properties, subscripts, and accessors.
1 parent da2ae43 commit e39c550

File tree

6 files changed

+85
-34
lines changed

6 files changed

+85
-34
lines changed

include/swift/AST/Attr.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ DECL_ATTR(exclusivity, Exclusivity,
718718
128)
719719

720720
DECL_ATTR(_backDeploy, BackDeploy,
721-
OnAbstractFunction |
721+
OnAbstractFunction | OnAccessor | OnSubscript | OnVar |
722722
AllowMultipleAttributes | LongAttribute | UserInaccessible |
723723
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIBreakingToRemove,
724724
129)

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3125,6 +3125,8 @@ ERROR(attr_not_on_variadic_parameters,none,
31253125
"'%0' must not be used on variadic parameters", (StringRef))
31263126
ERROR(attr_not_on_subscript_parameters,none,
31273127
"'%0' must not be used on subscript parameters", (StringRef))
3128+
ERROR(attr_not_on_stored_properties,none,
3129+
"'%0' must not be used on stored properties", (DeclAttribute))
31283130

31293131
WARNING(attr_has_no_effect_on_decl_with_access_level,none,
31303132
"'%0' does not have any effect on "

lib/AST/DeclContext.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,8 @@ swift::FragileFunctionKindRequest::evaluate(Evaluator &evaluator,
424424
/*allowUsableFromInline=*/true};
425425
}
426426

427-
// If a property or subscript is @inlinable or @_alwaysEmitIntoClient,
428-
// the accessors are @inlinable or @_alwaysEmitIntoClient also.
427+
// Property and subscript accessors inherit @_alwaysEmitIntoClient,
428+
// @_backDeploy, and @inlinable from their storage declarations.
429429
if (auto accessor = dyn_cast<AccessorDecl>(AFD)) {
430430
auto *storage = accessor->getStorage();
431431
if (storage->getAttrs().getAttribute<InlinableAttr>()) {
@@ -436,6 +436,10 @@ swift::FragileFunctionKindRequest::evaluate(Evaluator &evaluator,
436436
return {FragileFunctionKind::AlwaysEmitIntoClient,
437437
/*allowUsableFromInline=*/true};
438438
}
439+
if (storage->getAttrs().hasAttribute<BackDeployAttr>()) {
440+
return {FragileFunctionKind::BackDeploy,
441+
/*allowUsableFromInline=*/true};
442+
}
439443
}
440444
}
441445
}

lib/Sema/TypeCheckAttr.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3526,6 +3526,15 @@ void AttributeChecker::checkBackDeployAttrs(ArrayRef<BackDeployAttr *> Attrs) {
35263526
if (diagnoseAndRemoveAttrIfDeclIsNonPublic(Attr, /*isError=*/true))
35273527
continue;
35283528

3529+
if (auto *VD = dyn_cast<VarDecl>(D)) {
3530+
// There must be a function body to back deploy so for vars we require
3531+
// that they be computed in order to allow back deployment.
3532+
if (VD->hasStorageOrWrapsStorage()) {
3533+
diagnoseAndRemoveAttr(Attr, diag::attr_not_on_stored_properties, Attr);
3534+
continue;
3535+
}
3536+
}
3537+
35293538
auto AtLoc = Attr->AtLoc;
35303539
auto Platform = Attr->Platform;
35313540

test/ModuleInterface/back-deploy-attr.swift

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,61 @@
1515

1616
public struct TopLevelStruct {
1717
// CHECK: @_backDeploy(macOS 12.0)
18-
// FROMSOURCE: public func backDeployedFunc1_SinglePlatform() -> Swift.Int { return 42 }
19-
// FROMMODULE: public func backDeployedFunc1_SinglePlatform() -> Swift.Int
18+
// FROMSOURCE: public func backDeployedFunc_SinglePlatform() -> Swift.Int { return 42 }
19+
// FROMMODULE: public func backDeployedFunc_SinglePlatform() -> Swift.Int
2020
@available(macOS 11.0, *)
2121
@_backDeploy(macOS 12.0)
22-
public func backDeployedFunc1_SinglePlatform() -> Int { return 42 }
22+
public func backDeployedFunc_SinglePlatform() -> Int { return 42 }
2323

2424
// CHECK: @_backDeploy(macOS 12.0)
2525
// CHECK: @_backDeploy(iOS 15.0)
26-
// FROMSOURCE: public func backDeployedFunc2_MultiPlatform() -> Swift.Int { return 43 }
27-
// FROMMODULE: public func backDeployedFunc2_MultiPlatform() -> Swift.Int
26+
// FROMSOURCE: public func backDeployedFunc_MultiPlatform() -> Swift.Int { return 43 }
27+
// FROMMODULE: public func backDeployedFunc_MultiPlatform() -> Swift.Int
2828
@available(macOS 11.0, iOS 14.0, *)
2929
@_backDeploy(macOS 12.0, iOS 15.0)
30-
public func backDeployedFunc2_MultiPlatform() -> Int { return 43 }
30+
public func backDeployedFunc_MultiPlatform() -> Int { return 43 }
31+
32+
// CHECK: @_backDeploy(macOS 12.0)
33+
// FROMSOURCE: public var backDeployedComputedProperty: Swift.Int {
34+
// FROMSOURCE: get { 44 }
35+
// FROMSOURCE: }
36+
// FROMMODULE: public var backDeployedComputedProperty: Swift.Int
37+
@available(macOS 11.0, *)
38+
@_backDeploy(macOS 12.0)
39+
public var backDeployedComputedProperty: Int { 44 }
40+
41+
// CHECK: @_backDeploy(macOS 12.0)
42+
// FROMSOURCE: public var backDeployedPropertyWithAccessors: Swift.Int {
43+
// FROMSOURCE: get { 45 }
44+
// FROMSOURCE: set(newValue) { print("set property") }
45+
// FROMSOURCE: }
46+
// FROMMODULE: public var backDeployedPropertyWithAccessors: Swift.Int
47+
@available(macOS 11.0, *)
48+
@_backDeploy(macOS 12.0)
49+
public var backDeployedPropertyWithAccessors: Int {
50+
get { 45 }
51+
set(newValue) { print("set property") }
52+
}
53+
54+
// CHECK: @_backDeploy(macOS 12.0)
55+
// FROMSOURCE: public subscript(index: Swift.Int) -> Swift.Int {
56+
// FROMSOURCE: get { 46 }
57+
// FROMSOURCE: set(newValue) { print("set subscript") }
58+
// FROMSOURCE: }
59+
// FROMMODULE: public subscript(index: Swift.Int) -> Swift.Int
60+
@available(macOS 11.0, *)
61+
@_backDeploy(macOS 12.0)
62+
public subscript(index: Int) -> Int {
63+
get { 46 }
64+
set(newValue) { print("set subscript") }
65+
}
3166
}
3267

3368
// CHECK: @_backDeploy(macOS 12.0)
34-
// FROMSOURCE: public func backDeployTopLevelFunc() -> Swift.Int { return 44 }
69+
// FROMSOURCE: public func backDeployTopLevelFunc() -> Swift.Int { return 47 }
3570
// FROMMODULE: public func backDeployTopLevelFunc() -> Swift.Int
3671
@available(macOS 11.0, *)
3772
@_backDeploy(macOS 12.0)
38-
public func backDeployTopLevelFunc() -> Int { return 44 }
73+
public func backDeployTopLevelFunc() -> Int { return 47 }
74+
75+
// FIXME(backDeploy): Availability macros should be supported

test/attr/attr_backDeploy.swift

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,55 @@
22

33
// MARK: - Valid declarations
44

5+
// OK: top level functions
56
@available(macOS 11.0, *)
67
@_backDeploy(macOS 12.0)
78
public func backDeployedTopLevelFunc() {}
89

9-
// OK: @usableFromInline decls may be back deployed
10+
// OK: internal decls may be back deployed when @usableFromInline
1011
@available(macOS 11.0, *)
1112
@_backDeploy(macOS 12.0)
1213
@usableFromInline
1314
internal func backDeployedUsableFromInlineTopLevelFunc() {}
1415

15-
// FIXME(backDeploy): Availability macros should be supported
16-
17-
public class TopLevelClass {
16+
// OK: function decls in a struct
17+
public struct TopLevelStruct {
1818
@available(macOS 11.0, *)
1919
@_backDeploy(macOS 12.0)
20-
final public func backDeployedFinalMethod() {}
20+
public func backDeployedMethod() {}
2121

22-
// FIXME(backDeploy): Computed properties should be supported
2322
@available(macOS 11.0, *)
24-
@_backDeploy(macOS 12.0) // expected-error {{'@_backDeploy' attribute cannot be applied to this declaration}}
23+
@_backDeploy(macOS 12.0)
2524
public var backDeployedComputedProperty: Int { 98 }
25+
}
2626

27-
// FIXME(backDeploy): Subscripts should be supported
27+
public class TopLevelClass {
2828
@available(macOS 11.0, *)
29-
@_backDeploy(macOS 12.0) // expected-error {{'@_backDeploy' attribute cannot be applied to this declaration}}
30-
subscript(index: Int) -> Int {
31-
get { return 1 }
32-
set(newValue) {}
33-
}
34-
}
29+
@_backDeploy(macOS 12.0)
30+
final public func backDeployedFinalMethod() {}
3531

36-
public struct TopLevelStruct {
3732
@available(macOS 11.0, *)
3833
@_backDeploy(macOS 12.0)
39-
public func backDeployedMethod() {}
34+
public var backDeployedComputedProperty: Int { 98 }
4035
}
4136

4237
// MARK: - Unsupported declaration types
4338

4439
@available(macOS 11.0, *)
4540
@_backDeploy(macOS 12.0) // expected-error {{'@_backDeploy' attribute cannot be applied to this declaration}}
46-
public class CannotBackDeployClass {
47-
@available(macOS 11.0, *)
48-
@_backDeploy(macOS 12.0) // expected-error {{'@_backDeploy' attribute cannot be applied to this declaration}}
49-
public var cannotBackDeploystoredProperty: Int = 83
50-
}
41+
public class CannotBackDeployClass {}
5142

5243
@available(macOS 11.0, *)
5344
@_backDeploy(macOS 12.0) // expected-error {{'@_backDeploy' attribute cannot be applied to this declaration}}
54-
public struct CannotBackDeployStruct {}
45+
public struct CannotBackDeployStruct {
46+
@available(macOS 11.0, *)
47+
@_backDeploy(macOS 12.0) // expected-error {{'@_backDeploy' must not be used on stored properties}}
48+
public var cannotBackDeployStoredProperty: Int = 83
49+
50+
@available(macOS 11.0, *)
51+
@_backDeploy(macOS 12.0) // expected-error {{'@_backDeploy' must not be used on stored properties}}
52+
public lazy var cannotBackDeployLazyStoredProperty: Int = 15
53+
}
5554

5655
@available(macOS 11.0, *)
5756
@_backDeploy(macOS 12.0) // expected-error {{'@_backDeploy' attribute cannot be applied to this declaration}}
@@ -62,7 +61,7 @@ public enum CannotBackDeployEnum {
6261
}
6362

6463
@available(macOS 11.0, *)
65-
@_backDeploy(macOS 12.0) // expected-error {{'@_backDeploy' attribute cannot be applied to this declaration}}
64+
@_backDeploy(macOS 12.0) // expected-error {{'@_backDeploy' must not be used on stored properties}}
6665
public var cannotBackDeployTopLevelVar = 79
6766

6867
@available(macOS 11.0, *)

0 commit comments

Comments
 (0)