Skip to content

Commit 1523b04

Browse files
committed
Employ macro to unify a error/lint emission
1 parent cdf96cf commit 1523b04

File tree

2 files changed

+28
-53
lines changed

2 files changed

+28
-53
lines changed

compiler/rustc_hir_analysis/src/coherence/orphan.rs

+27-53
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ pub(crate) fn orphan_check_impl(
3030
Ok(()) => {}
3131
Err(err) => match orphan_check(tcx, impl_def_id, OrphanCheckMode::Compat) {
3232
Ok(()) => match err {
33-
OrphanCheckErr::UncoveredTyParams(uncovered_ty_params) => {
34-
lint_uncovered_ty_params(tcx, uncovered_ty_params, impl_def_id)
33+
OrphanCheckErr::UncoveredTyParams(err) => {
34+
let hir_id = tcx.local_def_id_to_hir_id(impl_def_id);
35+
complain_about_uncovered_ty_params!(tcx, err, |span, diag| {
36+
tcx.emit_node_span_lint(UNCOVERED_PARAM_IN_PROJECTION, hir_id, span, diag)
37+
});
3538
}
3639
OrphanCheckErr::NonLocalInputType(_) => {
3740
bug!("orphanck: shouldn't've gotten non-local input tys in compat mode")
@@ -461,62 +464,33 @@ fn emit_orphan_check_error<'tcx>(
461464

462465
diag.emit()
463466
}
464-
traits::OrphanCheckErr::UncoveredTyParams(UncoveredTyParams { uncovered, local_ty }) => {
465-
let mut reported = None;
466-
for param_def_id in uncovered {
467-
let span = tcx.def_ident_span(param_def_id).unwrap();
468-
let name = tcx.item_name(param_def_id);
469-
470-
reported.get_or_insert(match local_ty {
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(),
486-
});
487-
}
488-
reported.unwrap() // FIXME(fmease): This is very likely reachable.
467+
traits::OrphanCheckErr::UncoveredTyParams(err) => {
468+
complain_about_uncovered_ty_params!(tcx, err, |span, diag| tcx
469+
.dcx()
470+
.create_err(diag)
471+
.with_span(span)
472+
.emit())
489473
}
490474
}
491475
}
492476

493-
fn lint_uncovered_ty_params<'tcx>(
494-
tcx: TyCtxt<'tcx>,
495-
UncoveredTyParams { uncovered, local_ty }: UncoveredTyParams<TyCtxt<'tcx>, FxIndexSet<DefId>>,
496-
impl_def_id: LocalDefId,
497-
) {
498-
let hir_id = tcx.local_def_id_to_hir_id(impl_def_id);
499-
500-
for param_def_id in uncovered {
501-
let span = tcx.def_ident_span(param_def_id).unwrap();
502-
let name = tcx.item_name(param_def_id);
503-
504-
match local_ty {
505-
Some(local_type) => tcx.emit_node_span_lint(
506-
UNCOVERED_PARAM_IN_PROJECTION,
507-
hir_id,
508-
span,
509-
errors::TyParamFirstLocal { label: span, note: (), param: name, local_type },
510-
),
511-
None => tcx.emit_node_span_lint(
512-
UNCOVERED_PARAM_IN_PROJECTION,
513-
hir_id,
514-
span,
515-
errors::TyParamSome { label: span, note: (), param: name },
516-
),
517-
};
477+
macro complain_about_uncovered_ty_params($tcx:ident, $err:ident, $emit:expr) {{
478+
let mut guar = None;
479+
for param_def_id in $err.uncovered {
480+
let span = $tcx.def_ident_span(param_def_id).unwrap();
481+
let name = $tcx.item_name(param_def_id);
482+
guar = Some(match $err.local_ty {
483+
Some(local_type) => $emit(span, errors::TyParamFirstLocal {
484+
label: span,
485+
note: (),
486+
param: name,
487+
local_type,
488+
}),
489+
None => $emit(span, errors::TyParamSome { label: span, note: (), param: name }),
490+
});
518491
}
519-
}
492+
guar.unwrap() // FIXME(fmease): This very likely reachable
493+
}}
520494

521495
struct UncoveredTyParamCollector<'cx, 'tcx> {
522496
infcx: &'cx InferCtxt<'tcx>,

compiler/rustc_hir_analysis/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ This API is completely unstable and subject to change.
6262
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
6363
#![doc(rust_logo)]
6464
#![feature(assert_matches)]
65+
#![feature(decl_macro)]
6566
#![feature(if_let_guard)]
6667
#![feature(iter_intersperse)]
6768
#![feature(let_chains)]

0 commit comments

Comments
 (0)