use crate::infer::error_reporting::nice_region_error::NiceRegionError;
use rustc_hir as hir;
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_hir::Node;
use rustc_middle::hir::map::Map;
use rustc_middle::middle::resolve_lifetime as rl;
use rustc_middle::ty::{self, Region, TyCtxt};
impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
pub(super) fn find_anon_type(
&self,
region: Region<'tcx>,
br: &ty::BoundRegion,
) -> Option<(&hir::Ty<'tcx>, &hir::FnDecl<'tcx>)> {
if let Some(anon_reg) = self.tcx().is_suitable_region(region) {
let hir_id = self.tcx().hir().local_def_id_to_hir_id(anon_reg.def_id);
let fndecl = match self.tcx().hir().get(hir_id) {
Node::Item(&hir::Item { kind: hir::ItemKind::Fn(ref m, ..), .. })
| Node::TraitItem(&hir::TraitItem {
kind: hir::TraitItemKind::Fn(ref m, ..),
..
})
| Node::ImplItem(&hir::ImplItem {
kind: hir::ImplItemKind::Fn(ref m, ..), ..
}) => &m.decl,
_ => return None,
};
fndecl
.inputs
.iter()
.find_map(|arg| self.find_component_for_bound_region(arg, br))
.map(|ty| (ty, &**fndecl))
} else {
None
}
}
fn find_component_for_bound_region(
&self,
arg: &'tcx hir::Ty<'tcx>,
br: &ty::BoundRegion,
) -> Option<&'tcx hir::Ty<'tcx>> {
let mut nested_visitor = FindNestedTypeVisitor {
tcx: self.tcx(),
bound_region: *br,
found_type: None,
current_index: ty::INNERMOST,
};
nested_visitor.visit_ty(arg);
nested_visitor.found_type
}
}
struct FindNestedTypeVisitor<'tcx> {
tcx: TyCtxt<'tcx>,
bound_region: ty::BoundRegion,
found_type: Option<&'tcx hir::Ty<'tcx>>,
current_index: ty::DebruijnIndex,
}
impl Visitor<'tcx> for FindNestedTypeVisitor<'tcx> {
type Map = Map<'tcx>;
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
NestedVisitorMap::OnlyBodies(self.tcx.hir())
}
fn visit_ty(&mut self, arg: &'tcx hir::Ty<'tcx>) {
match arg.kind {
hir::TyKind::BareFn(_) => {
self.current_index.shift_in(1);
intravisit::walk_ty(self, arg);
self.current_index.shift_out(1);
return;
}
hir::TyKind::TraitObject(bounds, _) => {
for bound in bounds {
self.current_index.shift_in(1);
self.visit_poly_trait_ref(bound, hir::TraitBoundModifier::None);
self.current_index.shift_out(1);
}
}
hir::TyKind::Rptr(ref lifetime, _) => {
let hir_id = lifetime.hir_id;
match (self.tcx.named_region(hir_id), self.bound_region) {
(
Some(rl::Region::LateBoundAnon(debruijn_index, anon_index)),
ty::BrAnon(br_index),
) => {
debug!(
"LateBoundAnon depth = {:?} anon_index = {:?} br_index={:?}",
debruijn_index, anon_index, br_index
);
if debruijn_index == self.current_index && anon_index == br_index {
self.found_type = Some(arg);
return;
}
}
(Some(rl::Region::EarlyBound(_, id, _)), ty::BrNamed(def_id, _)) => {
debug!("EarlyBound id={:?} def_id={:?}", id, def_id);
if id == def_id {
self.found_type = Some(arg);
return;
}
}
(
Some(rl::Region::LateBound(debruijn_index, id, _)),
ty::BrNamed(def_id, _),
) => {
debug!(
"FindNestedTypeVisitor::visit_ty: LateBound depth = {:?}",
debruijn_index
);
debug!("LateBound id={:?} def_id={:?}", id, def_id);
if debruijn_index == self.current_index && id == def_id {
self.found_type = Some(arg);
return;
}
}
(
Some(
rl::Region::Static
| rl::Region::Free(_, _)
| rl::Region::EarlyBound(_, _, _)
| rl::Region::LateBound(_, _, _)
| rl::Region::LateBoundAnon(_, _),
)
| None,
_,
) => {
debug!("no arg found");
}
}
}
hir::TyKind::Path(_) => {
let subvisitor = &mut TyPathVisitor {
tcx: self.tcx,
found_it: false,
bound_region: self.bound_region,
current_index: self.current_index,
};
intravisit::walk_ty(subvisitor, arg);
if subvisitor.found_it {
self.found_type = Some(arg);
}
}
_ => {}
}
intravisit::walk_ty(self, arg);
}
}
struct TyPathVisitor<'tcx> {
tcx: TyCtxt<'tcx>,
found_it: bool,
bound_region: ty::BoundRegion,
current_index: ty::DebruijnIndex,
}
impl Visitor<'tcx> for TyPathVisitor<'tcx> {
type Map = Map<'tcx>;
fn nested_visit_map(&mut self) -> NestedVisitorMap<Map<'tcx>> {
NestedVisitorMap::OnlyBodies(self.tcx.hir())
}
fn visit_lifetime(&mut self, lifetime: &hir::Lifetime) {
match (self.tcx.named_region(lifetime.hir_id), self.bound_region) {
(Some(rl::Region::LateBoundAnon(debruijn_index, anon_index)), ty::BrAnon(br_index)) => {
if debruijn_index == self.current_index && anon_index == br_index {
self.found_it = true;
return;
}
}
(Some(rl::Region::EarlyBound(_, id, _)), ty::BrNamed(def_id, _)) => {
debug!("EarlyBound id={:?} def_id={:?}", id, def_id);
if id == def_id {
self.found_it = true;
return;
}
}
(Some(rl::Region::LateBound(debruijn_index, id, _)), ty::BrNamed(def_id, _)) => {
debug!("FindNestedTypeVisitor::visit_ty: LateBound depth = {:?}", debruijn_index,);
debug!("id={:?}", id);
debug!("def_id={:?}", def_id);
if debruijn_index == self.current_index && id == def_id {
self.found_it = true;
return;
}
}
(
Some(
rl::Region::Static
| rl::Region::EarlyBound(_, _, _)
| rl::Region::LateBound(_, _, _)
| rl::Region::LateBoundAnon(_, _)
| rl::Region::Free(_, _),
)
| None,
_,
) => {
debug!("no arg found");
}
}
}
fn visit_ty(&mut self, arg: &'tcx hir::Ty<'tcx>) {
debug!("`Ty` corresponding to a struct is {:?}", arg);
}
}