Skip to content

Commit c96da08

Browse files
committed
Add -enable-future-feature X command-line argument.
Introduce the `-enable-future-feature X` command-line argument to allow one to opt into features that will be enabled in a future language mode. Stage in one feature, `ConciseMagicFile` for SE-0274, that can be enabled by this flag.
1 parent c6ff96f commit c96da08

File tree

8 files changed

+43
-0
lines changed

8 files changed

+43
-0
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ ERROR(error_experimental_feature_not_available, none,
4040
"experimental feature '%0' cannot be enabled in a production compiler",
4141
(StringRef))
4242

43+
ERROR(error_future_feature_on_by_default, none,
44+
"future feature '%0' is already enabled as of Swift version %1",
45+
(StringRef, unsigned))
46+
4347
ERROR(error_unknown_library_level, none,
4448
"unknown library level '%0', "
4549
"expected one of 'api', 'spi' or 'other'", (StringRef))

include/swift/Basic/LangOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "llvm/ADT/SmallString.h"
2929
#include "llvm/ADT/SmallSet.h"
3030
#include "llvm/ADT/SmallVector.h"
31+
#include "llvm/ADT/SmallSet.h"
3132
#include "llvm/ADT/StringRef.h"
3233
#include "llvm/ADT/Triple.h"
3334
#include "llvm/Support/Regex.h"

include/swift/Option/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,11 @@ def enable_experimental_feature :
696696
Flags<[FrontendOption]>,
697697
HelpText<"Enable an experimental feature">;
698698

699+
def enable_future_feature : Separate<["-"], "enable-future-feature">,
700+
Flags<[FrontendOption]>,
701+
HelpText<"Enable a feature that will be introduced in a future language "
702+
"version">;
703+
699704
def Rpass_EQ : Joined<["-"], "Rpass=">,
700705
Flags<[FrontendOption]>,
701706
HelpText<"Report performed transformations by optimization passes whose "

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
237237
inputArgs.AddLastArg(arguments, options::OPT_warn_concurrency);
238238
inputArgs.AddLastArg(arguments, options::OPT_strict_concurrency);
239239
inputArgs.AddAllArgs(arguments, options::OPT_enable_experimental_feature);
240+
inputArgs.AddAllArgs(arguments, options::OPT_enable_future_feature);
240241
inputArgs.AddLastArg(arguments, options::OPT_warn_implicit_overrides);
241242
inputArgs.AddLastArg(arguments, options::OPT_typo_correction_limit);
242243
inputArgs.AddLastArg(arguments, options::OPT_enable_app_extension);

lib/Frontend/CompilerInvocation.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "ArgsToFrontendOptionsConverter.h"
1717
#include "swift/AST/DiagnosticsFrontend.h"
18+
#include "swift/Basic/Feature.h"
1819
#include "swift/Basic/Platform.h"
1920
#include "swift/Option/Options.h"
2021
#include "swift/Option/SanitizerOptions.h"
@@ -640,6 +641,26 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
640641
}
641642
}
642643

644+
// Map historical flags over to future features.
645+
for (const Arg *A : Args.filtered(OPT_enable_future_feature)) {
646+
// Ignore unknown features.
647+
auto feature = getFutureFeature(A->getValue());
648+
if (!feature)
649+
continue;
650+
651+
// Check if this feature was introduced already in this language version.
652+
if (auto firstVersion = getFeatureLanguageVersion(*feature)) {
653+
if (Opts.isSwiftVersionAtLeast(*firstVersion)) {
654+
Diags.diagnose(SourceLoc(), diag::error_future_feature_on_by_default,
655+
A->getValue(), *firstVersion);
656+
continue;
657+
}
658+
}
659+
660+
// Add the feature.
661+
Opts.Features.insert(*feature);
662+
}
663+
643664
// Map historical flags over to experimental features. We do this for all
644665
// compilers because that's how existing experimental feature flags work.
645666
if (Args.hasArg(OPT_enable_experimental_variadic_generics))

lib/Parse/ParseIfConfig.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ class EvaluateIfConfigCondition :
484484
bool isKnownFeature = llvm::StringSwitch<bool>(Name)
485485
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description, Option) \
486486
.Case("$" #FeatureName, Option)
487+
#define FUTURE_FEATURE(FeatureName, SENumber, Version)
487488
#include "swift/Basic/Features.def"
488489
.Default(false);
489490

test/Frontend/future_feature.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
// It's fine to provide a feature that we don't know about
3+
// RUN: %target-swift-frontend -typecheck -enable-future-feature UnknownFeature %s
4+
5+
// It's not fine to provide a feature that's in the specified language version.
6+
// RUN: not %target-swift-frontend -typecheck -enable-future-feature ConciseMagicFile -swift-version 6 %s 2>&1 | %FileCheck %s
7+
// REQUIRES: asserts
8+
9+
// CHECK: error: future feature 'ConciseMagicFile' is already enabled as of Swift version 6

test/Sema/diag_mismatched_magic_literals_swift6.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// The future "Swift 6 mode" behavior is staged in behind `-enable-experimental-concise-pound-file`.
22
// RUN: %target-typecheck-verify-swift -enable-experimental-concise-pound-file
3+
// RUN: %target-typecheck-verify-swift -enable-future-feature ConciseMagicFile
34

45
// And is also available in Swift 6 mode on asserts compilers.
56
// RUN: %target-typecheck-verify-swift -swift-version 6

0 commit comments

Comments
 (0)