Skip to content

Commit 9d864c8

Browse files
committed
macros: add diagnostic derive for lints
`SessionDiagnostic` isn't suitable for use on lints as whether or not it creates an error or a warning is decided at compile-time by the macro, whereas lints decide this at runtime based on the location of the lint being reported (as it will depend on the user's `allow`/`deny` attributes, etc). Re-using most of the machinery for `SessionDiagnostic`, this macro introduces a `LintDiagnostic` derive which implements a `DecorateLint` trait, taking a `LintDiagnosticBuilder` and adding to the lint according to the diagnostic struct.
1 parent 7f9d848 commit 9d864c8

File tree

12 files changed

+845
-612
lines changed

12 files changed

+845
-612
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4009,6 +4009,7 @@ dependencies = [
40094009
"rustc_hir",
40104010
"rustc_index",
40114011
"rustc_infer",
4012+
"rustc_macros",
40124013
"rustc_middle",
40134014
"rustc_parse_format",
40144015
"rustc_session",

compiler/rustc_lint/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ rustc_trait_selection = { path = "../rustc_trait_selection" }
2222
rustc_parse_format = { path = "../rustc_parse_format" }
2323
rustc_infer = { path = "../rustc_infer" }
2424
rustc_type_ir = { path = "../rustc_type_ir" }
25+
rustc_macros = { path = "../rustc_macros" }

compiler/rustc_lint/src/types.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_data_structures::fx::FxHashSet;
55
use rustc_errors::{fluent, Applicability, DiagnosticMessage};
66
use rustc_hir as hir;
77
use rustc_hir::{is_range_literal, Expr, ExprKind, Node};
8+
use rustc_macros::LintDiagnostic;
89
use rustc_middle::ty::layout::{IntegerExt, LayoutOf, SizeSkeleton};
910
use rustc_middle::ty::subst::SubstsRef;
1011
use rustc_middle::ty::{self, AdtKind, DefIdTree, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable};
@@ -1553,13 +1554,20 @@ impl InvalidAtomicOrdering {
15531554
let Some(fail_ordering) = Self::match_ordering(cx, fail_order_arg) else { return };
15541555

15551556
if matches!(fail_ordering, sym::Release | sym::AcqRel) {
1556-
cx.struct_span_lint(INVALID_ATOMIC_ORDERING, fail_order_arg.span, |diag| {
1557-
diag.build(fluent::lint::atomic_ordering_invalid)
1558-
.set_arg("method", method)
1559-
.span_label(fail_order_arg.span, fluent::lint::label)
1560-
.help(fluent::lint::help)
1561-
.emit();
1562-
});
1557+
#[derive(LintDiagnostic)]
1558+
#[lint(lint::atomic_ordering_invalid)]
1559+
#[help]
1560+
struct InvalidAtomicOrderingDiag {
1561+
method: Symbol,
1562+
#[label]
1563+
fail_order_arg_span: Span,
1564+
}
1565+
1566+
cx.emit_spanned_lint(
1567+
INVALID_ATOMIC_ORDERING,
1568+
fail_order_arg.span,
1569+
InvalidAtomicOrderingDiag { method, fail_order_arg_span: fail_order_arg.span },
1570+
);
15631571
}
15641572

15651573
let Some(success_ordering) = Self::match_ordering(cx, success_order_arg) else { return };

compiler/rustc_macros/src/diagnostics/diagnostic.rs

+102-575
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)