Skip to content

Commit 6682f24

Browse files
committedJan 8, 2024
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`.
1 parent bd4e623 commit 6682f24

File tree

14 files changed

+84
-180
lines changed

14 files changed

+84
-180
lines changed
 

‎compiler/rustc_errors/src/diagnostic_builder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -538,11 +538,11 @@ impl<G: EmissionGuarantee> Drop for DiagnosticBuilder<'_, G> {
538538
#[macro_export]
539539
macro_rules! struct_span_err {
540540
($dcx:expr, $span:expr, $code:ident, $($message:tt)*) => ({
541-
$dcx.struct_span_err_with_code(
541+
$dcx.struct_span_err(
542542
$span,
543543
format!($($message)*),
544-
$crate::error_code!($code),
545544
)
545+
.code_mv($crate::error_code!($code))
546546
})
547547
}
548548

‎compiler/rustc_errors/src/lib.rs

-92
Original file line numberDiff line numberDiff line change
@@ -732,19 +732,6 @@ impl DiagCtxt {
732732
self.struct_warn(msg).span_mv(span)
733733
}
734734

735-
/// Construct a builder at the `Warning` level at the given `span` and with the `msg`.
736-
/// Also include a code.
737-
#[rustc_lint_diagnostics]
738-
#[track_caller]
739-
pub fn struct_span_warn_with_code(
740-
&self,
741-
span: impl Into<MultiSpan>,
742-
msg: impl Into<DiagnosticMessage>,
743-
code: DiagnosticId,
744-
) -> DiagnosticBuilder<'_, ()> {
745-
self.struct_span_warn(span, msg).code_mv(code)
746-
}
747-
748735
/// Construct a builder at the `Warning` level with the `msg`.
749736
///
750737
/// Attempting to `.emit()` the builder will only emit if either:
@@ -785,18 +772,6 @@ impl DiagCtxt {
785772
self.struct_err(msg).span_mv(span)
786773
}
787774

788-
/// Construct a builder at the `Error` level at the given `span`, with the `msg`, and `code`.
789-
#[rustc_lint_diagnostics]
790-
#[track_caller]
791-
pub fn struct_span_err_with_code(
792-
&self,
793-
span: impl Into<MultiSpan>,
794-
msg: impl Into<DiagnosticMessage>,
795-
code: DiagnosticId,
796-
) -> DiagnosticBuilder<'_> {
797-
self.struct_span_err(span, msg).code_mv(code)
798-
}
799-
800775
/// Construct a builder at the `Error` level with the `msg`.
801776
// FIXME: This method should be removed (every error should have an associated error code).
802777
#[rustc_lint_diagnostics]
@@ -805,28 +780,6 @@ impl DiagCtxt {
805780
DiagnosticBuilder::new(self, Error, msg)
806781
}
807782

808-
/// Construct a builder at the `Error` level with the `msg` and the `code`.
809-
#[rustc_lint_diagnostics]
810-
#[track_caller]
811-
pub fn struct_err_with_code(
812-
&self,
813-
msg: impl Into<DiagnosticMessage>,
814-
code: DiagnosticId,
815-
) -> DiagnosticBuilder<'_> {
816-
self.struct_err(msg).code_mv(code)
817-
}
818-
819-
/// Construct a builder at the `Warn` level with the `msg` and the `code`.
820-
#[rustc_lint_diagnostics]
821-
#[track_caller]
822-
pub fn struct_warn_with_code(
823-
&self,
824-
msg: impl Into<DiagnosticMessage>,
825-
code: DiagnosticId,
826-
) -> DiagnosticBuilder<'_, ()> {
827-
self.struct_warn(msg).code_mv(code)
828-
}
829-
830783
/// Construct a builder at the `Fatal` level at the given `span` and with the `msg`.
831784
#[rustc_lint_diagnostics]
832785
#[track_caller]
@@ -838,18 +791,6 @@ impl DiagCtxt {
838791
self.struct_fatal(msg).span_mv(span)
839792
}
840793

841-
/// Construct a builder at the `Fatal` level at the given `span`, with the `msg`, and `code`.
842-
#[rustc_lint_diagnostics]
843-
#[track_caller]
844-
pub fn struct_span_fatal_with_code(
845-
&self,
846-
span: impl Into<MultiSpan>,
847-
msg: impl Into<DiagnosticMessage>,
848-
code: DiagnosticId,
849-
) -> DiagnosticBuilder<'_, FatalAbort> {
850-
self.struct_span_fatal(span, msg).code_mv(code)
851-
}
852-
853794
/// Construct a builder at the `Fatal` level with the `msg`.
854795
#[rustc_lint_diagnostics]
855796
#[track_caller]
@@ -897,17 +838,6 @@ impl DiagCtxt {
897838
self.struct_span_fatal(span, msg).emit()
898839
}
899840

900-
#[rustc_lint_diagnostics]
901-
#[track_caller]
902-
pub fn span_fatal_with_code(
903-
&self,
904-
span: impl Into<MultiSpan>,
905-
msg: impl Into<DiagnosticMessage>,
906-
code: DiagnosticId,
907-
) -> ! {
908-
self.struct_span_fatal_with_code(span, msg, code).emit()
909-
}
910-
911841
#[rustc_lint_diagnostics]
912842
#[track_caller]
913843
pub fn span_err(
@@ -918,34 +848,12 @@ impl DiagCtxt {
918848
self.struct_span_err(span, msg).emit()
919849
}
920850

921-
#[rustc_lint_diagnostics]
922-
#[track_caller]
923-
pub fn span_err_with_code(
924-
&self,
925-
span: impl Into<MultiSpan>,
926-
msg: impl Into<DiagnosticMessage>,
927-
code: DiagnosticId,
928-
) -> ErrorGuaranteed {
929-
self.struct_span_err_with_code(span, msg, code).emit()
930-
}
931-
932851
#[rustc_lint_diagnostics]
933852
#[track_caller]
934853
pub fn span_warn(&self, span: impl Into<MultiSpan>, msg: impl Into<DiagnosticMessage>) {
935854
self.struct_span_warn(span, msg).emit()
936855
}
937856

938-
#[rustc_lint_diagnostics]
939-
#[track_caller]
940-
pub fn span_warn_with_code(
941-
&self,
942-
span: impl Into<MultiSpan>,
943-
msg: impl Into<DiagnosticMessage>,
944-
code: DiagnosticId,
945-
) {
946-
self.struct_span_warn_with_code(span, msg, code).emit()
947-
}
948-
949857
pub fn span_bug(&self, span: impl Into<MultiSpan>, msg: impl Into<DiagnosticMessage>) -> ! {
950858
self.struct_span_bug(span, msg).emit()
951859
}

‎compiler/rustc_hir_analysis/src/astconv/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1647,7 +1647,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
16471647
let msg = format!("{kind} `{name}` is private");
16481648
let def_span = tcx.def_span(item);
16491649
tcx.dcx()
1650-
.struct_span_err_with_code(span, msg, rustc_errors::error_code!(E0624))
1650+
.struct_span_err(span, msg)
1651+
.code_mv(rustc_errors::error_code!(E0624))
16511652
.span_label_mv(span, format!("private {kind}"))
16521653
.span_label_mv(def_span, format!("{kind} defined here"))
16531654
.emit();

‎compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,7 @@ fn compare_number_of_generics<'tcx>(
13711371
let spans = arg_spans(impl_.kind, impl_item.generics);
13721372
let span = spans.first().copied();
13731373

1374-
let mut err = tcx.dcx().struct_span_err_with_code(
1374+
let mut err = tcx.dcx().struct_span_err(
13751375
spans,
13761376
format!(
13771377
"{} `{}` has {} {kind} parameter{} but its trait \
@@ -1384,8 +1384,8 @@ fn compare_number_of_generics<'tcx>(
13841384
pluralize!(trait_count),
13851385
kind = kind,
13861386
),
1387-
DiagnosticId::Error("E0049".into()),
13881387
);
1388+
err.code(DiagnosticId::Error("E0049".into()));
13891389

13901390
let msg =
13911391
format!("expected {trait_count} {kind} parameter{}", pluralize!(trait_count),);

‎compiler/rustc_hir_analysis/src/structured_errors/wrong_number_of_generic_args.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
523523
fn start_diagnostics(&self) -> DiagnosticBuilder<'tcx> {
524524
let span = self.path_segment.ident.span;
525525
let msg = self.create_error_message();
526-
527-
self.tcx.dcx().struct_span_err_with_code(span, msg, self.code())
526+
self.tcx.dcx().struct_span_err(span, msg).code_mv(self.code())
528527
}
529528

530529
/// Builds the `expected 1 type argument / supplied 2 type arguments` message.

‎compiler/rustc_hir_typeck/src/expr.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -940,12 +940,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
940940
return;
941941
}
942942

943-
// FIXME: Make this use Diagnostic once error codes can be dynamically set.
944-
let mut err = self.dcx().struct_span_err_with_code(
945-
op_span,
946-
"invalid left-hand side of assignment",
947-
DiagnosticId::Error(err_code.into()),
948-
);
943+
let mut err = self.dcx().struct_span_err(op_span, "invalid left-hand side of assignment");
944+
err.code(DiagnosticId::Error(err_code.into()));
949945
err.span_label(lhs.span, "cannot assign to this expression");
950946

951947
self.comes_from_while_condition(lhs.hir_id, |expr| {

‎compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
664664
format!("arguments to this {call_name} are incorrect"),
665665
);
666666
} else {
667-
err = tcx.dcx().struct_span_err_with_code(
667+
err = tcx.dcx().struct_span_err(
668668
full_call_span,
669669
format!(
670670
"{call_name} takes {}{} but {} {} supplied",
@@ -676,8 +676,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
676676
potentially_plural_count(provided_args.len(), "argument"),
677677
pluralize!("was", provided_args.len())
678678
),
679-
DiagnosticId::Error(err_code.to_owned()),
680679
);
680+
err.code(DiagnosticId::Error(err_code.to_owned()));
681681
err.multipart_suggestion_verbose(
682682
"wrap these arguments in parentheses to construct a tuple",
683683
vec![
@@ -815,18 +815,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
815815
call_name,
816816
)
817817
} else {
818-
tcx.dcx().struct_span_err_with_code(
819-
full_call_span,
820-
format!(
821-
"this {} takes {}{} but {} {} supplied",
822-
call_name,
823-
if c_variadic { "at least " } else { "" },
824-
potentially_plural_count(formal_and_expected_inputs.len(), "argument"),
825-
potentially_plural_count(provided_args.len(), "argument"),
826-
pluralize!("was", provided_args.len())
827-
),
828-
DiagnosticId::Error(err_code.to_owned()),
829-
)
818+
tcx.dcx()
819+
.struct_span_err(
820+
full_call_span,
821+
format!(
822+
"this {} takes {}{} but {} {} supplied",
823+
call_name,
824+
if c_variadic { "at least " } else { "" },
825+
potentially_plural_count(formal_and_expected_inputs.len(), "argument"),
826+
potentially_plural_count(provided_args.len(), "argument"),
827+
pluralize!("was", provided_args.len())
828+
),
829+
)
830+
.code_mv(DiagnosticId::Error(err_code.to_owned()))
830831
};
831832

832833
// As we encounter issues, keep track of what we want to provide for the suggestion

‎compiler/rustc_hir_typeck/src/lib.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,10 @@ fn report_unexpected_variant_res(
366366
_ => res.descr(),
367367
};
368368
let path_str = rustc_hir_pretty::qpath_to_string(qpath);
369-
let err = tcx.dcx().struct_span_err_with_code(
370-
span,
371-
format!("expected {expected}, found {res_descr} `{path_str}`"),
372-
DiagnosticId::Error(err_code.into()),
373-
);
369+
let err = tcx
370+
.dcx()
371+
.struct_span_err(span, format!("expected {expected}, found {res_descr} `{path_str}`"))
372+
.code_mv(DiagnosticId::Error(err_code.into()));
374373
match res {
375374
Res::Def(DefKind::Fn | DefKind::AssocFn, _) if err_code == "E0164" => {
376375
let patterns_url = "https://doc.rust-lang.org/book/ch18-00-patterns.html";

‎compiler/rustc_infer/src/infer/error_reporting/mod.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -2356,15 +2356,15 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
23562356
},
23572357
};
23582358

2359-
let mut err = self.tcx.dcx().struct_span_err_with_code(
2360-
span,
2361-
format!("{labeled_user_string} may not live long enough"),
2362-
match sub.kind() {
2363-
ty::ReEarlyParam(_) | ty::ReLateParam(_) if sub.has_name() => error_code!(E0309),
2364-
ty::ReStatic => error_code!(E0310),
2365-
_ => error_code!(E0311),
2366-
},
2367-
);
2359+
let mut err = self
2360+
.tcx
2361+
.dcx()
2362+
.struct_span_err(span, format!("{labeled_user_string} may not live long enough"));
2363+
err.code(match sub.kind() {
2364+
ty::ReEarlyParam(_) | ty::ReLateParam(_) if sub.has_name() => error_code!(E0309),
2365+
ty::ReStatic => error_code!(E0310),
2366+
_ => error_code!(E0311),
2367+
});
23682368

23692369
'_explain: {
23702370
let (description, span) = match sub.kind() {

0 commit comments

Comments
 (0)