Skip to content

Commit 1842aaa

Browse files
authored
Diagnose typealiases of non-public types in inlinable functions (#23929)
...as a warning, since we didn't check this correctly in Swift 5. For parseable interfaces, these typealiases won't work at all. (cherry picked from commit a15dec8)
1 parent e9f4b15 commit 1842aaa

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

lib/AST/DeclContext.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,8 @@ bool DeclContext::isGenericContext() const {
309309
/// domains, this ensures that only sufficiently-conservative access patterns
310310
/// are used.
311311
ResilienceExpansion DeclContext::getResilienceExpansion() const {
312-
for (const auto *dc = this; dc->isLocalContext(); dc = dc->getParent()) {
312+
for (const auto *dc = getLocalContext(); dc && dc->isLocalContext();
313+
dc = dc->getParent()) {
313314
// Default argument initializer contexts have their resilience expansion
314315
// set when they're type checked.
315316
if (isa<DefaultArgumentInitializer>(dc)) {

lib/Sema/ResilienceDiagnostics.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ using FragileFunctionKind = TypeChecker::FragileFunctionKind;
2525

2626
std::pair<FragileFunctionKind, bool>
2727
TypeChecker::getFragileFunctionKind(const DeclContext *DC) {
28-
for (; DC->isLocalContext(); DC = DC->getParent()) {
28+
for (DC = DC->getLocalContext(); DC && DC->isLocalContext();
29+
DC = DC->getParent()) {
2930
if (isa<DefaultArgumentInitializer>(DC)) {
3031
// Default argument generators of public functions cannot reference
3132
// @usableFromInline declarations; all other fragile function kinds
@@ -176,6 +177,11 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
176177
diagName = accessor->getStorage()->getFullName();
177178
}
178179

180+
// Swift 5.0 did not check the underlying types of local typealiases.
181+
// FIXME: Conditionalize this once we have a new language mode.
182+
if (isa<TypeAliasDecl>(DC))
183+
downgradeToWarning = DowngradeToWarning::Yes;
184+
179185
auto diagID = diag::resilience_decl_unavailable;
180186
if (downgradeToWarning == DowngradeToWarning::Yes)
181187
diagID = diag::resilience_decl_unavailable_warn;

test/attr/attr_inlinable_typealias.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ private typealias PrivateAlias = Int
55

66
internal typealias InternalAlias = Int
77
// expected-note@-1 {{type alias 'InternalAlias' is not '@usableFromInline' or public}}
8+
// expected-note@-2 * {{type alias 'InternalAlias' is not '@usableFromInline' or public}}
89

910
@usableFromInline typealias UsableFromInlineAlias = Int
1011

@@ -21,3 +22,8 @@ public typealias PublicAlias = Int
2122

2223
_ = PublicAlias.self
2324
}
25+
26+
@inlinable public func localTypealiases() {
27+
typealias LocalAlias = InternalAlias // expected-warning {{type alias 'InternalAlias' is internal and should not be referenced from an '@inlinable' function}}
28+
typealias GenericAlias<T> = (T, InternalAlias) // expected-warning {{type alias 'InternalAlias' is internal and should not be referenced from an '@inlinable' function}}
29+
}

0 commit comments

Comments
 (0)