Skip to content

Commit a06c4af

Browse files
committed
[Diagnostics] Add InFlightDiagnostic::warnUntilSwiftVersion to limit the
diagnostic behavior to a warning until the specified language version. This helper can be used to stage in fixes for stricter diagnostics as warnings until the next major language version.
1 parent 7a35c47 commit a06c4af

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

include/swift/AST/DiagnosticEngine.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/AST/DeclNameLoc.h"
2323
#include "swift/AST/DiagnosticConsumer.h"
2424
#include "swift/AST/TypeLoc.h"
25+
#include "swift/Basic/Version.h"
2526
#include "swift/Localization/LocalizationFormat.h"
2627
#include "llvm/ADT/BitVector.h"
2728
#include "llvm/ADT/StringRef.h"
@@ -528,6 +529,12 @@ namespace swift {
528529
/// emitted as a warning, but a note will still be emitted as a note.
529530
InFlightDiagnostic &limitBehavior(DiagnosticBehavior limit);
530531

532+
/// Limit the diagnostic behavior to warning until the specified version.
533+
///
534+
/// This helps stage in fixes for stricter diagnostics as warnings
535+
/// until the next major language version.
536+
InFlightDiagnostic &warnUntilSwiftVersion(unsigned majorVersion);
537+
531538
/// Wraps this diagnostic in another diagnostic. That is, \p wrapper will be
532539
/// emitted in place of the diagnostic that otherwise would have been
533540
/// emitted.
@@ -803,6 +810,10 @@ namespace swift {
803810
/// Path to diagnostic documentation directory.
804811
std::string diagnosticDocumentationPath = "";
805812

813+
/// The Swift language version. This is used to limit diagnostic behavior
814+
/// until a specific language version, e.g. Swift 6.
815+
version::Version languageVersion;
816+
806817
/// Whether we are actively pretty-printing a declaration as part of
807818
/// diagnostics.
808819
bool IsPrettyPrintingDecl = false;
@@ -865,6 +876,8 @@ namespace swift {
865876

866877
bool isPrettyPrintingDecl() const { return IsPrettyPrintingDecl; }
867878

879+
void setLanguageVersion(version::Version v) { languageVersion = v; }
880+
868881
void setLocalization(StringRef locale, StringRef path) {
869882
assert(!locale.empty());
870883
assert(!path.empty());

lib/AST/DiagnosticEngine.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,14 @@ InFlightDiagnostic::limitBehavior(DiagnosticBehavior limit) {
319319
return *this;
320320
}
321321

322+
InFlightDiagnostic &
323+
InFlightDiagnostic::warnUntilSwiftVersion(unsigned majorVersion) {
324+
if (!Engine->languageVersion.isVersionAtLeast(majorVersion))
325+
this->limitBehavior(DiagnosticBehavior::Warning);
326+
327+
return *this;
328+
}
329+
322330
InFlightDiagnostic &
323331
InFlightDiagnostic::wrapIn(const Diagnostic &wrapper) {
324332
// Save current active diagnostic into WrappedDiagnostics, ignoring state

lib/Frontend/Frontend.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,8 @@ void CompilerInstance::setUpDiagnosticOptions() {
451451
}
452452
Diagnostics.setDiagnosticDocumentationPath(
453453
Invocation.getDiagnosticOptions().DiagnosticDocumentationPath);
454+
Diagnostics.setLanguageVersion(
455+
Invocation.getLangOptions().EffectiveLanguageVersion);
454456
if (!Invocation.getDiagnosticOptions().LocalizationCode.empty()) {
455457
Diagnostics.setLocalization(
456458
Invocation.getDiagnosticOptions().LocalizationCode,

0 commit comments

Comments
 (0)