Skip to content

[Diagnostics] Add InFlightDiagnostic::warnUntilSwiftVersion to downgrade errors to warnings #38302

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 3 commits into from
Jul 8, 2021
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
13 changes: 13 additions & 0 deletions include/swift/AST/DiagnosticEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "swift/AST/DeclNameLoc.h"
#include "swift/AST/DiagnosticConsumer.h"
#include "swift/AST/TypeLoc.h"
#include "swift/Basic/Version.h"
#include "swift/Localization/LocalizationFormat.h"
#include "llvm/ADT/BitVector.h"
#include "llvm/ADT/StringRef.h"
Expand Down Expand Up @@ -528,6 +529,12 @@ namespace swift {
/// emitted as a warning, but a note will still be emitted as a note.
InFlightDiagnostic &limitBehavior(DiagnosticBehavior limit);

/// Limit the diagnostic behavior to warning until the specified version.
///
/// This helps stage in fixes for stricter diagnostics as warnings
/// until the next major language version.
InFlightDiagnostic &warnUntilSwiftVersion(unsigned majorVersion);

/// Wraps this diagnostic in another diagnostic. That is, \p wrapper will be
/// emitted in place of the diagnostic that otherwise would have been
/// emitted.
Expand Down Expand Up @@ -803,6 +810,10 @@ namespace swift {
/// Path to diagnostic documentation directory.
std::string diagnosticDocumentationPath = "";

/// The Swift language version. This is used to limit diagnostic behavior
/// until a specific language version, e.g. Swift 6.
version::Version languageVersion;

/// Whether we are actively pretty-printing a declaration as part of
/// diagnostics.
bool IsPrettyPrintingDecl = false;
Expand Down Expand Up @@ -865,6 +876,8 @@ namespace swift {

bool isPrettyPrintingDecl() const { return IsPrettyPrintingDecl; }

void setLanguageVersion(version::Version v) { languageVersion = v; }

void setLocalization(StringRef locale, StringRef path) {
assert(!locale.empty());
assert(!path.empty());
Expand Down
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsCommon.def
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ NOTE(previous_decldef,none,
NOTE(brace_stmt_suggest_do,none,
"did you mean to use a 'do' statement?", ())

WARNING(error_in_future_swift_version,none,
"%0; this is an error in Swift %1",
(DiagnosticInfo *, unsigned))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The diagnostic wrapping approach here is very cool. The "will be" wording feels a little off for existing warnings, because "will be an error in Swift 5" should really be "is an error in Swift 5". Should we say "is an error in Swift 5" instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. As soon as you use warnUntilSwiftVersion, that diagnostic is already an error under that language version. I'll update the wording to your suggestion 👍


// Generic disambiguation
NOTE(while_parsing_as_left_angle_bracket,none,
"while parsing this '<' as a type parameter bracket", ())
Expand Down
4 changes: 0 additions & 4 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -4728,10 +4728,6 @@ ERROR(invalid_ownership_is_let,none,
ERROR(ownership_invalid_in_protocols,none,
"%0 cannot be applied to a property declaration in a protocol",
(ReferenceOwnership))
WARNING(ownership_invalid_in_protocols_compat_warning,none,
"%0 should not be applied to a property declaration "
"in a protocol and will be disallowed in future versions",
(ReferenceOwnership))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good find. I have so many places I'd like to use this!


// required
ERROR(required_initializer_nonclass,none,
Expand Down
11 changes: 11 additions & 0 deletions lib/AST/DiagnosticEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
//===----------------------------------------------------------------------===//

#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/DiagnosticsCommon.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTPrinter.h"
#include "swift/AST/Decl.h"
Expand Down Expand Up @@ -319,6 +320,16 @@ InFlightDiagnostic::limitBehavior(DiagnosticBehavior limit) {
return *this;
}

InFlightDiagnostic &
InFlightDiagnostic::warnUntilSwiftVersion(unsigned majorVersion) {
if (!Engine->languageVersion.isVersionAtLeast(majorVersion)) {
limitBehavior(DiagnosticBehavior::Warning)
.wrapIn(diag::error_in_future_swift_version, majorVersion);
}

return *this;
}

InFlightDiagnostic &
InFlightDiagnostic::wrapIn(const Diagnostic &wrapper) {
// Save current active diagnostic into WrappedDiagnostics, ignoring state
Expand Down
2 changes: 2 additions & 0 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,8 @@ void CompilerInstance::setUpDiagnosticOptions() {
}
Diagnostics.setDiagnosticDocumentationPath(
Invocation.getDiagnosticOptions().DiagnosticDocumentationPath);
Diagnostics.setLanguageVersion(
Invocation.getLangOptions().EffectiveLanguageVersion);
if (!Invocation.getDiagnosticOptions().LocalizationCode.empty()) {
Diagnostics.setLocalization(
Invocation.getDiagnosticOptions().LocalizationCode,
Expand Down
5 changes: 2 additions & 3 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3354,10 +3354,9 @@ Type TypeChecker::checkReferenceOwnershipAttr(VarDecl *var, Type type,
if (PDC && !PDC->isObjC()) {
// Ownership does not make sense in protocols, except for "weak" on
// properties of Objective-C protocols.
auto D = var->getASTContext().isSwiftVersionAtLeast(5)
? diag::ownership_invalid_in_protocols
: diag::ownership_invalid_in_protocols_compat_warning;
auto D = diag::ownership_invalid_in_protocols;
Diags.diagnose(attr->getLocation(), D, ownershipKind)
.warnUntilSwiftVersion(5)
.fixItRemove(attr->getRange());
attr->setInvalid();
}
Expand Down
8 changes: 4 additions & 4 deletions test/Compatibility/ownership_protocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
class SomeClass {}

protocol P {
// expected-warning@+1 {{'weak' should not be applied to a property declaration in a protocol and will be disallowed in future versions}}
// expected-warning@+1 {{'weak' cannot be applied to a property declaration in a protocol; this is an error in Swift 5}}
weak var foo: SomeClass? { get set }
// expected-warning@+1 {{'unowned' should not be applied to a property declaration in a protocol and will be disallowed in future versions}}
// expected-warning@+1 {{'unowned' cannot be applied to a property declaration in a protocol; this is an error in Swift 5}}
unowned var foo2: SomeClass { get set }
// expected-warning@+2 {{'weak' should not be applied to a property declaration in a protocol and will be disallowed in future versions}}
// expected-warning@+2 {{'weak' cannot be applied to a property declaration in a protocol; this is an error in Swift 5}}
// expected-error@+1 {{'weak' may only be applied to class and class-bound protocol types, not 'Int'}}
weak var foo3: Int? { get set }
// expected-warning@+2 {{'unowned' should not be applied to a property declaration in a protocol and will be disallowed in future versions}}
// expected-warning@+2 {{'unowned' cannot be applied to a property declaration in a protocol; this is an error in Swift 5}}
// expected-error@+1 {{'unowned' may only be applied to class and class-bound protocol types, not 'Int'}}
unowned var foo4: Int { get set }
}
Expand Down
2 changes: 1 addition & 1 deletion test/decl/protocol/protocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ protocol ShouldntCrash {

// rdar://problem/18168866
protocol FirstProtocol {
// expected-warning@+1 {{'weak' should not be applied to a property declaration in a protocol and will be disallowed in future versions}}
// expected-warning@+1 {{'weak' cannot be applied to a property declaration in a protocol; this is an error in Swift 5}}
weak var delegate : SecondProtocol? { get } // expected-error{{'weak' must not be applied to non-class-bound 'SecondProtocol'; consider adding a protocol conformance that has a class bound}}
}

Expand Down