Skip to content

[ModuleInterface] Add mechanism to exclude experimental flags from th… #66088

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
May 24, 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
4 changes: 4 additions & 0 deletions include/swift/Basic/Feature.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ llvm::Optional<Feature> getExperimentalFeature(llvm::StringRef name);
/// \c None if it does not have such a version.
llvm::Optional<unsigned> getFeatureLanguageVersion(Feature feature);

/// Determine whether this feature should be included in the
/// module interface
bool includeInModuleInterface(Feature feature);

}

#endif // SWIFT_BASIC_FEATURES_H
10 changes: 8 additions & 2 deletions include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
langOpts.hasFeature(#FeatureName))
#endif

#ifndef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
# define EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE(FeatureName, AvailableInProd) \
EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd)
#endif

LANGUAGE_FEATURE(AsyncAwait, 296, "async/await", true)
LANGUAGE_FEATURE(EffectfulProp, 310, "Effectful properties", true)
LANGUAGE_FEATURE(MarkerProtocol, 0, "@_marker protocol", true)
Expand Down Expand Up @@ -134,8 +139,8 @@ EXPERIMENTAL_FEATURE(ModuleInterfaceExportAs, true)
EXPERIMENTAL_FEATURE(AccessLevelOnImport, true)

/// Whether to enable experimental layout string value witnesses
EXPERIMENTAL_FEATURE(LayoutStringValueWitnesses, true)
EXPERIMENTAL_FEATURE(LayoutStringValueWitnessesInstantiation, true)
EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE(LayoutStringValueWitnesses, true)
EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE(LayoutStringValueWitnessesInstantiation, true)

/// Whether to enable experimental differentiable programming features:
/// `@differentiable` declaration attribute, etc.
Expand Down Expand Up @@ -196,6 +201,7 @@ EXPERIMENTAL_FEATURE(ReferenceBindings, false)
/// Enable the explicit 'import Builtin' and allow Builtin usage.
EXPERIMENTAL_FEATURE(BuiltinModule, true)

#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
#undef EXPERIMENTAL_FEATURE
#undef UPCOMING_FEATURE
#undef SUPPRESSIBLE_LANGUAGE_FEATURE
Expand Down
15 changes: 13 additions & 2 deletions lib/Basic/LangOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ bool swift::isFeatureAvailableInProduction(Feature feature) {
switch (feature) {
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description, Option) \
case Feature::FeatureName: return true;
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
case Feature::FeatureName: return AvailableInProd;
#include "swift/Basic/Features.def"
}
Expand All @@ -508,7 +508,7 @@ llvm::Optional<Feature> swift::getUpcomingFeature(llvm::StringRef name) {
llvm::Optional<Feature> swift::getExperimentalFeature(llvm::StringRef name) {
return llvm::StringSwitch<Optional<Feature>>(name)
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description, Option)
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
.Case(#FeatureName, Feature::FeatureName)
#include "swift/Basic/Features.def"
.Default(None);
Expand All @@ -524,6 +524,17 @@ llvm::Optional<unsigned> swift::getFeatureLanguageVersion(Feature feature) {
}
}

bool swift::includeInModuleInterface(Feature feature) {
switch (feature) {
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description, Option) \
case Feature::FeatureName: return true;
#define EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE(FeatureName, AvailableInProd) \
case Feature::FeatureName: return false;
#include "swift/Basic/Features.def"
}
llvm_unreachable("covered switch");
}

DiagnosticBehavior LangOptions::getAccessNoteFailureLimit() const {
switch (AccessNoteBehavior) {
case AccessNoteDiagnosticBehavior::Ignore:
Expand Down
16 changes: 16 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,19 @@ static void ParseModuleInterfaceArgs(ModuleInterfaceOptions &Opts,
}
}

/// Checks if an arg is generally allowed to be included
/// in a module interface
static bool ShouldIncludeModuleInterfaceArg(const Arg *A) {
if (!A->getOption().matches(options::OPT_enable_experimental_feature))
return true;

if (auto feature = getExperimentalFeature(A->getValue())) {
return swift::includeInModuleInterface(*feature);
}

return true;
}

/// Save a copy of any flags marked as ModuleInterfaceOption, if running
/// in a mode that is going to emit a .swiftinterface file.
static void SaveModuleInterfaceArgs(ModuleInterfaceOptions &Opts,
Expand All @@ -428,6 +441,9 @@ static void SaveModuleInterfaceArgs(ModuleInterfaceOptions &Opts,
ArgStringList RenderedArgsIgnorable;
ArgStringList RenderedArgsIgnorablePrivate;
for (auto A : Args) {
if (!ShouldIncludeModuleInterfaceArg(A))
continue;

if (A->getOption().hasFlag(options::ModuleInterfaceOptionIgnorablePrivate)) {
A->render(Args, RenderedArgsIgnorablePrivate);
} else if (A->getOption().hasFlag(options::ModuleInterfaceOptionIgnorable)) {
Expand Down
10 changes: 10 additions & 0 deletions test/ModuleInterface/option-filtering.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %empty-directory(%t)

// RUN: %target-swift-frontend -enable-library-evolution -emit-module-interface-path %t.swiftinterface -module-name t %s -target-min-inlining-version 42 -emit-module -o /dev/null -O -enable-experimental-feature LayoutStringValueWitnesses -enable-experimental-feature LayoutStringValueWitnessesInstantiation
// RUN: %FileCheck %s < %t.swiftinterface -check-prefix=CHECK-SWIFTINTERFACE
//
// CHECK-SWIFTINTERFACE: swift-module-flags-ignorable:
// CHECK-SWIFTINTERFACE-NOT: -enable-experimental-feature LayoutStringValueWitnesses
// CHECK-SWIFTINTERFACE-NOT: -enable-experimental-feature LayoutStringValueWitnessesInstantiation

public func foo() { }