Skip to content

Commit c98094b

Browse files
Remove TyCtxt::node_span_lint usage from rustc_hir_typeck
1 parent e1ee669 commit c98094b

8 files changed

Lines changed: 614 additions & 392 deletions

File tree

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use std::slice;
33

44
use rustc_abi::FieldIdx;
55
use rustc_data_structures::fx::FxHashSet;
6-
use rustc_errors::{Applicability, Diag, ErrorGuaranteed, MultiSpan};
6+
use rustc_errors::{
7+
Applicability, Diag, DiagCtxtHandle, Diagnostic, ErrorGuaranteed, Level, MultiSpan,
8+
};
79
use rustc_hir::def::{CtorOf, DefKind, Res};
810
use rustc_hir::def_id::DefId;
911
use rustc_hir::intravisit::VisitorExt;
@@ -80,6 +82,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
8082
/// Produces warning on the given node, if the current point in the
8183
/// function is unreachable, and there hasn't been another warning.
8284
pub(crate) fn warn_if_unreachable(&self, id: HirId, span: Span, kind: &str) {
85+
struct UnreachableItem<'a, 'b> {
86+
kind: &'a str,
87+
span: Span,
88+
orig_span: Span,
89+
custom_note: Option<&'b str>,
90+
}
91+
92+
impl<'a, 'b, 'c> Diagnostic<'a, ()> for UnreachableItem<'b, 'c> {
93+
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
94+
let Self { kind, span, orig_span, custom_note } = self;
95+
let msg = format!("unreachable {kind}");
96+
Diag::new(dcx, level, msg.clone()).with_span_label(span, msg).with_span_label(
97+
orig_span,
98+
custom_note.map(|c| c.to_owned()).unwrap_or_else(|| {
99+
"any code following this expression is unreachable".to_owned()
100+
}),
101+
)
102+
}
103+
}
104+
83105
let Diverges::Always { span: orig_span, custom_note } = self.diverges.get() else {
84106
return;
85107
};
@@ -102,14 +124,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
102124

103125
debug!("warn_if_unreachable: id={:?} span={:?} kind={}", id, span, kind);
104126

105-
let msg = format!("unreachable {kind}");
106-
self.tcx().node_span_lint(lint::builtin::UNREACHABLE_CODE, id, span, |lint| {
107-
lint.primary_message(msg.clone());
108-
lint.span_label(span, msg).span_label(
109-
orig_span,
110-
custom_note.unwrap_or("any code following this expression is unreachable"),
111-
);
112-
})
127+
self.tcx().emit_node_span_lint(
128+
lint::builtin::UNREACHABLE_CODE,
129+
id,
130+
span,
131+
UnreachableItem { kind, span, orig_span, custom_note },
132+
);
113133
}
114134

115135
/// Resolves type and const variables in `t` if possible. Unlike the infcx

compiler/rustc_hir_typeck/src/inline_asm.rs

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rustc_abi::FieldIdx;
22
use rustc_ast::InlineAsmTemplatePiece;
33
use rustc_data_structures::fx::FxIndexSet;
4+
use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, Level};
45
use rustc_hir::def_id::DefId;
56
use rustc_hir::{self as hir, LangItem};
67
use rustc_middle::bug;
@@ -168,6 +169,40 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
168169
is_input: bool,
169170
tied_input: Option<(&'tcx hir::Expr<'tcx>, Option<InlineAsmType>)>,
170171
) -> Option<InlineAsmType> {
172+
struct FormattingSubRegisterArg<'a> {
173+
expr_span: Span,
174+
idx: usize,
175+
suggested_modifier: char,
176+
suggested_result: &'a str,
177+
suggested_size: u16,
178+
default_modifier: char,
179+
default_result: &'a str,
180+
default_size: u16,
181+
}
182+
183+
impl<'a, 'b> Diagnostic<'a, ()> for FormattingSubRegisterArg<'b> {
184+
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
185+
let Self {
186+
expr_span,
187+
idx,
188+
suggested_modifier,
189+
suggested_result,
190+
suggested_size,
191+
default_modifier,
192+
default_result,
193+
default_size,
194+
} = self;
195+
Diag::new(dcx, level, "formatting may not be suitable for sub-register argument")
196+
.with_span_label(expr_span, "for this argument")
197+
.with_help(format!(
198+
"use `{{{idx}:{suggested_modifier}}}` to have the register formatted as `{suggested_result}` (for {suggested_size}-bit values)",
199+
))
200+
.with_help(format!(
201+
"or use `{{{idx}:{default_modifier}}}` to keep the default formatting of `{default_result}` (for {default_size}-bit values)",
202+
))
203+
}
204+
}
205+
171206
let ty = self.expr_ty(expr);
172207
if ty.has_non_region_infer() {
173208
bug!("inference variable in asm operand ty: {:?} {:?}", expr, ty);
@@ -362,19 +397,19 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
362397
result: default_result,
363398
size: default_size,
364399
} = reg_class.default_modifier(asm_arch).unwrap();
365-
self.tcx().node_span_lint(
400+
self.tcx().emit_node_span_lint(
366401
lint::builtin::ASM_SUB_REGISTER,
367402
expr.hir_id,
368403
spans,
369-
|lint| {
370-
lint.primary_message("formatting may not be suitable for sub-register argument");
371-
lint.span_label(expr.span, "for this argument");
372-
lint.help(format!(
373-
"use `{{{idx}:{suggested_modifier}}}` to have the register formatted as `{suggested_result}` (for {suggested_size}-bit values)",
374-
));
375-
lint.help(format!(
376-
"or use `{{{idx}:{default_modifier}}}` to keep the default formatting of `{default_result}` (for {default_size}-bit values)",
377-
));
404+
FormattingSubRegisterArg {
405+
expr_span: expr.span,
406+
idx,
407+
suggested_modifier,
408+
suggested_result,
409+
suggested_size,
410+
default_modifier,
411+
default_result,
412+
default_size,
378413
},
379414
);
380415
}

0 commit comments

Comments
 (0)