Skip to content

Commit 123068c

Browse files
committed
[Sema] Resilience: Diagnose uses of init accessors in inlinable contexts if they are not marked as @usableFromInline
1 parent 6925940 commit 123068c

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

lib/Sema/ResilienceDiagnostics.cpp

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

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

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

test/attr/attr_alwaysEmitIntoClient.swift

+28-1
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)