Skip to content

Commit 8bb6b30

Browse files
authored
Merge pull request #75160 from swiftlang/elsh/lookup-pkg-interface
Correct discrepancies in the package interface file lookup.
2 parents 447c904 + 5e58876 commit 8bb6b30

File tree

4 files changed

+26
-19
lines changed

4 files changed

+26
-19
lines changed

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -618,9 +618,6 @@ std::string SerializedModuleBaseName::getName(file_types::ID fileTy) const {
618618
std::optional<std::string>
619619
SerializedModuleBaseName::getPackageInterfacePathIfInSamePackage(
620620
llvm::vfs::FileSystem &fs, ASTContext &ctx) const {
621-
if (!ctx.LangOpts.EnablePackageInterfaceLoad)
622-
return std::nullopt;
623-
624621
std::string packagePath{
625622
getName(file_types::TY_PackageSwiftModuleInterfaceFile)};
626623

@@ -667,11 +664,15 @@ SerializedModuleBaseName::findInterfacePath(llvm::vfs::FileSystem &fs,
667664
if (!fs.exists(interfacePath))
668665
return std::nullopt;
669666

670-
// If there is a package name, try look for the package interface.
671-
if (!ctx.LangOpts.PackageName.empty()) {
672-
if (auto maybePackageInterface =
667+
// If both -package-name and -experimental-package-interface-load
668+
// are passed to the client, try to look for the package interface
669+
// to load; if either flag is missing, fall back to loading private
670+
// or public interface.
671+
if (!ctx.LangOpts.PackageName.empty() &&
672+
ctx.LangOpts.EnablePackageInterfaceLoad) {
673+
if (auto found =
673674
getPackageInterfacePathIfInSamePackage(fs, ctx))
674-
return *maybePackageInterface;
675+
return *found;
675676

676677
// If package interface is not found, check if we can load the
677678
// public/private interface file by checking:

test/ModuleInterface/load_package_interface.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525

2626
// CHECK-LOAD-PKG-ENABLED: loaded module 'Bar'; source: '{{.*}}Bar.package.swiftinterface', loaded: '{{.*}}Bar-{{.*}}.swiftmodule'
2727

28-
/// Client should not load a package interface module without the flag or the env var
28+
/// Client should not load a package interface without the flag or the env var above;
29+
/// in such case, private swiftinterface is loaded but an error should be thrown in typecheck.
2930
// RUN: not %target-swift-frontend -typecheck %t/Client.swift -I %t \
3031
// RUN: -package-name barpkg \
3132
// RUN: -Rmodule-loading 2> %t/load-pkg-off.output
3233
// RUN: %FileCheck -check-prefix=CHECK-LOAD-PKG-OFF %s < %t/load-pkg-off.output
33-
// CHECK-LOAD-PKG-OFF: no such module 'Bar'
34+
// CHECK-LOAD-PKG-OFF: error: module 'Bar' is in package 'barpkg' but was built from a non-package interface; modules of the same package can only be loaded if built from source or package interface:{{.*}}Bar.private.swiftinterface
3435

3536
/// Client loads a private interface since the package-name is different from the loaded module's.
3637
// RUN: not %target-swift-frontend -typecheck %t/Client.swift -I %t \

test/ScanDependencies/package_interface.swift

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,34 @@
99
// RUN: -emit-private-module-interface-path %t/Bar.private.swiftinterface \
1010
// RUN: -emit-package-module-interface-path %t/Bar.package.swiftinterface
1111

12-
// RUN: %target-swift-frontend -scan-dependencies -module-load-mode prefer-interface -o %t/deps.json -I %t -experimental-package-interface-load \
13-
// RUN: %t/Client.swift -module-name Client -package-name barpkg -swift-version 5
12+
// RUN: %target-swift-frontend -scan-dependencies \
13+
// RUN: -module-load-mode prefer-interface -o %t/deps.json -I %t \
14+
// RUN: -experimental-package-interface-load -swift-version 5 \
15+
// RUN: %t/Client.swift -module-name Client -package-name barpkg
1416
// RUN: %FileCheck %s --input-file=%t/deps.json --check-prefix CHECK --check-prefix CHECK-PACKAGE
1517

1618
/// When package name doesn't match or not used, it should find private interface.
1719
// RUN: %target-swift-frontend -scan-dependencies -module-load-mode prefer-interface -o %t/deps2.json -I %t -experimental-package-interface-load \
1820
// RUN: %t/Client.swift -module-name Client -package-name foopkg -swift-version 5
1921
// RUN: %FileCheck %s --input-file=%t/deps2.json --check-prefix CHECK --check-prefix CHECK-PRIVATE
2022

21-
// RUN: %target-swift-frontend -scan-dependencies -module-load-mode prefer-interface -o %t/deps3.json -I %t -experimental-package-interface-load \
22-
// RUN: %t/Client.swift -module-name Client -swift-version 5
23+
// RUN: %target-swift-frontend -scan-dependencies \
24+
// RUN: -module-load-mode prefer-interface -o %t/deps3.json -I %t \
25+
// RUN: -experimental-package-interface-load -swift-version 5 \
26+
// RUN: %t/Client.swift -module-name Client
2327
// RUN: %FileCheck %s --input-file=%t/deps3.json --check-prefix CHECK --check-prefix CHECK-PRIVATE
2428

25-
/// If -experimental-package-interface-load is not used but in the same package, it should find the binary module
26-
// RUN: %target-swift-frontend -scan-dependencies -module-load-mode prefer-interface -I %t \
27-
// RUN: %t/Client.swift -module-name Client -package-name barpkg -swift-version 5 | \
28-
// RUN: %FileCheck %s --check-prefix CHECK-BINARY
29+
/// If -experimental-package-interface-load is not used but in the same package, it should find private interface.
30+
// RUN: %target-swift-frontend -scan-dependencies \
31+
// RUN: -module-load-mode prefer-interface -o %t/deps4.json -I %t \
32+
// RUN: -swift-version 5 \
33+
// RUN: %t/Client.swift -module-name Client -package-name barpkg | \
34+
// RUN: %FileCheck %s --input-file=%t/deps4.json --check-prefix CHECK-PRIVATE
2935

3036
// CHECK: "swift": "Bar"
3137
// CHECK: "modulePath": "{{.*}}{{/|\\}}Bar-{{.*}}.swiftmodule"
3238
// CHECK-PACKAGE: "moduleInterfacePath": "{{.*}}{{/|\\}}Bar.package.swiftinterface"
3339
// CHECK-PRIVATE: "moduleInterfacePath": "{{.*}}{{/|\\}}Bar.private.swiftinterface"
34-
// CHECK-BINARY: "swiftPrebuiltExternal": "Bar"
3540

3641
//--- Bar.swift
3742
public enum PubEnum {

test/Serialization/load_package_module.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
// RUN: not %target-swift-frontend -typecheck %t/ClientLoadInterface.swift -package-name libPkg -I %t 2> %t/resultY.output
3333
// RUN: %FileCheck %s -check-prefix CHECK-Y < %t/resultY.output
34-
// CHECK-Y: error: no such module 'LibInterface'
34+
// CHECK-Y: error: module 'LibInterface' is in package 'libPkg' but was built from a non-package interface; modules of the same package can only be loaded if built from source or package interface: {{.*}}LibInterface.private.swiftinterface
3535

3636

3737
//--- Lib.swift

0 commit comments

Comments
 (0)