rustc_trait_selection/error_reporting/infer/nice_region_error/
util.rsuse rustc_hir as hir;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_middle::ty::fold::fold_regions;
use rustc_middle::ty::{self, Binder, Region, Ty, TyCtxt, TypeFoldable};
use rustc_span::Span;
use tracing::instrument;
use crate::error_reporting::infer::nice_region_error::NiceRegionError;
#[derive(Debug)]
pub struct AnonymousParamInfo<'tcx> {
pub param: &'tcx hir::Param<'tcx>,
pub param_ty: Ty<'tcx>,
pub kind: ty::LateParamRegionKind,
pub param_ty_span: Span,
pub is_first: bool,
}
#[instrument(skip(tcx), level = "debug")]
pub fn find_param_with_region<'tcx>(
tcx: TyCtxt<'tcx>,
generic_param_scope: LocalDefId,
anon_region: Region<'tcx>,
replace_region: Region<'tcx>,
) -> Option<AnonymousParamInfo<'tcx>> {
let (id, kind) = match *anon_region {
ty::ReLateParam(late_param) => (late_param.scope, late_param.kind),
ty::ReEarlyParam(ebr) => {
let region_def = tcx.generics_of(generic_param_scope).region_param(ebr, tcx).def_id;
(tcx.parent(region_def), ty::LateParamRegionKind::Named(region_def, ebr.name))
}
_ => return None, };
let hir = &tcx.hir();
let def_id = id.as_local()?;
match tcx.hir_node_by_def_id(generic_param_scope) {
hir::Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
return None;
}
_ => {}
}
let body = hir.maybe_body_owned_by(def_id)?;
let owner_id = hir.body_owner(body.id());
let fn_decl = hir.fn_decl_by_hir_id(owner_id)?;
let poly_fn_sig = tcx.fn_sig(id).instantiate_identity();
let fn_sig = tcx.liberate_late_bound_regions(id, poly_fn_sig);
body.params
.iter()
.take(if fn_sig.c_variadic {
fn_sig.inputs().len()
} else {
assert_eq!(fn_sig.inputs().len(), body.params.len());
body.params.len()
})
.enumerate()
.find_map(|(index, param)| {
let ty = fn_sig.inputs()[index];
let mut found_anon_region = false;
let new_param_ty = fold_regions(tcx, ty, |r, _| {
if r == anon_region {
found_anon_region = true;
replace_region
} else {
r
}
});
found_anon_region.then(|| {
let ty_hir_id = fn_decl.inputs[index].hir_id;
let param_ty_span = hir.span(ty_hir_id);
let is_first = index == 0;
AnonymousParamInfo { param, param_ty: new_param_ty, param_ty_span, kind, is_first }
})
})
}
impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
pub(super) fn find_param_with_region(
&self,
anon_region: Region<'tcx>,
replace_region: Region<'tcx>,
) -> Option<AnonymousParamInfo<'tcx>> {
find_param_with_region(self.tcx(), self.generic_param_scope, anon_region, replace_region)
}
pub(super) fn is_return_type_anon(
&self,
scope_def_id: LocalDefId,
region_def_id: DefId,
hir_sig: &hir::FnSig<'_>,
) -> Option<Span> {
let fn_ty = self.tcx().type_of(scope_def_id).instantiate_identity();
if let ty::FnDef(_, _) = fn_ty.kind() {
let ret_ty = fn_ty.fn_sig(self.tcx()).output();
let span = hir_sig.decl.output.span();
let future_output = if hir_sig.header.is_async() {
ret_ty.map_bound(|ty| self.cx.get_impl_future_output_ty(ty)).transpose()
} else {
None
};
return match future_output {
Some(output) if self.includes_region(output, region_def_id) => Some(span),
None if self.includes_region(ret_ty, region_def_id) => Some(span),
_ => None,
};
}
None
}
fn includes_region(
&self,
ty: Binder<'tcx, impl TypeFoldable<TyCtxt<'tcx>>>,
region_def_id: DefId,
) -> bool {
let late_bound_regions = self.tcx().collect_referenced_late_bound_regions(ty);
#[allow(rustc::potential_query_instability)]
late_bound_regions.iter().any(|r| match *r {
ty::BoundRegionKind::Named(def_id, _) => def_id == region_def_id,
_ => false,
})
}
pub(super) fn is_self_anon(&self, is_first: bool, scope_def_id: LocalDefId) -> bool {
is_first
&& self
.tcx()
.opt_associated_item(scope_def_id.to_def_id())
.is_some_and(|i| i.fn_has_self_parameter)
}
}