Skip to content

Commit 60c5abf

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

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
@@ -783,7 +783,7 @@ class SourceFile final : public FileUnit {
783783
/// This is the list of modules that are imported by this module.
784784
///
785785
/// This is filled in by the Name Binding phase.
786-
ArrayRef<std::pair<ModuleDecl::ImportedModule, ImportOptions>> Imports;
786+
MutableArrayRef<std::pair<ModuleDecl::ImportedModule, ImportOptions>> Imports;
787787

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

890890
bool hasTestableImport(const ModuleDecl *module) const;
891891

892+
void markUsableFromInlineImport(const ModuleDecl *module);
893+
892894
void clearLookupCache();
893895

894896
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
@@ -2501,12 +2501,19 @@ bool AvailabilityWalker::diagAvailability(const ValueDecl *D, SourceRange R,
25012501
return true;
25022502
}
25032503

2504-
if (FragileKind)
2505-
if (R.isValid())
2504+
if (FragileKind) {
2505+
if (R.isValid()) {
25062506
if (TC.diagnoseInlinableDeclRef(R.Start, D, DC, *FragileKind,
25072507
TreatUsableFromInlineAsPublic))
25082508
return true;
25092509

2510+
auto *SF = cast<SourceFile>(DC->getModuleScopeContext());
2511+
auto *M = D->getDeclContext()->getParentModule();
2512+
if (SF->getParentModule() != M)
2513+
SF->markUsableFromInlineImport(M);
2514+
}
2515+
}
2516+
25102517
if (TC.diagnoseExplicitUnavailability(D, R, DC, call))
25112518
return true;
25122519

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)