Skip to content

Commit ee6e190

Browse files
authored
Sema: Infer '@_usableFromInline' imports by looking at references from inlinable functions (#16326)
Basic fix for <rdar://problem/39338239>, but there are still some more cases we haven't handled yet.
1 parent d037d29 commit ee6e190

File tree

5 files changed

+59
-3
lines changed

5 files changed

+59
-3
lines changed

include/swift/AST/Module.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ class SourceFile final : public FileUnit {
791791
/// This is the list of modules that are imported by this module.
792792
///
793793
/// This is filled in by the Name Binding phase.
794-
ArrayRef<std::pair<ModuleDecl::ImportedModule, ImportOptions>> Imports;
794+
MutableArrayRef<std::pair<ModuleDecl::ImportedModule, ImportOptions>> Imports;
795795

796796
/// A unique identifier representing this file; used to mark private decls
797797
/// within the file to keep them from conflicting with other files in the
@@ -897,6 +897,8 @@ class SourceFile final : public FileUnit {
897897

898898
bool hasTestableImport(const ModuleDecl *module) const;
899899

900+
void markUsableFromInlineImport(const ModuleDecl *module);
901+
900902
void clearLookupCache();
901903

902904
void cacheVisibleDecls(SmallVectorImpl<ValueDecl *> &&globals) const;

lib/AST/Module.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,14 @@ bool SourceFile::hasTestableImport(const swift::ModuleDecl *module) const {
12871287
});
12881288
}
12891289

1290+
void SourceFile::markUsableFromInlineImport(const ModuleDecl *module) {
1291+
for (auto &Import : Imports) {
1292+
if (Import.first.second == module) {
1293+
Import.second |= ImportFlags::UsableFromInline;
1294+
}
1295+
}
1296+
}
1297+
12901298
void SourceFile::clearLookupCache() {
12911299
if (!Cache)
12921300
return;

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2509,12 +2509,19 @@ bool AvailabilityWalker::diagAvailability(const ValueDecl *D, SourceRange R,
25092509
return true;
25102510
}
25112511

2512-
if (FragileKind)
2513-
if (R.isValid())
2512+
if (FragileKind) {
2513+
if (R.isValid()) {
25142514
if (TC.diagnoseInlinableDeclRef(R.Start, D, DC, *FragileKind,
25152515
TreatUsableFromInlineAsPublic))
25162516
return true;
25172517

2518+
auto *SF = cast<SourceFile>(DC->getModuleScopeContext());
2519+
auto *M = D->getDeclContext()->getParentModule();
2520+
if (SF->getParentModule() != M)
2521+
SF->markUsableFromInlineImport(M);
2522+
}
2523+
}
2524+
25182525
if (TC.diagnoseExplicitUnavailability(D, R, DC, call))
25192526
return true;
25202527

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@_exported import autolinking_public
2+
import autolinking_other // inferred as @_usableFromInline
3+
import autolinking_private
4+
5+
public func bfunc(x: Int = afunc()) {
6+
cfunc()
7+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend -emit-module %S/Inputs/autolinking_public.swift -emit-module-path %t/autolinking_public.swiftmodule -module-link-name autolinking_public -swift-version 4
4+
// RUN: %target-swift-frontend -emit-module %S/Inputs/autolinking_other.swift -emit-module-path %t/autolinking_other.swiftmodule -module-link-name autolinking_other -swift-version 4
5+
// RUN: %target-swift-frontend -emit-module %S/Inputs/autolinking_private.swift -emit-module-path %t/autolinking_private.swiftmodule -module-link-name autolinking_private -I %t -swift-version 4
6+
// RUN: %target-swift-frontend -emit-module %S/Inputs/autolinking_module_inferred.swift -emit-module-path %t/autolinking_module_inferred.swiftmodule -module-link-name autolinking_module_inferred -I %t -swift-version 4
7+
// RUN: %target-swift-frontend -emit-ir %s -I %t -swift-version 4 | %FileCheck %s
8+
9+
// This test is identical to autolinking-inlinable, except we also rely on the
10+
// '@_usableFromInline' attribute getting inferred correctly on the import.
11+
12+
// Linux uses a different autolinking mechanism, based on
13+
// swift-autolink-extract. This file tests the Darwin mechanism.
14+
// UNSUPPORTED: OS=linux-gnu
15+
// UNSUPPORTED: OS=linux-gnueabihf
16+
// UNSUPPORTED: OS=freebsd
17+
// UNSUPPORTED: OS=linux-androideabi
18+
19+
import autolinking_module_inferred
20+
21+
bfunc()
22+
23+
// Note: we don't autolink autolinking_private even though autolinking_module imports it also.
24+
25+
// CHECK: !llvm.linker.options = !{[[SWIFTCORE:![0-9]+]], [[SWIFTONONESUPPORT:![0-9]+]], [[MODULE:![0-9]+]], [[PUBLIC:![0-9]+]], [[OTHER:![0-9]+]], [[OBJC:![0-9]+]]}
26+
27+
// CHECK: [[SWIFTCORE]] = !{!"-lswiftCore"}
28+
// CHECK: [[SWIFTONONESUPPORT]] = !{!"-lswiftSwiftOnoneSupport"}
29+
// CHECK: [[MODULE]] = !{!"-lautolinking_module_inferred"}
30+
// CHECK: [[PUBLIC]] = !{!"-lautolinking_public"}
31+
// CHECK: [[OTHER]] = !{!"-lautolinking_other"}
32+
// CHECK: [[OBJC]] = !{!"-lobjc"}

0 commit comments

Comments
 (0)