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_rules! struct_span_err {
($dcx:expr, $span:expr, $code:ident, $($message:tt)*) => ({
$dcx.struct_span_err_with_code(
$dcx.struct_span_err(
$span,
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)
}
/// 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`.
///
/// Attempting to `.emit()` the builder will only emit if either:
@ -785,18 +772,6 @@ impl DiagCtxt {
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`.
// FIXME: This method should be removed (every error should have an associated error code).
#[rustc_lint_diagnostics]
@ -805,28 +780,6 @@ impl DiagCtxt {
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`.
#[rustc_lint_diagnostics]
#[track_caller]
@ -838,18 +791,6 @@ impl DiagCtxt {
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`.
#[rustc_lint_diagnostics]
#[track_caller]
@ -897,17 +838,6 @@ impl DiagCtxt {
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]
#[track_caller]
pub fn span_err(
@ -918,34 +848,12 @@ impl DiagCtxt {
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]
#[track_caller]
pub fn span_warn(&self, span: impl Into<MultiSpan>, msg: impl Into<DiagnosticMessage>) {
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>) -> ! {
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 def_span = tcx.def_span(item);
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(def_span, format!("{kind} defined here"))
.emit();

View File

@ -1371,7 +1371,7 @@ fn compare_number_of_generics<'tcx>(
let spans = arg_spans(impl_.kind, impl_item.generics);
let span = spans.first().copied();
let mut err = tcx.dcx().struct_span_err_with_code(
let mut err = tcx.dcx().struct_span_err(
spans,
format!(
"{} `{}` has {} {kind} parameter{} but its trait \
@ -1384,8 +1384,8 @@ fn compare_number_of_generics<'tcx>(
pluralize!(trait_count),
kind = kind,
),
DiagnosticId::Error("E0049".into()),
);
err.code(DiagnosticId::Error("E0049".into()));
let msg =
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> {
let span = self.path_segment.ident.span;
let msg = self.create_error_message();
self.tcx.dcx().struct_span_err_with_code(span, msg, self.code())
self.tcx.dcx().struct_span_err(span, msg).code_mv(self.code())
}
/// Builds the `expected 1 type argument / supplied 2 type arguments` message.

View File

@ -940,12 +940,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return;
}
// FIXME: Make this use Diagnostic once error codes can be dynamically set.
let mut err = self.dcx().struct_span_err_with_code(
op_span,
"invalid left-hand side of assignment",
DiagnosticId::Error(err_code.into()),
);
let mut err = self.dcx().struct_span_err(op_span, "invalid left-hand side of assignment");
err.code(DiagnosticId::Error(err_code.into()));
err.span_label(lhs.span, "cannot assign to this expression");
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"),
);
} else {
err = tcx.dcx().struct_span_err_with_code(
err = tcx.dcx().struct_span_err(
full_call_span,
format!(
"{call_name} takes {}{} but {} {} supplied",
@ -676,8 +676,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
potentially_plural_count(provided_args.len(), "argument"),
pluralize!("was", provided_args.len())
),
DiagnosticId::Error(err_code.to_owned()),
);
err.code(DiagnosticId::Error(err_code.to_owned()));
err.multipart_suggestion_verbose(
"wrap these arguments in parentheses to construct a tuple",
vec![
@ -815,18 +815,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
call_name,
)
} else {
tcx.dcx().struct_span_err_with_code(
full_call_span,
format!(
"this {} takes {}{} but {} {} supplied",
call_name,
if c_variadic { "at least " } else { "" },
potentially_plural_count(formal_and_expected_inputs.len(), "argument"),
potentially_plural_count(provided_args.len(), "argument"),
pluralize!("was", provided_args.len())
),
DiagnosticId::Error(err_code.to_owned()),
)
tcx.dcx()
.struct_span_err(
full_call_span,
format!(
"this {} takes {}{} but {} {} supplied",
call_name,
if c_variadic { "at least " } else { "" },
potentially_plural_count(formal_and_expected_inputs.len(), "argument"),
potentially_plural_count(provided_args.len(), "argument"),
pluralize!("was", provided_args.len())
),
)
.code_mv(DiagnosticId::Error(err_code.to_owned()))
};
// 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(),
};
let path_str = rustc_hir_pretty::qpath_to_string(qpath);
let err = tcx.dcx().struct_span_err_with_code(
span,
format!("expected {expected}, found {res_descr} `{path_str}`"),
DiagnosticId::Error(err_code.into()),
);
let err = tcx
.dcx()
.struct_span_err(span, format!("expected {expected}, found {res_descr} `{path_str}`"))
.code_mv(DiagnosticId::Error(err_code.into()));
match res {
Res::Def(DefKind::Fn | DefKind::AssocFn, _) if err_code == "E0164" => {
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(
span,
format!("{labeled_user_string} may not live long enough"),
match sub.kind() {
ty::ReEarlyParam(_) | ty::ReLateParam(_) if sub.has_name() => error_code!(E0309),
ty::ReStatic => error_code!(E0310),
_ => error_code!(E0311),
},
);
let mut err = self
.tcx
.dcx()
.struct_span_err(span, format!("{labeled_user_string} may not live long enough"));
err.code(match sub.kind() {
ty::ReEarlyParam(_) | ty::ReLateParam(_) if sub.has_name() => error_code!(E0309),
ty::ReStatic => error_code!(E0310),
_ => error_code!(E0311),
});
'_explain: {
let (description, span) = match sub.kind() {

View File

@ -395,51 +395,58 @@ impl<'a> StringReader<'a> {
match kind {
rustc_lexer::LiteralKind::Char { terminated } => {
if !terminated {
self.dcx().span_fatal_with_code(
self.mk_sp(start, end),
"unterminated character literal",
error_code!(E0762),
)
self.dcx()
.struct_span_fatal(self.mk_sp(start, end), "unterminated character literal")
.code_mv(error_code!(E0762))
.emit()
}
self.cook_quoted(token::Char, Mode::Char, start, end, 1, 1) // ' '
}
rustc_lexer::LiteralKind::Byte { terminated } => {
if !terminated {
self.dcx().span_fatal_with_code(
self.mk_sp(start + BytePos(1), end),
"unterminated byte constant",
error_code!(E0763),
)
self.dcx()
.struct_span_fatal(
self.mk_sp(start + BytePos(1), end),
"unterminated byte constant",
)
.code_mv(error_code!(E0763))
.emit()
}
self.cook_quoted(token::Byte, Mode::Byte, start, end, 2, 1) // b' '
}
rustc_lexer::LiteralKind::Str { terminated } => {
if !terminated {
self.dcx().span_fatal_with_code(
self.mk_sp(start, end),
"unterminated double quote string",
error_code!(E0765),
)
self.dcx()
.struct_span_fatal(
self.mk_sp(start, end),
"unterminated double quote string",
)
.code_mv(error_code!(E0765))
.emit()
}
self.cook_quoted(token::Str, Mode::Str, start, end, 1, 1) // " "
}
rustc_lexer::LiteralKind::ByteStr { terminated } => {
if !terminated {
self.dcx().span_fatal_with_code(
self.mk_sp(start + BytePos(1), end),
"unterminated double quote byte string",
error_code!(E0766),
)
self.dcx()
.struct_span_fatal(
self.mk_sp(start + BytePos(1), end),
"unterminated double quote byte string",
)
.code_mv(error_code!(E0766))
.emit()
}
self.cook_quoted(token::ByteStr, Mode::ByteStr, start, end, 2, 1) // b" "
}
rustc_lexer::LiteralKind::CStr { terminated } => {
if !terminated {
self.dcx().span_fatal_with_code(
self.mk_sp(start + BytePos(1), end),
"unterminated C string",
error_code!(E0767),
)
self.dcx()
.struct_span_fatal(
self.mk_sp(start + BytePos(1), end),
"unterminated C string",
)
.code_mv(error_code!(E0767))
.emit()
}
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>,
found_terminators: u32,
) -> ! {
let mut err = self.dcx().struct_span_fatal_with_code(
self.mk_sp(start, start),
"unterminated raw string",
error_code!(E0748),
);
let mut err =
self.dcx().struct_span_fatal(self.mk_sp(start, start), "unterminated raw string");
err.code(error_code!(E0748));
err.span_label(self.mk_sp(start, start), "unterminated raw string");
if n_hashes > 0 {
@ -609,11 +613,8 @@ impl<'a> StringReader<'a> {
None => "unterminated block comment",
};
let last_bpos = self.pos;
let mut err = self.dcx().struct_span_fatal_with_code(
self.mk_sp(start, last_bpos),
msg,
error_code!(E0758),
);
let mut err = self.dcx().struct_span_fatal(self.mk_sp(start, last_bpos), msg);
err.code(error_code!(E0758));
let mut nested_block_comment_open_idxs = vec![];
let mut last_nested_block_comment_idxs = None;
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 {
if attr_style != ast::AttrStyle::Outer {
let span = self.token.span;
let mut err = self.dcx().struct_span_err_with_code(
span,
fluent::parse_inner_doc_comment_not_permitted,
error_code!(E0753),
);
let mut err = self
.dcx()
.struct_span_err(span, fluent::parse_inner_doc_comment_not_permitted);
err.code(error_code!(E0753));
if let Some(replacement_span) = self.annotate_following_item_if_applicable(
&mut err,
span,

View File

@ -944,13 +944,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
trait_item_span,
trait_path,
} => {
self.dcx().struct_span_err_with_code(
self.dcx().struct_span_err(
span,
format!(
"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(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 code = source.error_code(res.is_some());
let mut err =
self.r.dcx().struct_span_err_with_code(base_error.span, base_error.msg.clone(), code);
let mut err = self.r.dcx().struct_span_err(base_error.span, base_error.msg.clone());
err.code(code);
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);

View File

@ -2525,14 +2525,14 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
// Replace the more general E0283 with a more specific error
err.cancel();
err = self.dcx().struct_span_err_with_code(
err = self.dcx().struct_span_err(
span,
format!(
"cannot {verb} associated {noun} on trait without specifying the \
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()
&& let Some(hir::Node::Item(hir::Item {