Skip to content

Commit aef5e42

Browse files
committed
Sema: Inherit SPI groups when synthesizing modify accessor.
Resolves rdar://108069565
1 parent 9c68aaa commit aef5e42

File tree

5 files changed

+60
-4
lines changed

5 files changed

+60
-4
lines changed

lib/Sema/CodeSynthesis.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,3 +1494,15 @@ bool swift::addNonIsolatedToSynthesized(NominalTypeDecl *nominal,
14941494
value->getAttrs().add(new (ctx) NonisolatedAttr(/*isImplicit=*/true));
14951495
return true;
14961496
}
1497+
1498+
void swift::applyInferredSPIAccessControlAttr(Decl *decl,
1499+
const Decl *inferredFromDecl,
1500+
ASTContext &ctx) {
1501+
auto spiGroups = inferredFromDecl->getSPIGroups();
1502+
if (spiGroups.empty())
1503+
return;
1504+
1505+
auto spiAttr =
1506+
SPIAccessControlAttr::create(ctx, SourceLoc(), SourceRange(), spiGroups);
1507+
decl->getAttrs().add(spiAttr);
1508+
}

lib/Sema/CodeSynthesis.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ bool hasLetStoredPropertyWithInitialValue(NominalTypeDecl *nominal);
8989
/// if an attribute was added.
9090
bool addNonIsolatedToSynthesized(NominalTypeDecl *nominal, ValueDecl *value);
9191

92+
/// Adds the `@_spi` groups from \p inferredFromDecl to \p decl.
93+
void applyInferredSPIAccessControlAttr(Decl *decl, const Decl *inferredFromDecl,
94+
ASTContext &ctx);
95+
9296
} // end namespace swift
9397

9498
#endif

lib/Sema/TypeCheckStorage.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,6 +2324,12 @@ createCoroutineAccessorPrototype(AbstractStorageDecl *storage,
23242324
AvailabilityInference::applyInferredAvailableAttrs(accessor,
23252325
asAvailableAs, ctx);
23262326

2327+
// A modify coroutine should have the same SPI visibility as the setter.
2328+
if (kind == AccessorKind::Modify) {
2329+
if (FuncDecl *setter = storage->getParsedAccessor(AccessorKind::Set))
2330+
applyInferredSPIAccessControlAttr(accessor, setter, ctx);
2331+
}
2332+
23272333
finishImplicitAccessor(accessor, ctx);
23282334

23292335
return accessor;

test/SPI/private_swiftinterface.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,22 @@ public class SomeClass {
139139
}
140140

141141
public struct PublicStruct {
142+
@_spi(S) public var spiVar: SomeClass
143+
// CHECK-PRIVATE: @_spi(S) public var spiVar: {{.*}}.SomeClass
144+
// CHECK-PUBLIC-NOT: spiVar
145+
146+
public var publicVarWithSPISet: SomeClass {
147+
get { SomeClass() }
148+
@_spi(S) set {}
149+
}
150+
// CHECK-PRIVATE: public var publicVarWithSPISet: {{.*}}.SomeClass {
151+
// CHECK-PRIVATE-NEXT: get
152+
// CHECK-PRIVATE-NEXT: @_spi(S) set
153+
// CHECK-PRIVATE-NEXT: }
154+
// CHECK-PUBLIC: public var publicVarWithSPISet: {{.*}}.SomeClass {
155+
// CHECK-PUBLIC-NEXT: get
156+
// CHECK-PUBLIC-NEXT: }
157+
142158
@_spi(S) @Wrapper public var spiWrappedSimple: SomeClass
143159
// CHECK-PRIVATE: @_spi(S) @{{.*}}.Wrapper public var spiWrappedSimple: {{.*}}.SomeClass
144160
// CHECK-PUBLIC-NOT: spiWrappedSimple

test/api-digester/ignore-spi-by-given-group-name.swift

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,39 @@
22

33
// RUN: %target-swift-frontend -emit-module -o %t/Foo.swiftmodule -emit-abi-descriptor-path %t/abi-before.json %s -enable-library-evolution -DBASELINE -emit-tbd-path %t/abi-before.tbd
44
// RUN: %target-swift-frontend -emit-module -o %t/Foo.swiftmodule -emit-abi-descriptor-path %t/abi-after.json %s -enable-library-evolution -emit-tbd-path %t/abi-after.tbd
5-
// RUN: %api-digester -diagnose-sdk --input-paths %t/abi-before.json -input-paths %t/abi-after.json -abi -o %t/result.txt
6-
// RUN: %FileCheck %s -check-prefix CHECK_INCLUDE_SPI < %t/result.txt
5+
// RUN: %api-digester -diagnose-sdk --input-paths %t/abi-before.json -input-paths %t/abi-after.json -abi -o %t/include-result.txt
6+
// RUN: %FileCheck %s -check-prefix CHECK_INCLUDE_SPI < %t/include-result.txt
77

8-
// RUN: %api-digester -diagnose-sdk --input-paths %t/abi-before.json -input-paths %t/abi-after.json -abi -o %t/result.txt -ignore-spi-group secret
9-
// RUN: %FileCheck -check-prefix CHECK_EXCLUDE_SPI %s < %t/result.txt
8+
// RUN: %api-digester -diagnose-sdk --input-paths %t/abi-before.json -input-paths %t/abi-after.json -abi -o %t/exclude-result.txt -ignore-spi-group secret
9+
// RUN: %FileCheck -check-prefix CHECK_EXCLUDE_SPI %s < %t/exclude-result.txt
1010

1111
#if BASELINE
1212

13+
public struct Struct {
14+
public var x: Int {
15+
get { 0 }
16+
@_spi(secret) set {}
17+
}
18+
}
19+
1320
@_spi(secret)
1421
public func foo() {}
1522

1623
#else
1724

25+
public struct Struct {
26+
public var x: Int {
27+
get { 0 }
28+
}
29+
}
1830

1931
#endif
2032

33+
// CHECK_INCLUDE_SPI: Accessor Struct.x.Modify() has been removed
34+
// CHECK_EXCLUDE_SPI-NOT: Struct.x.Modify()
35+
36+
// CHECK_INCLUDE_SPI: Accessor Struct.x.Set() has been removed
37+
// CHECK_EXCLUDE_SPI-NOT: Struct.x.Set()
38+
2139
// CHECK_INCLUDE_SPI: Func foo() has been removed
2240
// CHECK_EXCLUDE_SPI-NOT: foo()

0 commit comments

Comments
 (0)