Reduce use of local_def_id_to_hir_id.

This commit is contained in:
Camille GILLOT 2021-10-20 20:59:15 +02:00
parent ebcc847369
commit 67727aa7c3
39 changed files with 182 additions and 237 deletions

View file

@ -376,12 +376,9 @@ pub fn provide(providers: &mut ty::query::Providers) {
named_region_map: |tcx, id| resolve_lifetimes_for(tcx, id).defs.get(&id),
is_late_bound_map,
object_lifetime_defaults_map: |tcx, id| {
let hir_id = tcx.hir().local_def_id_to_hir_id(id);
match tcx.hir().find(hir_id) {
Some(Node::Item(item)) => compute_object_lifetime_defaults(tcx, item),
_ => None,
}
object_lifetime_defaults_map: |tcx, id| match tcx.hir().find_by_def_id(id) {
Some(Node::Item(item)) => compute_object_lifetime_defaults(tcx, item),
_ => None,
},
late_bound_vars_map: |tcx, id| resolve_lifetimes_for(tcx, id).late_bound_vars.get(&id),
lifetime_scope_map: |tcx, id| {
@ -514,14 +511,14 @@ fn resolve_lifetimes_for<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx R
/// Finds the `Item` that contains the given `LocalDefId`
fn item_for(tcx: TyCtxt<'_>, local_def_id: LocalDefId) -> LocalDefId {
let hir_id = tcx.hir().local_def_id_to_hir_id(local_def_id);
match tcx.hir().find(hir_id) {
match tcx.hir().find_by_def_id(local_def_id) {
Some(Node::Item(item)) => {
return item.def_id;
}
_ => {}
}
let item = {
let hir_id = tcx.hir().local_def_id_to_hir_id(local_def_id);
let mut parent_iter = tcx.hir().parent_iter(hir_id);
loop {
let node = parent_iter.next().map(|n| n.1);
@ -1672,13 +1669,10 @@ fn extract_labels(ctxt: &mut LifetimeContext<'_, '_>, body: &hir::Body<'_>) {
if let Some(def) =
lifetimes.get(&hir::ParamName::Plain(label.normalize_to_macros_2_0()))
{
let hir_id =
tcx.hir().local_def_id_to_hir_id(def.id().unwrap().expect_local());
signal_shadowing_problem(
tcx,
label.name,
original_lifetime(tcx.hir().span(hir_id)),
original_lifetime(tcx.def_span(def.id().unwrap().expect_local())),
shadower_label(label.span),
);
return;
@ -1910,6 +1904,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
let remove_decl = self
.tcx
.parent(def_id)
.and_then(|parent_def_id| parent_def_id.as_local())
.and_then(|parent_def_id| self.tcx.hir().get_generics(parent_def_id))
.and_then(|generics| self.lifetime_deletion_span(name, generics));
@ -2032,19 +2027,20 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
match lifetimeuseset {
Some(LifetimeUseSet::One(lifetime)) => {
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
debug!("hir id first={:?}", hir_id);
if let Some((id, span, name)) = match self.tcx.hir().get(hir_id) {
Node::Lifetime(hir_lifetime) => Some((
hir_lifetime.hir_id,
hir_lifetime.span,
hir_lifetime.name.ident(),
)),
Node::GenericParam(param) => {
Some((param.hir_id, param.span, param.name.ident()))
debug!(?def_id);
if let Some((id, span, name)) =
match self.tcx.hir().get_by_def_id(def_id.expect_local()) {
Node::Lifetime(hir_lifetime) => Some((
hir_lifetime.hir_id,
hir_lifetime.span,
hir_lifetime.name.ident(),
)),
Node::GenericParam(param) => {
Some((param.hir_id, param.span, param.name.ident()))
}
_ => None,
}
_ => None,
} {
{
debug!("id = {:?} span = {:?} name = {:?}", id, span, name);
if name.name == kw::UnderscoreLifetime {
continue;
@ -2052,12 +2048,10 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
if let Some(parent_def_id) = self.tcx.parent(def_id) {
if let Some(def_id) = parent_def_id.as_local() {
let parent_hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
// lifetimes in `derive` expansions don't count (Issue #53738)
if self
.tcx
.hir()
.attrs(parent_hir_id)
.get_attrs(def_id.to_def_id())
.iter()
.any(|attr| attr.has_name(sym::automatically_derived))
{
@ -2069,7 +2063,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
if let hir::Node::Item(hir::Item {
kind: hir::ItemKind::OpaqueTy(ref opaque),
..
}) = self.tcx.hir().get(parent_hir_id)
}) = self.tcx.hir().get_by_def_id(def_id)
{
if !matches!(opaque.origin, hir::OpaqueTyOrigin::AsyncFn(..)) {
continue 'lifetimes;
@ -2115,18 +2109,19 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
debug!("not one use lifetime");
}
None => {
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
if let Some((id, span, name)) = match self.tcx.hir().get(hir_id) {
Node::Lifetime(hir_lifetime) => Some((
hir_lifetime.hir_id,
hir_lifetime.span,
hir_lifetime.name.ident(),
)),
Node::GenericParam(param) => {
Some((param.hir_id, param.span, param.name.ident()))
if let Some((id, span, name)) =
match self.tcx.hir().get_by_def_id(def_id.expect_local()) {
Node::Lifetime(hir_lifetime) => Some((
hir_lifetime.hir_id,
hir_lifetime.span,
hir_lifetime.name.ident(),
)),
Node::GenericParam(param) => {
Some((param.hir_id, param.span, param.name.ident()))
}
_ => None,
}
_ => None,
} {
{
debug!("id ={:?} span = {:?} name = {:?}", id, span, name);
self.tcx.struct_span_lint_hir(
lint::builtin::UNUSED_LIFETIMES,
@ -2137,7 +2132,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
.build(&format!("lifetime parameter `{}` never used", name));
if let Some(parent_def_id) = self.tcx.parent(def_id) {
if let Some(generics) =
self.tcx.hir().get_generics(parent_def_id)
self.tcx.hir().get_generics(parent_def_id.expect_local())
{
let unused_lt_span =
self.lifetime_deletion_span(name, generics);
@ -3339,13 +3334,10 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
Scope::Binder { ref lifetimes, s, .. } => {
if let Some(&def) = lifetimes.get(&param.name.normalize_to_macros_2_0()) {
let hir_id =
self.tcx.hir().local_def_id_to_hir_id(def.id().unwrap().expect_local());
signal_shadowing_problem(
self.tcx,
param.name.ident().name,
original_lifetime(self.tcx.hir().span(hir_id)),
original_lifetime(self.tcx.def_span(def.id().unwrap())),
shadower_lifetime(&param),
);
return;