Skip to content

Commit b102c98

Browse files
committed
[NFC] Basic: Use scoped enum for feature states
To prevent unexpected implicit conversions to integral types.
1 parent 6b12faf commit b102c98

File tree

4 files changed

+14
-13
lines changed

4 files changed

+14
-13
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ namespace swift {
814814

815815
/// A wrapper around the feature state enumeration.
816816
struct FeatureState {
817-
enum Kind : uint8_t { Off, EnabledForAdoption, Enabled };
817+
enum class Kind : uint8_t { Off, EnabledForAdoption, Enabled };
818818

819819
private:
820820
Feature feature;

lib/Basic/LangOptions.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -295,18 +295,18 @@ bool LangOptions::isCustomConditionalCompilationFlagSet(StringRef Name) const {
295295
}
296296

297297
bool LangOptions::FeatureState::isEnabled() const {
298-
return this->state != FeatureState::Off;
298+
return this->state != FeatureState::Kind::Off;
299299
}
300300

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

305-
return this->state == FeatureState::EnabledForAdoption;
305+
return this->state == FeatureState::Kind::EnabledForAdoption;
306306
}
307307

308308
LangOptions::FeatureStateStorage::FeatureStateStorage()
309-
: states(numFeatures(), FeatureState::Off) {}
309+
: states(numFeatures(), FeatureState::Kind::Off) {}
310310

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

330330
if (auto version = getFeatureLanguageVersion(feature)) {
331331
if (this->isSwiftVersionAtLeast(*version)) {
332-
return FeatureState(feature, FeatureState::Enabled);
332+
return FeatureState(feature, FeatureState::Kind::Enabled);
333333
}
334334
}
335335

@@ -361,14 +361,15 @@ bool LangOptions::hasFeature(llvm::StringRef featureName) const {
361361
void LangOptions::enableFeature(Feature feature, bool forAdoption) {
362362
if (forAdoption) {
363363
ASSERT(isFeatureAdoptable(feature));
364-
this->featureStates.setState(feature, FeatureState::EnabledForAdoption);
364+
this->featureStates.setState(feature,
365+
FeatureState::Kind::EnabledForAdoption);
365366
} else {
366-
this->featureStates.setState(feature, FeatureState::Enabled);
367+
this->featureStates.setState(feature, FeatureState::Kind::Enabled);
367368
}
368369
}
369370

370371
void LangOptions::disableFeature(Feature feature) {
371-
this->featureStates.setState(feature, FeatureState::Off);
372+
this->featureStates.setState(feature, FeatureState::Kind::Off);
372373
}
373374

374375
void LangOptions::setHasAtomicBitWidth(llvm::Triple triple) {

unittests/Frontend/FeatureParsingTest.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ void swift::PrintTo(const LangOptions::FeatureState &value, std::ostream *os) {
4949
void swift::PrintTo(const LangOptions::FeatureState::Kind &value,
5050
std::ostream *os) {
5151
switch (value) {
52-
case LangOptions::FeatureState::Off:
52+
case LangOptions::FeatureState::Kind::Off:
5353
*os << "Off";
5454
break;
55-
case LangOptions::FeatureState::EnabledForAdoption:
55+
case LangOptions::FeatureState::Kind::EnabledForAdoption:
5656
*os << "EnabledForAdoption";
5757
break;
58-
case LangOptions::FeatureState::Enabled:
58+
case LangOptions::FeatureState::Kind::Enabled:
5959
*os << "Enabled";
6060
break;
6161
}

unittests/Frontend/IsFeatureEnabledTests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ static const FeatureWrapper adoptableUpcomingF(Feature::ExistentialAny);
2323
static const FeatureWrapper experimentalF(Feature::StructLetDestructuring);
2424
static const FeatureWrapper strictConcurrencyF(Feature::StrictConcurrency);
2525

26-
using FeatureState = LangOptions::FeatureState;
26+
using FeatureState = LangOptions::FeatureState::Kind;
2727

2828
using IsFeatureEnabledTestCase =
29-
ArgParsingTestCase<std::map<Feature, FeatureState::Kind>>;
29+
ArgParsingTestCase<std::map<Feature, FeatureState>>;
3030

3131
class IsFeatureEnabledTest
3232
: public FeatureParsingTest,

0 commit comments

Comments
 (0)