Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deduplicate some error+lint diagnostic structs #132164

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 27 additions & 45 deletions compiler/rustc_hir_analysis/src/coherence/orphan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ pub(crate) fn orphan_check_impl(
Ok(()) => {}
Err(err) => match orphan_check(tcx, impl_def_id, OrphanCheckMode::Compat) {
Ok(()) => match err {
OrphanCheckErr::UncoveredTyParams(uncovered_ty_params) => {
lint_uncovered_ty_params(tcx, uncovered_ty_params, impl_def_id)
OrphanCheckErr::UncoveredTyParams(err) => {
let hir_id = tcx.local_def_id_to_hir_id(impl_def_id);
complain_about_uncovered_ty_params!(tcx, err, |span, diag| {
tcx.emit_node_span_lint(UNCOVERED_PARAM_IN_PROJECTION, hir_id, span, diag)
});
}
OrphanCheckErr::NonLocalInputType(_) => {
bug!("orphanck: shouldn't've gotten non-local input tys in compat mode")
Expand Down Expand Up @@ -461,54 +464,33 @@ fn emit_orphan_check_error<'tcx>(

diag.emit()
}
traits::OrphanCheckErr::UncoveredTyParams(UncoveredTyParams { uncovered, local_ty }) => {
let mut reported = None;
for param_def_id in uncovered {
let span = tcx.def_ident_span(param_def_id).unwrap();
let name = tcx.item_name(param_def_id);

reported.get_or_insert(match local_ty {
Some(local_type) => tcx.dcx().emit_err(errors::TyParamFirstLocal {
span,
note: (),
param: name,
local_type,
}),
None => tcx.dcx().emit_err(errors::TyParamSome { span, note: (), param: name }),
});
}
reported.unwrap() // FIXME(fmease): This is very likely reachable.
traits::OrphanCheckErr::UncoveredTyParams(err) => {
complain_about_uncovered_ty_params!(tcx, err, |span, diag| tcx
.dcx()
.create_err(diag)
.with_span(span)
.emit())
}
}
}

fn lint_uncovered_ty_params<'tcx>(
tcx: TyCtxt<'tcx>,
UncoveredTyParams { uncovered, local_ty }: UncoveredTyParams<TyCtxt<'tcx>, FxIndexSet<DefId>>,
impl_def_id: LocalDefId,
) {
let hir_id = tcx.local_def_id_to_hir_id(impl_def_id);

for param_def_id in uncovered {
let span = tcx.def_ident_span(param_def_id).unwrap();
let name = tcx.item_name(param_def_id);

match local_ty {
Some(local_type) => tcx.emit_node_span_lint(
UNCOVERED_PARAM_IN_PROJECTION,
hir_id,
span,
errors::TyParamFirstLocalLint { span, note: (), param: name, local_type },
),
None => tcx.emit_node_span_lint(
UNCOVERED_PARAM_IN_PROJECTION,
hir_id,
span,
errors::TyParamSomeLint { span, note: (), param: name },
),
};
macro complain_about_uncovered_ty_params($tcx:ident, $err:ident, $emit:expr) {{
let mut guar = None;
for param_def_id in $err.uncovered {
let span = $tcx.def_ident_span(param_def_id).unwrap();
let name = $tcx.item_name(param_def_id);
guar = Some(match $err.local_ty {
Some(local_type) => $emit(span, errors::TyParamFirstLocal {
label: span,
note: (),
param: name,
local_type,
}),
None => $emit(span, errors::TyParamSome { label: span, note: (), param: name }),
});
}
}
guar.unwrap() // FIXME(fmease): This very likely reachable
}}

struct UncoveredTyParamCollector<'cx, 'tcx> {
infcx: &'cx InferCtxt<'tcx>,
Expand Down
35 changes: 4 additions & 31 deletions compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1379,51 +1379,24 @@ pub(crate) struct CrossCrateTraitsDefined {
pub traits: String,
}

// FIXME(fmease): Deduplicate:

#[derive(Diagnostic)]
#[derive(Diagnostic, LintDiagnostic)]
#[diag(hir_analysis_ty_param_first_local, code = E0210)]
#[note]
pub(crate) struct TyParamFirstLocal<'tcx> {
#[primary_span]
#[label]
pub span: Span,
#[note(hir_analysis_case_note)]
pub note: (),
pub param: Symbol,
pub local_type: Ty<'tcx>,
}

#[derive(LintDiagnostic)]
#[diag(hir_analysis_ty_param_first_local, code = E0210)]
#[note]
pub(crate) struct TyParamFirstLocalLint<'tcx> {
#[label]
pub span: Span,
pub label: Span,
#[note(hir_analysis_case_note)]
pub note: (),
pub param: Symbol,
pub local_type: Ty<'tcx>,
}

#[derive(Diagnostic)]
#[derive(Diagnostic, LintDiagnostic)]
#[diag(hir_analysis_ty_param_some, code = E0210)]
#[note]
pub(crate) struct TyParamSome {
#[primary_span]
#[label]
pub span: Span,
#[note(hir_analysis_only_note)]
pub note: (),
pub param: Symbol,
}

#[derive(LintDiagnostic)]
#[diag(hir_analysis_ty_param_some, code = E0210)]
#[note]
pub(crate) struct TyParamSomeLint {
#[label]
pub span: Span,
pub label: Span,
#[note(hir_analysis_only_note)]
pub note: (),
pub param: Symbol,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ This API is completely unstable and subject to change.
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
#![doc(rust_logo)]
#![feature(assert_matches)]
#![feature(decl_macro)]
#![feature(if_let_guard)]
#![feature(iter_intersperse)]
#![feature(let_chains)]
Expand Down
13 changes: 1 addition & 12 deletions compiler/rustc_hir_typeck/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,20 +670,9 @@ pub(crate) enum SuggestBoxingForReturnImplTrait {
},
}

#[derive(Diagnostic)]
#[derive(Diagnostic, LintDiagnostic)]
#[diag(hir_typeck_self_ctor_from_outer_item, code = E0401)]
pub(crate) struct SelfCtorFromOuterItem {
#[primary_span]
pub span: Span,
#[label]
pub impl_span: Span,
#[subdiagnostic]
pub sugg: Option<ReplaceWithName>,
}

#[derive(LintDiagnostic)]
#[diag(hir_typeck_self_ctor_from_outer_item)]
pub(crate) struct SelfCtorFromOuterItemLint {
#[label]
pub impl_span: Span,
#[subdiagnostic]
Expand Down
13 changes: 4 additions & 9 deletions compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1140,22 +1140,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
span: path_span,
name: self.tcx.item_name(def.did()).to_ident_string(),
});
let diag =
errors::SelfCtorFromOuterItem { impl_span: tcx.def_span(impl_def_id), sugg };
if ty.raw.has_param() {
let guar = self.dcx().emit_err(errors::SelfCtorFromOuterItem {
span: path_span,
impl_span: tcx.def_span(impl_def_id),
sugg,
});
let guar = self.dcx().create_err(diag).with_span(path_span).emit();
return (Ty::new_error(self.tcx, guar), res);
} else {
self.tcx.emit_node_span_lint(
SELF_CONSTRUCTOR_FROM_OUTER_ITEM,
hir_id,
path_span,
errors::SelfCtorFromOuterItemLint {
impl_span: tcx.def_span(impl_def_id),
sugg,
},
diag,
);
}
}
Expand Down
13 changes: 6 additions & 7 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1860,19 +1860,18 @@ impl<'tcx> CheckAttrVisitor<'tcx> {

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

// Error on repr(transparent, <anything else>).
if is_transparent && hints.len() > 1 {
let hint_spans = hint_spans.clone().collect();
self.dcx().emit_err(errors::TransparentIncompatible {
hint_spans,
hint_spans: hint_spans.clone(),
target: target.to_string(),
});
}
if is_explicit_rust && (int_reprs > 0 || is_c || is_simd) {
let hint_spans = hint_spans.clone().collect();
self.dcx().emit_err(errors::ReprConflicting { hint_spans });
#[allow(rustc::diagnostic_outside_of_impl)]
self.dcx().create_err(errors::ReprConflicting).with_span(hint_spans.clone()).emit();
}
// Warn on repr(u8, u16), repr(C, simd), and c-like-enum-repr(C, u8)
if (int_reprs > 1)
Expand All @@ -1886,8 +1885,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
self.tcx.emit_node_span_lint(
CONFLICTING_REPR_HINTS,
hir_id,
hint_spans.collect::<Vec<Span>>(),
errors::ReprConflictingLint,
hint_spans,
errors::ReprConflicting,
);
}
}
Expand Down
11 changes: 2 additions & 9 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,16 +560,9 @@ pub(crate) struct ReprIdent {
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(passes_repr_conflicting, code = E0566)]
pub(crate) struct ReprConflicting {
#[primary_span]
pub hint_spans: Vec<Span>,
}

#[derive(LintDiagnostic)]
#[derive(Diagnostic, LintDiagnostic)]
#[diag(passes_repr_conflicting, code = E0566)]
pub(crate) struct ReprConflictingLint;
pub(crate) struct ReprConflicting;

#[derive(Diagnostic)]
#[diag(passes_used_static)]
Expand Down
5 changes: 3 additions & 2 deletions tests/ui/self/self-ctor-nongeneric.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
warning: can't reference `Self` constructor from outer item
warning[E0401]: can't reference `Self` constructor from outer item
--> $DIR/self-ctor-nongeneric.rs:8:23
|
LL | impl S0 {
Expand All @@ -11,7 +11,7 @@ LL | const C: S0 = Self(0);
= note: for more information, see issue #124186 <https://github.com/rust-lang/rust/issues/124186>
= note: `#[warn(self_constructor_from_outer_item)]` on by default

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

warning: 2 warnings emitted

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