Skip to content

Commit fcc6513

Browse files
committed
- Do not load an in-package module if built from interface.
- Loading package modules from a different package should still be allowed even if built from interface. Resolves rdar://104617990
1 parent 25280cb commit fcc6513

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,10 @@ ERROR(module_not_testable,Fatal,
10051005
ERROR(module_not_compiled_for_private_import,none,
10061006
"module %0 was not compiled for private import", (Identifier))
10071007

1008+
ERROR(in_package_module_not_compiled_from_source,Fatal,
1009+
"module %0 is in package %1 and can only be loaded if built from source",
1010+
(Identifier, Identifier))
1011+
10081012
ERROR(import_restriction_conflict,none,
10091013
"module %0 cannot be both "
10101014
"%select{implementation-only|SPI only|exported}1 and "

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,8 +798,16 @@ LoadedFile *SerializedModuleLoaderBase::loadAST(
798798
M.setABIName(Ctx.getIdentifier(loadedModuleFile->getModuleABIName()));
799799
if (loadedModuleFile->isConcurrencyChecked())
800800
M.setIsConcurrencyChecked();
801-
if (!loadedModuleFile->getModulePackageName().empty())
801+
if (!loadedModuleFile->getModulePackageName().empty()) {
802+
if (loadedModuleFile->isBuiltFromInterface() &&
803+
loadedModuleFile->getModulePackageName().str() == Ctx.LangOpts.PackageName) {
804+
Ctx.Diags.diagnose(SourceLoc(),
805+
diag::in_package_module_not_compiled_from_source,
806+
M.getBaseIdentifier(),
807+
Ctx.LangOpts.PackageName);
808+
}
802809
M.setPackageName(Ctx.getIdentifier(loadedModuleFile->getModulePackageName()));
810+
}
803811
M.setUserModuleVersion(loadedModuleFile->getUserModuleVersion());
804812
for (auto name: loadedModuleFile->getAllowableClientNames()) {
805813
M.addAllowableClientName(Ctx.getIdentifier(name));
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
4+
// RUN: %target-swift-frontend -module-name LibFromInterface -emit-module -emit-module-interface-path %t/LibFromInterface.swiftinterface -parse-as-library %t/Lib.swift -enable-library-evolution -package-name mypkg -swift-version 5
5+
// RUN: test -f %t/LibFromInterface.swiftinterface
6+
// RUN: %FileCheck %s -check-prefix CHECK-LIB < %t/LibFromInterface.swiftinterface
7+
// CHECK-LIB: -package-name mypkg
8+
// CHECK-LIB-NOT: func log(level: Int)
9+
10+
// RUN: not %target-swift-frontend -module-name ClientInSamePkg %t/ClientLoadInterfaceModule.swift -emit-module -emit-module-path %t/ClientInSamePkg.swiftmodule -package-name mypkg -I %t 2> %t/resultA.output
11+
// RUN: %FileCheck %s -check-prefix CHECK-A < %t/resultA.output
12+
// CHECK-A: error: module 'LibFromInterface' is in package 'mypkg' and can only be loaded if built from source
13+
14+
// RUN: not %target-swift-frontend -module-name ClientInDiffPkg %t/ClientLoadInterfaceModule.swift -emit-module -emit-module-path %t/ClientInDiffPkg.swiftmodule -package-name otherPkg -I %t 2> %t/resultB.output
15+
// RUN: %FileCheck %s -check-prefix CHECK-B < %t/resultB.output
16+
// CHECK-B: error: cannot find 'log' in scope
17+
18+
// RUN: %target-swift-frontend -module-name LibFromSource -emit-module -emit-module-path %t/LibFromSource.swiftmodule -parse-as-library %t/Lib.swift -package-name mypkg
19+
// RUN: test -f %t/LibFromSource.swiftmodule
20+
21+
// RUN: %target-swift-frontend -module-name ClientInSamePkgSrc %t/ClientLoadSourceModule.swift -emit-module -emit-module-path %t/ClientInSamePkgSrc.swiftmodule -package-name mypkg -I %t
22+
// RUN: test -f %t/ClientInSamePkgSrc.swiftmodule
23+
24+
//--- Lib.swift
25+
package func log(level: Int) {}
26+
27+
//--- ClientLoadInterfaceModule.swift
28+
import LibFromInterface
29+
30+
func someFun() {
31+
log(level: 1)
32+
}
33+
34+
//--- ClientLoadSourceModule.swift
35+
import LibFromSource
36+
37+
func someFun() {
38+
log(level: 1)
39+
}
40+

0 commit comments

Comments
 (0)