Skip to content

Commit 15c7467

Browse files
committed
NFC: Abstract away the use of '7' to represent the next language mode
Introduce `Version::getFutureMajorLanguageVersion` to make it easier to find clients that will need to be updated once we have a new language mode.
1 parent c363658 commit 15c7467

File tree

8 files changed

+44
-16
lines changed

8 files changed

+44
-16
lines changed

include/swift/AST/DiagnosticEngine.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,17 @@ namespace swift {
767767
return limitBehaviorUntilSwiftVersion(limit, languageMode);
768768
}
769769

770+
/// Limit the diagnostic behavior to warning until the next future
771+
/// language mode.
772+
///
773+
/// This should be preferred over passing the next major version to
774+
/// `warnUntilSwiftVersion` to make it easier to find and update clients
775+
/// when a new language mode is introduced.
776+
///
777+
/// This helps stage in fixes for stricter diagnostics as warnings
778+
/// until the next major language version.
779+
InFlightDiagnostic &warnUntilFutureSwiftVersion();
780+
770781
/// Limit the diagnostic behavior to warning until the specified version.
771782
///
772783
/// This helps stage in fixes for stricter diagnostics as warnings

include/swift/Basic/Version.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ class Version {
131131
/// SWIFT_VERSION_MINOR.
132132
static Version getCurrentLanguageVersion();
133133

134+
/// Returns a major version to represent the next future language mode. This
135+
/// exists to make it easier to find and update clients when a new language
136+
/// mode is added.
137+
static constexpr unsigned getFutureMajorLanguageVersion() {
138+
return 7;
139+
}
140+
134141
// List of backward-compatibility versions that we permit passing as
135142
// -swift-version <vers>
136143
static std::array<StringRef, 4> getValidEffectiveVersions() {

lib/AST/DiagnosticEngine.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ InFlightDiagnostic::limitBehaviorUntilSwiftVersion(
452452
// version. We do this before limiting the behavior, because
453453
// wrapIn will result in the behavior of the wrapping diagnostic.
454454
if (limit >= DiagnosticBehavior::Warning) {
455-
if (majorVersion > 6) {
455+
if (majorVersion >= version::Version::getFutureMajorLanguageVersion()) {
456456
wrapIn(diag::error_in_a_future_swift_lang_mode);
457457
} else {
458458
wrapIn(diag::error_in_swift_lang_mode, majorVersion);
@@ -472,6 +472,11 @@ InFlightDiagnostic::limitBehaviorUntilSwiftVersion(
472472
return *this;
473473
}
474474

475+
InFlightDiagnostic &InFlightDiagnostic::warnUntilFutureSwiftVersion() {
476+
using namespace version;
477+
return warnUntilSwiftVersion(Version::getFutureMajorLanguageVersion());
478+
}
479+
475480
InFlightDiagnostic &
476481
InFlightDiagnostic::warnUntilSwiftVersion(unsigned majorVersion) {
477482
return limitBehaviorUntilSwiftVersion(DiagnosticBehavior::Warning,

lib/Basic/Version.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,16 +181,19 @@ std::optional<Version> Version::getEffectiveLanguageVersion() const {
181181
static_assert(SWIFT_VERSION_MAJOR == 6,
182182
"getCurrentLanguageVersion is no longer correct here");
183183
return Version::getCurrentLanguageVersion();
184-
case 7:
185-
// Allow version '7' in asserts compilers *only* so that we can start
186-
// testing changes planned for after Swift 6. Note that it's still not
187-
// listed in `Version::getValidEffectiveVersions()`.
188-
// FIXME: When Swift 7 becomes real, remove 'REQUIRES: swift7' from tests
189-
// using '-swift-version 7'.
184+
185+
// FIXME: When Swift 7 becomes real, remove 'REQUIRES: swift7' from tests
186+
// using '-swift-version 7'.
187+
188+
case Version::getFutureMajorLanguageVersion():
189+
// Allow the future language mode version in asserts compilers *only* so
190+
// that we can start testing changes planned for after the current latest
191+
// language mode. Note that it'll not be listed in
192+
// `Version::getValidEffectiveVersions()`.
190193
#ifdef NDEBUG
191194
LLVM_FALLTHROUGH;
192195
#else
193-
return Version{7};
196+
return Version{Version::getFutureMajorLanguageVersion()};
194197
#endif
195198
default:
196199
return std::nullopt;

lib/Sema/MiscDiagnostics.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2243,11 +2243,12 @@ class ImplicitSelfUsageChecker : public BaseDiagnosticWalker {
22432243
invalidImplicitSelfShouldOnlyWarn510(base, closure)) {
22442244
warnUntilVersion.emplace(6);
22452245
}
2246-
// Prior to Swift 7, downgrade to a warning if we're in a macro to preserve
2247-
// compatibility with the Swift 6 diagnostic behavior where we previously
2248-
// skipped diagnosing.
2249-
if (!Ctx.isSwiftVersionAtLeast(7) && isInMacro())
2250-
warnUntilVersion.emplace(7);
2246+
// Prior to the next language mode, downgrade to a warning if we're in a
2247+
// macro to preserve compatibility with the Swift 6 diagnostic behavior
2248+
// where we previously skipped diagnosing.
2249+
auto futureVersion = version::Version::getFutureMajorLanguageVersion();
2250+
if (!Ctx.isSwiftVersionAtLeast(futureVersion) && isInMacro())
2251+
warnUntilVersion.emplace(futureVersion);
22512252

22522253
auto diag = Ctx.Diags.diagnose(loc, ID, std::move(Args)...);
22532254
if (warnUntilVersion)

lib/Sema/TypeCheckAttr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1249,7 +1249,7 @@ void AttributeChecker::visitAccessControlAttr(AccessControlAttr *attr) {
12491249
diagnose(attr->getLocation(),
12501250
diag::access_control_non_objc_open_member, VD)
12511251
.fixItReplace(attr->getRange(), "public")
1252-
.warnUntilSwiftVersion(7);
1252+
.warnUntilFutureSwiftVersion();
12531253
}
12541254
}
12551255
}

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2676,7 +2676,7 @@ namespace {
26762676
fromType, toType);
26772677

26782678
if (downgradeToWarning)
2679-
diag.warnUntilSwiftVersion(7);
2679+
diag.warnUntilFutureSwiftVersion();
26802680
}
26812681

26822682
for (auto type : nonSendableTypes) {

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5169,7 +5169,8 @@ static bool diagnoseTypeWitnessAvailability(
51695169
return false;
51705170

51715171
// In Swift 6 and earlier type witness availability diagnostics are warnings.
5172-
const unsigned warnBeforeVersion = 7;
5172+
using namespace version;
5173+
const unsigned warnBeforeVersion = Version::getFutureMajorLanguageVersion();
51735174
bool shouldError =
51745175
ctx.LangOpts.EffectiveLanguageVersion.isVersionAtLeast(warnBeforeVersion);
51755176

0 commit comments

Comments
 (0)