Skip to content

Warn if a package binary module is loaded from SDK #64600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1007,11 +1007,16 @@ ERROR(module_not_testable,Fatal,
ERROR(module_not_compiled_for_private_import,none,
"module %0 was not compiled for private import", (Identifier))

ERROR(in_package_module_not_compiled_from_source,Fatal,
"module %0 is in package '%1' but was built from interface '%2'; "
"modules of the same package can only be loaded if built from source",
ERROR(in_package_module_not_compiled_from_source,none,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we use this error somewhere?

Copy link
Contributor Author

@elsh elsh Mar 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it was already added to SerializationLoader::loadAST in main; its message is just updated in this PR.

"module %0 is in package '%1' but was built from interface; "
"modules of the same package can only be loaded if built from source: %2",
(Identifier, StringRef, StringRef))

WARNING(in_package_module_not_compiled_locally,none,
"module %0 is in package %1 but was loaded from SDK; "
"modules of the same package should be built locally from source only: %2",
(Identifier, Identifier, StringRef))

ERROR(import_restriction_conflict,none,
"module %0 cannot be both "
"%select{implementation-only|SPI only|exported}1 and "
Expand Down
18 changes: 17 additions & 1 deletion lib/Sema/TypeCheckDeclPrimary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1960,9 +1960,25 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
// diagnostics.
(void)ID->getDecls();

auto target = ID->getModule();
if (!getASTContext().LangOpts.PackageName.empty() &&
getASTContext().LangOpts.PackageName == target->getPackageName().str() &&
!target->isNonSwiftModule() && // target is a Swift module
target->isNonUserModule()) { // target module is in distributed SDK
// If reached here, a binary module (.swiftmodule) instead of interface of the
// target was loaded for the main module, where both belong to the same package;
// this is an expected behavior, but it should have been loaded from the local
// build directory, not from distributed SDK. In such case, we show a warning.
auto &diags = ID->getASTContext().Diags;
diags.diagnose(ID,
diag::in_package_module_not_compiled_locally,
target->getBaseIdentifier(),
target->getPackageName(),
target->getModuleFilename());
}

// Report the public import of a private module.
if (ID->getASTContext().LangOpts.LibraryLevel == LibraryLevel::API) {
auto target = ID->getModule();
auto importer = ID->getModuleContext();
if (target &&
!ID->getAttrs().hasAttribute<ImplementationOnlyAttr>() &&
Expand Down
41 changes: 41 additions & 0 deletions test/Sema/import_package_module_from_sdk.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t

// --- Prepare SDK (.swiftmodule).
// RUN: %empty-directory(%t/SDK)
// RUN: mkdir -p %t/SDK/Frameworks/LibInSDK.framework/Modules/LibInSDK.swiftmodule
// RUN: %target-swift-frontend \
// RUN: -emit-module \
// RUN: -module-name LibInSDK \
// RUN: -package-name libPkg \
// RUN: -o %t/SDK/Frameworks/LibInSDK.framework/Modules/LibInSDK.swiftmodule/%module-target-triple.swiftmodule \
// RUN: -swift-version 5 \
// RUN: %t/Lib.swift

// RUN: test -f %t/SDK/Frameworks/LibInSDK.framework/Modules/LibInSDK.swiftmodule/%module-target-triple.swiftmodule

// RUN: %target-swift-frontend -typecheck -verify -verify-ignore-unknown %t/Client1.swift -package-name libPkg -sdk %t/SDK -I %t -I %t/SDK/Frameworks/LibInSDK.framework/Modules

// RUN: %target-swift-frontend -module-name LibLocal -emit-module -emit-module-path %t/LibLocal.swiftmodule -parse-as-library %t/Lib.swift -package-name libPkg
// RUN: test -f %t/LibLocal.swiftmodule

// RUN: %target-swift-frontend -typecheck -verify %t/Client2.swift -package-name libPkg -I %t

//--- Lib.swift
package func log(level: Int) {}

//--- Client1.swift
import LibInSDK // expected-warning {{module 'LibInSDK' is in package 'libPkg' but was loaded from SDK; modules of the same package should be built locally from source only}}

func someFunc() {
log(level: 1)
}

//--- Client2.swift
import LibLocal

func someFunc() {
log(level: 1)
}


2 changes: 1 addition & 1 deletion test/Serialization/load_package_module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

// 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
// RUN: %FileCheck %s -check-prefix CHECK-A < %t/resultA.output
// 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
// CHECK-A: error: module 'LibFromInterface' is in package 'mypkg' but was built from interface; modules of the same package can only be loaded if built from source: {{.*}}LibFromInterface.swiftinterface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel similar about this error message as the one above, but I'm less sure of another way to word it. As a user I'm just not sure this would mean much to me. Really I want to know what I've done wrong. Maybe this is fine though since IIUC it would be fairly uncommon for a regular user to hit this.


// 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
// RUN: %FileCheck %s -check-prefix CHECK-B < %t/resultB.output
Expand Down