Skip to content

Commit 4af1a8b

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

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

lib/Sema/ResilienceDiagnostics.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,24 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
8383
D->getFormalAccessScope(/*useDC=*/DC,
8484
/*allowUsableFromInline=*/true);
8585

86-
// Public declarations are OK, even if they're SPI or came from an
87-
// implementation-only import. We'll diagnose exportability violations
88-
// from diagnoseDeclRefExportability().
89-
if (declAccessScope.isPublic())
86+
if (declAccessScope.isPublic()) {
87+
// Diagnose private setters accessed from inlinable functions
88+
if (auto *accessor = dyn_cast<AccessorDecl>(D)) {
89+
if (accessor->getAccessorKind() == AccessorKind::Set) {
90+
auto storage = accessor->getStorage();
91+
if (accessor->getFormalAccess() < storage->getFormalAccess()) {
92+
auto diagID = diag::resilience_decl_unavailable;
93+
Context.Diags.diagnose(loc, diagID, D, accessor->getFormalAccess(),
94+
fragileKind.getSelector());
95+
Context.Diags.diagnose(D, diag::resilience_decl_declared_here, D);
96+
}
97+
}
98+
}
99+
// Public declarations are OK, even if they're SPI or came from an
100+
// implementation-only import. We'll diagnose exportability violations
101+
// from diagnoseDeclRefExportability().
90102
return false;
103+
}
91104

92105
// Dynamic declarations were mistakenly not checked in Swift 4.2.
93106
// Do enforce the restriction even in pre-Swift-5 modes if the module we're

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)