Skip to content

Commit f54054a

Browse files
committed
Basic: Adjust feature macros for adoption mode
1 parent 07d163f commit f54054a

File tree

9 files changed

+80
-37
lines changed

9 files changed

+80
-37
lines changed

include/swift/Basic/BasicBridging.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2022 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2022 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -214,7 +214,8 @@ void BridgedData_free(BridgedData data);
214214
//===----------------------------------------------------------------------===//
215215

216216
enum ENUM_EXTENSIBILITY_ATTR(open) BridgedFeature {
217-
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) FeatureName,
217+
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description) \
218+
FeatureName,
218219
#include "swift/Basic/Features.def"
219220
};
220221

include/swift/Basic/Feature.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -22,13 +22,15 @@ class LangOptions;
2222

2323
/// Enumeration describing all of the named features.
2424
enum class Feature {
25-
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) FeatureName,
25+
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description) \
26+
FeatureName,
2627
#include "swift/Basic/Features.def"
2728
};
2829

2930
constexpr unsigned numFeatures() {
3031
enum Features {
31-
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) FeatureName,
32+
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description) \
33+
FeatureName,
3234
#include "swift/Basic/Features.def"
3335
NumFeatures
3436
};
@@ -61,6 +63,9 @@ std::optional<Feature> getExperimentalFeature(llvm::StringRef name);
6163
/// \c None if it does not have such a version.
6264
std::optional<unsigned> getFeatureLanguageVersion(Feature feature);
6365

66+
/// Determine whether the given feature supports adoption mode.
67+
bool isFeatureAdoptable(Feature feature);
68+
6469
/// Determine whether this feature should be included in the
6570
/// module interface
6671
bool includeInModuleInterface(Feature feature);

include/swift/Basic/Features.def

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
// features.
1515
//
1616
//
17-
// LANGUAGE_FEATURE(FeatureName, SENumber, Description)
17+
// LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description)
1818
//
1919
// The LANGUAGE_FEATURE macro describes each named feature that is
2020
// introduced in Swift. It allows Swift code to check for a particular
2121
// feature with "#if $FeatureName" in source code.
2222
//
2323
// FeatureName: The name given to this feature to be used in source code,
2424
// e.g., AsyncAwait.
25+
// IsAdoptable: Whether the feature implements adoption mode.
2526
// SENumber: The number assigned to this feature in the Swift Evolution
2627
// process, or 0 if there isn't one.
2728
// Description: A string literal describing the feature.
@@ -85,8 +86,9 @@
8586
#endif
8687

8788
#ifndef SUPPRESSIBLE_LANGUAGE_FEATURE
88-
# define SUPPRESSIBLE_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
89-
LANGUAGE_FEATURE(FeatureName, SENumber, Description)
89+
#define SUPPRESSIBLE_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
90+
LANGUAGE_FEATURE(FeatureName, /*IsAdoptable=*/false, SENumber, \
91+
Description)
9092
#endif
9193

9294
// A feature that's both conditionally-suppressible and experimental.
@@ -105,20 +107,35 @@
105107
#endif
106108

107109
#ifndef CONDITIONALLY_SUPPRESSIBLE_LANGUAGE_FEATURE
108-
# define CONDITIONALLY_SUPPRESSIBLE_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
109-
LANGUAGE_FEATURE(FeatureName, SENumber, Description)
110+
#define CONDITIONALLY_SUPPRESSIBLE_LANGUAGE_FEATURE(FeatureName, SENumber, \
111+
Description) \
112+
LANGUAGE_FEATURE(FeatureName, /*IsAdoptable=*/false, SENumber, \
113+
Description)
114+
#endif
115+
116+
// An upcoming feature that supports adoption mode.
117+
#ifndef ADOPTABLE_UPCOMING_FEATURE
118+
#if defined(UPCOMING_FEATURE)
119+
#define ADOPTABLE_UPCOMING_FEATURE(FeatureName, SENumber, Version) \
120+
UPCOMING_FEATURE(FeatureName, SENumber, Version)
121+
#else
122+
#define ADOPTABLE_UPCOMING_FEATURE(FeatureName, SENumber, Version) \
123+
LANGUAGE_FEATURE(FeatureName, /*IsAdoptable=*/true, SENumber, \
124+
#FeatureName)
125+
#endif
110126
#endif
111127

112128
#ifndef UPCOMING_FEATURE
113-
# define UPCOMING_FEATURE(FeatureName, SENumber, Version) \
114-
LANGUAGE_FEATURE(FeatureName, SENumber, #FeatureName)
129+
#define UPCOMING_FEATURE(FeatureName, SENumber, Version) \
130+
LANGUAGE_FEATURE(FeatureName, /*IsAdoptable=*/false, SENumber, \
131+
#FeatureName)
115132
#endif
116133

117134
#ifndef EXPERIMENTAL_FEATURE
118-
// Warning: setting `AvailableInProd` to `true` on a feature means that the flag
119-
// cannot be dropped in the future.
120-
# define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
121-
LANGUAGE_FEATURE(FeatureName, 0, #FeatureName)
135+
// Warning: setting `AvailableInProd` to `true` on a feature means that the
136+
// flag cannot be dropped in the future.
137+
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
138+
LANGUAGE_FEATURE(FeatureName, /*IsAdoptable=*/false, 0, #FeatureName)
122139
#endif
123140

124141
#ifndef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
@@ -127,8 +144,9 @@
127144
#endif
128145

129146
#ifndef BASELINE_LANGUAGE_FEATURE
130-
# define BASELINE_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
131-
LANGUAGE_FEATURE(FeatureName, SENumber, Description)
147+
#define BASELINE_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
148+
LANGUAGE_FEATURE(FeatureName, /*IsAdoptable=*/false, SENumber, \
149+
Description)
132150
#endif
133151

134152
BASELINE_LANGUAGE_FEATURE(AsyncAwait, 296, "async/await")
@@ -199,10 +217,14 @@ BASELINE_LANGUAGE_FEATURE(BodyMacros, 415, "Function body macros")
199217
SUPPRESSIBLE_LANGUAGE_FEATURE(SendingArgsAndResults, 430, "Sending arg and results")
200218
BASELINE_LANGUAGE_FEATURE(BorrowingSwitch, 432, "Noncopyable type pattern matching")
201219
CONDITIONALLY_SUPPRESSIBLE_LANGUAGE_FEATURE(IsolatedAny, 431, "@isolated(any) function types")
202-
LANGUAGE_FEATURE(IsolatedAny2, 431, "@isolated(any) function types")
203-
LANGUAGE_FEATURE(ObjCImplementation, 436, "@objc @implementation extensions")
204-
LANGUAGE_FEATURE(NonescapableTypes, 446, "Nonescapable types")
205-
LANGUAGE_FEATURE(BuiltinEmplaceTypedThrows, 0, "Builtin.emplace typed throws")
220+
LANGUAGE_FEATURE(IsolatedAny2, /*IsAdoptable=*/false, 431,
221+
"@isolated(any) function types")
222+
LANGUAGE_FEATURE(ObjCImplementation, /*IsAdoptable=*/false, 436,
223+
"@objc @implementation extensions")
224+
LANGUAGE_FEATURE(NonescapableTypes, /*IsAdoptable=*/false, 446,
225+
"Nonescapable types")
226+
LANGUAGE_FEATURE(BuiltinEmplaceTypedThrows, /*IsAdoptable=*/false, 0,
227+
"Builtin.emplace typed throws")
206228

207229
// Swift 6
208230
UPCOMING_FEATURE(ConciseMagicFile, 274, 6)
@@ -458,6 +480,7 @@ EXPERIMENTAL_FEATURE(ExtensibleEnums, true)
458480
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
459481
#undef EXPERIMENTAL_FEATURE
460482
#undef UPCOMING_FEATURE
483+
#undef ADOPTABLE_UPCOMING_FEATURE
461484
#undef BASELINE_LANGUAGE_FEATURE
462485
#undef CONDITIONALLY_SUPPRESSIBLE_EXPERIMENTAL_FEATURE
463486
#undef CONDITIONALLY_SUPPRESSIBLE_LANGUAGE_FEATURE

lib/AST/ASTPrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// This source file is part of the Swift.org open source project
33
//
4-
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
4+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
55
// Licensed under Apache License v2.0 with Runtime Library Exception
66
//
77
// See https://swift.org/LICENSE.txt for license information
@@ -3263,7 +3263,7 @@ suppressingFeatureExecutionAttribute(PrintOptions &options,
32633263
static void suppressingFeature(PrintOptions &options, Feature feature,
32643264
llvm::function_ref<void()> action) {
32653265
switch (feature) {
3266-
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
3266+
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description) \
32673267
case Feature::FeatureName: \
32683268
llvm_unreachable("not a suppressible feature");
32693269
#define SUPPRESSIBLE_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \

lib/AST/FeatureSet.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2024 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2024 - 2025 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -44,7 +44,7 @@ static bool usesTypeMatching(Decl *decl, llvm::function_ref<bool(Type)> fn) {
4444

4545
#define BASELINE_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
4646
static bool usesFeature##FeatureName(Decl *decl) { return false; }
47-
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
47+
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description)
4848
#include "swift/Basic/Features.def"
4949

5050
#define UNINTERESTING_FEATURE(FeatureName) \
@@ -511,7 +511,7 @@ void FeatureSet::collectFeaturesUsed(Decl *decl, InsertOrRemove operation) {
511511

512512
// Go through each of the features, checking whether the
513513
// declaration uses that feature.
514-
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
514+
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description) \
515515
if (CHECK(usesFeature##FeatureName)) \
516516
collectRequiredFeature(Feature::FeatureName, operation);
517517
#define SUPPRESSIBLE_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \

lib/Basic/Feature.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ using namespace swift;
1818

1919
bool swift::isFeatureAvailableInProduction(Feature feature) {
2020
switch (feature) {
21-
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
21+
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description) \
2222
case Feature::FeatureName: \
2323
return true;
2424
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
@@ -31,7 +31,7 @@ bool swift::isFeatureAvailableInProduction(Feature feature) {
3131

3232
llvm::StringRef swift::getFeatureName(Feature feature) {
3333
switch (feature) {
34-
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
34+
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description) \
3535
case Feature::FeatureName: \
3636
return #FeatureName;
3737
#include "swift/Basic/Features.def"
@@ -41,7 +41,7 @@ llvm::StringRef swift::getFeatureName(Feature feature) {
4141

4242
std::optional<Feature> swift::getUpcomingFeature(llvm::StringRef name) {
4343
return llvm::StringSwitch<std::optional<Feature>>(name)
44-
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
44+
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description)
4545
#define UPCOMING_FEATURE(FeatureName, SENumber, Version) \
4646
.Case(#FeatureName, Feature::FeatureName)
4747
#include "swift/Basic/Features.def"
@@ -50,7 +50,7 @@ std::optional<Feature> swift::getUpcomingFeature(llvm::StringRef name) {
5050

5151
std::optional<Feature> swift::getExperimentalFeature(llvm::StringRef name) {
5252
return llvm::StringSwitch<std::optional<Feature>>(name)
53-
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
53+
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description)
5454
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
5555
.Case(#FeatureName, Feature::FeatureName)
5656
#include "swift/Basic/Features.def"
@@ -59,7 +59,7 @@ std::optional<Feature> swift::getExperimentalFeature(llvm::StringRef name) {
5959

6060
std::optional<unsigned> swift::getFeatureLanguageVersion(Feature feature) {
6161
switch (feature) {
62-
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
62+
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description)
6363
#define UPCOMING_FEATURE(FeatureName, SENumber, Version) \
6464
case Feature::FeatureName: \
6565
return Version;
@@ -69,9 +69,18 @@ std::optional<unsigned> swift::getFeatureLanguageVersion(Feature feature) {
6969
}
7070
}
7171

72+
bool swift::isFeatureAdoptable(Feature feature) {
73+
switch (feature) {
74+
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description) \
75+
case Feature::FeatureName: \
76+
return IsAdoptable;
77+
#include "swift/Basic/Features.def"
78+
}
79+
}
80+
7281
bool swift::includeInModuleInterface(Feature feature) {
7382
switch (feature) {
74-
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
83+
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description) \
7584
case Feature::FeatureName: \
7685
return true;
7786
#define EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE(FeatureName, \

lib/Basic/LangOptions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ using namespace swift;
3434

3535
LangOptions::LangOptions() {
3636
// Add all promoted language features
37-
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
37+
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description) \
3838
Features.insert(Feature::FeatureName);
3939
#define UPCOMING_FEATURE(FeatureName, SENumber, Version)
4040
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd)
@@ -302,7 +302,7 @@ bool LangOptions::hasFeature(Feature feature) const {
302302

303303
bool LangOptions::hasFeature(llvm::StringRef featureName) const {
304304
auto feature = llvm::StringSwitch<std::optional<Feature>>(featureName)
305-
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
305+
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description) \
306306
.Case(#FeatureName, Feature::FeatureName)
307307
#include "swift/Basic/Features.def"
308308
.Default(std::nullopt);

test/lit.swift-features.cfg.inc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* This source file is part of the Swift.org open source project
55
*
6-
* Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
6+
* Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
77
* Licensed under Apache License v2.0 with Runtime Library Exception
88
*
99
* See https://swift.org/LICENSE.txt for license information
@@ -30,6 +30,7 @@ def language_feature(feature_name, enabled):
3030

3131
#define UPCOMING_FEATURE(FeatureName, SENumber, Version) language_feature(#FeatureName, True)
3232
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) language_feature(#FeatureName, #AvailableInProd == "true")
33-
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) language_feature(#FeatureName, True)
33+
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description) \
34+
language_feature(#FeatureName, True)
3435

3536
#include <swift/Basic/Features.def>

unittests/Frontend/IsFeatureEnabledTests.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,27 @@ TEST_F(IsFeatureEnabledTest, VerifyTestedFeatures) {
3434
{
3535
ASSERT_FALSE(getUpcomingFeature(feature.name));
3636
ASSERT_FALSE(getExperimentalFeature(feature.name));
37+
ASSERT_FALSE(isFeatureAdoptable(feature));
3738
}
3839

3940
feature = upcomingF;
4041
{
4142
ASSERT_TRUE(getUpcomingFeature(feature.name));
43+
ASSERT_FALSE(isFeatureAdoptable(feature));
4244
ASSERT_LT(defaultLangMode, feature.langMode);
4345
}
4446

4547
feature = strictConcurrencyF;
4648
{
4749
ASSERT_TRUE(getUpcomingFeature(feature.name));
50+
ASSERT_FALSE(isFeatureAdoptable(feature));
4851
ASSERT_LT(defaultLangMode, feature.langMode);
4952
}
5053

5154
feature = experimentalF;
5255
{
5356
ASSERT_TRUE(getExperimentalFeature(feature.name));
57+
ASSERT_FALSE(isFeatureAdoptable(feature));
5458
}
5559
}
5660

0 commit comments

Comments
 (0)