Skip to content

Commit c09fd60

Browse files
committed
[Sema] Resilience: Diagnose uses of init accessors in inlinable contexts if they are not marked as @usableFromInline
(cherry picked from commit 123068c)
1 parent 70fda4a commit c09fd60

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

lib/Sema/ResilienceDiagnostics.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
6363
auto *DC = where.getDeclContext();
6464
auto &Context = DC->getASTContext();
6565

66+
if (auto *init = dyn_cast<ConstructorDecl>(DC)) {
67+
if (init->isDesignatedInit()) {
68+
auto *storage = dyn_cast<AbstractStorageDecl>(D);
69+
if (storage && storage->hasInitAccessor()) {
70+
if (diagnoseInlinableDeclRefAccess(
71+
loc, storage->getAccessor(AccessorKind::Init), where))
72+
return true;
73+
}
74+
}
75+
}
76+
6677
ImportAccessLevel problematicImport = D->getImportAccessFrom(DC);
6778
if (problematicImport.has_value()) {
6879
auto SF = DC->getParentSourceFile();
@@ -111,7 +122,7 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
111122

112123
// Swift 4.2 did not check accessor accessibility.
113124
if (auto accessor = dyn_cast<AccessorDecl>(D)) {
114-
if (!Context.isSwiftVersionAtLeast(5))
125+
if (!accessor->isInitAccessor() && !Context.isSwiftVersionAtLeast(5))
115126
downgradeToWarning = DowngradeToWarning::Yes;
116127
}
117128

test/attr/attr_alwaysEmitIntoClient.swift

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,31 @@ public func publicFunction() {}
1515
internalFunction() // expected-error {{global function 'internalFunction()' is internal and cannot be referenced from an '@_alwaysEmitIntoClient' function}}
1616
versionedFunction()
1717
publicFunction()
18-
}
18+
}
19+
20+
public struct TestInitAccessors {
21+
var _x: Int
22+
23+
public var x: Int {
24+
@storageRestrictions(initializes: _x)
25+
init { // expected-note 2 {{init acecssor for property 'x' is not '@usableFromInline' or public}}
26+
self._x = newValue
27+
}
28+
29+
get {
30+
self._x
31+
}
32+
33+
set {}
34+
}
35+
36+
@_alwaysEmitIntoClient
37+
public init(x: Int) {
38+
self.x = 0 // expected-error {{init acecssor for property 'x' is internal and cannot be referenced from an '@_alwaysEmitIntoClient' function}}
39+
}
40+
41+
@inlinable
42+
public init() {
43+
self.x = 0 // expected-error {{init acecssor for property 'x' is internal and cannot be referenced from an '@inlinable' function}}
44+
}
45+
}

0 commit comments

Comments
 (0)