Skip to content

Commit f11eaf1

Browse files
committed
Sema: Diagnose accessor exportability.
Properties may have `@_spi` setters and therefore the exportability of a referenced accessor may differ from the exportability of the storage.
1 parent aef5e42 commit f11eaf1

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

lib/Sema/ResilienceDiagnostics.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,18 @@ static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
222222
if (originKind == DisallowedOriginKind::None)
223223
return false;
224224

225+
auto diagName = D->getName();
226+
if (auto accessor = dyn_cast<AccessorDecl>(D)) {
227+
// Only diagnose accessors if their disallowed origin kind differs from
228+
// that of their storage.
229+
if (getDisallowedOriginKind(accessor->getStorage(), where) == originKind)
230+
return false;
231+
232+
// For accessors, diagnose with the name of the storage instead of the
233+
// implicit '_'.
234+
diagName = accessor->getStorage()->getName();
235+
}
236+
225237
ASTContext &ctx = where.getDeclContext()->getASTContext();
226238

227239
auto fragileKind = where.getFragileFunctionKind();
@@ -233,7 +245,7 @@ static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
233245
diag::decl_from_hidden_module;
234246
ctx.Diags.diagnose(loc, errorOrWarning,
235247
D->getDescriptiveKind(),
236-
D->getName(),
248+
diagName,
237249
static_cast<unsigned>(*reason),
238250
definingModule->getName(),
239251
static_cast<unsigned>(originKind));
@@ -250,7 +262,7 @@ static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
250262
diag::inlinable_decl_ref_from_hidden_module;
251263

252264
ctx.Diags.diagnose(loc, errorOrWarning,
253-
D->getDescriptiveKind(), D->getName(),
265+
D->getDescriptiveKind(), diagName,
254266
fragileKind.getSelector(), definingModule->getName(),
255267
static_cast<unsigned>(originKind));
256268

@@ -265,11 +277,6 @@ static bool diagnoseValueDeclRefExportability(SourceLoc loc, const ValueDecl *D,
265277
bool TypeChecker::diagnoseDeclRefExportability(SourceLoc loc,
266278
const ValueDecl *D,
267279
const ExportContext &where) {
268-
// Accessors cannot have exportability that's different than the storage,
269-
// so skip them for now.
270-
if (isa<AccessorDecl>(D))
271-
return false;
272-
273280
if (!where.mustOnlyReferenceExportedDecls())
274281
return false;
275282

test/SPI/local_spi_decls.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,18 @@ public func useOfSPITypeInvalid() -> SPIClass { fatalError() } // expected-error
2626
@_spi(S) public func spiUseOfInternalType() -> InternalClass { fatalError() } // expected-error{{function cannot be declared public because its result uses an internal type}}
2727
@_spi(S) public func spiUseOfPrivateType(_ a: PrivateClass) { fatalError() } // expected-error{{function cannot be declared public because its parameter uses a private type}}
2828

29+
public var globalArrayWithSPISetter: [Int] {
30+
get { fatalError() }
31+
@_spi(S) set {}
32+
}
33+
2934
@inlinable
3035
func inlinable() -> SPIClass { // expected-error {{class 'SPIClass' cannot be used in an '@inlinable' function because it is SPI}}
3136
spiFunc() // expected-error {{global function 'spiFunc()' cannot be used in an '@inlinable' function because it is SPI}}
3237
_ = SPIClass() // expected-error {{class 'SPIClass' cannot be used in an '@inlinable' function because it is SPI}}
3338
// expected-error@-1 {{initializer 'init()' cannot be used in an '@inlinable' function because it is SPI}}
39+
globalArrayWithSPISetter = [] // expected-error {{setter 'globalArrayWithSPISetter' cannot be used in an '@inlinable' function because it is SPI}}
40+
globalArrayWithSPISetter.append(0) // expected-error {{setter 'globalArrayWithSPISetter' cannot be used in an '@inlinable' function because it is SPI}}
3441
}
3542

3643
@_spi(S) public struct SPIStruct {

0 commit comments

Comments
 (0)