Skip to content

Commit 1dca620

Browse files
committed
Diagnosing a private setter being accessed from the inlinable function.
rdar://81879146
1 parent 3754042 commit 1dca620

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

lib/Sema/ResilienceDiagnostics.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
7878
// Remember that the module defining the decl must be imported publicly.
7979
recordRequiredImportAccessLevelForDecl(D, DC, AccessLevel::Public, loc);
8080

81+
// Accessing a private setter from inlinable function is deprecated
82+
if (auto *accessor = dyn_cast<AccessorDecl>(D)) {
83+
if (accessor->getAccessorKind() == AccessorKind::Set &&
84+
accessor->getFormalAccess() == AccessLevel::Private) {
85+
auto *storage = accessor->getStorage();
86+
if (storage->getAttrs().hasAttribute<UsableFromInlineAttr>()) {
87+
auto diagID = diag::resilience_decl_unavailable;
88+
Context.Diags.diagnose(loc, diagID, D, accessor->getFormalAccess(),
89+
fragileKind.getSelector());
90+
Context.Diags.diagnose(D, diag::resilience_decl_declared_here, D);
91+
}
92+
}
93+
}
94+
8195
// General check on access-level of the decl.
8296
auto declAccessScope =
8397
D->getFormalAccessScope(/*useDC=*/DC,

test/attr/attr_inlinable.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,17 @@ public struct HasInternalSetProperty {
314314
}
315315
}
316316

317+
public struct HasUsableFromInlinePrivateSetProperty {
318+
@usableFromInline private(set) var bytes: UnsafeMutableRawPointer // expected-note {{setter for property 'bytes' is not '@usableFromInline' or public}}
319+
public init() {
320+
self.bytes = UnsafeMutableRawPointer.allocate(byteCount: 1024, alignment: 8)
321+
}
322+
@inlinable
323+
public mutating func resetPointer() {
324+
self.bytes = UnsafeMutableRawPointer.allocate(byteCount: 2048, alignment: 8) // expected-error {{setter for property 'bytes' is private and cannot be referenced from an '@inlinable' function}}
325+
}
326+
}
327+
317328
@usableFromInline protocol P {
318329
typealias T = Int
319330
}

0 commit comments

Comments
 (0)