Sema: Gate SPI operator fix behind EnforceSPIOperatorGroup

This new check is source breaking. Let's gate it behind an opt-in flag
for now and enable it by default on a new language mode or similar.
This commit is contained in:
Alexis Laferrière
2025-11-11 15:23:36 -08:00
parent b248d2d9b4
commit a4865be7e3
4 changed files with 13 additions and 3 deletions

View File

@@ -561,6 +561,9 @@ EXPERIMENTAL_FEATURE(CheckImplementationOnly, true)
/// report references to implementation-only imported modules.
EXPERIMENTAL_FEATURE(CheckImplementationOnlyStrict, false)
/// Check that use sites have the required @_spi import for operators.
EXPERIMENTAL_FEATURE(EnforceSPIOperatorGroup, true)
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
#undef EXPERIMENTAL_FEATURE
#undef UPCOMING_FEATURE

View File

@@ -5537,7 +5537,9 @@ static bool checkAccess(const DeclContext *useDC, const ValueDecl *VD,
}
case AccessLevel::Public:
case AccessLevel::Open: {
if (VD->isOperator() && VD->isSPI()) {
if (VD->getASTContext().LangOpts.hasFeature(
Feature::EnforceSPIOperatorGroup) &&
VD->isOperator() && VD->isSPI()) {
const DeclContext *useFile = useDC->getModuleScopeContext();
if (useFile->getParentModule() == sourceDC->getParentModule())
return true;

View File

@@ -146,6 +146,7 @@ UNINTERESTING_FEATURE(SameElementRequirements)
UNINTERESTING_FEATURE(SendingArgsAndResults)
UNINTERESTING_FEATURE(CheckImplementationOnly)
UNINTERESTING_FEATURE(CheckImplementationOnlyStrict)
UNINTERESTING_FEATURE(EnforceSPIOperatorGroup)
static bool findUnderscoredLifetimeAttr(Decl *decl) {
auto hasUnderscoredLifetimeAttr = [](Decl *decl) {

View File

@@ -6,17 +6,21 @@
// RUN: %target-swift-frontend -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
// RUN: %target-typecheck-verify-swift -verify-ignore-unrelated -I %t -verify-ignore-unknown
// RUN: %target-typecheck-verify-swift -verify-ignore-unrelated -I %t -verify-ignore-unknown \
// RUN: -enable-experimental-feature EnforceSPIOperatorGroup
/// Reading from .private.swiftinterface
// RUN: rm %t/SPIHelper.swiftmodule
// RUN: %target-typecheck-verify-swift -verify-ignore-unrelated -I %t -verify-ignore-unknown
// RUN: %target-typecheck-verify-swift -verify-ignore-unrelated -I %t -verify-ignore-unknown \
// RUN: -enable-experimental-feature EnforceSPIOperatorGroup
/// Reading from the public .swiftinterface should raise errors on missing
/// declarations.
// RUN: rm %t/SPIHelper.private.swiftinterface
// RUN: not %target-swift-frontend -typecheck -I %t %s
// REQUIRES: swift_feature_EnforceSPIOperatorGroup
import SPIHelper
// Use the public API