diff --git a/compiler/rustc_hir_analysis/src/coherence/orphan.rs b/compiler/rustc_hir_analysis/src/coherence/orphan.rs index 04770469132bb..eb8935a78a9af 100644 --- a/compiler/rustc_hir_analysis/src/coherence/orphan.rs +++ b/compiler/rustc_hir_analysis/src/coherence/orphan.rs @@ -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") @@ -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, FxIndexSet>, - 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>, diff --git a/compiler/rustc_hir_analysis/src/errors.rs b/compiler/rustc_hir_analysis/src/errors.rs index 77e81af3ca9a1..aae28b1e147a3 100644 --- a/compiler/rustc_hir_analysis/src/errors.rs +++ b/compiler/rustc_hir_analysis/src/errors.rs @@ -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, diff --git a/compiler/rustc_hir_analysis/src/lib.rs b/compiler/rustc_hir_analysis/src/lib.rs index 3ad35163191e5..6bd972e90379e 100644 --- a/compiler/rustc_hir_analysis/src/lib.rs +++ b/compiler/rustc_hir_analysis/src/lib.rs @@ -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)] diff --git a/compiler/rustc_hir_typeck/src/errors.rs b/compiler/rustc_hir_typeck/src/errors.rs index cceaabaff65b6..f11de5ccd9b91 100644 --- a/compiler/rustc_hir_typeck/src/errors.rs +++ b/compiler/rustc_hir_typeck/src/errors.rs @@ -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, -} - -#[derive(LintDiagnostic)] -#[diag(hir_typeck_self_ctor_from_outer_item)] -pub(crate) struct SelfCtorFromOuterItemLint { #[label] pub impl_span: Span, #[subdiagnostic] diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index 0fc566c58f72f..081c884c93782 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -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, ); } } diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 62c502f952429..ddce191d55e1f 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -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, ). 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) @@ -1886,8 +1885,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> { self.tcx.emit_node_span_lint( CONFLICTING_REPR_HINTS, hir_id, - hint_spans.collect::>(), - errors::ReprConflictingLint, + hint_spans, + errors::ReprConflicting, ); } } diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs index 042e50d890e9e..05136d45597f4 100644 --- a/compiler/rustc_passes/src/errors.rs +++ b/compiler/rustc_passes/src/errors.rs @@ -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, -} - -#[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)] diff --git a/tests/ui/self/self-ctor-nongeneric.stderr b/tests/ui/self/self-ctor-nongeneric.stderr index 6c03c6f3e38a4..308cf6bad8d72 100644 --- a/tests/ui/self/self-ctor-nongeneric.stderr +++ b/tests/ui/self/self-ctor-nongeneric.stderr @@ -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 { @@ -11,7 +11,7 @@ LL | const C: S0 = Self(0); = note: for more information, see issue #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 { @@ -25,3 +25,4 @@ LL | Self(0) warning: 2 warnings emitted +For more information about this error, try `rustc --explain E0401`.