1
Fork 0

Remove all eight DiagnosticBuilder::*_with_code methods.

These all have relatively low use, and can be perfectly emulated with
a simpler construction method combined with `code` or `code_mv`.
This commit is contained in:
Nicholas Nethercote 2024-01-03 21:50:36 +11:00
parent bd4e623485
commit 6682f243dc
14 changed files with 84 additions and 180 deletions

View file

@ -538,11 +538,11 @@ impl<G: EmissionGuarantee> Drop for DiagnosticBuilder<'_, G> {
#[macro_export] #[macro_export]
macro_rules! struct_span_err { macro_rules! struct_span_err {
($dcx:expr, $span:expr, $code:ident, $($message:tt)*) => ({ ($dcx:expr, $span:expr, $code:ident, $($message:tt)*) => ({
$dcx.struct_span_err_with_code( $dcx.struct_span_err(
$span, $span,
format!($($message)*), format!($($message)*),
$crate::error_code!($code),
) )
.code_mv($crate::error_code!($code))
}) })
} }

View file

@ -732,19 +732,6 @@ impl DiagCtxt {
self.struct_warn(msg).span_mv(span) self.struct_warn(msg).span_mv(span)
} }
/// Construct a builder at the `Warning` level at the given `span` and with the `msg`.
/// Also include a code.
#[rustc_lint_diagnostics]
#[track_caller]
pub fn struct_span_warn_with_code(
&self,
span: impl Into<MultiSpan>,
msg: impl Into<DiagnosticMessage>,
code: DiagnosticId,
) -> DiagnosticBuilder<'_, ()> {
self.struct_span_warn(span, msg).code_mv(code)
}
/// Construct a builder at the `Warning` level with the `msg`. /// Construct a builder at the `Warning` level with the `msg`.
/// ///
/// Attempting to `.emit()` the builder will only emit if either: /// Attempting to `.emit()` the builder will only emit if either:
@ -785,18 +772,6 @@ impl DiagCtxt {
self.struct_err(msg).span_mv(span) self.struct_err(msg).span_mv(span)
} }
/// Construct a builder at the `Error` level at the given `span`, with the `msg`, and `code`.
#[rustc_lint_diagnostics]
#[track_caller]
pub fn struct_span_err_with_code(
&self,
span: impl Into<MultiSpan>,
msg: impl Into<DiagnosticMessage>,
code: DiagnosticId,
) -> DiagnosticBuilder<'_> {
self.struct_span_err(span, msg).code_mv(code)
}
/// Construct a builder at the `Error` level with the `msg`. /// Construct a builder at the `Error` level with the `msg`.
// FIXME: This method should be removed (every error should have an associated error code). // FIXME: This method should be removed (every error should have an associated error code).
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
@ -805,28 +780,6 @@ impl DiagCtxt {
DiagnosticBuilder::new(self, Error, msg) DiagnosticBuilder::new(self, Error, msg)
} }
/// Construct a builder at the `Error` level with the `msg` and the `code`.
#[rustc_lint_diagnostics]
#[track_caller]
pub fn struct_err_with_code(
&self,
msg: impl Into<DiagnosticMessage>,
code: DiagnosticId,
) -> DiagnosticBuilder<'_> {
self.struct_err(msg).code_mv(code)
}
/// Construct a builder at the `Warn` level with the `msg` and the `code`.
#[rustc_lint_diagnostics]
#[track_caller]
pub fn struct_warn_with_code(
&self,
msg: impl Into<DiagnosticMessage>,
code: DiagnosticId,
) -> DiagnosticBuilder<'_, ()> {
self.struct_warn(msg).code_mv(code)
}
/// Construct a builder at the `Fatal` level at the given `span` and with the `msg`. /// Construct a builder at the `Fatal` level at the given `span` and with the `msg`.
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
@ -838,18 +791,6 @@ impl DiagCtxt {
self.struct_fatal(msg).span_mv(span) self.struct_fatal(msg).span_mv(span)
} }
/// Construct a builder at the `Fatal` level at the given `span`, with the `msg`, and `code`.
#[rustc_lint_diagnostics]
#[track_caller]
pub fn struct_span_fatal_with_code(
&self,
span: impl Into<MultiSpan>,
msg: impl Into<DiagnosticMessage>,
code: DiagnosticId,
) -> DiagnosticBuilder<'_, FatalAbort> {
self.struct_span_fatal(span, msg).code_mv(code)
}
/// Construct a builder at the `Fatal` level with the `msg`. /// Construct a builder at the `Fatal` level with the `msg`.
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
@ -897,17 +838,6 @@ impl DiagCtxt {
self.struct_span_fatal(span, msg).emit() self.struct_span_fatal(span, msg).emit()
} }
#[rustc_lint_diagnostics]
#[track_caller]
pub fn span_fatal_with_code(
&self,
span: impl Into<MultiSpan>,
msg: impl Into<DiagnosticMessage>,
code: DiagnosticId,
) -> ! {
self.struct_span_fatal_with_code(span, msg, code).emit()
}
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
pub fn span_err( pub fn span_err(
@ -918,34 +848,12 @@ impl DiagCtxt {
self.struct_span_err(span, msg).emit() self.struct_span_err(span, msg).emit()
} }
#[rustc_lint_diagnostics]
#[track_caller]
pub fn span_err_with_code(
&self,
span: impl Into<MultiSpan>,
msg: impl Into<DiagnosticMessage>,
code: DiagnosticId,
) -> ErrorGuaranteed {
self.struct_span_err_with_code(span, msg, code).emit()
}
#[rustc_lint_diagnostics] #[rustc_lint_diagnostics]
#[track_caller] #[track_caller]
pub fn span_warn(&self, span: impl Into<MultiSpan>, msg: impl Into<DiagnosticMessage>) { pub fn span_warn(&self, span: impl Into<MultiSpan>, msg: impl Into<DiagnosticMessage>) {
self.struct_span_warn(span, msg).emit() self.struct_span_warn(span, msg).emit()
} }
#[rustc_lint_diagnostics]
#[track_caller]
pub fn span_warn_with_code(
&self,
span: impl Into<MultiSpan>,
msg: impl Into<DiagnosticMessage>,
code: DiagnosticId,
) {
self.struct_span_warn_with_code(span, msg, code).emit()
}
pub fn span_bug(&self, span: impl Into<MultiSpan>, msg: impl Into<DiagnosticMessage>) -> ! { pub fn span_bug(&self, span: impl Into<MultiSpan>, msg: impl Into<DiagnosticMessage>) -> ! {
self.struct_span_bug(span, msg).emit() self.struct_span_bug(span, msg).emit()
} }

View file

@ -1647,7 +1647,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let msg = format!("{kind} `{name}` is private"); let msg = format!("{kind} `{name}` is private");
let def_span = tcx.def_span(item); let def_span = tcx.def_span(item);
tcx.dcx() tcx.dcx()
.struct_span_err_with_code(span, msg, rustc_errors::error_code!(E0624)) .struct_span_err(span, msg)
.code_mv(rustc_errors::error_code!(E0624))
.span_label_mv(span, format!("private {kind}")) .span_label_mv(span, format!("private {kind}"))
.span_label_mv(def_span, format!("{kind} defined here")) .span_label_mv(def_span, format!("{kind} defined here"))
.emit(); .emit();

View file

@ -1371,7 +1371,7 @@ fn compare_number_of_generics<'tcx>(
let spans = arg_spans(impl_.kind, impl_item.generics); let spans = arg_spans(impl_.kind, impl_item.generics);
let span = spans.first().copied(); let span = spans.first().copied();
let mut err = tcx.dcx().struct_span_err_with_code( let mut err = tcx.dcx().struct_span_err(
spans, spans,
format!( format!(
"{} `{}` has {} {kind} parameter{} but its trait \ "{} `{}` has {} {kind} parameter{} but its trait \
@ -1384,8 +1384,8 @@ fn compare_number_of_generics<'tcx>(
pluralize!(trait_count), pluralize!(trait_count),
kind = kind, kind = kind,
), ),
DiagnosticId::Error("E0049".into()),
); );
err.code(DiagnosticId::Error("E0049".into()));
let msg = let msg =
format!("expected {trait_count} {kind} parameter{}", pluralize!(trait_count),); format!("expected {trait_count} {kind} parameter{}", pluralize!(trait_count),);

View file

@ -523,8 +523,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
fn start_diagnostics(&self) -> DiagnosticBuilder<'tcx> { fn start_diagnostics(&self) -> DiagnosticBuilder<'tcx> {
let span = self.path_segment.ident.span; let span = self.path_segment.ident.span;
let msg = self.create_error_message(); let msg = self.create_error_message();
self.tcx.dcx().struct_span_err(span, msg).code_mv(self.code())
self.tcx.dcx().struct_span_err_with_code(span, msg, self.code())
} }
/// Builds the `expected 1 type argument / supplied 2 type arguments` message. /// Builds the `expected 1 type argument / supplied 2 type arguments` message.

View file

@ -940,12 +940,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return; return;
} }
// FIXME: Make this use Diagnostic once error codes can be dynamically set. let mut err = self.dcx().struct_span_err(op_span, "invalid left-hand side of assignment");
let mut err = self.dcx().struct_span_err_with_code( err.code(DiagnosticId::Error(err_code.into()));
op_span,
"invalid left-hand side of assignment",
DiagnosticId::Error(err_code.into()),
);
err.span_label(lhs.span, "cannot assign to this expression"); err.span_label(lhs.span, "cannot assign to this expression");
self.comes_from_while_condition(lhs.hir_id, |expr| { self.comes_from_while_condition(lhs.hir_id, |expr| {

View file

@ -664,7 +664,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
format!("arguments to this {call_name} are incorrect"), format!("arguments to this {call_name} are incorrect"),
); );
} else { } else {
err = tcx.dcx().struct_span_err_with_code( err = tcx.dcx().struct_span_err(
full_call_span, full_call_span,
format!( format!(
"{call_name} takes {}{} but {} {} supplied", "{call_name} takes {}{} but {} {} supplied",
@ -676,8 +676,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
potentially_plural_count(provided_args.len(), "argument"), potentially_plural_count(provided_args.len(), "argument"),
pluralize!("was", provided_args.len()) pluralize!("was", provided_args.len())
), ),
DiagnosticId::Error(err_code.to_owned()),
); );
err.code(DiagnosticId::Error(err_code.to_owned()));
err.multipart_suggestion_verbose( err.multipart_suggestion_verbose(
"wrap these arguments in parentheses to construct a tuple", "wrap these arguments in parentheses to construct a tuple",
vec![ vec![
@ -815,18 +815,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
call_name, call_name,
) )
} else { } else {
tcx.dcx().struct_span_err_with_code( tcx.dcx()
full_call_span, .struct_span_err(
format!( full_call_span,
"this {} takes {}{} but {} {} supplied", format!(
call_name, "this {} takes {}{} but {} {} supplied",
if c_variadic { "at least " } else { "" }, call_name,
potentially_plural_count(formal_and_expected_inputs.len(), "argument"), if c_variadic { "at least " } else { "" },
potentially_plural_count(provided_args.len(), "argument"), potentially_plural_count(formal_and_expected_inputs.len(), "argument"),
pluralize!("was", provided_args.len()) potentially_plural_count(provided_args.len(), "argument"),
), pluralize!("was", provided_args.len())
DiagnosticId::Error(err_code.to_owned()), ),
) )
.code_mv(DiagnosticId::Error(err_code.to_owned()))
}; };
// As we encounter issues, keep track of what we want to provide for the suggestion // As we encounter issues, keep track of what we want to provide for the suggestion

View file

@ -366,11 +366,10 @@ fn report_unexpected_variant_res(
_ => res.descr(), _ => res.descr(),
}; };
let path_str = rustc_hir_pretty::qpath_to_string(qpath); let path_str = rustc_hir_pretty::qpath_to_string(qpath);
let err = tcx.dcx().struct_span_err_with_code( let err = tcx
span, .dcx()
format!("expected {expected}, found {res_descr} `{path_str}`"), .struct_span_err(span, format!("expected {expected}, found {res_descr} `{path_str}`"))
DiagnosticId::Error(err_code.into()), .code_mv(DiagnosticId::Error(err_code.into()));
);
match res { match res {
Res::Def(DefKind::Fn | DefKind::AssocFn, _) if err_code == "E0164" => { Res::Def(DefKind::Fn | DefKind::AssocFn, _) if err_code == "E0164" => {
let patterns_url = "https://doc.rust-lang.org/book/ch18-00-patterns.html"; let patterns_url = "https://doc.rust-lang.org/book/ch18-00-patterns.html";

View file

@ -2356,15 +2356,15 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
}, },
}; };
let mut err = self.tcx.dcx().struct_span_err_with_code( let mut err = self
span, .tcx
format!("{labeled_user_string} may not live long enough"), .dcx()
match sub.kind() { .struct_span_err(span, format!("{labeled_user_string} may not live long enough"));
ty::ReEarlyParam(_) | ty::ReLateParam(_) if sub.has_name() => error_code!(E0309), err.code(match sub.kind() {
ty::ReStatic => error_code!(E0310), ty::ReEarlyParam(_) | ty::ReLateParam(_) if sub.has_name() => error_code!(E0309),
_ => error_code!(E0311), ty::ReStatic => error_code!(E0310),
}, _ => error_code!(E0311),
); });
'_explain: { '_explain: {
let (description, span) = match sub.kind() { let (description, span) = match sub.kind() {

View file

@ -395,51 +395,58 @@ impl<'a> StringReader<'a> {
match kind { match kind {
rustc_lexer::LiteralKind::Char { terminated } => { rustc_lexer::LiteralKind::Char { terminated } => {
if !terminated { if !terminated {
self.dcx().span_fatal_with_code( self.dcx()
self.mk_sp(start, end), .struct_span_fatal(self.mk_sp(start, end), "unterminated character literal")
"unterminated character literal", .code_mv(error_code!(E0762))
error_code!(E0762), .emit()
)
} }
self.cook_quoted(token::Char, Mode::Char, start, end, 1, 1) // ' ' self.cook_quoted(token::Char, Mode::Char, start, end, 1, 1) // ' '
} }
rustc_lexer::LiteralKind::Byte { terminated } => { rustc_lexer::LiteralKind::Byte { terminated } => {
if !terminated { if !terminated {
self.dcx().span_fatal_with_code( self.dcx()
self.mk_sp(start + BytePos(1), end), .struct_span_fatal(
"unterminated byte constant", self.mk_sp(start + BytePos(1), end),
error_code!(E0763), "unterminated byte constant",
) )
.code_mv(error_code!(E0763))
.emit()
} }
self.cook_quoted(token::Byte, Mode::Byte, start, end, 2, 1) // b' ' self.cook_quoted(token::Byte, Mode::Byte, start, end, 2, 1) // b' '
} }
rustc_lexer::LiteralKind::Str { terminated } => { rustc_lexer::LiteralKind::Str { terminated } => {
if !terminated { if !terminated {
self.dcx().span_fatal_with_code( self.dcx()
self.mk_sp(start, end), .struct_span_fatal(
"unterminated double quote string", self.mk_sp(start, end),
error_code!(E0765), "unterminated double quote string",
) )
.code_mv(error_code!(E0765))
.emit()
} }
self.cook_quoted(token::Str, Mode::Str, start, end, 1, 1) // " " self.cook_quoted(token::Str, Mode::Str, start, end, 1, 1) // " "
} }
rustc_lexer::LiteralKind::ByteStr { terminated } => { rustc_lexer::LiteralKind::ByteStr { terminated } => {
if !terminated { if !terminated {
self.dcx().span_fatal_with_code( self.dcx()
self.mk_sp(start + BytePos(1), end), .struct_span_fatal(
"unterminated double quote byte string", self.mk_sp(start + BytePos(1), end),
error_code!(E0766), "unterminated double quote byte string",
) )
.code_mv(error_code!(E0766))
.emit()
} }
self.cook_quoted(token::ByteStr, Mode::ByteStr, start, end, 2, 1) // b" " self.cook_quoted(token::ByteStr, Mode::ByteStr, start, end, 2, 1) // b" "
} }
rustc_lexer::LiteralKind::CStr { terminated } => { rustc_lexer::LiteralKind::CStr { terminated } => {
if !terminated { if !terminated {
self.dcx().span_fatal_with_code( self.dcx()
self.mk_sp(start + BytePos(1), end), .struct_span_fatal(
"unterminated C string", self.mk_sp(start + BytePos(1), end),
error_code!(E0767), "unterminated C string",
) )
.code_mv(error_code!(E0767))
.emit()
} }
self.cook_c_string(token::CStr, Mode::CStr, start, end, 2, 1) // c" " self.cook_c_string(token::CStr, Mode::CStr, start, end, 2, 1) // c" "
} }
@ -573,12 +580,9 @@ impl<'a> StringReader<'a> {
possible_offset: Option<u32>, possible_offset: Option<u32>,
found_terminators: u32, found_terminators: u32,
) -> ! { ) -> ! {
let mut err = self.dcx().struct_span_fatal_with_code( let mut err =
self.mk_sp(start, start), self.dcx().struct_span_fatal(self.mk_sp(start, start), "unterminated raw string");
"unterminated raw string", err.code(error_code!(E0748));
error_code!(E0748),
);
err.span_label(self.mk_sp(start, start), "unterminated raw string"); err.span_label(self.mk_sp(start, start), "unterminated raw string");
if n_hashes > 0 { if n_hashes > 0 {
@ -609,11 +613,8 @@ impl<'a> StringReader<'a> {
None => "unterminated block comment", None => "unterminated block comment",
}; };
let last_bpos = self.pos; let last_bpos = self.pos;
let mut err = self.dcx().struct_span_fatal_with_code( let mut err = self.dcx().struct_span_fatal(self.mk_sp(start, last_bpos), msg);
self.mk_sp(start, last_bpos), err.code(error_code!(E0758));
msg,
error_code!(E0758),
);
let mut nested_block_comment_open_idxs = vec![]; let mut nested_block_comment_open_idxs = vec![];
let mut last_nested_block_comment_idxs = None; let mut last_nested_block_comment_idxs = None;
let mut content_chars = self.str_from(start).char_indices().peekable(); let mut content_chars = self.str_from(start).char_indices().peekable();

View file

@ -55,11 +55,10 @@ impl<'a> Parser<'a> {
} else if let token::DocComment(comment_kind, attr_style, data) = self.token.kind { } else if let token::DocComment(comment_kind, attr_style, data) = self.token.kind {
if attr_style != ast::AttrStyle::Outer { if attr_style != ast::AttrStyle::Outer {
let span = self.token.span; let span = self.token.span;
let mut err = self.dcx().struct_span_err_with_code( let mut err = self
span, .dcx()
fluent::parse_inner_doc_comment_not_permitted, .struct_span_err(span, fluent::parse_inner_doc_comment_not_permitted);
error_code!(E0753), err.code(error_code!(E0753));
);
if let Some(replacement_span) = self.annotate_following_item_if_applicable( if let Some(replacement_span) = self.annotate_following_item_if_applicable(
&mut err, &mut err,
span, span,

View file

@ -944,13 +944,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
trait_item_span, trait_item_span,
trait_path, trait_path,
} => { } => {
self.dcx().struct_span_err_with_code( self.dcx().struct_span_err(
span, span,
format!( format!(
"item `{name}` is an associated {kind}, which doesn't match its trait `{trait_path}`", "item `{name}` is an associated {kind}, which doesn't match its trait `{trait_path}`",
), ),
code,
) )
.code_mv(code)
.span_label_mv(span, "does not match trait") .span_label_mv(span, "does not match trait")
.span_label_mv(trait_item_span, "item in trait") .span_label_mv(trait_item_span, "item in trait")
} }

View file

@ -429,8 +429,8 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
let base_error = self.make_base_error(path, span, source, res); let base_error = self.make_base_error(path, span, source, res);
let code = source.error_code(res.is_some()); let code = source.error_code(res.is_some());
let mut err = let mut err = self.r.dcx().struct_span_err(base_error.span, base_error.msg.clone());
self.r.dcx().struct_span_err_with_code(base_error.span, base_error.msg.clone(), code); err.code(code);
self.suggest_at_operator_in_slice_pat_with_range(&mut err, path); self.suggest_at_operator_in_slice_pat_with_range(&mut err, path);
self.suggest_swapping_misplaced_self_ty_and_trait(&mut err, source, res, base_error.span); self.suggest_swapping_misplaced_self_ty_and_trait(&mut err, source, res, base_error.span);

View file

@ -2525,14 +2525,14 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
// Replace the more general E0283 with a more specific error // Replace the more general E0283 with a more specific error
err.cancel(); err.cancel();
err = self.dcx().struct_span_err_with_code( err = self.dcx().struct_span_err(
span, span,
format!( format!(
"cannot {verb} associated {noun} on trait without specifying the \ "cannot {verb} associated {noun} on trait without specifying the \
corresponding `impl` type", corresponding `impl` type",
), ),
rustc_errors::error_code!(E0790),
); );
err.code(rustc_errors::error_code!(E0790));
if let Some(local_def_id) = data.trait_ref.def_id.as_local() if let Some(local_def_id) = data.trait_ref.def_id.as_local()
&& let Some(hir::Node::Item(hir::Item { && let Some(hir::Node::Item(hir::Item {