Skip to content

Commit 5f513f5

Browse files
authored
[ModuleInterface] Add mechanism to exclude experimental flags from th… (#66088) (#66101)
* [ModuleInterface] Add mechanism to exclude experimental flags from the module interface rdar://109722548 * Separate filtered flags from the typical/unfiltered case
1 parent 39a25ec commit 5f513f5

File tree

5 files changed

+51
-4
lines changed

5 files changed

+51
-4
lines changed

include/swift/Basic/Feature.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ llvm::Optional<Feature> getExperimentalFeature(llvm::StringRef name);
6666
/// \c None if it does not have such a version.
6767
llvm::Optional<unsigned> getFeatureLanguageVersion(Feature feature);
6868

69+
/// Determine whether this feature should be included in the
70+
/// module interface
71+
bool includeInModuleInterface(Feature feature);
72+
6973
}
7074

7175
#endif // SWIFT_BASIC_FEATURES_H

include/swift/Basic/Features.def

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@
6262
langOpts.hasFeature(#FeatureName))
6363
#endif
6464

65+
#ifndef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
66+
# define EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE(FeatureName, AvailableInProd) \
67+
EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd)
68+
#endif
69+
6570
LANGUAGE_FEATURE(AsyncAwait, 296, "async/await", true)
6671
LANGUAGE_FEATURE(EffectfulProp, 310, "Effectful properties", true)
6772
LANGUAGE_FEATURE(MarkerProtocol, 0, "@_marker protocol", true)
@@ -133,8 +138,8 @@ EXPERIMENTAL_FEATURE(ModuleInterfaceExportAs, true)
133138
EXPERIMENTAL_FEATURE(AccessLevelOnImport, true)
134139

135140
/// Whether to enable experimental layout string value witnesses
136-
EXPERIMENTAL_FEATURE(LayoutStringValueWitnesses, true)
137-
EXPERIMENTAL_FEATURE(LayoutStringValueWitnessesInstantiation, true)
141+
EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE(LayoutStringValueWitnesses, true)
142+
EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE(LayoutStringValueWitnessesInstantiation, true)
138143

139144
/// Whether to enable experimental differentiable programming features:
140145
/// `@differentiable` declaration attribute, etc.
@@ -195,6 +200,7 @@ EXPERIMENTAL_FEATURE(ReferenceBindings, false)
195200
/// Enable the explicit 'import Builtin' and allow Builtin usage.
196201
EXPERIMENTAL_FEATURE(BuiltinModule, true)
197202

203+
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
198204
#undef EXPERIMENTAL_FEATURE
199205
#undef UPCOMING_FEATURE
200206
#undef SUPPRESSIBLE_LANGUAGE_FEATURE

lib/Basic/LangOptions.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ bool swift::isFeatureAvailableInProduction(Feature feature) {
485485
switch (feature) {
486486
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description, Option) \
487487
case Feature::FeatureName: return true;
488-
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
488+
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
489489
case Feature::FeatureName: return AvailableInProd;
490490
#include "swift/Basic/Features.def"
491491
}
@@ -504,7 +504,7 @@ llvm::Optional<Feature> swift::getUpcomingFeature(llvm::StringRef name) {
504504
llvm::Optional<Feature> swift::getExperimentalFeature(llvm::StringRef name) {
505505
return llvm::StringSwitch<Optional<Feature>>(name)
506506
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description, Option)
507-
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
507+
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
508508
.Case(#FeatureName, Feature::FeatureName)
509509
#include "swift/Basic/Features.def"
510510
.Default(None);
@@ -520,6 +520,17 @@ llvm::Optional<unsigned> swift::getFeatureLanguageVersion(Feature feature) {
520520
}
521521
}
522522

523+
bool swift::includeInModuleInterface(Feature feature) {
524+
switch (feature) {
525+
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description, Option) \
526+
case Feature::FeatureName: return true;
527+
#define EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE(FeatureName, AvailableInProd) \
528+
case Feature::FeatureName: return false;
529+
#include "swift/Basic/Features.def"
530+
}
531+
llvm_unreachable("covered switch");
532+
}
533+
523534
DiagnosticBehavior LangOptions::getAccessNoteFailureLimit() const {
524535
switch (AccessNoteBehavior) {
525536
case AccessNoteDiagnosticBehavior::Ignore:

lib/Frontend/CompilerInvocation.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,19 @@ static void ParseModuleInterfaceArgs(ModuleInterfaceOptions &Opts,
392392
}
393393
}
394394

395+
/// Checks if an arg is generally allowed to be included
396+
/// in a module interface
397+
static bool ShouldIncludeModuleInterfaceArg(const Arg *A) {
398+
if (!A->getOption().matches(options::OPT_enable_experimental_feature))
399+
return true;
400+
401+
if (auto feature = getExperimentalFeature(A->getValue())) {
402+
return swift::includeInModuleInterface(*feature);
403+
}
404+
405+
return true;
406+
}
407+
395408
/// Save a copy of any flags marked as ModuleInterfaceOption, if running
396409
/// in a mode that is going to emit a .swiftinterface file.
397410
static void SaveModuleInterfaceArgs(ModuleInterfaceOptions &Opts,
@@ -402,6 +415,9 @@ static void SaveModuleInterfaceArgs(ModuleInterfaceOptions &Opts,
402415
ArgStringList RenderedArgs;
403416
ArgStringList RenderedArgsIgnorable;
404417
for (auto A : Args) {
418+
if (!ShouldIncludeModuleInterfaceArg(A))
419+
continue;
420+
405421
if (A->getOption().hasFlag(options::ModuleInterfaceOptionIgnorable)) {
406422
A->render(Args, RenderedArgsIgnorable);
407423
} else if (A->getOption().hasFlag(options::ModuleInterfaceOption)) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// 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
4+
// RUN: %FileCheck %s < %t.swiftinterface -check-prefix=CHECK-SWIFTINTERFACE
5+
//
6+
// CHECK-SWIFTINTERFACE: swift-module-flags-ignorable:
7+
// CHECK-SWIFTINTERFACE-NOT: -enable-experimental-feature LayoutStringValueWitnesses
8+
// CHECK-SWIFTINTERFACE-NOT: -enable-experimental-feature LayoutStringValueWitnessesInstantiation
9+
10+
public func foo() { }

0 commit comments

Comments
 (0)