Skip to content

[Sema] Warn on @_spi imports of modules built from their public interface #34912

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 2 commits into from
Dec 2, 2020
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
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,10 @@ ERROR(spi_attribute_on_protocol_requirement,none,
ERROR(spi_attribute_on_frozen_stored_properties,none,
"stored property %0 cannot be declared '@_spi' in a '@frozen' struct",
(DeclName))
WARNING(spi_attribute_on_import_of_public_module,none,
"'@_spi' import of %0 will not include any SPI symbols; "
"%0 was built from the public interface at %1",
(DeclName, StringRef))

// Opaque return types
ERROR(opaque_type_invalid_constraint,none,
Expand Down
15 changes: 15 additions & 0 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,21 @@ void AttributeChecker::visitSPIAccessControlAttr(SPIAccessControlAttr *attr) {
}
}
}

if (auto ID = dyn_cast<ImportDecl>(D)) {
auto importedModule = ID->getModule();
if (importedModule) {
auto path = importedModule->getModuleFilename();
if (llvm::sys::path::extension(path) == ".swiftinterface" &&
!path.endswith(".private.swiftinterface")) {
// If the module was built from the public swiftinterface, it can't
// have any SPI.
diagnose(attr->getLocation(),
diag::spi_attribute_on_import_of_public_module,
importedModule->getName(), path);
}
}
}
}

static bool checkObjCDeclContext(Decl *D) {
Expand Down
5 changes: 0 additions & 5 deletions test/SPI/experimental_spi_imports_type_check.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@
// RUN: %target-typecheck-verify-swift -DCLIENT -I %t
// RUN: %target-swift-frontend -typecheck %s -DCLIENT -DCLIENT_LOAD_CORE -I %t

/// Test with the public swiftinterface file, the SPI is unknown.
// RUN: rm %t/LibPublic.private.swiftinterface
// RUN: %target-typecheck-verify-swift -DCLIENT -I %t
// RUN: %target-typecheck-verify-swift -DCLIENT -DCLIENT_LOAD_CORE -I %t

#if LIB_CORE

public struct CoreStruct {
Expand Down
19 changes: 19 additions & 0 deletions test/SPI/warn_on_ineffective_spi_import.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// Test the warning on an SPI import of the public interface of a module.

// RUN: %empty-directory(%t)

/// Compile the SPI lib.
// RUN: %target-swift-frontend -enable-experimental-prespecialization -emit-module %S/Inputs/spi_helper.swift -module-name SPIHelper -emit-module-path %t/SPIHelper.swiftmodule -emit-module-interface-path %t/SPIHelper.swiftinterface -emit-private-module-interface-path %t/SPIHelper.private.swiftinterface -enable-library-evolution -swift-version 5 -parse-as-library

/// Reading from swiftmodule, no warning.
// RUN: %target-swift-frontend -typecheck %s -I %t

/// Reading from .private.swiftinterface, no warning.
// RUN: rm %t/SPIHelper.swiftmodule
// RUN: %target-swift-frontend -typecheck %s -I %t

/// Reading from the public .swiftinterface should produce the warning.
// RUN: rm %t/SPIHelper.private.swiftinterface
// RUN: %target-typecheck-verify-swift -I %t

@_spi(SPIHelper) import SPIHelper // expected-warning {{'@_spi' import of 'SPIHelper' will not include any SPI symbols; 'SPIHelper' was built from the public interface at}}