Skip to content

Commit

Permalink
Remove all eight DiagnosticBuilder::*_with_code methods.
Browse files Browse the repository at this point in the history
These all have relatively low use, and can be perfectly emulated with
a simpler construction method combined with `code` or `code_mv`.
  • Loading branch information
nnethercote committed Jan 8, 2024
1 parent bd4e623 commit 6682f24
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 180 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_errors/src/diagnostic_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}

Expand Down
92 changes: 0 additions & 92 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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(
Expand All @@ -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()
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir_analysis/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_analysis/src/check/compare_impl_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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),);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 2 additions & 6 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
29 changes: 15 additions & 14 deletions compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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![
Expand Down Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions compiler/rustc_hir_typeck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
18 changes: 9 additions & 9 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Loading

0 comments on commit 6682f24

Please sign in to comment.