Skip to content

Commit 480aa4d

Browse files
Migrate rustc_mir_transform to use TyCtxt::emit_diag_node_span_lint
1 parent d926c1b commit 480aa4d

12 files changed

Lines changed: 81 additions & 71 deletions

compiler/rustc_mir_transform/src/check_call_recursion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ fn check_recursion<'tcx>(
8383

8484
let sp = tcx.def_span(def_id);
8585
let hir_id = tcx.local_def_id_to_hir_id(def_id);
86-
tcx.emit_node_span_lint(
86+
tcx.emit_diag_node_span_lint(
8787
UNCONDITIONAL_RECURSION,
8888
hir_id,
8989
sp,

compiler/rustc_mir_transform/src/check_const_item_mutation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl<'tcx> Visitor<'tcx> for ConstMutationChecker<'_, 'tcx> {
104104
&& let Some((lint_root, span, item)) =
105105
self.should_lint_const_item_usage(lhs, def_id, loc)
106106
{
107-
self.tcx.emit_node_span_lint(
107+
self.tcx.emit_diag_node_span_lint(
108108
CONST_ITEM_MUTATION,
109109
lint_root,
110110
span,
@@ -150,7 +150,7 @@ impl<'tcx> Visitor<'tcx> for ConstMutationChecker<'_, 'tcx> {
150150
if let Some((lint_root, span, item)) =
151151
self.should_lint_const_item_usage(place, def_id, lint_loc)
152152
{
153-
self.tcx.emit_node_span_lint(
153+
self.tcx.emit_diag_node_span_lint(
154154
CONST_ITEM_MUTATION,
155155
lint_root,
156156
span,

compiler/rustc_mir_transform/src/coroutine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1990,7 +1990,7 @@ fn check_must_not_suspend_def(
19901990
if let Some(reason_str) = find_attr!(tcx, def_id, MustNotSupend {reason} => reason) {
19911991
let reason =
19921992
reason_str.map(|s| errors::MustNotSuspendReason { span: data.source_span, reason: s });
1993-
tcx.emit_node_span_lint(
1993+
tcx.emit_diag_node_span_lint(
19941994
rustc_session::lint::builtin::MUST_NOT_SUSPEND,
19951995
hir_id,
19961996
data.source_span,

compiler/rustc_mir_transform/src/errors.rs

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use rustc_errors::codes::*;
2-
use rustc_errors::{Applicability, Diag, EmissionGuarantee, LintDiagnostic, Subdiagnostic, msg};
3-
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
2+
use rustc_errors::{
3+
Applicability, Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level, Subdiagnostic, msg,
4+
};
5+
use rustc_macros::{Diagnostic, Subdiagnostic};
46
use rustc_middle::mir::AssertKind;
57
use rustc_middle::query::Key;
68
use rustc_middle::ty::TyCtxt;
@@ -48,7 +50,7 @@ pub(crate) fn emit_inline_always_target_feature_diagnostic<'a, 'tcx>(
4850
);
4951
}
5052

51-
#[derive(LintDiagnostic)]
53+
#[derive(Diagnostic)]
5254
#[diag("function cannot return without recursing")]
5355
#[help("a `loop` may express intention better if this is on purpose")]
5456
pub(crate) struct UnconditionalRecursion {
@@ -70,7 +72,7 @@ pub(crate) struct InvalidForceInline {
7072
pub reason: &'static str,
7173
}
7274

73-
#[derive(LintDiagnostic)]
75+
#[derive(Diagnostic)]
7476
pub(crate) enum ConstMutate {
7577
#[diag("attempting to modify a `const` item")]
7678
#[note(
@@ -129,21 +131,26 @@ pub(crate) enum AssertLintKind {
129131
UnconditionalPanic,
130132
}
131133

132-
impl<'a, P: std::fmt::Debug> LintDiagnostic<'a, ()> for AssertLint<P> {
133-
fn decorate_lint<'b>(self, diag: &'b mut Diag<'a, ()>) {
134-
diag.primary_message(match self.lint_kind {
135-
AssertLintKind::ArithmeticOverflow => {
136-
msg!("this arithmetic operation will overflow")
137-
}
138-
AssertLintKind::UnconditionalPanic => {
139-
msg!("this operation will panic at runtime")
140-
}
141-
});
134+
impl<'a, P: std::fmt::Debug> Diagnostic<'a, ()> for AssertLint<P> {
135+
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
136+
let mut diag = Diag::new(
137+
dcx,
138+
level,
139+
match self.lint_kind {
140+
AssertLintKind::ArithmeticOverflow => {
141+
msg!("this arithmetic operation will overflow")
142+
}
143+
AssertLintKind::UnconditionalPanic => {
144+
msg!("this operation will panic at runtime")
145+
}
146+
},
147+
);
142148
let label = self.assert_kind.diagnostic_message();
143149
self.assert_kind.add_args(&mut |name, value| {
144150
diag.arg(name, value);
145151
});
146152
diag.span_label(self.span, label);
153+
diag
147154
}
148155
}
149156

@@ -156,14 +163,14 @@ impl AssertLintKind {
156163
}
157164
}
158165

159-
#[derive(LintDiagnostic)]
166+
#[derive(Diagnostic)]
160167
#[diag("call to inline assembly that may unwind")]
161168
pub(crate) struct AsmUnwindCall {
162169
#[label("call to inline assembly that may unwind")]
163170
pub span: Span,
164171
}
165172

166-
#[derive(LintDiagnostic)]
173+
#[derive(Diagnostic)]
167174
#[diag(
168175
"call to {$foreign ->
169176
[true] foreign function
@@ -181,7 +188,7 @@ pub(crate) struct FfiUnwindCall {
181188
pub foreign: bool,
182189
}
183190

184-
#[derive(LintDiagnostic)]
191+
#[derive(Diagnostic)]
185192
#[diag("taking a reference to a function item does not give a function pointer")]
186193
pub(crate) struct FnItemRef {
187194
#[suggestion(
@@ -194,14 +201,14 @@ pub(crate) struct FnItemRef {
194201
pub ident: Ident,
195202
}
196203

197-
#[derive(LintDiagnostic)]
204+
#[derive(Diagnostic)]
198205
#[diag("value captured by `{$name}` is never read")]
199206
#[help("did you mean to capture by reference instead?")]
200207
pub(crate) struct UnusedCaptureMaybeCaptureRef {
201208
pub name: Symbol,
202209
}
203210

204-
#[derive(LintDiagnostic)]
211+
#[derive(Diagnostic)]
205212
#[diag("variable `{$name}` is assigned to, but never used")]
206213
#[note("consider using `_{$name}` instead")]
207214
pub(crate) struct UnusedVarAssignedOnly {
@@ -210,7 +217,7 @@ pub(crate) struct UnusedVarAssignedOnly {
210217
pub typo: Option<PatternTypo>,
211218
}
212219

213-
#[derive(LintDiagnostic)]
220+
#[derive(Diagnostic)]
214221
#[diag("value assigned to `{$name}` is never read")]
215222
pub(crate) struct UnusedAssign {
216223
pub name: Symbol,
@@ -237,14 +244,14 @@ pub(crate) struct UnusedAssignSuggestion {
237244
pub rhs_borrow_span: Span,
238245
}
239246

240-
#[derive(LintDiagnostic)]
247+
#[derive(Diagnostic)]
241248
#[diag("value passed to `{$name}` is never read")]
242249
#[help("maybe it is overwritten before being read?")]
243250
pub(crate) struct UnusedAssignPassed {
244251
pub name: Symbol,
245252
}
246253

247-
#[derive(LintDiagnostic)]
254+
#[derive(Diagnostic)]
248255
#[diag("unused variable: `{$name}`")]
249256
pub(crate) struct UnusedVariable {
250257
pub name: Symbol,
@@ -331,11 +338,13 @@ pub(crate) struct MustNotSupend<'a, 'tcx> {
331338
}
332339

333340
// Needed for def_path_str
334-
impl<'a> LintDiagnostic<'a, ()> for MustNotSupend<'_, '_> {
335-
fn decorate_lint<'b>(self, diag: &'b mut rustc_errors::Diag<'a, ()>) {
336-
diag.primary_message(msg!(
337-
"{$pre}`{$def_path}`{$post} held across a suspend point, but should not be"
338-
));
341+
impl<'a> Diagnostic<'a, ()> for MustNotSupend<'_, '_> {
342+
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
343+
let mut diag = Diag::new(
344+
dcx,
345+
level,
346+
msg!("{$pre}`{$def_path}`{$post} held across a suspend point, but should not be"),
347+
);
339348
diag.span_label(self.yield_sp, msg!("the value is held across this suspend point"));
340349
if let Some(reason) = self.reason {
341350
diag.subdiagnostic(reason);
@@ -344,6 +353,7 @@ impl<'a> LintDiagnostic<'a, ()> for MustNotSupend<'_, '_> {
344353
diag.arg("pre", self.pre);
345354
diag.arg("def_path", self.tcx.def_path_str(self.def_id));
346355
diag.arg("post", self.post);
356+
diag
347357
}
348358
}
349359

compiler/rustc_mir_transform/src/ffi_unwind_calls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ fn has_ffi_unwind_calls(tcx: TyCtxt<'_>, local_def_id: LocalDefId) -> bool {
6363
.lint_root;
6464
let span = terminator.source_info.span;
6565

66-
tcx.emit_node_span_lint(
66+
tcx.emit_diag_node_span_lint(
6767
FFI_UNWIND_CALLS,
6868
lint_root,
6969
span,
@@ -115,7 +115,7 @@ fn has_ffi_unwind_calls(tcx: TyCtxt<'_>, local_def_id: LocalDefId) -> bool {
115115
let span = terminator.source_info.span;
116116

117117
let foreign = fn_def_id.is_some();
118-
tcx.emit_node_span_lint(
118+
tcx.emit_diag_node_span_lint(
119119
FFI_UNWIND_CALLS,
120120
lint_root,
121121
span,

compiler/rustc_mir_transform/src/function_item_references.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ impl<'tcx> FunctionItemRefChecker<'_, 'tcx> {
175175
ret,
176176
);
177177

178-
self.tcx.emit_node_span_lint(
178+
self.tcx.emit_diag_node_span_lint(
179179
FUNCTION_ITEM_REFERENCES,
180180
lint_root,
181181
span,

compiler/rustc_mir_transform/src/known_panics_lint.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
297297
let source_info = self.body.source_info(location);
298298
if let Some(lint_root) = self.lint_root(*source_info) {
299299
let span = source_info.span;
300-
self.tcx.emit_node_span_lint(
300+
self.tcx.emit_diag_node_span_lint(
301301
lint_kind.lint(),
302302
lint_root,
303303
span,

compiler/rustc_mir_transform/src/lint_tail_expr_drop_order.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_hir::CRATE_HIR_ID;
1010
use rustc_hir::def_id::LocalDefId;
1111
use rustc_index::bit_set::MixedBitSet;
1212
use rustc_index::{IndexSlice, IndexVec};
13-
use rustc_macros::{LintDiagnostic, Subdiagnostic};
13+
use rustc_macros::{Diagnostic, Subdiagnostic};
1414
use rustc_middle::bug;
1515
use rustc_middle::mir::{
1616
self, BasicBlock, Body, ClearCrossCrate, Local, Location, MirDumper, Place, StatementKind,
@@ -451,7 +451,7 @@ pub(crate) fn run_lint<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &Body<
451451
}
452452

453453
let span = local_labels[0].span;
454-
tcx.emit_node_span_lint(
454+
tcx.emit_diag_node_span_lint(
455455
lint::builtin::TAIL_EXPR_DROP_ORDER,
456456
lint_root.unwrap_or(CRATE_HIR_ID),
457457
span,
@@ -498,7 +498,7 @@ fn assign_observables_names(
498498
names
499499
}
500500

501-
#[derive(LintDiagnostic)]
501+
#[derive(Diagnostic)]
502502
#[diag("relative drop order changing in Rust 2024")]
503503
struct TailExprDropOrderLint<'a> {
504504
#[subdiagnostic]

compiler/rustc_mir_transform/src/liveness.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ impl<'a, 'tcx> AssignmentResult<'a, 'tcx> {
968968
let typo = maybe_suggest_typo();
969969
errors::UnusedVariableSugg::TryPrefix { spans: vec![def_span], name, typo }
970970
};
971-
tcx.emit_node_span_lint(
971+
tcx.emit_diag_node_span_lint(
972972
lint::builtin::UNUSED_VARIABLES,
973973
hir_id,
974974
def_span,
@@ -1018,7 +1018,7 @@ impl<'a, 'tcx> AssignmentResult<'a, 'tcx> {
10181018
}
10191019

10201020
let typo = maybe_suggest_typo();
1021-
tcx.emit_node_span_lint(
1021+
tcx.emit_diag_node_span_lint(
10221022
lint::builtin::UNUSED_VARIABLES,
10231023
hir_id,
10241024
def_span,
@@ -1060,7 +1060,7 @@ impl<'a, 'tcx> AssignmentResult<'a, 'tcx> {
10601060
errors::UnusedVariableSugg::TryPrefix { name, typo, spans: vec![def_span] }
10611061
};
10621062

1063-
tcx.emit_node_span_lint(
1063+
tcx.emit_diag_node_span_lint(
10641064
lint::builtin::UNUSED_VARIABLES,
10651065
hir_id,
10661066
spans,
@@ -1126,20 +1126,20 @@ impl<'a, 'tcx> AssignmentResult<'a, 'tcx> {
11261126
source_info.span,
11271127
self.body,
11281128
);
1129-
tcx.emit_node_span_lint(
1129+
tcx.emit_diag_node_span_lint(
11301130
lint::builtin::UNUSED_ASSIGNMENTS,
11311131
hir_id,
11321132
source_info.span,
11331133
errors::UnusedAssign { name, help: suggestion.is_none(), suggestion },
11341134
)
11351135
}
1136-
AccessKind::Param => tcx.emit_node_span_lint(
1136+
AccessKind::Param => tcx.emit_diag_node_span_lint(
11371137
lint::builtin::UNUSED_ASSIGNMENTS,
11381138
hir_id,
11391139
source_info.span,
11401140
errors::UnusedAssignPassed { name },
11411141
),
1142-
AccessKind::Capture => tcx.emit_node_span_lint(
1142+
AccessKind::Capture => tcx.emit_diag_node_span_lint(
11431143
lint::builtin::UNUSED_ASSIGNMENTS,
11441144
hir_id,
11451145
decl_span,

0 commit comments

Comments
 (0)