Skip to content

Commit c2bb890

Browse files
authored
Merge pull request #64488 from apple/es-load
Do not load modules of the same package if built from interface
2 parents 769612b + fc2b61d commit c2bb890

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,11 @@ ERROR(module_not_testable,Fatal,
10071007
ERROR(module_not_compiled_for_private_import,none,
10081008
"module %0 was not compiled for private import", (Identifier))
10091009

1010+
ERROR(in_package_module_not_compiled_from_source,Fatal,
1011+
"module %0 is in package '%1' but was built from interface '%2'; "
1012+
"modules of the same package can only be loaded if built from source",
1013+
(Identifier, StringRef, StringRef))
1014+
10101015
ERROR(import_restriction_conflict,none,
10111016
"module %0 cannot be both "
10121017
"%select{implementation-only|SPI only|exported}1 and "

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,8 +802,18 @@ LoadedFile *SerializedModuleLoaderBase::loadAST(
802802
M.setABIName(Ctx.getIdentifier(loadedModuleFile->getModuleABIName()));
803803
if (loadedModuleFile->isConcurrencyChecked())
804804
M.setIsConcurrencyChecked();
805-
if (!loadedModuleFile->getModulePackageName().empty())
805+
if (!loadedModuleFile->getModulePackageName().empty()) {
806+
if (loadedModuleFile->isBuiltFromInterface() &&
807+
loadedModuleFile->getModulePackageName().str() == Ctx.LangOpts.PackageName) {
808+
Ctx.Diags.diagnose(SourceLoc(),
809+
diag::in_package_module_not_compiled_from_source,
810+
M.getBaseIdentifier(),
811+
Ctx.LangOpts.PackageName,
812+
loadedModuleFile->getModuleSourceFilename()
813+
);
814+
}
806815
M.setPackageName(Ctx.getIdentifier(loadedModuleFile->getModulePackageName()));
816+
}
807817
M.setUserModuleVersion(loadedModuleFile->getUserModuleVersion());
808818
for (auto name: loadedModuleFile->getAllowableClientNames()) {
809819
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' but was built from interface '{{.*}}LibFromInterface.swiftinterface'; modules of the same package 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)