1
Fork 0

Auto merge of #91299 - cjgillot:expect-ldid, r=petrochenkov

Take a LocalDefId in expect_*item.

Items and item-likes are always HIR owners.
When trying to find such nodes, there is no ambiguity, the `LocalDefId` and the `HirId::owner` always match.
In such cases, `local_def_id_to_hir_id` does not carry any meaningful information, so we can just skip calling it altogether.
This commit is contained in:
bors 2021-11-29 15:02:01 +00:00
commit 6db0a0e9a4
28 changed files with 95 additions and 137 deletions

View file

@ -467,7 +467,7 @@ impl<'a, 'tcx> Instantiator<'a, 'tcx> {
parent_def_id == tcx.hir().local_def_id(opaque_parent_hir_id) parent_def_id == tcx.hir().local_def_id(opaque_parent_hir_id)
}; };
let (in_definition_scope, origin) = let (in_definition_scope, origin) =
match tcx.hir().expect_item(opaque_hir_id).kind { match tcx.hir().expect_item(def_id).kind {
// Anonymous `impl Trait` // Anonymous `impl Trait`
hir::ItemKind::OpaqueTy(hir::OpaqueTy { hir::ItemKind::OpaqueTy(hir::OpaqueTy {
impl_trait_fn: Some(parent), impl_trait_fn: Some(parent),

View file

@ -3011,7 +3011,7 @@ impl<'tcx> LateLintPass<'tcx> for ClashingExternDeclarations {
this_decl_ty, this_decl_ty,
CItemKind::Declaration, CItemKind::Declaration,
) { ) {
let orig_fi = tcx.hir().expect_foreign_item(existing_hid); let orig_fi = tcx.hir().expect_foreign_item(existing_hid.expect_owner());
let orig = Self::name_of_extern_decl(tcx, orig_fi); let orig = Self::name_of_extern_decl(tcx, orig_fi);
// We want to ensure that we use spans for both decls that include where the // We want to ensure that we use spans for both decls that include where the

View file

@ -1152,8 +1152,7 @@ impl EncodeContext<'a, 'tcx> {
debug!("EncodeContext::encode_info_for_trait_item({:?})", def_id); debug!("EncodeContext::encode_info_for_trait_item({:?})", def_id);
let tcx = self.tcx; let tcx = self.tcx;
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let ast_item = tcx.hir().expect_trait_item(def_id.expect_local());
let ast_item = tcx.hir().expect_trait_item(hir_id);
let trait_item = tcx.associated_item(def_id); let trait_item = tcx.associated_item(def_id);
let container = match trait_item.defaultness { let container = match trait_item.defaultness {
@ -1221,8 +1220,7 @@ impl EncodeContext<'a, 'tcx> {
debug!("EncodeContext::encode_info_for_impl_item({:?})", def_id); debug!("EncodeContext::encode_info_for_impl_item({:?})", def_id);
let tcx = self.tcx; let tcx = self.tcx;
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let ast_item = self.tcx.hir().expect_impl_item(def_id.expect_local());
let ast_item = self.tcx.hir().expect_impl_item(hir_id);
let impl_item = self.tcx.associated_item(def_id); let impl_item = self.tcx.associated_item(def_id);
let container = match impl_item.defaultness { let container = match impl_item.defaultness {

View file

@ -869,24 +869,24 @@ impl<'hir> Map<'hir> {
bug!("expected foreign mod or inlined parent, found {}", self.node_to_string(parent)) bug!("expected foreign mod or inlined parent, found {}", self.node_to_string(parent))
} }
pub fn expect_item(&self, id: HirId) -> &'hir Item<'hir> { pub fn expect_item(&self, id: LocalDefId) -> &'hir Item<'hir> {
match self.tcx.hir_owner(id.expect_owner()) { match self.tcx.hir_owner(id) {
Some(Owner { node: OwnerNode::Item(item), .. }) => item, Some(Owner { node: OwnerNode::Item(item), .. }) => item,
_ => bug!("expected item, found {}", self.node_to_string(id)), _ => bug!("expected item, found {}", self.node_to_string(HirId::make_owner(id))),
} }
} }
pub fn expect_impl_item(&self, id: HirId) -> &'hir ImplItem<'hir> { pub fn expect_impl_item(&self, id: LocalDefId) -> &'hir ImplItem<'hir> {
match self.tcx.hir_owner(id.expect_owner()) { match self.tcx.hir_owner(id) {
Some(Owner { node: OwnerNode::ImplItem(item), .. }) => item, Some(Owner { node: OwnerNode::ImplItem(item), .. }) => item,
_ => bug!("expected impl item, found {}", self.node_to_string(id)), _ => bug!("expected impl item, found {}", self.node_to_string(HirId::make_owner(id))),
} }
} }
pub fn expect_trait_item(&self, id: HirId) -> &'hir TraitItem<'hir> { pub fn expect_trait_item(&self, id: LocalDefId) -> &'hir TraitItem<'hir> {
match self.tcx.hir_owner(id.expect_owner()) { match self.tcx.hir_owner(id) {
Some(Owner { node: OwnerNode::TraitItem(item), .. }) => item, Some(Owner { node: OwnerNode::TraitItem(item), .. }) => item,
_ => bug!("expected trait item, found {}", self.node_to_string(id)), _ => bug!("expected trait item, found {}", self.node_to_string(HirId::make_owner(id))),
} }
} }
@ -897,10 +897,12 @@ impl<'hir> Map<'hir> {
} }
} }
pub fn expect_foreign_item(&self, id: HirId) -> &'hir ForeignItem<'hir> { pub fn expect_foreign_item(&self, id: LocalDefId) -> &'hir ForeignItem<'hir> {
match self.tcx.hir_owner(id.expect_owner()) { match self.tcx.hir_owner(id) {
Some(Owner { node: OwnerNode::ForeignItem(item), .. }) => item, Some(Owner { node: OwnerNode::ForeignItem(item), .. }) => item,
_ => bug!("expected foreign item, found {}", self.node_to_string(id)), _ => {
bug!("expected foreign item, found {}", self.node_to_string(HirId::make_owner(id)))
}
} }
} }

View file

@ -777,9 +777,7 @@ fn foo(&self) -> Self::T { String::new() }
if let ty::Opaque(def_id, _) = *proj_ty.self_ty().kind() { if let ty::Opaque(def_id, _) = *proj_ty.self_ty().kind() {
let opaque_local_def_id = def_id.as_local(); let opaque_local_def_id = def_id.as_local();
let opaque_hir_ty = if let Some(opaque_local_def_id) = opaque_local_def_id { let opaque_hir_ty = if let Some(opaque_local_def_id) = opaque_local_def_id {
let hir = self.hir(); match &self.hir().expect_item(opaque_local_def_id).kind {
let opaque_hir_id = hir.local_def_id_to_hir_id(opaque_local_def_id);
match &hir.expect_item(opaque_hir_id).kind {
hir::ItemKind::OpaqueTy(opaque_hir_ty) => opaque_hir_ty, hir::ItemKind::OpaqueTy(opaque_hir_ty) => opaque_hir_ty,
_ => bug!("The HirId comes from a `ty::Opaque`"), _ => bug!("The HirId comes from a `ty::Opaque`"),
} }

View file

@ -32,7 +32,7 @@ pub(crate) fn target_from_impl_item<'tcx>(
match impl_item.kind { match impl_item.kind {
hir::ImplItemKind::Const(..) => Target::AssocConst, hir::ImplItemKind::Const(..) => Target::AssocConst,
hir::ImplItemKind::Fn(..) => { hir::ImplItemKind::Fn(..) => {
let parent_hir_id = tcx.hir().get_parent_item(impl_item.hir_id()); let parent_hir_id = tcx.hir().get_parent_item(impl_item.hir_id()).expect_owner();
let containing_item = tcx.hir().expect_item(parent_hir_id); let containing_item = tcx.hir().expect_item(parent_hir_id);
let containing_impl_is_for_trait = match &containing_item.kind { let containing_impl_is_for_trait = match &containing_item.kind {
hir::ItemKind::Impl(impl_) => impl_.of_trait.is_some(), hir::ItemKind::Impl(impl_) => impl_.of_trait.is_some(),
@ -582,7 +582,7 @@ impl CheckAttrVisitor<'tcx> {
Target::Impl => Some("implementation block"), Target::Impl => Some("implementation block"),
Target::ForeignMod => Some("extern block"), Target::ForeignMod => Some("extern block"),
Target::AssocTy => { Target::AssocTy => {
let parent_hir_id = self.tcx.hir().get_parent_item(hir_id); let parent_hir_id = self.tcx.hir().get_parent_item(hir_id).expect_owner();
let containing_item = self.tcx.hir().expect_item(parent_hir_id); let containing_item = self.tcx.hir().expect_item(parent_hir_id);
if Target::from_item(containing_item) == Target::Impl { if Target::from_item(containing_item) == Target::Impl {
Some("type alias in implementation block") Some("type alias in implementation block")
@ -591,7 +591,7 @@ impl CheckAttrVisitor<'tcx> {
} }
} }
Target::AssocConst => { Target::AssocConst => {
let parent_hir_id = self.tcx.hir().get_parent_item(hir_id); let parent_hir_id = self.tcx.hir().get_parent_item(hir_id).expect_owner();
let containing_item = self.tcx.hir().expect_item(parent_hir_id); let containing_item = self.tcx.hir().expect_item(parent_hir_id);
// We can't link to trait impl's consts. // We can't link to trait impl's consts.
let err = "associated constant in trait implementation block"; let err = "associated constant in trait implementation block";

View file

@ -173,8 +173,7 @@ impl<'tcx> ReachableContext<'tcx> {
// Check the impl. If the generics on the self // Check the impl. If the generics on the self
// type of the impl require inlining, this method // type of the impl require inlining, this method
// does too. // does too.
let impl_hir_id = self.tcx.hir().local_def_id_to_hir_id(impl_did); match self.tcx.hir().expect_item(impl_did).kind {
match self.tcx.hir().expect_item(impl_hir_id).kind {
hir::ItemKind::Impl { .. } => { hir::ItemKind::Impl { .. } => {
let generics = self.tcx.generics_of(impl_did); let generics = self.tcx.generics_of(impl_did);
generics.requires_monomorphization(self.tcx) generics.requires_monomorphization(self.tcx)

View file

@ -559,8 +559,7 @@ impl EmbargoVisitor<'tcx> {
// have normal hygine, so we can treat them like other items without type // have normal hygine, so we can treat them like other items without type
// privacy and mark them reachable. // privacy and mark them reachable.
DefKind::Macro(_) => { DefKind::Macro(_) => {
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id); let item = self.tcx.hir().expect_item(def_id);
let item = self.tcx.hir().expect_item(hir_id);
if let hir::ItemKind::Macro(MacroDef { macro_rules: false, .. }) = item.kind { if let hir::ItemKind::Macro(MacroDef { macro_rules: false, .. }) = item.kind {
if vis.is_accessible_from(module.to_def_id(), self.tcx) { if vis.is_accessible_from(module.to_def_id(), self.tcx) {
self.update(def_id, level); self.update(def_id, level);
@ -581,8 +580,7 @@ impl EmbargoVisitor<'tcx> {
DefKind::Struct | DefKind::Union => { DefKind::Struct | DefKind::Union => {
// While structs and unions have type privacy, their fields do not. // While structs and unions have type privacy, their fields do not.
if vis.is_public() { if vis.is_public() {
let item = let item = self.tcx.hir().expect_item(def_id);
self.tcx.hir().expect_item(self.tcx.hir().local_def_id_to_hir_id(def_id));
if let hir::ItemKind::Struct(ref struct_def, _) if let hir::ItemKind::Struct(ref struct_def, _)
| hir::ItemKind::Union(ref struct_def, _) = item.kind | hir::ItemKind::Union(ref struct_def, _) = item.kind
{ {
@ -653,9 +651,7 @@ impl EmbargoVisitor<'tcx> {
// If the module is `self`, i.e. the current crate, // If the module is `self`, i.e. the current crate,
// there will be no corresponding item. // there will be no corresponding item.
.filter(|def_id| def_id.index != CRATE_DEF_INDEX || def_id.krate != LOCAL_CRATE) .filter(|def_id| def_id.index != CRATE_DEF_INDEX || def_id.krate != LOCAL_CRATE)
.and_then(|def_id| { .and_then(|def_id| def_id.as_local())
def_id.as_local().map(|def_id| self.tcx.hir().local_def_id_to_hir_id(def_id))
})
.map(|module_hir_id| self.tcx.hir().expect_item(module_hir_id)) .map(|module_hir_id| self.tcx.hir().expect_item(module_hir_id))
{ {
if let hir::ItemKind::Mod(m) = &item.kind { if let hir::ItemKind::Mod(m) = &item.kind {

View file

@ -445,7 +445,7 @@ fn do_resolve(
trait_definition_only: bool, trait_definition_only: bool,
with_scope_for_path: bool, with_scope_for_path: bool,
) -> NamedRegionMap { ) -> NamedRegionMap {
let item = tcx.hir().expect_item(tcx.hir().local_def_id_to_hir_id(local_def_id)); let item = tcx.hir().expect_item(local_def_id);
let mut named_region_map = NamedRegionMap { let mut named_region_map = NamedRegionMap {
defs: Default::default(), defs: Default::default(),
late_bound: Default::default(), late_bound: Default::default(),
@ -1134,7 +1134,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
self.missing_named_lifetime_spots.push((&trait_item.generics).into()); self.missing_named_lifetime_spots.push((&trait_item.generics).into());
let tcx = self.tcx; let tcx = self.tcx;
self.visit_early_late( self.visit_early_late(
Some(tcx.hir().get_parent_item(trait_item.hir_id())), Some(tcx.hir().get_parent_did(trait_item.hir_id())),
trait_item.hir_id(), trait_item.hir_id(),
&sig.decl, &sig.decl,
&trait_item.generics, &trait_item.generics,
@ -1203,7 +1203,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
self.missing_named_lifetime_spots.push((&impl_item.generics).into()); self.missing_named_lifetime_spots.push((&impl_item.generics).into());
let tcx = self.tcx; let tcx = self.tcx;
self.visit_early_late( self.visit_early_late(
Some(tcx.hir().get_parent_item(impl_item.hir_id())), Some(tcx.hir().get_parent_did(impl_item.hir_id())),
impl_item.hir_id(), impl_item.hir_id(),
&sig.decl, &sig.decl,
&impl_item.generics, &impl_item.generics,
@ -2176,7 +2176,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
/// ordering is not important there. /// ordering is not important there.
fn visit_early_late<F>( fn visit_early_late<F>(
&mut self, &mut self,
parent_id: Option<hir::HirId>, parent_id: Option<LocalDefId>,
hir_id: hir::HirId, hir_id: hir::HirId,
decl: &'tcx hir::FnDecl<'tcx>, decl: &'tcx hir::FnDecl<'tcx>,
generics: &'tcx hir::Generics<'tcx>, generics: &'tcx hir::Generics<'tcx>,
@ -2758,7 +2758,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
Node::TraitItem(&hir::TraitItem { kind: hir::TraitItemKind::Fn(_, ref m), .. }) => { Node::TraitItem(&hir::TraitItem { kind: hir::TraitItemKind::Fn(_, ref m), .. }) => {
if let hir::ItemKind::Trait(.., ref trait_items) = if let hir::ItemKind::Trait(.., ref trait_items) =
self.tcx.hir().expect_item(self.tcx.hir().get_parent_item(parent)).kind self.tcx.hir().expect_item(self.tcx.hir().get_parent_did(parent)).kind
{ {
assoc_item_kind = assoc_item_kind =
trait_items.iter().find(|ti| ti.id.hir_id() == parent).map(|ti| ti.kind); trait_items.iter().find(|ti| ti.id.hir_id() == parent).map(|ti| ti.kind);
@ -2771,7 +2771,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(_, body), .. }) => { Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(_, body), .. }) => {
if let hir::ItemKind::Impl(hir::Impl { ref self_ty, ref items, .. }) = if let hir::ItemKind::Impl(hir::Impl { ref self_ty, ref items, .. }) =
self.tcx.hir().expect_item(self.tcx.hir().get_parent_item(parent)).kind self.tcx.hir().expect_item(self.tcx.hir().get_parent_did(parent)).kind
{ {
impl_self = Some(self_ty); impl_self = Some(self_ty);
assoc_item_kind = assoc_item_kind =

View file

@ -123,7 +123,7 @@ fn associated_item(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItem {
let id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
let parent_id = tcx.hir().get_parent_item(id); let parent_id = tcx.hir().get_parent_item(id);
let parent_def_id = tcx.hir().local_def_id(parent_id); let parent_def_id = tcx.hir().local_def_id(parent_id);
let parent_item = tcx.hir().expect_item(parent_id); let parent_item = tcx.hir().expect_item(parent_def_id);
match parent_item.kind { match parent_item.kind {
hir::ItemKind::Impl(ref impl_) => { hir::ItemKind::Impl(ref impl_) => {
if let Some(impl_item_ref) = if let Some(impl_item_ref) =
@ -158,8 +158,7 @@ fn associated_item(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItem {
} }
fn impl_defaultness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Defaultness { fn impl_defaultness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Defaultness {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let item = tcx.hir().expect_item(def_id.expect_local());
let item = tcx.hir().expect_item(hir_id);
if let hir::ItemKind::Impl(impl_) = &item.kind { if let hir::ItemKind::Impl(impl_) = &item.kind {
impl_.defaultness impl_.defaultness
} else { } else {
@ -168,8 +167,7 @@ fn impl_defaultness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Defaultness {
} }
fn impl_constness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Constness { fn impl_constness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Constness {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let item = tcx.hir().expect_item(def_id.expect_local());
let item = tcx.hir().expect_item(hir_id);
if let hir::ItemKind::Impl(impl_) = &item.kind { if let hir::ItemKind::Impl(impl_) = &item.kind {
impl_.constness impl_.constness
} else { } else {
@ -202,8 +200,7 @@ fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AdtSizedConstrain
} }
fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: DefId) -> &[DefId] { fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: DefId) -> &[DefId] {
let id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let item = tcx.hir().expect_item(def_id.expect_local());
let item = tcx.hir().expect_item(id);
match item.kind { match item.kind {
hir::ItemKind::Trait(.., ref trait_item_refs) => tcx.arena.alloc_from_iter( hir::ItemKind::Trait(.., ref trait_item_refs) => tcx.arena.alloc_from_iter(
trait_item_refs.iter().map(|trait_item_ref| trait_item_ref.id.def_id.to_def_id()), trait_item_refs.iter().map(|trait_item_ref| trait_item_ref.id.def_id.to_def_id()),

View file

@ -458,7 +458,7 @@ pub(super) fn check_opaque_for_inheriting_lifetimes(
def_id: LocalDefId, def_id: LocalDefId,
span: Span, span: Span,
) { ) {
let item = tcx.hir().expect_item(tcx.hir().local_def_id_to_hir_id(def_id)); let item = tcx.hir().expect_item(def_id);
debug!(?item, ?span); debug!(?item, ?span);
struct FoundParentLifetime; struct FoundParentLifetime;

View file

@ -1637,11 +1637,10 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
let ty = <dyn AstConv<'_>>::ast_ty_to_ty(fcx, ty); let ty = <dyn AstConv<'_>>::ast_ty_to_ty(fcx, ty);
// Get the `impl Trait`'s `DefId`. // Get the `impl Trait`'s `DefId`.
if let ty::Opaque(def_id, _) = ty.kind() { if let ty::Opaque(def_id, _) = ty.kind() {
let hir_id = fcx.tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
// Get the `impl Trait`'s `Item` so that we can get its trait bounds and // Get the `impl Trait`'s `Item` so that we can get its trait bounds and
// get the `Trait`'s `DefId`. // get the `Trait`'s `DefId`.
if let hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds, .. }) = if let hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds, .. }) =
fcx.tcx.hir().expect_item(hir_id).kind fcx.tcx.hir().expect_item(def_id.expect_local()).kind
{ {
// Are of this `impl Trait`'s traits object safe? // Are of this `impl Trait`'s traits object safe?
is_object_safe = bounds.iter().all(|bound| { is_object_safe = bounds.iter().all(|bound| {

View file

@ -322,9 +322,7 @@ fn compare_predicate_entailment<'tcx>(
// When the `impl` receiver is an arbitrary self type, like `self: Box<Self>`, the // When the `impl` receiver is an arbitrary self type, like `self: Box<Self>`, the
// span points only at the type `Box<Self`>, but we want to cover the whole // span points only at the type `Box<Self`>, but we want to cover the whole
// argument pattern and type. // argument pattern and type.
let impl_m_hir_id = let span = match tcx.hir().expect_impl_item(impl_m.def_id.expect_local()).kind {
tcx.hir().local_def_id_to_hir_id(impl_m.def_id.expect_local());
let span = match tcx.hir().expect_impl_item(impl_m_hir_id).kind {
ImplItemKind::Fn(ref sig, body) => tcx ImplItemKind::Fn(ref sig, body) => tcx
.hir() .hir()
.body_param_names(body) .body_param_names(body)
@ -346,9 +344,7 @@ fn compare_predicate_entailment<'tcx>(
if trait_sig.inputs().len() == *i { if trait_sig.inputs().len() == *i {
// Suggestion to change output type. We do not suggest in `async` functions // Suggestion to change output type. We do not suggest in `async` functions
// to avoid complex logic or incorrect output. // to avoid complex logic or incorrect output.
let impl_m_hir_id = match tcx.hir().expect_impl_item(impl_m.def_id.expect_local()).kind {
tcx.hir().local_def_id_to_hir_id(impl_m.def_id.expect_local());
match tcx.hir().expect_impl_item(impl_m_hir_id).kind {
ImplItemKind::Fn(ref sig, _) ImplItemKind::Fn(ref sig, _)
if sig.header.asyncness == hir::IsAsync::NotAsync => if sig.header.asyncness == hir::IsAsync::NotAsync =>
{ {
@ -467,21 +463,18 @@ fn extract_spans_for_error_reporting<'a, 'tcx>(
trait_m: &ty::AssocItem, trait_m: &ty::AssocItem,
) -> (Span, Option<Span>) { ) -> (Span, Option<Span>) {
let tcx = infcx.tcx; let tcx = infcx.tcx;
let impl_m_hir_id = tcx.hir().local_def_id_to_hir_id(impl_m.def_id.expect_local()); let mut impl_args = match tcx.hir().expect_impl_item(impl_m.def_id.expect_local()).kind {
let mut impl_args = match tcx.hir().expect_impl_item(impl_m_hir_id).kind {
ImplItemKind::Fn(ref sig, _) => { ImplItemKind::Fn(ref sig, _) => {
sig.decl.inputs.iter().map(|t| t.span).chain(iter::once(sig.decl.output.span())) sig.decl.inputs.iter().map(|t| t.span).chain(iter::once(sig.decl.output.span()))
} }
_ => bug!("{:?} is not a method", impl_m), _ => bug!("{:?} is not a method", impl_m),
}; };
let trait_args = trait_m.def_id.as_local().map(|def_id| { let trait_args =
let trait_m_hir_id = tcx.hir().local_def_id_to_hir_id(def_id); trait_m.def_id.as_local().map(|def_id| match tcx.hir().expect_trait_item(def_id).kind {
match tcx.hir().expect_trait_item(trait_m_hir_id).kind {
TraitItemKind::Fn(ref sig, _) => { TraitItemKind::Fn(ref sig, _) => {
sig.decl.inputs.iter().map(|t| t.span).chain(iter::once(sig.decl.output.span())) sig.decl.inputs.iter().map(|t| t.span).chain(iter::once(sig.decl.output.span()))
} }
_ => bug!("{:?} is not a TraitItemKind::Fn", trait_m), _ => bug!("{:?} is not a TraitItemKind::Fn", trait_m),
}
}); });
match *terr { match *terr {
@ -600,8 +593,7 @@ fn compare_number_of_generics<'tcx>(
err_occurred = true; err_occurred = true;
let (trait_spans, impl_trait_spans) = if let Some(def_id) = trait_.def_id.as_local() { let (trait_spans, impl_trait_spans) = if let Some(def_id) = trait_.def_id.as_local() {
let trait_hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let trait_item = tcx.hir().expect_trait_item(def_id);
let trait_item = tcx.hir().expect_trait_item(trait_hir_id);
if trait_item.generics.params.is_empty() { if trait_item.generics.params.is_empty() {
(Some(vec![trait_item.generics.span]), vec![]) (Some(vec![trait_item.generics.span]), vec![])
} else { } else {
@ -622,8 +614,7 @@ fn compare_number_of_generics<'tcx>(
(trait_span.map(|s| vec![s]), vec![]) (trait_span.map(|s| vec![s]), vec![])
}; };
let impl_hir_id = tcx.hir().local_def_id_to_hir_id(impl_.def_id.expect_local()); let impl_item = tcx.hir().expect_impl_item(impl_.def_id.expect_local());
let impl_item = tcx.hir().expect_impl_item(impl_hir_id);
let impl_item_impl_trait_spans: Vec<Span> = impl_item let impl_item_impl_trait_spans: Vec<Span> = impl_item
.generics .generics
.params .params
@ -711,8 +702,7 @@ fn compare_number_of_method_arguments<'tcx>(
let impl_number_args = impl_m_fty.inputs().skip_binder().len(); let impl_number_args = impl_m_fty.inputs().skip_binder().len();
if trait_number_args != impl_number_args { if trait_number_args != impl_number_args {
let trait_span = if let Some(def_id) = trait_m.def_id.as_local() { let trait_span = if let Some(def_id) = trait_m.def_id.as_local() {
let trait_id = tcx.hir().local_def_id_to_hir_id(def_id); match tcx.hir().expect_trait_item(def_id).kind {
match tcx.hir().expect_trait_item(trait_id).kind {
TraitItemKind::Fn(ref trait_m_sig, _) => { TraitItemKind::Fn(ref trait_m_sig, _) => {
let pos = if trait_number_args > 0 { trait_number_args - 1 } else { 0 }; let pos = if trait_number_args > 0 { trait_number_args - 1 } else { 0 };
if let Some(arg) = trait_m_sig.decl.inputs.get(pos) { if let Some(arg) = trait_m_sig.decl.inputs.get(pos) {
@ -730,8 +720,7 @@ fn compare_number_of_method_arguments<'tcx>(
} else { } else {
trait_item_span trait_item_span
}; };
let impl_m_hir_id = tcx.hir().local_def_id_to_hir_id(impl_m.def_id.expect_local()); let impl_span = match tcx.hir().expect_impl_item(impl_m.def_id.expect_local()).kind {
let impl_span = match tcx.hir().expect_impl_item(impl_m_hir_id).kind {
ImplItemKind::Fn(ref impl_m_sig, _) => { ImplItemKind::Fn(ref impl_m_sig, _) => {
let pos = if impl_number_args > 0 { impl_number_args - 1 } else { 0 }; let pos = if impl_number_args > 0 { impl_number_args - 1 } else { 0 };
if let Some(arg) = impl_m_sig.decl.inputs.get(pos) { if let Some(arg) = impl_m_sig.decl.inputs.get(pos) {
@ -1055,7 +1044,7 @@ crate fn compare_const_impl<'tcx>(
); );
// Locate the Span containing just the type of the offending impl // Locate the Span containing just the type of the offending impl
match tcx.hir().expect_impl_item(impl_c_hir_id).kind { match tcx.hir().expect_impl_item(impl_c.def_id.expect_local()).kind {
ImplItemKind::Const(ref ty, _) => cause.make_mut().span = ty.span, ImplItemKind::Const(ref ty, _) => cause.make_mut().span = ty.span,
_ => bug!("{:?} is not a impl const", impl_c), _ => bug!("{:?} is not a impl const", impl_c),
} }
@ -1068,11 +1057,9 @@ crate fn compare_const_impl<'tcx>(
trait_c.ident trait_c.ident
); );
let trait_c_hir_id = let trait_c_span = trait_c.def_id.as_local().map(|trait_c_def_id| {
trait_c.def_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id));
let trait_c_span = trait_c_hir_id.map(|trait_c_hir_id| {
// Add a label to the Span containing just the type of the const // Add a label to the Span containing just the type of the const
match tcx.hir().expect_trait_item(trait_c_hir_id).kind { match tcx.hir().expect_trait_item(trait_c_def_id).kind {
TraitItemKind::Const(ref ty, _) => ty.span, TraitItemKind::Const(ref ty, _) => ty.span,
_ => bug!("{:?} is not a trait const", trait_c), _ => bug!("{:?} is not a trait const", trait_c),
} }

View file

@ -1097,12 +1097,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
(_, _) => return None, (_, _) => return None,
}; };
let last_hir_id = self.tcx.hir().local_def_id_to_hir_id(last_local_id);
let exp_hir_id = self.tcx.hir().local_def_id_to_hir_id(exp_local_id);
match ( match (
&self.tcx.hir().expect_item(last_hir_id).kind, &self.tcx.hir().expect_item(last_local_id).kind,
&self.tcx.hir().expect_item(exp_hir_id).kind, &self.tcx.hir().expect_item(exp_local_id).kind,
) { ) {
( (
hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds: last_bounds, .. }), hir::ItemKind::OpaqueTy(hir::OpaqueTy { bounds: last_bounds, .. }),

View file

@ -345,10 +345,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let import_items: Vec<_> = applicable_trait let import_items: Vec<_> = applicable_trait
.import_ids .import_ids
.iter() .iter()
.map(|&import_id| { .map(|&import_id| self.tcx.hir().expect_item(import_id))
let hir_id = self.tcx.hir().local_def_id_to_hir_id(import_id);
self.tcx.hir().expect_item(hir_id)
})
.collect(); .collect();
// Find an identifier with which this trait was imported (note that `_` doesn't count). // Find an identifier with which this trait was imported (note that `_` doesn't count).

View file

@ -84,8 +84,7 @@ impl<'tcx> CheckWfFcxBuilder<'tcx> {
/// the types first. /// the types first.
#[instrument(skip(tcx), level = "debug")] #[instrument(skip(tcx), level = "debug")]
pub fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) { pub fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let item = tcx.hir().expect_item(def_id);
let item = tcx.hir().expect_item(hir_id);
debug!( debug!(
?item.def_id, ?item.def_id,
@ -197,7 +196,7 @@ pub fn check_item_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) {
pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: LocalDefId) { pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
let trait_item = tcx.hir().expect_trait_item(hir_id); let trait_item = tcx.hir().expect_trait_item(def_id);
let (method_sig, span) = match trait_item.kind { let (method_sig, span) = match trait_item.kind {
hir::TraitItemKind::Fn(ref sig, _) => (Some(sig), trait_item.span), hir::TraitItemKind::Fn(ref sig, _) => (Some(sig), trait_item.span),
@ -207,8 +206,8 @@ pub fn check_trait_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
check_object_unsafe_self_trait_by_name(tcx, trait_item); check_object_unsafe_self_trait_by_name(tcx, trait_item);
check_associated_item(tcx, trait_item.def_id, span, method_sig); check_associated_item(tcx, trait_item.def_id, span, method_sig);
let encl_trait_hir_id = tcx.hir().get_parent_item(hir_id); let encl_trait_def_id = tcx.hir().get_parent_did(hir_id);
let encl_trait = tcx.hir().expect_item(encl_trait_hir_id); let encl_trait = tcx.hir().expect_item(encl_trait_def_id);
let encl_trait_def_id = encl_trait.def_id.to_def_id(); let encl_trait_def_id = encl_trait.def_id.to_def_id();
let fn_lang_item_name = if Some(encl_trait_def_id) == tcx.lang_items().fn_trait() { let fn_lang_item_name = if Some(encl_trait_def_id) == tcx.lang_items().fn_trait() {
Some("fn") Some("fn")
@ -680,8 +679,7 @@ fn check_object_unsafe_self_trait_by_name(tcx: TyCtxt<'_>, item: &hir::TraitItem
} }
pub fn check_impl_item(tcx: TyCtxt<'_>, def_id: LocalDefId) { pub fn check_impl_item(tcx: TyCtxt<'_>, def_id: LocalDefId) {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let impl_item = tcx.hir().expect_impl_item(def_id);
let impl_item = tcx.hir().expect_impl_item(hir_id);
let (method_sig, span) = match impl_item.kind { let (method_sig, span) = match impl_item.kind {
hir::ImplItemKind::Fn(ref sig, _) => (Some(sig), impl_item.span), hir::ImplItemKind::Fn(ref sig, _) => (Some(sig), impl_item.span),

View file

@ -119,13 +119,13 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) {
for extern_crate in &crates_to_lint { for extern_crate in &crates_to_lint {
let def_id = extern_crate.def_id.expect_local(); let def_id = extern_crate.def_id.expect_local();
let id = tcx.hir().local_def_id_to_hir_id(def_id); let item = tcx.hir().expect_item(def_id);
let item = tcx.hir().expect_item(id);
// If the crate is fully unused, we suggest removing it altogether. // If the crate is fully unused, we suggest removing it altogether.
// We do this in any edition. // We do this in any edition.
if extern_crate.warn_if_unused { if extern_crate.warn_if_unused {
if let Some(&span) = unused_extern_crates.get(&def_id) { if let Some(&span) = unused_extern_crates.get(&def_id) {
let id = tcx.hir().local_def_id_to_hir_id(def_id);
tcx.struct_span_lint_hir(lint, id, span, |lint| { tcx.struct_span_lint_hir(lint, id, span, |lint| {
// Removal suggestion span needs to include attributes (Issue #54400) // Removal suggestion span needs to include attributes (Issue #54400)
let span_with_attrs = tcx let span_with_attrs = tcx
@ -173,6 +173,7 @@ fn unused_crates_lint(tcx: TyCtxt<'_>) {
if !tcx.get_attrs(extern_crate.def_id).is_empty() { if !tcx.get_attrs(extern_crate.def_id).is_empty() {
continue; continue;
} }
let id = tcx.hir().local_def_id_to_hir_id(def_id);
tcx.struct_span_lint_hir(lint, id, extern_crate.span, |lint| { tcx.struct_span_lint_hir(lint, id, extern_crate.span, |lint| {
// Otherwise, we can convert it into a `use` of some kind. // Otherwise, we can convert it into a `use` of some kind.
let base_replacement = match extern_crate.orig_name { let base_replacement = match extern_crate.orig_name {

View file

@ -52,8 +52,7 @@ fn visit_implementation_of_drop(tcx: TyCtxt<'_>, impl_did: LocalDefId) {
return; return;
} }
let impl_hir_id = tcx.hir().local_def_id_to_hir_id(impl_did); let sp = match tcx.hir().expect_item(impl_did).kind {
let sp = match tcx.hir().expect_item(impl_hir_id).kind {
ItemKind::Impl(ref impl_) => impl_.self_ty.span, ItemKind::Impl(ref impl_) => impl_.self_ty.span,
_ => bug!("expected Drop impl item"), _ => bug!("expected Drop impl item"),
}; };
@ -78,7 +77,7 @@ fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: LocalDefId) {
match can_type_implement_copy(tcx, param_env, self_type) { match can_type_implement_copy(tcx, param_env, self_type) {
Ok(()) => {} Ok(()) => {}
Err(CopyImplementationError::InfrigingFields(fields)) => { Err(CopyImplementationError::InfrigingFields(fields)) => {
let item = tcx.hir().expect_item(impl_hir_id); let item = tcx.hir().expect_item(impl_did);
let span = if let ItemKind::Impl(hir::Impl { of_trait: Some(ref tr), .. }) = item.kind { let span = if let ItemKind::Impl(hir::Impl { of_trait: Some(ref tr), .. }) = item.kind {
tr.path.span tr.path.span
} else { } else {
@ -97,7 +96,7 @@ fn visit_implementation_of_copy(tcx: TyCtxt<'_>, impl_did: LocalDefId) {
err.emit() err.emit()
} }
Err(CopyImplementationError::NotAnAdt) => { Err(CopyImplementationError::NotAnAdt) => {
let item = tcx.hir().expect_item(impl_hir_id); let item = tcx.hir().expect_item(impl_did);
let span = let span =
if let ItemKind::Impl(ref impl_) = item.kind { impl_.self_ty.span } else { span }; if let ItemKind::Impl(ref impl_) = item.kind { impl_.self_ty.span } else { span };
@ -292,8 +291,8 @@ pub fn coerce_unsized_info(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUnsizedI
debug!("compute_coerce_unsized_info(impl_did={:?})", impl_did); debug!("compute_coerce_unsized_info(impl_did={:?})", impl_did);
// this provider should only get invoked for local def-ids // this provider should only get invoked for local def-ids
let impl_hir_id = tcx.hir().local_def_id_to_hir_id(impl_did.expect_local()); let impl_did = impl_did.expect_local();
let span = tcx.hir().span(impl_hir_id); let span = tcx.def_span(impl_did);
let coerce_unsized_trait = tcx.require_lang_item(LangItem::CoerceUnsized, Some(span)); let coerce_unsized_trait = tcx.require_lang_item(LangItem::CoerceUnsized, Some(span));
@ -315,6 +314,7 @@ pub fn coerce_unsized_info(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUnsizedI
debug!("visit_implementation_of_coerce_unsized: {:?} -> {:?} (free)", source, target); debug!("visit_implementation_of_coerce_unsized: {:?} -> {:?} (free)", source, target);
tcx.infer_ctxt().enter(|infcx| { tcx.infer_ctxt().enter(|infcx| {
let impl_hir_id = tcx.hir().local_def_id_to_hir_id(impl_did);
let cause = ObligationCause::misc(span, impl_hir_id); let cause = ObligationCause::misc(span, impl_hir_id);
let check_mutbl = |mt_a: ty::TypeAndMut<'tcx>, let check_mutbl = |mt_a: ty::TypeAndMut<'tcx>,
mt_b: ty::TypeAndMut<'tcx>, mt_b: ty::TypeAndMut<'tcx>,
@ -452,13 +452,13 @@ pub fn coerce_unsized_info(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUnsizedI
.emit(); .emit();
return err_info; return err_info;
} else if diff_fields.len() > 1 { } else if diff_fields.len() > 1 {
let item = tcx.hir().expect_item(impl_hir_id); let item = tcx.hir().expect_item(impl_did);
let span = if let ItemKind::Impl(hir::Impl { of_trait: Some(ref t), .. }) = let span = if let ItemKind::Impl(hir::Impl { of_trait: Some(ref t), .. }) =
item.kind item.kind
{ {
t.path.span t.path.span
} else { } else {
tcx.hir().span(impl_hir_id) tcx.def_span(impl_did)
}; };
struct_span_err!( struct_span_err!(
@ -530,7 +530,11 @@ pub fn coerce_unsized_info(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUnsizedI
// Finally, resolve all regions. // Finally, resolve all regions.
let outlives_env = OutlivesEnvironment::new(param_env); let outlives_env = OutlivesEnvironment::new(param_env);
infcx.resolve_regions_and_report_errors(impl_did, &outlives_env, RegionckMode::default()); infcx.resolve_regions_and_report_errors(
impl_did.to_def_id(),
&outlives_env,
RegionckMode::default(),
);
CoerceUnsizedInfo { custom_kind: kind } CoerceUnsizedInfo { custom_kind: kind }
}) })

View file

@ -431,7 +431,7 @@ impl AstConv<'tcx> for ItemCtxt<'tcx> {
match self.node() { match self.node() {
hir::Node::Field(_) | hir::Node::Ctor(_) | hir::Node::Variant(_) => { hir::Node::Field(_) | hir::Node::Ctor(_) | hir::Node::Variant(_) => {
let item = let item =
self.tcx.hir().expect_item(self.tcx.hir().get_parent_item(self.hir_id())); self.tcx.hir().expect_item(self.tcx.hir().get_parent_did(self.hir_id()));
match &item.kind { match &item.kind {
hir::ItemKind::Enum(_, generics) hir::ItemKind::Enum(_, generics)
| hir::ItemKind::Struct(_, generics) | hir::ItemKind::Struct(_, generics)
@ -1184,8 +1184,7 @@ fn super_predicates_that_define_assoc_type(
} }
fn trait_def(tcx: TyCtxt<'_>, def_id: DefId) -> ty::TraitDef { fn trait_def(tcx: TyCtxt<'_>, def_id: DefId) -> ty::TraitDef {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); let item = tcx.hir().expect_item(def_id.expect_local());
let item = tcx.hir().expect_item(hir_id);
let (is_auto, unsafety) = match item.kind { let (is_auto, unsafety) = match item.kind {
hir::ItemKind::Trait(is_auto, unsafety, ..) => (is_auto == hir::IsAuto::Yes, unsafety), hir::ItemKind::Trait(is_auto, unsafety, ..) => (is_auto == hir::IsAuto::Yes, unsafety),
@ -1880,9 +1879,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
fn impl_trait_ref(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::TraitRef<'_>> { fn impl_trait_ref(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::TraitRef<'_>> {
let icx = ItemCtxt::new(tcx, def_id); let icx = ItemCtxt::new(tcx, def_id);
match tcx.hir().expect_item(def_id.expect_local()).kind {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
match tcx.hir().expect_item(hir_id).kind {
hir::ItemKind::Impl(ref impl_) => impl_.of_trait.as_ref().map(|ast_trait_ref| { hir::ItemKind::Impl(ref impl_) => impl_.of_trait.as_ref().map(|ast_trait_ref| {
let selfty = tcx.type_of(def_id); let selfty = tcx.type_of(def_id);
<dyn AstConv<'_>>::instantiate_mono_trait_ref(&icx, ast_trait_ref, selfty) <dyn AstConv<'_>>::instantiate_mono_trait_ref(&icx, ast_trait_ref, selfty)
@ -1892,9 +1889,8 @@ fn impl_trait_ref(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::TraitRef<'_>> {
} }
fn impl_polarity(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ImplPolarity { fn impl_polarity(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ImplPolarity {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
let is_rustc_reservation = tcx.has_attr(def_id, sym::rustc_reservation_impl); let is_rustc_reservation = tcx.has_attr(def_id, sym::rustc_reservation_impl);
let item = tcx.hir().expect_item(hir_id); let item = tcx.hir().expect_item(def_id.expect_local());
match &item.kind { match &item.kind {
hir::ItemKind::Impl(hir::Impl { hir::ItemKind::Impl(hir::Impl {
polarity: hir::ImplPolarity::Negative(span), polarity: hir::ImplPolarity::Negative(span),
@ -3225,7 +3221,7 @@ fn check_target_feature_trait_unsafe(tcx: TyCtxt<'_>, id: LocalDefId, attr_span:
let hir_id = tcx.hir().local_def_id_to_hir_id(id); let hir_id = tcx.hir().local_def_id_to_hir_id(id);
let node = tcx.hir().get(hir_id); let node = tcx.hir().get(hir_id);
if let Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }) = node { if let Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }) = node {
let parent_id = tcx.hir().get_parent_item(hir_id); let parent_id = tcx.hir().get_parent_did(hir_id);
let parent_item = tcx.hir().expect_item(parent_id); let parent_item = tcx.hir().expect_item(parent_id);
if let hir::ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) = parent_item.kind { if let hir::ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) = parent_item.kind {
tcx.sess tcx.sess

View file

@ -359,13 +359,10 @@ crate fn build_impl(
} }
let impl_item = match did.as_local() { let impl_item = match did.as_local() {
Some(did) => { Some(did) => match &tcx.hir().expect_item(did).kind {
let hir_id = tcx.hir().local_def_id_to_hir_id(did);
match &tcx.hir().expect_item(hir_id).kind {
hir::ItemKind::Impl(impl_) => Some(impl_), hir::ItemKind::Impl(impl_) => Some(impl_),
_ => panic!("`DefID` passed to `build_impl` is not an `impl"), _ => panic!("`DefID` passed to `build_impl` is not an `impl"),
} },
}
None => None, None => None,
}; };

View file

@ -950,7 +950,7 @@ impl Clean<Item> for hir::ImplItem<'_> {
let what_rustc_thinks = let what_rustc_thinks =
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx); Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx);
let parent_item = cx.tcx.hir().expect_item(cx.tcx.hir().get_parent_item(self.hir_id())); let parent_item = cx.tcx.hir().expect_item(cx.tcx.hir().get_parent_did(self.hir_id()));
if let hir::ItemKind::Impl(impl_) = &parent_item.kind { if let hir::ItemKind::Impl(impl_) = &parent_item.kind {
if impl_.of_trait.is_some() { if impl_.of_trait.is_some() {
// Trait impl items always inherit the impl's visibility -- // Trait impl items always inherit the impl's visibility --
@ -1189,9 +1189,8 @@ fn maybe_expand_private_type_alias(cx: &mut DocContext<'_>, path: &hir::Path<'_>
let Res::Def(DefKind::TyAlias, def_id) = path.res else { return None }; let Res::Def(DefKind::TyAlias, def_id) = path.res else { return None };
// Substitute private type aliases // Substitute private type aliases
let Some(def_id) = def_id.as_local() else { return None }; let Some(def_id) = def_id.as_local() else { return None };
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id);
let alias = if !cx.cache.access_levels.is_exported(def_id.to_def_id()) { let alias = if !cx.cache.access_levels.is_exported(def_id.to_def_id()) {
&cx.tcx.hir().expect_item(hir_id).kind &cx.tcx.hir().expect_item(def_id).kind
} else { } else {
return None; return None;
}; };

View file

@ -117,8 +117,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
if let Some(local_def_id) = def_id.as_local() { if let Some(local_def_id) = def_id.as_local() {
if self.cx.tcx.has_attr(def_id, sym::macro_export) { if self.cx.tcx.has_attr(def_id, sym::macro_export) {
if inserted.insert(def_id) { if inserted.insert(def_id) {
let hir_id = self.cx.tcx.hir().local_def_id_to_hir_id(local_def_id); let item = self.cx.tcx.hir().expect_item(local_def_id);
let item = self.cx.tcx.hir().expect_item(hir_id);
top_level_module.items.push((item, None)); top_level_module.items.push((item, None));
} }
} }

View file

@ -7,8 +7,8 @@ extern crate rustc_hir;
extern crate rustc_lint; extern crate rustc_lint;
#[macro_use] #[macro_use]
extern crate rustc_session; extern crate rustc_session;
extern crate rustc_span;
extern crate rustc_ast; extern crate rustc_ast;
extern crate rustc_span;
use rustc_ast_pretty::pprust; use rustc_ast_pretty::pprust;
use rustc_driver::plugin::Registry; use rustc_driver::plugin::Registry;
@ -44,7 +44,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingAllowedAttrPass {
) { ) {
let item = match cx.tcx.hir().get(id) { let item = match cx.tcx.hir().get(id) {
Node::Item(item) => item, Node::Item(item) => item,
_ => cx.tcx.hir().expect_item(cx.tcx.hir().get_parent_item(id)), _ => cx.tcx.hir().expect_item(cx.tcx.hir().get_parent_item(id).expect_owner()),
}; };
let allowed = |attr| pprust::attribute_to_string(attr).contains("allowed_attr"); let allowed = |attr| pprust::attribute_to_string(attr).contains("allowed_attr");

View file

@ -3,7 +3,6 @@ use clippy_utils::paths;
use clippy_utils::ty::{implements_trait, is_copy}; use clippy_utils::ty::{implements_trait, is_copy};
use clippy_utils::{get_trait_def_id, is_automatically_derived, is_lint_allowed, match_def_path}; use clippy_utils::{get_trait_def_id, is_automatically_derived, is_lint_allowed, match_def_path};
use if_chain::if_chain; use if_chain::if_chain;
use rustc_hir::def_id::DefId;
use rustc_hir::intravisit::{walk_expr, walk_fn, walk_item, FnKind, NestedVisitorMap, Visitor}; use rustc_hir::intravisit::{walk_expr, walk_fn, walk_item, FnKind, NestedVisitorMap, Visitor};
use rustc_hir::{ use rustc_hir::{
BlockCheckMode, BodyId, Expr, ExprKind, FnDecl, HirId, Impl, Item, ItemKind, TraitRef, UnsafeSource, Unsafety, BlockCheckMode, BodyId, Expr, ExprKind, FnDecl, HirId, Impl, Item, ItemKind, TraitRef, UnsafeSource, Unsafety,
@ -343,11 +342,6 @@ fn check_unsafe_derive_deserialize<'tcx>(
trait_ref: &TraitRef<'_>, trait_ref: &TraitRef<'_>,
ty: Ty<'tcx>, ty: Ty<'tcx>,
) { ) {
fn item_from_def_id<'tcx>(cx: &LateContext<'tcx>, def_id: DefId) -> &'tcx Item<'tcx> {
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
cx.tcx.hir().expect_item(hir_id)
}
fn has_unsafe<'tcx>(cx: &LateContext<'tcx>, item: &'tcx Item<'_>) -> bool { fn has_unsafe<'tcx>(cx: &LateContext<'tcx>, item: &'tcx Item<'_>) -> bool {
let mut visitor = UnsafeVisitor { cx, has_unsafe: false }; let mut visitor = UnsafeVisitor { cx, has_unsafe: false };
walk_item(&mut visitor, item); walk_item(&mut visitor, item);
@ -363,7 +357,7 @@ fn check_unsafe_derive_deserialize<'tcx>(
if !is_lint_allowed(cx, UNSAFE_DERIVE_DESERIALIZE, adt_hir_id); if !is_lint_allowed(cx, UNSAFE_DERIVE_DESERIALIZE, adt_hir_id);
if cx.tcx.inherent_impls(def.did) if cx.tcx.inherent_impls(def.did)
.iter() .iter()
.map(|imp_did| item_from_def_id(cx, *imp_did)) .map(|imp_did| cx.tcx.hir().expect_item(imp_did.expect_local()))
.any(|imp| has_unsafe(cx, imp)); .any(|imp| has_unsafe(cx, imp));
then { then {
span_lint_and_help( span_lint_and_help(

View file

@ -1939,7 +1939,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
return; return;
} }
let name = impl_item.ident.name.as_str(); let name = impl_item.ident.name.as_str();
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id()); let parent = cx.tcx.hir().get_parent_did(impl_item.hir_id());
let item = cx.tcx.hir().expect_item(parent); let item = cx.tcx.hir().expect_item(parent);
let self_ty = cx.tcx.type_of(item.def_id); let self_ty = cx.tcx.type_of(item.def_id);

View file

@ -279,8 +279,8 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst {
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>) { fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>) {
if let ImplItemKind::Const(hir_ty, body_id) = &impl_item.kind { if let ImplItemKind::Const(hir_ty, body_id) = &impl_item.kind {
let item_hir_id = cx.tcx.hir().get_parent_node(impl_item.hir_id()); let item_def_id = cx.tcx.hir().get_parent_did(impl_item.hir_id());
let item = cx.tcx.hir().expect_item(item_hir_id); let item = cx.tcx.hir().expect_item(item_def_id);
match &item.kind { match &item.kind {
ItemKind::Impl(Impl { ItemKind::Impl(Impl {

View file

@ -50,7 +50,7 @@ impl<'tcx> LateLintPass<'tcx> for SelfNamedConstructors {
_ => return, _ => return,
} }
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id()); let parent = cx.tcx.hir().get_parent_did(impl_item.hir_id());
let item = cx.tcx.hir().expect_item(parent); let item = cx.tcx.hir().expect_item(parent);
let self_ty = cx.tcx.type_of(item.def_id); let self_ty = cx.tcx.type_of(item.def_id);
let ret_ty = return_ty(cx, impl_item.hir_id()); let ret_ty = return_ty(cx, impl_item.hir_id());

View file

@ -41,7 +41,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedSelf {
if impl_item.span.from_expansion() { if impl_item.span.from_expansion() {
return; return;
} }
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id()); let parent = cx.tcx.hir().get_parent_did(impl_item.hir_id());
let parent_item = cx.tcx.hir().expect_item(parent); let parent_item = cx.tcx.hir().expect_item(parent);
let assoc_item = cx.tcx.associated_item(impl_item.def_id); let assoc_item = cx.tcx.associated_item(impl_item.def_id);
if_chain! { if_chain! {