Skip to content

Small adjustments to adoption mode modeling #79897

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 4 commits into from
Mar 11, 2025
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
3 changes: 1 addition & 2 deletions include/swift/Basic/BasicBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ void BridgedData_free(BridgedData data);
//===----------------------------------------------------------------------===//

enum ENUM_EXTENSIBILITY_ATTR(open) BridgedFeature {
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description) \
FeatureName,
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) FeatureName,
#include "swift/Basic/Features.def"
};

Expand Down
6 changes: 2 additions & 4 deletions include/swift/Basic/Feature.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ class LangOptions;

/// Enumeration describing all of the named features.
enum class Feature : uint16_t {
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description) \
FeatureName,
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) FeatureName,
#include "swift/Basic/Features.def"
};

constexpr unsigned numFeatures() {
enum Features {
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description) \
FeatureName,
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) FeatureName,
#include "swift/Basic/Features.def"
NumFeatures
};
Expand Down
74 changes: 37 additions & 37 deletions include/swift/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,14 @@
// features.
//
//
// LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description)
// LANGUAGE_FEATURE(FeatureName, SENumber, Description)
//
// The LANGUAGE_FEATURE macro describes each named feature that is
// introduced in Swift. It allows Swift code to check for a particular
// feature with "#if $FeatureName" in source code.
//
// FeatureName: The name given to this feature to be used in source code,
// e.g., AsyncAwait.
// IsAdoptable: Whether the feature implements adoption mode.
//
// If the feature is upcoming (source-breaking) and provides for a
// mechanical code migration, it should implement adoption mode.
//
// Adoption mode is a feature-oriented code migration mechanism: a mode
// of operation that should produce compiler warnings with attached
// fix-its that can be applied to preserve the behavior of the code once
// the upcoming feature is enacted.
// These warnings must belong to a diagnostic group named after the
// feature. Adoption mode itself *and* the fix-its it produces must be
// source and binary compatible with how the code is compiled when the
// feature is disabled.
//
// SENumber: The number assigned to this feature in the Swift Evolution
// process, or 0 if there isn't one.
// Description: A string literal describing the feature.
Expand Down Expand Up @@ -106,13 +92,12 @@

#ifndef SUPPRESSIBLE_LANGUAGE_FEATURE
#define SUPPRESSIBLE_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
LANGUAGE_FEATURE(FeatureName, /*IsAdoptable=*/false, SENumber, \
Description)
LANGUAGE_FEATURE(FeatureName, SENumber, Description)
#endif

#ifndef OPTIONAL_LANGUAGE_FEATURE
#define OPTIONAL_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
LANGUAGE_FEATURE(FeatureName, /*IsAdoptable=*/false, SENumber, Description)
LANGUAGE_FEATURE(FeatureName, SENumber, Description)
#endif

// A feature that's both conditionally-suppressible and experimental.
Expand All @@ -133,33 +118,53 @@
#ifndef CONDITIONALLY_SUPPRESSIBLE_LANGUAGE_FEATURE
#define CONDITIONALLY_SUPPRESSIBLE_LANGUAGE_FEATURE(FeatureName, SENumber, \
Description) \
LANGUAGE_FEATURE(FeatureName, /*IsAdoptable=*/false, SENumber, \
Description)
LANGUAGE_FEATURE(FeatureName, SENumber, Description)
#endif

// An upcoming feature that supports adoption mode.
//
// If the feature is source-breaking and provides for a
// mechanical code migration, it should implement adoption mode.
//
// Adoption mode is a feature-oriented code migration mechanism: a mode
// of operation that should produce compiler warnings with attached
// fix-its that can be applied to preserve the behavior of the code once
// the upcoming feature is enacted.
// These warnings must belong to a diagnostic group named after the
// feature. Adoption mode itself *and* the fix-its it produces must be
// source and binary compatible with how the code is compiled when the
// feature is disabled.
#ifndef ADOPTABLE_UPCOMING_FEATURE
#if defined(UPCOMING_FEATURE)
#define ADOPTABLE_UPCOMING_FEATURE(FeatureName, SENumber, Version) \
UPCOMING_FEATURE(FeatureName, SENumber, Version)
#else
#define ADOPTABLE_UPCOMING_FEATURE(FeatureName, SENumber, Version) \
LANGUAGE_FEATURE(FeatureName, /*IsAdoptable=*/true, SENumber, \
#FeatureName)
LANGUAGE_FEATURE(FeatureName, SENumber, #FeatureName)
#endif
#endif

// See `ADOPTABLE_UPCOMING_FEATURE`.
#ifndef ADOPTABLE_EXPERIMENTAL_FEATURE
#if defined(EXPERIMENTAL_FEATURE)
#define ADOPTABLE_EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd)
#else
#define ADOPTABLE_EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
LANGUAGE_FEATURE(FeatureName, 0, #FeatureName)
#endif
#endif

#ifndef UPCOMING_FEATURE
#define UPCOMING_FEATURE(FeatureName, SENumber, Version) \
LANGUAGE_FEATURE(FeatureName, /*IsAdoptable=*/false, SENumber, \
#FeatureName)
LANGUAGE_FEATURE(FeatureName, SENumber, #FeatureName)
#endif

#ifndef EXPERIMENTAL_FEATURE
// Warning: setting `AvailableInProd` to `true` on a feature means that the
// flag cannot be dropped in the future.
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
LANGUAGE_FEATURE(FeatureName, /*IsAdoptable=*/false, 0, #FeatureName)
LANGUAGE_FEATURE(FeatureName, 0, #FeatureName)
#endif

#ifndef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
Expand All @@ -169,8 +174,7 @@

#ifndef BASELINE_LANGUAGE_FEATURE
#define BASELINE_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
LANGUAGE_FEATURE(FeatureName, /*IsAdoptable=*/false, SENumber, \
Description)
LANGUAGE_FEATURE(FeatureName, SENumber, Description)
#endif

BASELINE_LANGUAGE_FEATURE(AsyncAwait, 296, "async/await")
Expand Down Expand Up @@ -242,17 +246,12 @@ BASELINE_LANGUAGE_FEATURE(BodyMacros, 415, "Function body macros")
SUPPRESSIBLE_LANGUAGE_FEATURE(SendingArgsAndResults, 430, "Sending arg and results")
BASELINE_LANGUAGE_FEATURE(BorrowingSwitch, 432, "Noncopyable type pattern matching")
CONDITIONALLY_SUPPRESSIBLE_LANGUAGE_FEATURE(IsolatedAny, 431, "@isolated(any) function types")
LANGUAGE_FEATURE(IsolatedAny2, /*IsAdoptable=*/false, 431,
"@isolated(any) function types")
LANGUAGE_FEATURE(ObjCImplementation, /*IsAdoptable=*/false, 436,
"@objc @implementation extensions")
LANGUAGE_FEATURE(NonescapableTypes, /*IsAdoptable=*/false, 446,
"Nonescapable types")
LANGUAGE_FEATURE(BuiltinEmplaceTypedThrows, /*IsAdoptable=*/false, 0,
"Builtin.emplace typed throws")
LANGUAGE_FEATURE(IsolatedAny2, 431, "@isolated(any) function types")
LANGUAGE_FEATURE(ObjCImplementation, 436, "@objc @implementation extensions")
LANGUAGE_FEATURE(NonescapableTypes, 446, "Nonescapable types")
LANGUAGE_FEATURE(BuiltinEmplaceTypedThrows, 0, "Builtin.emplace typed throws")
SUPPRESSIBLE_LANGUAGE_FEATURE(MemorySafetyAttributes, 458, "@unsafe attribute")
LANGUAGE_FEATURE(ValueGenerics, /*IsAdoptable=*/ false, 452,
"Value generics feature (integer generics)")
LANGUAGE_FEATURE(ValueGenerics, 452, "Value generics feature (integer generics)")

// Swift 6
UPCOMING_FEATURE(ConciseMagicFile, 274, 6)
Expand Down Expand Up @@ -518,6 +517,7 @@ EXPERIMENTAL_FEATURE(CompileTimeValues, true)
#undef EXPERIMENTAL_FEATURE
#undef UPCOMING_FEATURE
#undef ADOPTABLE_UPCOMING_FEATURE
#undef ADOPTABLE_EXPERIMENTAL_FEATURE
#undef BASELINE_LANGUAGE_FEATURE
#undef OPTIONAL_LANGUAGE_FEATURE
#undef CONDITIONALLY_SUPPRESSIBLE_EXPERIMENTAL_FEATURE
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ namespace swift {

/// A wrapper around the feature state enumeration.
struct FeatureState {
enum Kind : uint8_t { Off, EnabledForAdoption, Enabled };
enum class Kind : uint8_t { Off, EnabledForAdoption, Enabled };

private:
Feature feature;
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3267,7 +3267,7 @@ suppressingFeatureExecutionAttribute(PrintOptions &options,
static void suppressingFeature(PrintOptions &options, Feature feature,
llvm::function_ref<void()> action) {
switch (feature) {
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description) \
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
case Feature::FeatureName: \
llvm_unreachable("not a suppressible feature");
#define SUPPRESSIBLE_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
Expand Down
4 changes: 2 additions & 2 deletions lib/AST/FeatureSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static bool usesTypeMatching(Decl *decl, llvm::function_ref<bool(Type)> fn) {

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

#define UNINTERESTING_FEATURE(FeatureName) \
Expand Down Expand Up @@ -550,7 +550,7 @@ void FeatureSet::collectFeaturesUsed(Decl *decl, InsertOrRemove operation) {

// Go through each of the features, checking whether the
// declaration uses that feature.
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description) \
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
if (CHECK(usesFeature##FeatureName)) \
collectRequiredFeature(Feature::FeatureName, operation);
#define SUPPRESSIBLE_LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
Expand Down
27 changes: 18 additions & 9 deletions lib/Basic/Feature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ using namespace swift;

bool swift::isFeatureAvailableInProduction(Feature feature) {
switch (feature) {
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description) \
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
case Feature::FeatureName: \
return true;
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
Expand All @@ -31,7 +31,7 @@ bool swift::isFeatureAvailableInProduction(Feature feature) {

llvm::StringRef swift::getFeatureName(Feature feature) {
switch (feature) {
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description) \
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
case Feature::FeatureName: \
return #FeatureName;
#include "swift/Basic/Features.def"
Expand All @@ -41,7 +41,7 @@ llvm::StringRef swift::getFeatureName(Feature feature) {

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

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

std::optional<unsigned> swift::getFeatureLanguageVersion(Feature feature) {
switch (feature) {
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description)
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
#define UPCOMING_FEATURE(FeatureName, SENumber, Version) \
case Feature::FeatureName: \
return Version;
Expand All @@ -71,16 +71,25 @@ std::optional<unsigned> swift::getFeatureLanguageVersion(Feature feature) {

bool swift::isFeatureAdoptable(Feature feature) {
switch (feature) {
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description) \
case Feature::FeatureName: \
return IsAdoptable;
#define ADOPTABLE_UPCOMING_FEATURE(FeatureName, SENumber, Version)
#define ADOPTABLE_EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd)
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
case Feature::FeatureName:
#include "swift/Basic/Features.def"
return false;
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description)
#define ADOPTABLE_UPCOMING_FEATURE(FeatureName, SENumber, Version) \
case Feature::FeatureName:
#define ADOPTABLE_EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) \
case Feature::FeatureName:
#include "swift/Basic/Features.def"
return true;
}
}

bool swift::includeInModuleInterface(Feature feature) {
switch (feature) {
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description) \
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
case Feature::FeatureName: \
return true;
#define EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE(FeatureName, \
Expand Down
19 changes: 10 additions & 9 deletions lib/Basic/LangOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ using namespace swift;

LangOptions::LangOptions() {
// Add all promoted language features
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description) \
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
this->enableFeature(Feature::FeatureName);
#define UPCOMING_FEATURE(FeatureName, SENumber, Version)
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd)
Expand Down Expand Up @@ -295,18 +295,18 @@ bool LangOptions::isCustomConditionalCompilationFlagSet(StringRef Name) const {
}

bool LangOptions::FeatureState::isEnabled() const {
return this->state != FeatureState::Off;
return this->state == FeatureState::Kind::Enabled;
}

bool LangOptions::FeatureState::isEnabledForAdoption() const {
ASSERT(isFeatureAdoptable(this->feature) &&
"You forgot to make the feature adoptable!");

return this->state == FeatureState::EnabledForAdoption;
return this->state == FeatureState::Kind::EnabledForAdoption;
}

LangOptions::FeatureStateStorage::FeatureStateStorage()
: states(numFeatures(), FeatureState::Off) {}
: states(numFeatures(), FeatureState::Kind::Off) {}

void LangOptions::FeatureStateStorage::setState(Feature feature,
FeatureState::Kind state) {
Expand All @@ -329,7 +329,7 @@ LangOptions::FeatureState LangOptions::getFeatureState(Feature feature) const {

if (auto version = getFeatureLanguageVersion(feature)) {
if (this->isSwiftVersionAtLeast(*version)) {
return FeatureState(feature, FeatureState::Enabled);
return FeatureState(feature, FeatureState::Kind::Enabled);
}
}

Expand All @@ -348,7 +348,7 @@ bool LangOptions::hasFeature(Feature feature) const {

bool LangOptions::hasFeature(llvm::StringRef featureName) const {
auto feature = llvm::StringSwitch<std::optional<Feature>>(featureName)
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description) \
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
.Case(#FeatureName, Feature::FeatureName)
#include "swift/Basic/Features.def"
.Default(std::nullopt);
Expand All @@ -361,14 +361,15 @@ bool LangOptions::hasFeature(llvm::StringRef featureName) const {
void LangOptions::enableFeature(Feature feature, bool forAdoption) {
if (forAdoption) {
ASSERT(isFeatureAdoptable(feature));
this->featureStates.setState(feature, FeatureState::EnabledForAdoption);
this->featureStates.setState(feature,
FeatureState::Kind::EnabledForAdoption);
} else {
this->featureStates.setState(feature, FeatureState::Enabled);
this->featureStates.setState(feature, FeatureState::Kind::Enabled);
}
}

void LangOptions::disableFeature(Feature feature) {
this->featureStates.setState(feature, FeatureState::Off);
this->featureStates.setState(feature, FeatureState::Kind::Off);
}

void LangOptions::setHasAtomicBitWidth(llvm::Triple triple) {
Expand Down
16 changes: 9 additions & 7 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6411,8 +6411,10 @@ class ExistentialTypeSyntaxChecker : public ASTWalker {
return false;
}

// A missing `any` or `some` is always diagnosed if this feature is enabled.
if (ctx.LangOpts.hasFeature(Feature::ExistentialAny)) {
// A missing `any` or `some` is always diagnosed if this feature not
// disabled.
auto featureState = ctx.LangOpts.getFeatureState(Feature::ExistentialAny);
if (featureState.isEnabled() || featureState.isEnabledForAdoption()) {
return true;
}

Expand Down Expand Up @@ -6505,13 +6507,13 @@ class ExistentialTypeSyntaxChecker : public ASTWalker {
/*isAlias=*/isa<TypeAliasDecl>(decl)));
}

// If the feature is enabled in adoption mode, warn unconditionally.
// Otherwise, until Swift 7.
if (Ctx.LangOpts.getFeatureState(Feature::ExistentialAny)
.isEnabledForAdoption()) {
// If `ExistentialAny` is enabled in adoption mode, warn unconditionally.
// Otherwise, warn until the feature's coming-of-age language mode.
const auto feature = Feature::ExistentialAny;
if (Ctx.LangOpts.getFeatureState(feature).isEnabledForAdoption()) {
diag->limitBehavior(DiagnosticBehavior::Warning);
} else {
diag->warnUntilSwiftVersion(7);
diag->warnUntilSwiftVersion(getFeatureLanguageVersion(feature).value());
}

emitInsertAnyFixit(*diag, T);
Expand Down
2 changes: 1 addition & 1 deletion test/lit.swift-features.cfg.inc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def language_feature(feature_name, enabled):

#define UPCOMING_FEATURE(FeatureName, SENumber, Version) language_feature(#FeatureName, True)
#define EXPERIMENTAL_FEATURE(FeatureName, AvailableInProd) language_feature(#FeatureName, #AvailableInProd == "true")
#define LANGUAGE_FEATURE(FeatureName, IsAdoptable, SENumber, Description) \
#define LANGUAGE_FEATURE(FeatureName, SENumber, Description) \
language_feature(#FeatureName, True)

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