From a7aee53fceeb99215622b6d081d3bd99ac23562d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Sat, 26 Oct 2024 00:56:35 +0200 Subject: [PATCH] Employ macro to unify a error/lint emission --- .../src/coherence/orphan.rs | 80 +++++++------------ compiler/rustc_hir_analysis/src/lib.rs | 1 + 2 files changed, 28 insertions(+), 53 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/coherence/orphan.rs b/compiler/rustc_hir_analysis/src/coherence/orphan.rs index 83ebe02d28885..31a8e90179fee 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,62 +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() - .create_err(errors::TyParamFirstLocal { - label: span, - note: (), - param: name, - local_type, - }) - .with_span(span) - .emit(), - None => tcx - .dcx() - .create_err(errors::TyParamSome { label: span, note: (), param: name }) - .with_span(span) - .emit(), - }); - } - 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::TyParamFirstLocal { label: span, note: (), param: name, local_type }, - ), - None => tcx.emit_node_span_lint( - UNCOVERED_PARAM_IN_PROJECTION, - hir_id, - span, - errors::TyParamSome { label: 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: This very likely reachable +}} struct UncoveredTyParamCollector<'cx, 'tcx> { infcx: &'cx InferCtxt<'tcx>, 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)]