Skip to content

Commit cdf96cf

Browse files
committed
Deduplicate some error+lint diagnostic structs
1 parent 6faf0bd commit cdf96cf

File tree

7 files changed

+37
-79
lines changed

7 files changed

+37
-79
lines changed

compiler/rustc_hir_analysis/src/coherence/orphan.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -468,13 +468,21 @@ fn emit_orphan_check_error<'tcx>(
468468
let name = tcx.item_name(param_def_id);
469469

470470
reported.get_or_insert(match local_ty {
471-
Some(local_type) => tcx.dcx().emit_err(errors::TyParamFirstLocal {
472-
span,
473-
note: (),
474-
param: name,
475-
local_type,
476-
}),
477-
None => tcx.dcx().emit_err(errors::TyParamSome { span, note: (), param: name }),
471+
Some(local_type) => tcx
472+
.dcx()
473+
.create_err(errors::TyParamFirstLocal {
474+
label: span,
475+
note: (),
476+
param: name,
477+
local_type,
478+
})
479+
.with_span(span)
480+
.emit(),
481+
None => tcx
482+
.dcx()
483+
.create_err(errors::TyParamSome { label: span, note: (), param: name })
484+
.with_span(span)
485+
.emit(),
478486
});
479487
}
480488
reported.unwrap() // FIXME(fmease): This is very likely reachable.
@@ -498,13 +506,13 @@ fn lint_uncovered_ty_params<'tcx>(
498506
UNCOVERED_PARAM_IN_PROJECTION,
499507
hir_id,
500508
span,
501-
errors::TyParamFirstLocalLint { span, note: (), param: name, local_type },
509+
errors::TyParamFirstLocal { label: span, note: (), param: name, local_type },
502510
),
503511
None => tcx.emit_node_span_lint(
504512
UNCOVERED_PARAM_IN_PROJECTION,
505513
hir_id,
506514
span,
507-
errors::TyParamSomeLint { span, note: (), param: name },
515+
errors::TyParamSome { label: span, note: (), param: name },
508516
),
509517
};
510518
}

compiler/rustc_hir_analysis/src/errors.rs

+4-31
Original file line numberDiff line numberDiff line change
@@ -1379,51 +1379,24 @@ pub(crate) struct CrossCrateTraitsDefined {
13791379
pub traits: String,
13801380
}
13811381

1382-
// FIXME(fmease): Deduplicate:
1383-
1384-
#[derive(Diagnostic)]
1382+
#[derive(Diagnostic, LintDiagnostic)]
13851383
#[diag(hir_analysis_ty_param_first_local, code = E0210)]
13861384
#[note]
13871385
pub(crate) struct TyParamFirstLocal<'tcx> {
1388-
#[primary_span]
1389-
#[label]
1390-
pub span: Span,
1391-
#[note(hir_analysis_case_note)]
1392-
pub note: (),
1393-
pub param: Symbol,
1394-
pub local_type: Ty<'tcx>,
1395-
}
1396-
1397-
#[derive(LintDiagnostic)]
1398-
#[diag(hir_analysis_ty_param_first_local, code = E0210)]
1399-
#[note]
1400-
pub(crate) struct TyParamFirstLocalLint<'tcx> {
14011386
#[label]
1402-
pub span: Span,
1387+
pub label: Span,
14031388
#[note(hir_analysis_case_note)]
14041389
pub note: (),
14051390
pub param: Symbol,
14061391
pub local_type: Ty<'tcx>,
14071392
}
14081393

1409-
#[derive(Diagnostic)]
1394+
#[derive(Diagnostic, LintDiagnostic)]
14101395
#[diag(hir_analysis_ty_param_some, code = E0210)]
14111396
#[note]
14121397
pub(crate) struct TyParamSome {
1413-
#[primary_span]
1414-
#[label]
1415-
pub span: Span,
1416-
#[note(hir_analysis_only_note)]
1417-
pub note: (),
1418-
pub param: Symbol,
1419-
}
1420-
1421-
#[derive(LintDiagnostic)]
1422-
#[diag(hir_analysis_ty_param_some, code = E0210)]
1423-
#[note]
1424-
pub(crate) struct TyParamSomeLint {
14251398
#[label]
1426-
pub span: Span,
1399+
pub label: Span,
14271400
#[note(hir_analysis_only_note)]
14281401
pub note: (),
14291402
pub param: Symbol,

compiler/rustc_hir_typeck/src/errors.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -670,20 +670,9 @@ pub(crate) enum SuggestBoxingForReturnImplTrait {
670670
},
671671
}
672672

673-
#[derive(Diagnostic)]
673+
#[derive(Diagnostic, LintDiagnostic)]
674674
#[diag(hir_typeck_self_ctor_from_outer_item, code = E0401)]
675675
pub(crate) struct SelfCtorFromOuterItem {
676-
#[primary_span]
677-
pub span: Span,
678-
#[label]
679-
pub impl_span: Span,
680-
#[subdiagnostic]
681-
pub sugg: Option<ReplaceWithName>,
682-
}
683-
684-
#[derive(LintDiagnostic)]
685-
#[diag(hir_typeck_self_ctor_from_outer_item)]
686-
pub(crate) struct SelfCtorFromOuterItemLint {
687676
#[label]
688677
pub impl_span: Span,
689678
#[subdiagnostic]

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -1140,22 +1140,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11401140
span: path_span,
11411141
name: self.tcx.item_name(def.did()).to_ident_string(),
11421142
});
1143+
let diag =
1144+
errors::SelfCtorFromOuterItem { impl_span: tcx.def_span(impl_def_id), sugg };
11431145
if ty.raw.has_param() {
1144-
let guar = self.dcx().emit_err(errors::SelfCtorFromOuterItem {
1145-
span: path_span,
1146-
impl_span: tcx.def_span(impl_def_id),
1147-
sugg,
1148-
});
1146+
let guar = self.dcx().create_err(diag).with_span(path_span).emit();
11491147
return (Ty::new_error(self.tcx, guar), res);
11501148
} else {
11511149
self.tcx.emit_node_span_lint(
11521150
SELF_CONSTRUCTOR_FROM_OUTER_ITEM,
11531151
hir_id,
11541152
path_span,
1155-
errors::SelfCtorFromOuterItemLint {
1156-
impl_span: tcx.def_span(impl_def_id),
1157-
sugg,
1158-
},
1153+
diag,
11591154
);
11601155
}
11611156
}

compiler/rustc_passes/src/check_attr.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1860,19 +1860,18 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
18601860

18611861
// Just point at all repr hints if there are any incompatibilities.
18621862
// This is not ideal, but tracking precisely which ones are at fault is a huge hassle.
1863-
let hint_spans = hints.iter().map(|hint| hint.span());
1863+
let hint_spans: Vec<_> = hints.iter().map(|hint| hint.span()).collect();
18641864

18651865
// Error on repr(transparent, <anything else>).
18661866
if is_transparent && hints.len() > 1 {
1867-
let hint_spans = hint_spans.clone().collect();
18681867
self.dcx().emit_err(errors::TransparentIncompatible {
1869-
hint_spans,
1868+
hint_spans: hint_spans.clone(),
18701869
target: target.to_string(),
18711870
});
18721871
}
18731872
if is_explicit_rust && (int_reprs > 0 || is_c || is_simd) {
1874-
let hint_spans = hint_spans.clone().collect();
1875-
self.dcx().emit_err(errors::ReprConflicting { hint_spans });
1873+
#[allow(rustc::diagnostic_outside_of_impl)]
1874+
self.dcx().create_err(errors::ReprConflicting).with_span(hint_spans.clone()).emit();
18761875
}
18771876
// Warn on repr(u8, u16), repr(C, simd), and c-like-enum-repr(C, u8)
18781877
if (int_reprs > 1)
@@ -1886,8 +1885,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
18861885
self.tcx.emit_node_span_lint(
18871886
CONFLICTING_REPR_HINTS,
18881887
hir_id,
1889-
hint_spans.collect::<Vec<Span>>(),
1890-
errors::ReprConflictingLint,
1888+
hint_spans,
1889+
errors::ReprConflicting,
18911890
);
18921891
}
18931892
}

compiler/rustc_passes/src/errors.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -560,16 +560,9 @@ pub(crate) struct ReprIdent {
560560
pub span: Span,
561561
}
562562

563-
#[derive(Diagnostic)]
564-
#[diag(passes_repr_conflicting, code = E0566)]
565-
pub(crate) struct ReprConflicting {
566-
#[primary_span]
567-
pub hint_spans: Vec<Span>,
568-
}
569-
570-
#[derive(LintDiagnostic)]
563+
#[derive(Diagnostic, LintDiagnostic)]
571564
#[diag(passes_repr_conflicting, code = E0566)]
572-
pub(crate) struct ReprConflictingLint;
565+
pub(crate) struct ReprConflicting;
573566

574567
#[derive(Diagnostic)]
575568
#[diag(passes_used_static)]

tests/ui/self/self-ctor-nongeneric.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
warning: can't reference `Self` constructor from outer item
1+
warning[E0401]: can't reference `Self` constructor from outer item
22
--> $DIR/self-ctor-nongeneric.rs:8:23
33
|
44
LL | impl S0 {
@@ -11,7 +11,7 @@ LL | const C: S0 = Self(0);
1111
= note: for more information, see issue #124186 <https://github.com/rust-lang/rust/issues/124186>
1212
= note: `#[warn(self_constructor_from_outer_item)]` on by default
1313

14-
warning: can't reference `Self` constructor from outer item
14+
warning[E0401]: can't reference `Self` constructor from outer item
1515
--> $DIR/self-ctor-nongeneric.rs:12:13
1616
|
1717
LL | impl S0 {
@@ -25,3 +25,4 @@ LL | Self(0)
2525

2626
warning: 2 warnings emitted
2727

28+
For more information about this error, try `rustc --explain E0401`.

0 commit comments

Comments
 (0)