Auto merge of #107206 - cjgillot:no-h2l-map, r=WaffleLapkin

Remove HirId -> LocalDefId map from HIR.

Having this map in HIR prevents the creating of new definitions after HIR has been built.
Thankfully, we do not need it.

Based on https://github.com/rust-lang/rust/pull/103902
This commit is contained in:
bors 2023-01-28 16:11:33 +00:00
commit d6f0642827
91 changed files with 557 additions and 592 deletions

View file

@ -275,19 +275,6 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
}); });
} }
fn visit_fn(
&mut self,
fk: intravisit::FnKind<'hir>,
fd: &'hir FnDecl<'hir>,
b: BodyId,
_: Span,
id: HirId,
) {
assert_eq!(self.owner, id.owner);
assert_eq!(self.parent_node, id.local_id);
intravisit::walk_fn(self, fk, fd, b, id);
}
fn visit_block(&mut self, block: &'hir Block<'hir>) { fn visit_block(&mut self, block: &'hir Block<'hir>) {
self.insert(block.span, block.hir_id, Node::Block(block)); self.insert(block.span, block.hir_id, Node::Block(block));
self.with_parent(block.hir_id, |this| { self.with_parent(block.hir_id, |this| {

View file

@ -67,7 +67,6 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
current_hir_id_owner: hir::CRATE_OWNER_ID, current_hir_id_owner: hir::CRATE_OWNER_ID,
item_local_id_counter: hir::ItemLocalId::new(0), item_local_id_counter: hir::ItemLocalId::new(0),
node_id_to_local_id: Default::default(), node_id_to_local_id: Default::default(),
local_id_to_def_id: SortedMap::new(),
trait_map: Default::default(), trait_map: Default::default(),
// Lowering state. // Lowering state.

View file

@ -119,7 +119,6 @@ struct LoweringContext<'a, 'hir> {
current_hir_id_owner: hir::OwnerId, current_hir_id_owner: hir::OwnerId,
item_local_id_counter: hir::ItemLocalId, item_local_id_counter: hir::ItemLocalId,
local_id_to_def_id: SortedMap<ItemLocalId, LocalDefId>,
trait_map: FxHashMap<ItemLocalId, Box<[TraitCandidate]>>, trait_map: FxHashMap<ItemLocalId, Box<[TraitCandidate]>>,
impl_trait_defs: Vec<hir::GenericParam<'hir>>, impl_trait_defs: Vec<hir::GenericParam<'hir>>,
@ -567,7 +566,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let current_attrs = std::mem::take(&mut self.attrs); let current_attrs = std::mem::take(&mut self.attrs);
let current_bodies = std::mem::take(&mut self.bodies); let current_bodies = std::mem::take(&mut self.bodies);
let current_node_ids = std::mem::take(&mut self.node_id_to_local_id); let current_node_ids = std::mem::take(&mut self.node_id_to_local_id);
let current_id_to_def_id = std::mem::take(&mut self.local_id_to_def_id);
let current_trait_map = std::mem::take(&mut self.trait_map); let current_trait_map = std::mem::take(&mut self.trait_map);
let current_owner = let current_owner =
std::mem::replace(&mut self.current_hir_id_owner, hir::OwnerId { def_id }); std::mem::replace(&mut self.current_hir_id_owner, hir::OwnerId { def_id });
@ -594,7 +592,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
self.attrs = current_attrs; self.attrs = current_attrs;
self.bodies = current_bodies; self.bodies = current_bodies;
self.node_id_to_local_id = current_node_ids; self.node_id_to_local_id = current_node_ids;
self.local_id_to_def_id = current_id_to_def_id;
self.trait_map = current_trait_map; self.trait_map = current_trait_map;
self.current_hir_id_owner = current_owner; self.current_hir_id_owner = current_owner;
self.item_local_id_counter = current_local_counter; self.item_local_id_counter = current_local_counter;
@ -629,7 +626,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
fn make_owner_info(&mut self, node: hir::OwnerNode<'hir>) -> &'hir hir::OwnerInfo<'hir> { fn make_owner_info(&mut self, node: hir::OwnerNode<'hir>) -> &'hir hir::OwnerInfo<'hir> {
let attrs = std::mem::take(&mut self.attrs); let attrs = std::mem::take(&mut self.attrs);
let mut bodies = std::mem::take(&mut self.bodies); let mut bodies = std::mem::take(&mut self.bodies);
let local_id_to_def_id = std::mem::take(&mut self.local_id_to_def_id);
let trait_map = std::mem::take(&mut self.trait_map); let trait_map = std::mem::take(&mut self.trait_map);
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
@ -645,13 +641,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
let (hash_including_bodies, hash_without_bodies) = self.hash_owner(node, &bodies); let (hash_including_bodies, hash_without_bodies) = self.hash_owner(node, &bodies);
let (nodes, parenting) = let (nodes, parenting) =
index::index_hir(self.tcx.sess, &*self.tcx.definitions_untracked(), node, &bodies); index::index_hir(self.tcx.sess, &*self.tcx.definitions_untracked(), node, &bodies);
let nodes = hir::OwnerNodes { let nodes = hir::OwnerNodes { hash_including_bodies, hash_without_bodies, nodes, bodies };
hash_including_bodies,
hash_without_bodies,
nodes,
bodies,
local_id_to_def_id,
};
let attrs = { let attrs = {
let hash = self.tcx.with_stable_hashing_context(|mut hcx| { let hash = self.tcx.with_stable_hashing_context(|mut hcx| {
let mut stable_hasher = StableHasher::new(); let mut stable_hasher = StableHasher::new();
@ -710,7 +700,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
assert_ne!(local_id, hir::ItemLocalId::new(0)); assert_ne!(local_id, hir::ItemLocalId::new(0));
if let Some(def_id) = self.opt_local_def_id(ast_node_id) { if let Some(def_id) = self.opt_local_def_id(ast_node_id) {
self.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id))); self.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
self.local_id_to_def_id.insert(local_id, def_id);
} }
if let Some(traits) = self.resolver.trait_map.remove(&ast_node_id) { if let Some(traits) = self.resolver.trait_map.remove(&ast_node_id) {

View file

@ -831,8 +831,6 @@ pub struct OwnerNodes<'tcx> {
pub nodes: IndexVec<ItemLocalId, Option<ParentedNode<'tcx>>>, pub nodes: IndexVec<ItemLocalId, Option<ParentedNode<'tcx>>>,
/// Content of local bodies. /// Content of local bodies.
pub bodies: SortedMap<ItemLocalId, &'tcx Body<'tcx>>, pub bodies: SortedMap<ItemLocalId, &'tcx Body<'tcx>>,
/// Non-owning definitions contained in this owner.
pub local_id_to_def_id: SortedMap<ItemLocalId, LocalDefId>,
} }
impl<'tcx> OwnerNodes<'tcx> { impl<'tcx> OwnerNodes<'tcx> {
@ -862,7 +860,6 @@ impl fmt::Debug for OwnerNodes<'_> {
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
) )
.field("bodies", &self.bodies) .field("bodies", &self.bodies)
.field("local_id_to_def_id", &self.local_id_to_def_id)
.field("hash_without_bodies", &self.hash_without_bodies) .field("hash_without_bodies", &self.hash_without_bodies)
.field("hash_including_bodies", &self.hash_including_bodies) .field("hash_including_bodies", &self.hash_including_bodies)
.finish() .finish()

View file

@ -67,6 +67,7 @@
use crate::hir::*; use crate::hir::*;
use rustc_ast::walk_list; use rustc_ast::walk_list;
use rustc_ast::{Attribute, Label}; use rustc_ast::{Attribute, Label};
use rustc_span::def_id::LocalDefId;
use rustc_span::symbol::{Ident, Symbol}; use rustc_span::symbol::{Ident, Symbol};
use rustc_span::Span; use rustc_span::Span;
@ -364,7 +365,7 @@ pub trait Visitor<'v>: Sized {
fn visit_fn_decl(&mut self, fd: &'v FnDecl<'v>) { fn visit_fn_decl(&mut self, fd: &'v FnDecl<'v>) {
walk_fn_decl(self, fd) walk_fn_decl(self, fd)
} }
fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl<'v>, b: BodyId, _: Span, id: HirId) { fn visit_fn(&mut self, fk: FnKind<'v>, fd: &'v FnDecl<'v>, b: BodyId, _: Span, id: LocalDefId) {
walk_fn(self, fk, fd, b, id) walk_fn(self, fk, fd, b, id)
} }
fn visit_use(&mut self, path: &'v UsePath<'v>, hir_id: HirId) { fn visit_use(&mut self, path: &'v UsePath<'v>, hir_id: HirId) {
@ -468,13 +469,16 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
visitor.visit_ty(typ); visitor.visit_ty(typ);
visitor.visit_nested_body(body); visitor.visit_nested_body(body);
} }
ItemKind::Fn(ref sig, ref generics, body_id) => visitor.visit_fn( ItemKind::Fn(ref sig, ref generics, body_id) => {
FnKind::ItemFn(item.ident, generics, sig.header), visitor.visit_id(item.hir_id());
sig.decl, visitor.visit_fn(
body_id, FnKind::ItemFn(item.ident, generics, sig.header),
item.span, sig.decl,
item.hir_id(), body_id,
), item.span,
item.owner_id.def_id,
)
}
ItemKind::Macro(..) => { ItemKind::Macro(..) => {
visitor.visit_id(item.hir_id()); visitor.visit_id(item.hir_id());
} }
@ -733,7 +737,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
walk_list!(visitor, visit_arm, arms); walk_list!(visitor, visit_arm, arms);
} }
ExprKind::Closure(&Closure { ExprKind::Closure(&Closure {
def_id: _, def_id,
binder: _, binder: _,
bound_generic_params, bound_generic_params,
fn_decl, fn_decl,
@ -745,7 +749,7 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
constness: _, constness: _,
}) => { }) => {
walk_list!(visitor, visit_generic_param, bound_generic_params); walk_list!(visitor, visit_generic_param, bound_generic_params);
visitor.visit_fn(FnKind::Closure, fn_decl, body, expression.span, expression.hir_id) visitor.visit_fn(FnKind::Closure, fn_decl, body, expression.span, def_id)
} }
ExprKind::Block(ref block, ref opt_label) => { ExprKind::Block(ref block, ref opt_label) => {
walk_list!(visitor, visit_label, opt_label); walk_list!(visitor, visit_label, opt_label);
@ -923,9 +927,8 @@ pub fn walk_fn<'v, V: Visitor<'v>>(
function_kind: FnKind<'v>, function_kind: FnKind<'v>,
function_declaration: &'v FnDecl<'v>, function_declaration: &'v FnDecl<'v>,
body_id: BodyId, body_id: BodyId,
id: HirId, _: LocalDefId,
) { ) {
visitor.visit_id(id);
visitor.visit_fn_decl(function_declaration); visitor.visit_fn_decl(function_declaration);
walk_fn_kind(visitor, function_kind); walk_fn_kind(visitor, function_kind);
visitor.visit_nested_body(body_id) visitor.visit_nested_body(body_id)
@ -953,26 +956,30 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v Trai
let TraitItem { ident, generics, ref defaultness, ref kind, span, owner_id: _ } = *trait_item; let TraitItem { ident, generics, ref defaultness, ref kind, span, owner_id: _ } = *trait_item;
let hir_id = trait_item.hir_id(); let hir_id = trait_item.hir_id();
visitor.visit_ident(ident); visitor.visit_ident(ident);
visitor.visit_generics(generics); visitor.visit_generics(&generics);
visitor.visit_defaultness(defaultness); visitor.visit_defaultness(&defaultness);
visitor.visit_id(hir_id);
match *kind { match *kind {
TraitItemKind::Const(ref ty, default) => { TraitItemKind::Const(ref ty, default) => {
visitor.visit_id(hir_id);
visitor.visit_ty(ty); visitor.visit_ty(ty);
walk_list!(visitor, visit_nested_body, default); walk_list!(visitor, visit_nested_body, default);
} }
TraitItemKind::Fn(ref sig, TraitFn::Required(param_names)) => { TraitItemKind::Fn(ref sig, TraitFn::Required(param_names)) => {
visitor.visit_id(hir_id);
visitor.visit_fn_decl(sig.decl); visitor.visit_fn_decl(sig.decl);
for &param_name in param_names { for &param_name in param_names {
visitor.visit_ident(param_name); visitor.visit_ident(param_name);
} }
} }
TraitItemKind::Fn(ref sig, TraitFn::Provided(body_id)) => { TraitItemKind::Fn(ref sig, TraitFn::Provided(body_id)) => {
visitor.visit_fn(FnKind::Method(ident, sig), sig.decl, body_id, span, hir_id); visitor.visit_fn(
FnKind::Method(ident, sig),
sig.decl,
body_id,
span,
trait_item.owner_id.def_id,
);
} }
TraitItemKind::Type(bounds, ref default) => { TraitItemKind::Type(bounds, ref default) => {
visitor.visit_id(hir_id);
walk_list!(visitor, visit_param_bound, bounds); walk_list!(visitor, visit_param_bound, bounds);
walk_list!(visitor, visit_ty, default); walk_list!(visitor, visit_ty, default);
} }
@ -1002,9 +1009,9 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt
visitor.visit_ident(ident); visitor.visit_ident(ident);
visitor.visit_generics(generics); visitor.visit_generics(generics);
visitor.visit_defaultness(defaultness); visitor.visit_defaultness(defaultness);
visitor.visit_id(impl_item.hir_id());
match *kind { match *kind {
ImplItemKind::Const(ref ty, body) => { ImplItemKind::Const(ref ty, body) => {
visitor.visit_id(impl_item.hir_id());
visitor.visit_ty(ty); visitor.visit_ty(ty);
visitor.visit_nested_body(body); visitor.visit_nested_body(body);
} }
@ -1014,11 +1021,10 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt
sig.decl, sig.decl,
body_id, body_id,
impl_item.span, impl_item.span,
impl_item.hir_id(), impl_item.owner_id.def_id,
); );
} }
ImplItemKind::Type(ref ty) => { ImplItemKind::Type(ref ty) => {
visitor.visit_id(impl_item.hir_id());
visitor.visit_ty(ty); visitor.visit_ty(ty);
} }
} }

View file

@ -100,13 +100,8 @@ impl<'tcx, HirCtx: crate::HashStableContext> HashStable<HirCtx> for OwnerNodes<'
// `local_id_to_def_id` is also ignored because is dependent on the body, then just hashing // `local_id_to_def_id` is also ignored because is dependent on the body, then just hashing
// the body satisfies the condition of two nodes being different have different // the body satisfies the condition of two nodes being different have different
// `hash_stable` results. // `hash_stable` results.
let OwnerNodes { let OwnerNodes { hash_including_bodies, hash_without_bodies: _, nodes: _, bodies: _ } =
hash_including_bodies, *self;
hash_without_bodies: _,
nodes: _,
bodies: _,
local_id_to_def_id: _,
} = *self;
hash_including_bodies.hash_stable(hcx, hasher); hash_including_bodies.hash_stable(hcx, hasher);
} }
} }

View file

@ -665,7 +665,7 @@ fn check_item_type(tcx: TyCtxt<'_>, id: hir::ItemId) {
DefKind::GlobalAsm => { DefKind::GlobalAsm => {
let it = tcx.hir().item(id); let it = tcx.hir().item(id);
let hir::ItemKind::GlobalAsm(asm) = it.kind else { span_bug!(it.span, "DefKind::GlobalAsm but got {:#?}", it) }; let hir::ItemKind::GlobalAsm(asm) = it.kind else { span_bug!(it.span, "DefKind::GlobalAsm but got {:#?}", it) };
InlineAsmCtxt::new_global_asm(tcx).check_asm(asm, id.hir_id()); InlineAsmCtxt::new_global_asm(tcx).check_asm(asm, id.owner_id.def_id);
} }
_ => {} _ => {}
} }

View file

@ -3,6 +3,7 @@ use rustc_data_structures::fx::FxHashSet;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_middle::ty::{self, Article, FloatTy, IntTy, Ty, TyCtxt, TypeVisitable, UintTy}; use rustc_middle::ty::{self, Article, FloatTy, IntTy, Ty, TyCtxt, TypeVisitable, UintTy};
use rustc_session::lint; use rustc_session::lint;
use rustc_span::def_id::LocalDefId;
use rustc_span::{Symbol, DUMMY_SP}; use rustc_span::{Symbol, DUMMY_SP};
use rustc_target::asm::{InlineAsmReg, InlineAsmRegClass, InlineAsmRegOrRegClass, InlineAsmType}; use rustc_target::asm::{InlineAsmReg, InlineAsmRegClass, InlineAsmRegOrRegClass, InlineAsmType};
@ -253,10 +254,8 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
Some(asm_ty) Some(asm_ty)
} }
pub fn check_asm(&self, asm: &hir::InlineAsm<'tcx>, enclosing_id: hir::HirId) { pub fn check_asm(&self, asm: &hir::InlineAsm<'tcx>, enclosing_id: LocalDefId) {
let hir = self.tcx.hir(); let target_features = self.tcx.asm_target_features(enclosing_id.to_def_id());
let enclosing_def_id = hir.local_def_id(enclosing_id).to_def_id();
let target_features = self.tcx.asm_target_features(enclosing_def_id);
let Some(asm_arch) = self.tcx.sess.asm_arch else { let Some(asm_arch) = self.tcx.sess.asm_arch else {
self.tcx.sess.delay_span_bug(DUMMY_SP, "target architecture does not support asm"); self.tcx.sess.delay_span_bug(DUMMY_SP, "target architecture does not support asm");
return; return;

View file

@ -391,7 +391,7 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, associated_items: &[hir::TraitItemRe
gather_gat_bounds( gather_gat_bounds(
tcx, tcx,
param_env, param_env,
item_def_id.def_id, item_def_id,
sig.inputs_and_output, sig.inputs_and_output,
// We also assume that all of the function signature's parameter types // We also assume that all of the function signature's parameter types
// are well formed. // are well formed.
@ -413,7 +413,7 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, associated_items: &[hir::TraitItemRe
gather_gat_bounds( gather_gat_bounds(
tcx, tcx,
param_env, param_env,
item_def_id.def_id, item_def_id,
tcx.explicit_item_bounds(item_def_id).to_vec(), tcx.explicit_item_bounds(item_def_id).to_vec(),
&FxIndexSet::default(), &FxIndexSet::default(),
gat_def_id.def_id, gat_def_id.def_id,
@ -563,7 +563,7 @@ fn augment_param_env<'tcx>(
fn gather_gat_bounds<'tcx, T: TypeFoldable<'tcx>>( fn gather_gat_bounds<'tcx, T: TypeFoldable<'tcx>>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>, param_env: ty::ParamEnv<'tcx>,
item_def_id: LocalDefId, item_def_id: hir::OwnerId,
to_check: T, to_check: T,
wf_tys: &FxIndexSet<Ty<'tcx>>, wf_tys: &FxIndexSet<Ty<'tcx>>,
gat_def_id: LocalDefId, gat_def_id: LocalDefId,
@ -596,7 +596,7 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<'tcx>>(
// reflected in a where clause on the GAT itself. // reflected in a where clause on the GAT itself.
for (ty, ty_idx) in &types { for (ty, ty_idx) in &types {
// In our example, requires that `Self: 'a` // In our example, requires that `Self: 'a`
if ty_known_to_outlive(tcx, item_def_id, param_env, &wf_tys, *ty, *region_a) { if ty_known_to_outlive(tcx, item_def_id.def_id, param_env, &wf_tys, *ty, *region_a) {
debug!(?ty_idx, ?region_a_idx); debug!(?ty_idx, ?region_a_idx);
debug!("required clause: {ty} must outlive {region_a}"); debug!("required clause: {ty} must outlive {region_a}");
// Translate into the generic parameters of the GAT. In // Translate into the generic parameters of the GAT. In
@ -634,7 +634,14 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<'tcx>>(
if ty::ReStatic == **region_b || region_a == region_b { if ty::ReStatic == **region_b || region_a == region_b {
continue; continue;
} }
if region_known_to_outlive(tcx, item_def_id, param_env, &wf_tys, *region_a, *region_b) { if region_known_to_outlive(
tcx,
item_def_id.def_id,
param_env,
&wf_tys,
*region_a,
*region_b,
) {
debug!(?region_a_idx, ?region_b_idx); debug!(?region_a_idx, ?region_b_idx);
debug!("required clause: {region_a} must outlive {region_b}"); debug!("required clause: {region_a} must outlive {region_b}");
// Translate into the generic parameters of the GAT. // Translate into the generic parameters of the GAT.

View file

@ -829,7 +829,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
fd: &'tcx hir::FnDecl<'tcx>, fd: &'tcx hir::FnDecl<'tcx>,
body_id: hir::BodyId, body_id: hir::BodyId,
_: Span, _: Span,
_: hir::HirId, _: LocalDefId,
) { ) {
let output = match fd.output { let output = match fd.output {
hir::FnRetTy::DefaultReturn(_) => None, hir::FnRetTy::DefaultReturn(_) => None,
@ -1264,14 +1264,21 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
} else if let Some(body_id) = outermost_body { } else if let Some(body_id) = outermost_body {
let fn_id = self.tcx.hir().body_owner(body_id); let fn_id = self.tcx.hir().body_owner(body_id);
match self.tcx.hir().get(fn_id) { match self.tcx.hir().get(fn_id) {
Node::Item(hir::Item { kind: hir::ItemKind::Fn(..), .. }) Node::Item(hir::Item { owner_id, kind: hir::ItemKind::Fn(..), .. })
| Node::TraitItem(hir::TraitItem { | Node::TraitItem(hir::TraitItem {
kind: hir::TraitItemKind::Fn(..), .. owner_id,
kind: hir::TraitItemKind::Fn(..),
..
}) })
| Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }) | Node::ImplItem(hir::ImplItem {
| Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(..), .. }) => { owner_id,
let scope = self.tcx.hir().local_def_id(fn_id); kind: hir::ImplItemKind::Fn(..),
def = Region::Free(scope.to_def_id(), def.id().unwrap()); ..
}) => {
def = Region::Free(owner_id.to_def_id(), def.id().unwrap());
}
Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(closure), .. }) => {
def = Region::Free(closure.def_id.to_def_id(), def.id().unwrap());
} }
_ => {} _ => {}
} }
@ -1658,10 +1665,12 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
/// "Constrained" basically means that it appears in any type but /// "Constrained" basically means that it appears in any type but
/// not amongst the inputs to a projection. In other words, `<&'a /// not amongst the inputs to a projection. In other words, `<&'a
/// T as Trait<''b>>::Foo` does not constrain `'a` or `'b`. /// T as Trait<''b>>::Foo` does not constrain `'a` or `'b`.
fn is_late_bound_map(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<&FxIndexSet<LocalDefId>> { fn is_late_bound_map(
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); tcx: TyCtxt<'_>,
let decl = tcx.hir().fn_decl_by_hir_id(hir_id)?; owner_id: hir::OwnerId,
let generics = tcx.hir().get_generics(def_id)?; ) -> Option<&FxIndexSet<hir::ItemLocalId>> {
let decl = tcx.hir().fn_decl_by_hir_id(owner_id.into())?;
let generics = tcx.hir().get_generics(owner_id.def_id)?;
let mut late_bound = FxIndexSet::default(); let mut late_bound = FxIndexSet::default();
@ -1695,24 +1704,22 @@ fn is_late_bound_map(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<&FxIndexSet<
hir::GenericParamKind::Type { .. } | hir::GenericParamKind::Const { .. } => continue, hir::GenericParamKind::Type { .. } | hir::GenericParamKind::Const { .. } => continue,
} }
let param_def_id = tcx.hir().local_def_id(param.hir_id);
// appears in the where clauses? early-bound. // appears in the where clauses? early-bound.
if appears_in_where_clause.regions.contains(&param_def_id) { if appears_in_where_clause.regions.contains(&param.def_id) {
continue; continue;
} }
// does not appear in the inputs, but appears in the return type? early-bound. // does not appear in the inputs, but appears in the return type? early-bound.
if !constrained_by_input.regions.contains(&param_def_id) if !constrained_by_input.regions.contains(&param.def_id)
&& appears_in_output.regions.contains(&param_def_id) && appears_in_output.regions.contains(&param.def_id)
{ {
continue; continue;
} }
debug!("lifetime {:?} with id {:?} is late-bound", param.name.ident(), param.hir_id); debug!("lifetime {:?} with id {:?} is late-bound", param.name.ident(), param.def_id);
let inserted = late_bound.insert(param_def_id); let inserted = late_bound.insert(param.hir_id.local_id);
assert!(inserted, "visited lifetime {:?} twice", param.hir_id); assert!(inserted, "visited lifetime {:?} twice", param.def_id);
} }
debug!(?late_bound); debug!(?late_bound);

View file

@ -280,7 +280,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericP
} }
let hir::GenericParamKind::Lifetime { .. } = duplicate.kind else { continue }; let hir::GenericParamKind::Lifetime { .. } = duplicate.kind else { continue };
let dup_def = tcx.hir().local_def_id(duplicate.hir_id).to_def_id(); let dup_def = duplicate.def_id.to_def_id();
let Some(dup_index) = generics.param_def_id_to_index(tcx, dup_def) else { bug!() }; let Some(dup_index) = generics.param_def_id_to_index(tcx, dup_def) else { bug!() };

View file

@ -54,15 +54,14 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
// ty which is a fully resolved projection. // ty which is a fully resolved projection.
// For the code example above, this would mean converting Self::Assoc<3> // For the code example above, this would mean converting Self::Assoc<3>
// into a ty::Alias(ty::Projection, <Self as Foo>::Assoc<3>) // into a ty::Alias(ty::Projection, <Self as Foo>::Assoc<3>)
let item_hir_id = tcx let item_def_id = tcx
.hir() .hir()
.parent_iter(hir_id) .parent_owner_iter(hir_id)
.filter(|(_, node)| matches!(node, Node::Item(_))) .find(|(_, node)| matches!(node, OwnerNode::Item(_)))
.map(|(id, _)| id) .unwrap()
.next() .0
.unwrap(); .to_def_id();
let item_did = tcx.hir().local_def_id(item_hir_id).to_def_id(); let item_ctxt = &ItemCtxt::new(tcx, item_def_id) as &dyn crate::astconv::AstConv<'_>;
let item_ctxt = &ItemCtxt::new(tcx, item_did) as &dyn crate::astconv::AstConv<'_>;
let ty = item_ctxt.ast_ty_to_ty(hir_ty); let ty = item_ctxt.ast_ty_to_ty(hir_ty);
// Iterate through the generics of the projection to find the one that corresponds to // Iterate through the generics of the projection to find the one that corresponds to

View file

@ -186,6 +186,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
prior_arm: Option<(Option<hir::HirId>, Ty<'tcx>, Span)>, prior_arm: Option<(Option<hir::HirId>, Ty<'tcx>, Span)>,
) { ) {
let hir = self.tcx.hir(); let hir = self.tcx.hir();
// First, check that we're actually in the tail of a function. // First, check that we're actually in the tail of a function.
let Some(body_id) = hir.maybe_body_owned_by(self.body_id) else { return; }; let Some(body_id) = hir.maybe_body_owned_by(self.body_id) else { return; };
let body = hir.body(body_id); let body = hir.body(body_id);

View file

@ -130,7 +130,12 @@ pub(super) fn check_fn<'a, 'tcx>(
let gen_ty = if let (Some(_), Some(gen_kind)) = (can_be_generator, body.generator_kind) { let gen_ty = if let (Some(_), Some(gen_kind)) = (can_be_generator, body.generator_kind) {
let interior = fcx let interior = fcx
.next_ty_var(TypeVariableOrigin { kind: TypeVariableOriginKind::MiscVariable, span }); .next_ty_var(TypeVariableOrigin { kind: TypeVariableOriginKind::MiscVariable, span });
fcx.deferred_generator_interiors.borrow_mut().push((fn_id, body.id(), interior, gen_kind)); fcx.deferred_generator_interiors.borrow_mut().push((
fn_def_id,
body.id(),
interior,
gen_kind,
));
let (resume_ty, yield_ty) = fcx.resume_yield_tys.unwrap(); let (resume_ty, yield_ty) = fcx.resume_yield_tys.unwrap();
Some(GeneratorTypes { Some(GeneratorTypes {
@ -167,12 +172,12 @@ pub(super) fn check_fn<'a, 'tcx>(
// Check that a function marked as `#[panic_handler]` has signature `fn(&PanicInfo) -> !` // Check that a function marked as `#[panic_handler]` has signature `fn(&PanicInfo) -> !`
if let Some(panic_impl_did) = tcx.lang_items().panic_impl() if let Some(panic_impl_did) = tcx.lang_items().panic_impl()
&& panic_impl_did == hir.local_def_id(fn_id).to_def_id() && panic_impl_did == fn_def_id.to_def_id()
{ {
check_panic_info_fn(tcx, panic_impl_did.expect_local(), fn_sig, decl, declared_ret_ty); check_panic_info_fn(tcx, panic_impl_did.expect_local(), fn_sig, decl, declared_ret_ty);
} }
if let Some(lang_start_defid) = tcx.lang_items().start_fn() && lang_start_defid == hir.local_def_id(fn_id).to_def_id() { if let Some(lang_start_defid) = tcx.lang_items().start_fn() && lang_start_defid == fn_def_id.to_def_id() {
check_lang_start_fn(tcx, fn_sig, decl, fn_def_id); check_lang_start_fn(tcx, fn_sig, decl, fn_def_id);
} }

View file

@ -549,8 +549,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let generators = std::mem::take(&mut *self.deferred_generator_interiors.borrow_mut()); let generators = std::mem::take(&mut *self.deferred_generator_interiors.borrow_mut());
debug!(?generators); debug!(?generators);
for &(expr_hir_id, body_id, interior, _) in generators.iter() { for &(expr_def_id, body_id, interior, _) in generators.iter() {
let expr_def_id = self.tcx.hir().local_def_id(expr_hir_id);
debug!(?expr_def_id); debug!(?expr_def_id);
// Create the `GeneratorWitness` type that we will unify with `interior`. // Create the `GeneratorWitness` type that we will unify with `interior`.

View file

@ -79,7 +79,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} }
}; };
InlineAsmCtxt::new_in_fn(self.tcx, self.param_env, get_operand_ty) InlineAsmCtxt::new_in_fn(self.tcx, self.param_env, get_operand_ty)
.check_asm(asm, self.tcx.hir().local_def_id_to_hir_id(enclosing_id)); .check_asm(asm, enclosing_id);
} }
} }

View file

@ -5,6 +5,7 @@ use rustc_hir::PatKind;
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind}; use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use rustc_middle::ty::Ty; use rustc_middle::ty::Ty;
use rustc_middle::ty::UserType; use rustc_middle::ty::UserType;
use rustc_span::def_id::LocalDefId;
use rustc_span::Span; use rustc_span::Span;
use rustc_trait_selection::traits; use rustc_trait_selection::traits;
@ -156,7 +157,7 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherLocalsVisitor<'a, 'tcx> {
_: &'tcx hir::FnDecl<'tcx>, _: &'tcx hir::FnDecl<'tcx>,
_: hir::BodyId, _: hir::BodyId,
_: Span, _: Span,
_: hir::HirId, _: LocalDefId,
) { ) {
} }
} }

View file

@ -56,7 +56,7 @@ pub struct Inherited<'tcx> {
pub(super) deferred_asm_checks: RefCell<Vec<(&'tcx hir::InlineAsm<'tcx>, hir::HirId)>>, pub(super) deferred_asm_checks: RefCell<Vec<(&'tcx hir::InlineAsm<'tcx>, hir::HirId)>>,
pub(super) deferred_generator_interiors: pub(super) deferred_generator_interiors:
RefCell<Vec<(hir::HirId, hir::BodyId, Ty<'tcx>, hir::GeneratorKind)>>, RefCell<Vec<(LocalDefId, hir::BodyId, Ty<'tcx>, hir::GeneratorKind)>>,
pub(super) body_id: Option<hir::BodyId>, pub(super) body_id: Option<hir::BodyId>,

View file

@ -40,8 +40,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&self, &self,
body: &'tcx hir::Body<'tcx>, body: &'tcx hir::Body<'tcx>,
) -> &'tcx ty::TypeckResults<'tcx> { ) -> &'tcx ty::TypeckResults<'tcx> {
let item_id = self.tcx.hir().body_owner(body.id()); let item_def_id = self.tcx.hir().body_owner_def_id(body.id());
let item_def_id = self.tcx.hir().local_def_id(item_id);
// This attribute causes us to dump some writeback information // This attribute causes us to dump some writeback information
// in the form of errors, which is used for unit tests. // in the form of errors, which is used for unit tests.
@ -55,7 +54,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Type only exists for constants and statics, not functions. // Type only exists for constants and statics, not functions.
match self.tcx.hir().body_owner_kind(item_def_id) { match self.tcx.hir().body_owner_kind(item_def_id) {
hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_) => { hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_) => {
wbcx.visit_node_id(body.value.span, item_id); let item_hir_id = self.tcx.hir().local_def_id_to_hir_id(item_def_id);
wbcx.visit_node_id(body.value.span, item_hir_id);
} }
hir::BodyOwnerKind::Closure | hir::BodyOwnerKind::Fn => (), hir::BodyOwnerKind::Closure | hir::BodyOwnerKind::Fn => (),
} }

View file

@ -38,7 +38,7 @@ use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::graph::implementation::{Direction, NodeIndex, INCOMING, OUTGOING}; use rustc_data_structures::graph::implementation::{Direction, NodeIndex, INCOMING, OUTGOING};
use rustc_graphviz as dot; use rustc_graphviz as dot;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::intravisit::{self, Visitor};
use rustc_middle::dep_graph::{ use rustc_middle::dep_graph::{
DepGraphQuery, DepKind, DepNode, DepNodeExt, DepNodeFilter, EdgeFilter, DepGraphQuery, DepKind, DepNode, DepNodeExt, DepNodeFilter, EdgeFilter,
@ -74,7 +74,7 @@ pub fn assert_dep_graph(tcx: TyCtxt<'_>) {
let (if_this_changed, then_this_would_need) = { let (if_this_changed, then_this_would_need) = {
let mut visitor = let mut visitor =
IfThisChanged { tcx, if_this_changed: vec![], then_this_would_need: vec![] }; IfThisChanged { tcx, if_this_changed: vec![], then_this_would_need: vec![] };
visitor.process_attrs(hir::CRATE_HIR_ID); visitor.process_attrs(CRATE_DEF_ID);
tcx.hir().visit_all_item_likes_in_crate(&mut visitor); tcx.hir().visit_all_item_likes_in_crate(&mut visitor);
(visitor.if_this_changed, visitor.then_this_would_need) (visitor.if_this_changed, visitor.then_this_would_need)
}; };
@ -119,9 +119,9 @@ impl<'tcx> IfThisChanged<'tcx> {
value value
} }
fn process_attrs(&mut self, hir_id: hir::HirId) { fn process_attrs(&mut self, def_id: LocalDefId) {
let def_id = self.tcx.hir().local_def_id(hir_id);
let def_path_hash = self.tcx.def_path_hash(def_id.to_def_id()); let def_path_hash = self.tcx.def_path_hash(def_id.to_def_id());
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
let attrs = self.tcx.hir().attrs(hir_id); let attrs = self.tcx.hir().attrs(hir_id);
for attr in attrs { for attr in attrs {
if attr.has_name(sym::rustc_if_this_changed) { if attr.has_name(sym::rustc_if_this_changed) {
@ -180,22 +180,22 @@ impl<'tcx> Visitor<'tcx> for IfThisChanged<'tcx> {
} }
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
self.process_attrs(item.hir_id()); self.process_attrs(item.owner_id.def_id);
intravisit::walk_item(self, item); intravisit::walk_item(self, item);
} }
fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) { fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
self.process_attrs(trait_item.hir_id()); self.process_attrs(trait_item.owner_id.def_id);
intravisit::walk_trait_item(self, trait_item); intravisit::walk_trait_item(self, trait_item);
} }
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) { fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
self.process_attrs(impl_item.hir_id()); self.process_attrs(impl_item.owner_id.def_id);
intravisit::walk_impl_item(self, impl_item); intravisit::walk_impl_item(self, impl_item);
} }
fn visit_field_def(&mut self, s: &'tcx hir::FieldDef<'tcx>) { fn visit_field_def(&mut self, s: &'tcx hir::FieldDef<'tcx>) {
self.process_attrs(s.hir_id); self.process_attrs(s.def_id);
intravisit::walk_field_def(self, s); intravisit::walk_field_def(self, s);
} }
} }

View file

@ -56,9 +56,7 @@ use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res}; use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdSet, CRATE_DEF_ID}; use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdSet, CRATE_DEF_ID};
use rustc_hir::intravisit::FnKind as HirFnKind; use rustc_hir::intravisit::FnKind as HirFnKind;
use rustc_hir::{ use rustc_hir::{Body, FnDecl, ForeignItemKind, GenericParamKind, Node, PatKind, PredicateOrigin};
Body, FnDecl, ForeignItemKind, GenericParamKind, HirId, Node, PatKind, PredicateOrigin,
};
use rustc_index::vec::Idx; use rustc_index::vec::Idx;
use rustc_middle::lint::in_external_macro; use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::layout::{LayoutError, LayoutOf}; use rustc_middle::ty::layout::{LayoutError, LayoutOf};
@ -583,12 +581,13 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) { fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {
// If the method is an impl for a trait, don't doc. // If the method is an impl for a trait, don't doc.
if method_context(cx, impl_item.hir_id()) == MethodLateContext::TraitImpl { let context = method_context(cx, impl_item.owner_id.def_id);
if context == MethodLateContext::TraitImpl {
return; return;
} }
// If the method is an impl for an item with docs_hidden, don't doc. // If the method is an impl for an item with docs_hidden, don't doc.
if method_context(cx, impl_item.hir_id()) == MethodLateContext::PlainImpl { if context == MethodLateContext::PlainImpl {
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id()); let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id());
let impl_ty = cx.tcx.type_of(parent); let impl_ty = cx.tcx.type_of(parent);
let outerdef = match impl_ty.kind() { let outerdef = match impl_ty.kind() {
@ -1296,19 +1295,18 @@ impl<'tcx> LateLintPass<'tcx> for UngatedAsyncFnTrackCaller {
_: &'tcx FnDecl<'_>, _: &'tcx FnDecl<'_>,
_: &'tcx Body<'_>, _: &'tcx Body<'_>,
span: Span, span: Span,
hir_id: HirId, def_id: LocalDefId,
) { ) {
if fn_kind.asyncness() == IsAsync::Async if fn_kind.asyncness() == IsAsync::Async
&& !cx.tcx.features().closure_track_caller && !cx.tcx.features().closure_track_caller
&& let attrs = cx.tcx.hir().attrs(hir_id)
// Now, check if the function has the `#[track_caller]` attribute // Now, check if the function has the `#[track_caller]` attribute
&& let Some(attr) = attrs.iter().find(|attr| attr.has_name(sym::track_caller)) && let Some(attr) = cx.tcx.get_attr(def_id.to_def_id(), sym::track_caller)
{ {
cx.emit_spanned_lint(UNGATED_ASYNC_FN_TRACK_CALLER, attr.span, BuiltinUngatedAsyncFnTrackCaller { cx.emit_spanned_lint(UNGATED_ASYNC_FN_TRACK_CALLER, attr.span, BuiltinUngatedAsyncFnTrackCaller {
label: span, label: span,
parse_sess: &cx.tcx.sess.parse_sess, parse_sess: &cx.tcx.sess.parse_sess,
}); });
} }
} }
} }
@ -2684,7 +2682,7 @@ pub struct ClashingExternDeclarations {
/// the symbol should be reported as a clashing declaration. /// the symbol should be reported as a clashing declaration.
// FIXME: Technically, we could just store a &'tcx str here without issue; however, the // FIXME: Technically, we could just store a &'tcx str here without issue; however, the
// `impl_lint_pass` macro doesn't currently support lints parametric over a lifetime. // `impl_lint_pass` macro doesn't currently support lints parametric over a lifetime.
seen_decls: FxHashMap<Symbol, HirId>, seen_decls: FxHashMap<Symbol, hir::OwnerId>,
} }
/// Differentiate between whether the name for an extern decl came from the link_name attribute or /// Differentiate between whether the name for an extern decl came from the link_name attribute or
@ -2710,19 +2708,20 @@ impl ClashingExternDeclarations {
pub(crate) fn new() -> Self { pub(crate) fn new() -> Self {
ClashingExternDeclarations { seen_decls: FxHashMap::default() } ClashingExternDeclarations { seen_decls: FxHashMap::default() }
} }
/// Insert a new foreign item into the seen set. If a symbol with the same name already exists /// Insert a new foreign item into the seen set. If a symbol with the same name already exists
/// for the item, return its HirId without updating the set. /// for the item, return its HirId without updating the set.
fn insert(&mut self, tcx: TyCtxt<'_>, fi: &hir::ForeignItem<'_>) -> Option<HirId> { fn insert(&mut self, tcx: TyCtxt<'_>, fi: &hir::ForeignItem<'_>) -> Option<hir::OwnerId> {
let did = fi.owner_id.to_def_id(); let did = fi.owner_id.to_def_id();
let instance = Instance::new(did, ty::List::identity_for_item(tcx, did)); let instance = Instance::new(did, ty::List::identity_for_item(tcx, did));
let name = Symbol::intern(tcx.symbol_name(instance).name); let name = Symbol::intern(tcx.symbol_name(instance).name);
if let Some(&hir_id) = self.seen_decls.get(&name) { if let Some(&existing_id) = self.seen_decls.get(&name) {
// Avoid updating the map with the new entry when we do find a collision. We want to // Avoid updating the map with the new entry when we do find a collision. We want to
// make sure we're always pointing to the first definition as the previous declaration. // make sure we're always pointing to the first definition as the previous declaration.
// This lets us avoid emitting "knock-on" diagnostics. // This lets us avoid emitting "knock-on" diagnostics.
Some(hir_id) Some(existing_id)
} else { } else {
self.seen_decls.insert(name, fi.hir_id()) self.seen_decls.insert(name, fi.owner_id)
} }
} }
@ -2949,16 +2948,16 @@ impl ClashingExternDeclarations {
impl_lint_pass!(ClashingExternDeclarations => [CLASHING_EXTERN_DECLARATIONS]); impl_lint_pass!(ClashingExternDeclarations => [CLASHING_EXTERN_DECLARATIONS]);
impl<'tcx> LateLintPass<'tcx> for ClashingExternDeclarations { impl<'tcx> LateLintPass<'tcx> for ClashingExternDeclarations {
#[instrument(level = "trace", skip(self, cx))]
fn check_foreign_item(&mut self, cx: &LateContext<'tcx>, this_fi: &hir::ForeignItem<'_>) { fn check_foreign_item(&mut self, cx: &LateContext<'tcx>, this_fi: &hir::ForeignItem<'_>) {
trace!("ClashingExternDeclarations: check_foreign_item: {:?}", this_fi);
if let ForeignItemKind::Fn(..) = this_fi.kind { if let ForeignItemKind::Fn(..) = this_fi.kind {
let tcx = cx.tcx; let tcx = cx.tcx;
if let Some(existing_hid) = self.insert(tcx, this_fi) { if let Some(existing_did) = self.insert(tcx, this_fi) {
let existing_decl_ty = tcx.type_of(tcx.hir().local_def_id(existing_hid)); let existing_decl_ty = tcx.type_of(existing_did);
let this_decl_ty = tcx.type_of(this_fi.owner_id); let this_decl_ty = tcx.type_of(this_fi.owner_id);
debug!( debug!(
"ClashingExternDeclarations: Comparing existing {:?}: {:?} to this {:?}: {:?}", "ClashingExternDeclarations: Comparing existing {:?}: {:?} to this {:?}: {:?}",
existing_hid, existing_decl_ty, this_fi.owner_id, this_decl_ty existing_did, existing_decl_ty, this_fi.owner_id, this_decl_ty
); );
// Check that the declarations match. // Check that the declarations match.
if !Self::structurally_same_type( if !Self::structurally_same_type(
@ -2967,7 +2966,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.expect_owner()); let orig_fi = tcx.hir().expect_foreign_item(existing_did);
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

@ -66,13 +66,12 @@ impl<'tcx, T: LateLintPass<'tcx>> LateContextAndPass<'tcx, T> {
self.context.last_node_with_lint_attrs = prev; self.context.last_node_with_lint_attrs = prev;
} }
fn with_param_env<F>(&mut self, id: hir::HirId, f: F) fn with_param_env<F>(&mut self, id: hir::OwnerId, f: F)
where where
F: FnOnce(&mut Self), F: FnOnce(&mut Self),
{ {
let old_param_env = self.context.param_env; let old_param_env = self.context.param_env;
self.context.param_env = self.context.param_env = self.context.tcx.param_env(id);
self.context.tcx.param_env(self.context.tcx.hir().local_def_id(id));
f(self); f(self);
self.context.param_env = old_param_env; self.context.param_env = old_param_env;
} }
@ -132,7 +131,7 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
let old_cached_typeck_results = self.context.cached_typeck_results.take(); let old_cached_typeck_results = self.context.cached_typeck_results.take();
let old_enclosing_body = self.context.enclosing_body.take(); let old_enclosing_body = self.context.enclosing_body.take();
self.with_lint_attrs(it.hir_id(), |cx| { self.with_lint_attrs(it.hir_id(), |cx| {
cx.with_param_env(it.hir_id(), |cx| { cx.with_param_env(it.owner_id, |cx| {
lint_callback!(cx, check_item, it); lint_callback!(cx, check_item, it);
hir_visit::walk_item(cx, it); hir_visit::walk_item(cx, it);
lint_callback!(cx, check_item_post, it); lint_callback!(cx, check_item_post, it);
@ -145,7 +144,7 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) { fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) {
self.with_lint_attrs(it.hir_id(), |cx| { self.with_lint_attrs(it.hir_id(), |cx| {
cx.with_param_env(it.hir_id(), |cx| { cx.with_param_env(it.owner_id, |cx| {
lint_callback!(cx, check_foreign_item, it); lint_callback!(cx, check_foreign_item, it);
hir_visit::walk_foreign_item(cx, it); hir_visit::walk_foreign_item(cx, it);
}); });
@ -180,7 +179,7 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
decl: &'tcx hir::FnDecl<'tcx>, decl: &'tcx hir::FnDecl<'tcx>,
body_id: hir::BodyId, body_id: hir::BodyId,
span: Span, span: Span,
id: hir::HirId, id: LocalDefId,
) { ) {
// Wrap in typeck results here, not just in visit_nested_body, // Wrap in typeck results here, not just in visit_nested_body,
// in order for `check_fn` to be able to use them. // in order for `check_fn` to be able to use them.
@ -268,7 +267,7 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
let generics = self.context.generics.take(); let generics = self.context.generics.take();
self.context.generics = Some(&trait_item.generics); self.context.generics = Some(&trait_item.generics);
self.with_lint_attrs(trait_item.hir_id(), |cx| { self.with_lint_attrs(trait_item.hir_id(), |cx| {
cx.with_param_env(trait_item.hir_id(), |cx| { cx.with_param_env(trait_item.owner_id, |cx| {
lint_callback!(cx, check_trait_item, trait_item); lint_callback!(cx, check_trait_item, trait_item);
hir_visit::walk_trait_item(cx, trait_item); hir_visit::walk_trait_item(cx, trait_item);
}); });
@ -280,7 +279,7 @@ impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPas
let generics = self.context.generics.take(); let generics = self.context.generics.take();
self.context.generics = Some(&impl_item.generics); self.context.generics = Some(&impl_item.generics);
self.with_lint_attrs(impl_item.hir_id(), |cx| { self.with_lint_attrs(impl_item.hir_id(), |cx| {
cx.with_param_env(impl_item.hir_id(), |cx| { cx.with_param_env(impl_item.owner_id, |cx| {
lint_callback!(cx, check_impl_item, impl_item); lint_callback!(cx, check_impl_item, impl_item);
hir_visit::walk_impl_item(cx, impl_item); hir_visit::walk_impl_item(cx, impl_item);
lint_callback!(cx, check_impl_item_post, impl_item); lint_callback!(cx, check_impl_item_post, impl_item);

View file

@ -10,8 +10,9 @@ use rustc_hir::def::{DefKind, Res};
use rustc_hir::intravisit::FnKind; use rustc_hir::intravisit::FnKind;
use rustc_hir::{GenericParamKind, PatKind}; use rustc_hir::{GenericParamKind, PatKind};
use rustc_middle::ty; use rustc_middle::ty;
use rustc_span::symbol::sym; use rustc_span::def_id::LocalDefId;
use rustc_span::{symbol::Ident, BytePos, Span}; use rustc_span::symbol::{sym, Ident};
use rustc_span::{BytePos, Span};
use rustc_target::spec::abi::Abi; use rustc_target::spec::abi::Abi;
#[derive(PartialEq)] #[derive(PartialEq)]
@ -21,9 +22,8 @@ pub enum MethodLateContext {
PlainImpl, PlainImpl,
} }
pub fn method_context(cx: &LateContext<'_>, id: hir::HirId) -> MethodLateContext { pub fn method_context(cx: &LateContext<'_>, id: LocalDefId) -> MethodLateContext {
let def_id = cx.tcx.hir().local_def_id(id); let item = cx.tcx.associated_item(id);
let item = cx.tcx.associated_item(def_id);
match item.container { match item.container {
ty::TraitContainer => MethodLateContext::TraitAutoImpl, ty::TraitContainer => MethodLateContext::TraitAutoImpl,
ty::ImplContainer => match cx.tcx.impl_trait_ref(item.container_id(cx.tcx)) { ty::ImplContainer => match cx.tcx.impl_trait_ref(item.container_id(cx.tcx)) {
@ -379,13 +379,13 @@ impl<'tcx> LateLintPass<'tcx> for NonSnakeCase {
_: &hir::FnDecl<'_>, _: &hir::FnDecl<'_>,
_: &hir::Body<'_>, _: &hir::Body<'_>,
_: Span, _: Span,
id: hir::HirId, id: LocalDefId,
) { ) {
let attrs = cx.tcx.hir().attrs(id);
match &fk { match &fk {
FnKind::Method(ident, sig, ..) => match method_context(cx, id) { FnKind::Method(ident, sig, ..) => match method_context(cx, id) {
MethodLateContext::PlainImpl => { MethodLateContext::PlainImpl => {
if sig.header.abi != Abi::Rust && cx.sess().contains_name(attrs, sym::no_mangle) if sig.header.abi != Abi::Rust
&& cx.tcx.has_attr(id.to_def_id(), sym::no_mangle)
{ {
return; return;
} }
@ -398,7 +398,7 @@ impl<'tcx> LateLintPass<'tcx> for NonSnakeCase {
}, },
FnKind::ItemFn(ident, _, header) => { FnKind::ItemFn(ident, _, header) => {
// Skip foreign-ABI #[no_mangle] functions (Issue #31924) // Skip foreign-ABI #[no_mangle] functions (Issue #31924)
if header.abi != Abi::Rust && cx.sess().contains_name(attrs, sym::no_mangle) { if header.abi != Abi::Rust && cx.tcx.has_attr(id.to_def_id(), sym::no_mangle) {
return; return;
} }
self.check_snake_case(cx, "function", ident); self.check_snake_case(cx, "function", ident);

View file

@ -4,6 +4,7 @@ use rustc_ast as ast;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_session::lint::builtin::HardwiredLints; use rustc_session::lint::builtin::HardwiredLints;
use rustc_session::lint::LintPass; use rustc_session::lint::LintPass;
use rustc_span::def_id::LocalDefId;
use rustc_span::symbol::Ident; use rustc_span::symbol::Ident;
use rustc_span::Span; use rustc_span::Span;
@ -36,7 +37,7 @@ macro_rules! late_lint_methods {
b: &'tcx hir::FnDecl<'tcx>, b: &'tcx hir::FnDecl<'tcx>,
c: &'tcx hir::Body<'tcx>, c: &'tcx hir::Body<'tcx>,
d: Span, d: Span,
e: hir::HirId); e: LocalDefId);
fn check_trait_item(a: &'tcx hir::TraitItem<'tcx>); fn check_trait_item(a: &'tcx hir::TraitItem<'tcx>);
fn check_impl_item(a: &'tcx hir::ImplItem<'tcx>); fn check_impl_item(a: &'tcx hir::ImplItem<'tcx>);
fn check_impl_item_post(a: &'tcx hir::ImplItem<'tcx>); fn check_impl_item_post(a: &'tcx hir::ImplItem<'tcx>);

View file

@ -14,6 +14,7 @@ use rustc_hir::{is_range_literal, Expr, ExprKind, Node};
use rustc_middle::ty::layout::{IntegerExt, LayoutOf, SizeSkeleton}; use rustc_middle::ty::layout::{IntegerExt, LayoutOf, SizeSkeleton};
use rustc_middle::ty::subst::SubstsRef; use rustc_middle::ty::subst::SubstsRef;
use rustc_middle::ty::{self, AdtKind, DefIdTree, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable}; use rustc_middle::ty::{self, AdtKind, DefIdTree, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable};
use rustc_span::def_id::LocalDefId;
use rustc_span::source_map; use rustc_span::source_map;
use rustc_span::symbol::sym; use rustc_span::symbol::sym;
use rustc_span::{Span, Symbol}; use rustc_span::{Span, Symbol};
@ -1224,8 +1225,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
} }
} }
fn check_foreign_fn(&mut self, id: hir::HirId, decl: &hir::FnDecl<'_>) { fn check_foreign_fn(&mut self, def_id: LocalDefId, decl: &hir::FnDecl<'_>) {
let def_id = self.cx.tcx.hir().local_def_id(id);
let sig = self.cx.tcx.fn_sig(def_id).subst_identity(); let sig = self.cx.tcx.fn_sig(def_id).subst_identity();
let sig = self.cx.tcx.erase_late_bound_regions(sig); let sig = self.cx.tcx.erase_late_bound_regions(sig);
@ -1239,9 +1239,8 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
} }
} }
fn check_foreign_static(&mut self, id: hir::HirId, span: Span) { fn check_foreign_static(&mut self, id: hir::OwnerId, span: Span) {
let def_id = self.cx.tcx.hir().local_def_id(id); let ty = self.cx.tcx.type_of(id);
let ty = self.cx.tcx.type_of(def_id);
self.check_type_for_ffi_and_report_errors(span, ty, true, false); self.check_type_for_ffi_and_report_errors(span, ty, true, false);
} }
@ -1261,10 +1260,10 @@ impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDeclarations {
if !vis.is_internal_abi(abi) { if !vis.is_internal_abi(abi) {
match it.kind { match it.kind {
hir::ForeignItemKind::Fn(ref decl, _, _) => { hir::ForeignItemKind::Fn(ref decl, _, _) => {
vis.check_foreign_fn(it.hir_id(), decl); vis.check_foreign_fn(it.owner_id.def_id, decl);
} }
hir::ForeignItemKind::Static(ref ty, _) => { hir::ForeignItemKind::Static(ref ty, _) => {
vis.check_foreign_static(it.hir_id(), ty.span); vis.check_foreign_static(it.owner_id, ty.span);
} }
hir::ForeignItemKind::Type => (), hir::ForeignItemKind::Type => (),
} }
@ -1280,7 +1279,7 @@ impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDefinitions {
decl: &'tcx hir::FnDecl<'_>, decl: &'tcx hir::FnDecl<'_>,
_: &'tcx hir::Body<'_>, _: &'tcx hir::Body<'_>,
_: Span, _: Span,
hir_id: hir::HirId, id: LocalDefId,
) { ) {
use hir::intravisit::FnKind; use hir::intravisit::FnKind;
@ -1292,7 +1291,7 @@ impl<'tcx> LateLintPass<'tcx> for ImproperCTypesDefinitions {
let mut vis = ImproperCTypesVisitor { cx, mode: CItemKind::Definition }; let mut vis = ImproperCTypesVisitor { cx, mode: CItemKind::Definition };
if !vis.is_internal_abi(abi) { if !vis.is_internal_abi(abi) {
vis.check_foreign_fn(hir_id, decl); vis.check_foreign_fn(id, decl);
} }
} }
} }

View file

@ -1310,8 +1310,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
hir::ItemKind::Struct(ref vdata, _) => { hir::ItemKind::Struct(ref vdata, _) => {
yield item_id.owner_id.def_id.local_def_index; yield item_id.owner_id.def_id.local_def_index;
// Encode constructors which take a separate slot in value namespace. // Encode constructors which take a separate slot in value namespace.
if let Some(ctor_hir_id) = vdata.ctor_hir_id() { if let Some(ctor_def_id) = vdata.ctor_def_id() {
yield tcx.hir().local_def_id(ctor_hir_id).local_def_index; yield ctor_def_id.local_def_index;
} }
} }
_ if tcx.def_key(item_id.owner_id.to_def_id()).get_opt_name().is_some() => { _ if tcx.def_key(item_id.owner_id.to_def_id()).get_opt_name().is_some() => {

View file

@ -105,7 +105,7 @@ macro_rules! arena_types {
// (during lowering) and the `librustc_middle` arena (for decoding MIR) // (during lowering) and the `librustc_middle` arena (for decoding MIR)
[decode] asm_template: rustc_ast::InlineAsmTemplatePiece, [decode] asm_template: rustc_ast::InlineAsmTemplatePiece,
[decode] used_trait_imports: rustc_data_structures::unord::UnordSet<rustc_hir::def_id::LocalDefId>, [decode] used_trait_imports: rustc_data_structures::unord::UnordSet<rustc_hir::def_id::LocalDefId>,
[decode] is_late_bound_map: rustc_data_structures::fx::FxIndexSet<rustc_hir::def_id::LocalDefId>, [decode] is_late_bound_map: rustc_data_structures::fx::FxIndexSet<rustc_hir::ItemLocalId>,
[decode] impl_source: rustc_middle::traits::ImplSource<'tcx, ()>, [decode] impl_source: rustc_middle::traits::ImplSource<'tcx, ()>,
[] dep_kind: rustc_middle::dep_graph::DepKindStruct<'tcx>, [] dep_kind: rustc_middle::dep_graph::DepKindStruct<'tcx>,

View file

@ -18,24 +18,30 @@ use rustc_span::Span;
use rustc_target::spec::abi::Abi; use rustc_target::spec::abi::Abi;
#[inline] #[inline]
pub fn associated_body(node: Node<'_>) -> Option<BodyId> { pub fn associated_body(node: Node<'_>) -> Option<(LocalDefId, BodyId)> {
match node { match node {
Node::Item(Item { Node::Item(Item {
owner_id,
kind: ItemKind::Const(_, body) | ItemKind::Static(.., body) | ItemKind::Fn(.., body), kind: ItemKind::Const(_, body) | ItemKind::Static(.., body) | ItemKind::Fn(.., body),
.. ..
}) })
| Node::TraitItem(TraitItem { | Node::TraitItem(TraitItem {
owner_id,
kind: kind:
TraitItemKind::Const(_, Some(body)) | TraitItemKind::Fn(_, TraitFn::Provided(body)), TraitItemKind::Const(_, Some(body)) | TraitItemKind::Fn(_, TraitFn::Provided(body)),
.. ..
}) })
| Node::ImplItem(ImplItem { | Node::ImplItem(ImplItem {
owner_id,
kind: ImplItemKind::Const(_, body) | ImplItemKind::Fn(_, body), kind: ImplItemKind::Const(_, body) | ImplItemKind::Fn(_, body),
.. ..
}) }) => Some((owner_id.def_id, *body)),
| Node::Expr(Expr { kind: ExprKind::Closure(Closure { body, .. }), .. }) => Some(*body),
Node::AnonConst(constant) => Some(constant.body), Node::Expr(Expr { kind: ExprKind::Closure(Closure { def_id, body, .. }), .. }) => {
Some((*def_id, *body))
}
Node::AnonConst(constant) => Some((constant.def_id, constant.body)),
_ => None, _ => None,
} }
@ -43,7 +49,7 @@ pub fn associated_body(node: Node<'_>) -> Option<BodyId> {
fn is_body_owner(node: Node<'_>, hir_id: HirId) -> bool { fn is_body_owner(node: Node<'_>, hir_id: HirId) -> bool {
match associated_body(node) { match associated_body(node) {
Some(b) => b.hir_id == hir_id, Some((_, b)) => b.hir_id == hir_id,
None => false, None => false,
} }
} }
@ -154,10 +160,6 @@ impl<'hir> Map<'hir> {
self.tcx.definitions_untracked().def_key(def_id) self.tcx.definitions_untracked().def_key(def_id)
} }
pub fn def_path_from_hir_id(self, id: HirId) -> Option<DefPath> {
self.opt_local_def_id(id).map(|def_id| self.def_path(def_id))
}
pub fn def_path(self, def_id: LocalDefId) -> DefPath { pub fn def_path(self, def_id: LocalDefId) -> DefPath {
// Accessing the DefPath is ok, since it is part of DefPathHash. // Accessing the DefPath is ok, since it is part of DefPathHash.
self.tcx.definitions_untracked().def_path(def_id) self.tcx.definitions_untracked().def_path(def_id)
@ -169,32 +171,6 @@ impl<'hir> Map<'hir> {
self.tcx.definitions_untracked().def_path_hash(def_id) self.tcx.definitions_untracked().def_path_hash(def_id)
} }
#[inline]
#[track_caller]
pub fn local_def_id(self, hir_id: HirId) -> LocalDefId {
self.opt_local_def_id(hir_id).unwrap_or_else(|| {
bug!(
"local_def_id: no entry for `{:?}`, which has a map of `{:?}`",
hir_id,
self.find(hir_id)
)
})
}
#[inline]
pub fn opt_local_def_id(self, hir_id: HirId) -> Option<LocalDefId> {
if hir_id.local_id == ItemLocalId::new(0) {
Some(hir_id.owner.def_id)
} else {
self.tcx
.hir_owner_nodes(hir_id.owner)
.as_owner()?
.local_id_to_def_id
.get(&hir_id.local_id)
.copied()
}
}
#[inline] #[inline]
pub fn local_def_id_to_hir_id(self, def_id: LocalDefId) -> HirId { pub fn local_def_id_to_hir_id(self, def_id: LocalDefId) -> HirId {
self.tcx.local_def_id_to_hir_id(def_id) self.tcx.local_def_id_to_hir_id(def_id)
@ -410,8 +386,8 @@ impl<'hir> Map<'hir> {
#[track_caller] #[track_caller]
pub fn enclosing_body_owner(self, hir_id: HirId) -> LocalDefId { pub fn enclosing_body_owner(self, hir_id: HirId) -> LocalDefId {
for (_, node) in self.parent_iter(hir_id) { for (_, node) in self.parent_iter(hir_id) {
if let Some(body) = associated_body(node) { if let Some((def_id, _)) = associated_body(node) {
return self.body_owner_def_id(body); return def_id;
} }
} }
@ -427,14 +403,17 @@ impl<'hir> Map<'hir> {
parent parent
} }
pub fn body_owner_def_id(self, id: BodyId) -> LocalDefId { pub fn body_owner_def_id(self, BodyId { hir_id }: BodyId) -> LocalDefId {
self.local_def_id(self.body_owner(id)) let parent = self.parent_id(hir_id);
associated_body(self.get(parent)).unwrap().0
} }
/// Given a `LocalDefId`, returns the `BodyId` associated with it, /// Given a `LocalDefId`, returns the `BodyId` associated with it,
/// if the node is a body owner, otherwise returns `None`. /// if the node is a body owner, otherwise returns `None`.
pub fn maybe_body_owned_by(self, id: LocalDefId) -> Option<BodyId> { pub fn maybe_body_owned_by(self, id: LocalDefId) -> Option<BodyId> {
self.find_by_def_id(id).and_then(associated_body) let node = self.find_by_def_id(id)?;
let (_, body_id) = associated_body(node)?;
Some(body_id)
} }
/// Given a body owner's id, returns the `BodyId` associated with it. /// Given a body owner's id, returns the `BodyId` associated with it.

View file

@ -2506,7 +2506,7 @@ impl<'tcx> ConstantKind<'tcx> {
let hir_id = tcx.hir().local_def_id_to_hir_id(def.did); let hir_id = tcx.hir().local_def_id_to_hir_id(def.did);
let parent_substs = if let Some(parent_hir_id) = tcx.hir().opt_parent_id(hir_id) { let parent_substs = if let Some(parent_hir_id) = tcx.hir().opt_parent_id(hir_id) {
if let Some(parent_did) = tcx.hir().opt_local_def_id(parent_hir_id) { if let Some(parent_did) = parent_hir_id.as_owner() {
InternalSubsts::identity_for_item(tcx, parent_did.to_def_id()) InternalSubsts::identity_for_item(tcx, parent_did.to_def_id())
} else { } else {
tcx.mk_substs(Vec::<GenericArg<'tcx>>::new().into_iter()) tcx.mk_substs(Vec::<GenericArg<'tcx>>::new().into_iter())

View file

@ -670,7 +670,7 @@ fn fn_span(tcx: TyCtxt<'_>, def_id: DefId) -> Span {
fn hir_body(tcx: TyCtxt<'_>, def_id: DefId) -> Option<&rustc_hir::Body<'_>> { fn hir_body(tcx: TyCtxt<'_>, def_id: DefId) -> Option<&rustc_hir::Body<'_>> {
let hir_node = tcx.hir().get_if_local(def_id).expect("expected DefId is local"); let hir_node = tcx.hir().get_if_local(def_id).expect("expected DefId is local");
hir::map::associated_body(hir_node).map(|fn_body_id| tcx.hir().body(fn_body_id)) hir::map::associated_body(hir_node).map(|(_, fn_body_id)| tcx.hir().body(fn_body_id))
} }
fn escape_html(s: &str) -> String { fn escape_html(s: &str) -> String {

View file

@ -1640,7 +1640,7 @@ rustc_queries! {
Option<&'tcx FxHashMap<ItemLocalId, Region>> { Option<&'tcx FxHashMap<ItemLocalId, Region>> {
desc { "looking up a named region" } desc { "looking up a named region" }
} }
query is_late_bound_map(_: LocalDefId) -> Option<&'tcx FxIndexSet<LocalDefId>> { query is_late_bound_map(_: hir::OwnerId) -> Option<&'tcx FxIndexSet<ItemLocalId>> {
desc { "testing if a region is late bound" } desc { "testing if a region is late bound" }
} }
/// For a given item's generic parameter, gets the default lifetimes to be used /// For a given item's generic parameter, gets the default lifetimes to be used

View file

@ -2157,10 +2157,7 @@ impl<'tcx> TyCtxt<'tcx> {
} }
pub fn is_late_bound(self, id: HirId) -> bool { pub fn is_late_bound(self, id: HirId) -> bool {
self.is_late_bound_map(id.owner.def_id).map_or(false, |set| { self.is_late_bound_map(id.owner).map_or(false, |set| set.contains(&id.local_id))
let def_id = self.hir().local_def_id(id);
set.contains(&def_id)
})
} }
pub fn late_bound_vars(self, id: HirId) -> &'tcx List<ty::BoundVariableKind> { pub fn late_bound_vars(self, id: HirId) -> &'tcx List<ty::BoundVariableKind> {

View file

@ -118,6 +118,7 @@ fn copy<T: Copy>(x: &T) -> T {
macro_rules! query_helper_param_ty { macro_rules! query_helper_param_ty {
(DefId) => { impl IntoQueryParam<DefId> }; (DefId) => { impl IntoQueryParam<DefId> };
(LocalDefId) => { impl IntoQueryParam<LocalDefId> };
($K:ty) => { $K }; ($K:ty) => { $K };
} }
@ -418,6 +419,13 @@ mod sealed {
} }
} }
impl IntoQueryParam<LocalDefId> for OwnerId {
#[inline(always)]
fn into_query_param(self) -> LocalDefId {
self.def_id
}
}
impl IntoQueryParam<DefId> for LocalDefId { impl IntoQueryParam<DefId> for LocalDefId {
#[inline(always)] #[inline(always)]
fn into_query_param(self) -> DefId { fn into_query_param(self) -> DefId {

View file

@ -5,7 +5,6 @@ use rustc_middle::mir::{self, Field};
use rustc_middle::thir::{FieldPat, Pat, PatKind}; use rustc_middle::thir::{FieldPat, Pat, PatKind};
use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_session::lint; use rustc_session::lint;
use rustc_span::def_id::CRATE_DEF_ID;
use rustc_span::Span; use rustc_span::Span;
use rustc_trait_selection::traits::predicate_for_trait_def; use rustc_trait_selection::traits::predicate_for_trait_def;
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt; use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
@ -190,11 +189,10 @@ impl<'tcx> ConstToPat<'tcx> {
// using `PartialEq::eq` in this scenario in the past.) // using `PartialEq::eq` in this scenario in the past.)
let partial_eq_trait_id = let partial_eq_trait_id =
self.tcx().require_lang_item(hir::LangItem::PartialEq, Some(self.span)); self.tcx().require_lang_item(hir::LangItem::PartialEq, Some(self.span));
let def_id = self.tcx().hir().opt_local_def_id(self.id).unwrap_or(CRATE_DEF_ID);
let obligation: PredicateObligation<'_> = predicate_for_trait_def( let obligation: PredicateObligation<'_> = predicate_for_trait_def(
self.tcx(), self.tcx(),
self.param_env, self.param_env,
ObligationCause::misc(self.span, def_id), ObligationCause::misc(self.span, self.id.owner.def_id),
partial_eq_trait_id, partial_eq_trait_id,
0, 0,
[ty, ty], [ty, ty],

View file

@ -445,7 +445,7 @@ impl<'tcx> intravisit::Visitor<'tcx> for UnusedUnsafeVisitor<'_, 'tcx> {
_fd: &'tcx hir::FnDecl<'tcx>, _fd: &'tcx hir::FnDecl<'tcx>,
b: hir::BodyId, b: hir::BodyId,
_s: rustc_span::Span, _s: rustc_span::Span,
_id: HirId, _id: LocalDefId,
) { ) {
if matches!(fk, intravisit::FnKind::Closure) { if matches!(fk, intravisit::FnKind::Closure) {
self.visit_body(self.tcx.hir().body(b)) self.visit_body(self.tcx.hir().body(b))

View file

@ -540,7 +540,8 @@ fn fn_sig_and_body(
// FIXME(#79625): Consider improving MIR to provide the information needed, to avoid going back // FIXME(#79625): Consider improving MIR to provide the information needed, to avoid going back
// to HIR for it. // to HIR for it.
let hir_node = tcx.hir().get_if_local(def_id).expect("expected DefId is local"); let hir_node = tcx.hir().get_if_local(def_id).expect("expected DefId is local");
let fn_body_id = hir::map::associated_body(hir_node).expect("HIR node is a function with body"); let (_, fn_body_id) =
hir::map::associated_body(hir_node).expect("HIR node is a function with body");
(hir_node.fn_sig(), tcx.hir().body(fn_body_id)) (hir_node.fn_sig(), tcx.hir().body(fn_body_id))
} }

View file

@ -453,7 +453,9 @@ impl CheckAttrVisitor<'_> {
/// Debugging aid for `object_lifetime_default` query. /// Debugging aid for `object_lifetime_default` query.
fn check_object_lifetime_default(&self, hir_id: HirId) { fn check_object_lifetime_default(&self, hir_id: HirId) {
let tcx = self.tcx; let tcx = self.tcx;
if let Some(generics) = tcx.hir().get_generics(tcx.hir().local_def_id(hir_id)) { if let Some(owner_id) = hir_id.as_owner()
&& let Some(generics) = tcx.hir().get_generics(owner_id.def_id)
{
for p in generics.params { for p in generics.params {
let hir::GenericParamKind::Type { .. } = p.kind else { continue }; let hir::GenericParamKind::Type { .. } = p.kind else { continue };
let default = tcx.object_lifetime_default(p.def_id); let default = tcx.object_lifetime_default(p.def_id);
@ -1943,7 +1945,7 @@ impl CheckAttrVisitor<'_> {
) -> bool { ) -> bool {
match target { match target {
Target::Fn | Target::Method(_) Target::Fn | Target::Method(_)
if self.tcx.is_const_fn_raw(self.tcx.hir().local_def_id(hir_id).to_def_id()) => if self.tcx.is_const_fn_raw(hir_id.expect_owner().to_def_id()) =>
{ {
true true
} }

View file

@ -451,29 +451,23 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
// referenced by it should be considered as used. // referenced by it should be considered as used.
let in_pat = mem::replace(&mut self.in_pat, false); let in_pat = mem::replace(&mut self.in_pat, false);
self.live_symbols.insert(self.tcx.hir().local_def_id(c.hir_id)); self.live_symbols.insert(c.def_id);
intravisit::walk_anon_const(self, c); intravisit::walk_anon_const(self, c);
self.in_pat = in_pat; self.in_pat = in_pat;
} }
} }
fn has_allow_dead_code_or_lang_attr_helper( fn has_allow_dead_code_or_lang_attr(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
tcx: TyCtxt<'_>, if tcx.has_attr(def_id.to_def_id(), sym::lang) {
id: hir::HirId,
lint: &'static lint::Lint,
) -> bool {
let attrs = tcx.hir().attrs(id);
if tcx.sess.contains_name(attrs, sym::lang) {
return true; return true;
} }
// Stable attribute for #[lang = "panic_impl"] // Stable attribute for #[lang = "panic_impl"]
if tcx.sess.contains_name(attrs, sym::panic_handler) { if tcx.has_attr(def_id.to_def_id(), sym::panic_handler) {
return true; return true;
} }
let def_id = tcx.hir().local_def_id(id);
if tcx.def_kind(def_id).has_codegen_attrs() { if tcx.def_kind(def_id).has_codegen_attrs() {
let cg_attrs = tcx.codegen_fn_attrs(def_id); let cg_attrs = tcx.codegen_fn_attrs(def_id);
@ -487,11 +481,8 @@ fn has_allow_dead_code_or_lang_attr_helper(
} }
} }
tcx.lint_level_at_node(lint, id).0 == lint::Allow let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
} tcx.lint_level_at_node(lint::builtin::DEAD_CODE, hir_id).0 == lint::Allow
fn has_allow_dead_code_or_lang_attr(tcx: TyCtxt<'_>, id: hir::HirId) -> bool {
has_allow_dead_code_or_lang_attr_helper(tcx, id, lint::builtin::DEAD_CODE)
} }
// These check_* functions seeds items that // These check_* functions seeds items that
@ -513,7 +504,7 @@ fn check_item<'tcx>(
struct_constructors: &mut FxHashMap<LocalDefId, LocalDefId>, struct_constructors: &mut FxHashMap<LocalDefId, LocalDefId>,
id: hir::ItemId, id: hir::ItemId,
) { ) {
let allow_dead_code = has_allow_dead_code_or_lang_attr(tcx, id.hir_id()); let allow_dead_code = has_allow_dead_code_or_lang_attr(tcx, id.owner_id.def_id);
if allow_dead_code { if allow_dead_code {
worklist.push(id.owner_id.def_id); worklist.push(id.owner_id.def_id);
} }
@ -548,9 +539,7 @@ fn check_item<'tcx>(
// And we access the Map here to get HirId from LocalDefId // And we access the Map here to get HirId from LocalDefId
for id in local_def_ids { for id in local_def_ids {
if of_trait.is_some() if of_trait.is_some() || has_allow_dead_code_or_lang_attr(tcx, id) {
|| has_allow_dead_code_or_lang_attr(tcx, tcx.hir().local_def_id_to_hir_id(id))
{
worklist.push(id); worklist.push(id);
} }
} }
@ -558,9 +547,9 @@ fn check_item<'tcx>(
DefKind::Struct => { DefKind::Struct => {
let item = tcx.hir().item(id); let item = tcx.hir().item(id);
if let hir::ItemKind::Struct(ref variant_data, _) = item.kind if let hir::ItemKind::Struct(ref variant_data, _) = item.kind
&& let Some(ctor_hir_id) = variant_data.ctor_hir_id() && let Some(ctor_def_id) = variant_data.ctor_def_id()
{ {
struct_constructors.insert(tcx.hir().local_def_id(ctor_hir_id), item.owner_id.def_id); struct_constructors.insert(ctor_def_id, item.owner_id.def_id);
} }
} }
DefKind::GlobalAsm => { DefKind::GlobalAsm => {
@ -576,7 +565,7 @@ fn check_trait_item(tcx: TyCtxt<'_>, worklist: &mut Vec<LocalDefId>, id: hir::Tr
if matches!(tcx.def_kind(id.owner_id), DefKind::AssocConst | DefKind::AssocFn) { if matches!(tcx.def_kind(id.owner_id), DefKind::AssocConst | DefKind::AssocFn) {
let trait_item = tcx.hir().trait_item(id); let trait_item = tcx.hir().trait_item(id);
if matches!(trait_item.kind, Const(_, Some(_)) | Fn(_, hir::TraitFn::Provided(_))) if matches!(trait_item.kind, Const(_, Some(_)) | Fn(_, hir::TraitFn::Provided(_)))
&& has_allow_dead_code_or_lang_attr(tcx, trait_item.hir_id()) && has_allow_dead_code_or_lang_attr(tcx, trait_item.owner_id.def_id)
{ {
worklist.push(trait_item.owner_id.def_id); worklist.push(trait_item.owner_id.def_id);
} }
@ -585,7 +574,7 @@ fn check_trait_item(tcx: TyCtxt<'_>, worklist: &mut Vec<LocalDefId>, id: hir::Tr
fn check_foreign_item(tcx: TyCtxt<'_>, worklist: &mut Vec<LocalDefId>, id: hir::ForeignItemId) { fn check_foreign_item(tcx: TyCtxt<'_>, worklist: &mut Vec<LocalDefId>, id: hir::ForeignItemId) {
if matches!(tcx.def_kind(id.owner_id), DefKind::Static(_) | DefKind::Fn) if matches!(tcx.def_kind(id.owner_id), DefKind::Static(_) | DefKind::Fn)
&& has_allow_dead_code_or_lang_attr(tcx, id.hir_id()) && has_allow_dead_code_or_lang_attr(tcx, id.owner_id.def_id)
{ {
worklist.push(id.owner_id.def_id); worklist.push(id.owner_id.def_id);
} }
@ -806,8 +795,7 @@ impl<'tcx> DeadVisitor<'tcx> {
if self.live_symbols.contains(&def_id) { if self.live_symbols.contains(&def_id) {
return; return;
} }
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id); if has_allow_dead_code_or_lang_attr(self.tcx, def_id) {
if has_allow_dead_code_or_lang_attr(self.tcx, hir_id) {
return; return;
} }
let Some(name) = self.tcx.opt_item_name(def_id.to_def_id()) else { let Some(name) = self.tcx.opt_item_name(def_id.to_def_id()) else {

View file

@ -12,6 +12,7 @@ use rustc_hir::HirId;
use rustc_middle::hir::map::Map; use rustc_middle::hir::map::Map;
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
use rustc_middle::util::common::to_readable_str; use rustc_middle::util::common::to_readable_str;
use rustc_span::def_id::LocalDefId;
use rustc_span::Span; use rustc_span::Span;
#[derive(Copy, Clone, PartialEq, Eq, Hash)] #[derive(Copy, Clone, PartialEq, Eq, Hash)]
@ -363,7 +364,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> {
fd: &'v hir::FnDecl<'v>, fd: &'v hir::FnDecl<'v>,
b: hir::BodyId, b: hir::BodyId,
_: Span, _: Span,
id: hir::HirId, id: LocalDefId,
) { ) {
self.record("FnDecl", Id::None, fd); self.record("FnDecl", Id::None, fd);
hir_visit::walk_fn(self, fk, fd, b, id) hir_visit::walk_fn(self, fk, fd, b, id)

View file

@ -15,9 +15,9 @@ use crate::weak_lang_items;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::DefKind; use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::lang_items::{extract, GenericRequirement}; use rustc_hir::lang_items::{extract, GenericRequirement};
use rustc_hir::{HirId, LangItem, LanguageItems, Target}; use rustc_hir::{LangItem, LanguageItems, Target};
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
use rustc_session::cstore::ExternCrate; use rustc_session::cstore::ExternCrate;
use rustc_span::{symbol::kw::Empty, Span}; use rustc_span::{symbol::kw::Empty, Span};
@ -40,13 +40,13 @@ impl<'tcx> LanguageItemCollector<'tcx> {
LanguageItemCollector { tcx, items: LanguageItems::new() } LanguageItemCollector { tcx, items: LanguageItems::new() }
} }
fn check_for_lang(&mut self, actual_target: Target, hir_id: HirId) { fn check_for_lang(&mut self, actual_target: Target, def_id: LocalDefId) {
let attrs = self.tcx.hir().attrs(hir_id); let attrs = self.tcx.hir().attrs(self.tcx.hir().local_def_id_to_hir_id(def_id));
if let Some((name, span)) = extract(&attrs) { if let Some((name, span)) = extract(&attrs) {
match LangItem::from_name(name) { match LangItem::from_name(name) {
// Known lang item with attribute on correct target. // Known lang item with attribute on correct target.
Some(lang_item) if actual_target == lang_item.target() => { Some(lang_item) if actual_target == lang_item.target() => {
self.collect_item_extended(lang_item, hir_id, span); self.collect_item_extended(lang_item, def_id, span);
} }
// Known lang item with attribute on incorrect target. // Known lang item with attribute on incorrect target.
Some(lang_item) => { Some(lang_item) => {
@ -142,8 +142,7 @@ impl<'tcx> LanguageItemCollector<'tcx> {
// Like collect_item() above, but also checks whether the lang item is declared // Like collect_item() above, but also checks whether the lang item is declared
// with the right number of generic arguments. // with the right number of generic arguments.
fn collect_item_extended(&mut self, lang_item: LangItem, hir_id: HirId, span: Span) { fn collect_item_extended(&mut self, lang_item: LangItem, item_def_id: LocalDefId, span: Span) {
let item_def_id = self.tcx.hir().local_def_id(hir_id).to_def_id();
let name = lang_item.name(); let name = lang_item.name();
// Now check whether the lang_item has the expected number of generic // Now check whether the lang_item has the expected number of generic
@ -154,7 +153,8 @@ impl<'tcx> LanguageItemCollector<'tcx> {
// Some other types like Box and various functions like drop_in_place // Some other types like Box and various functions like drop_in_place
// have minimum requirements. // have minimum requirements.
if let hir::Node::Item(hir::Item { kind, span: item_span, .. }) = self.tcx.hir().get(hir_id) if let hir::Node::Item(hir::Item { kind, span: item_span, .. }) =
self.tcx.hir().get_by_def_id(item_def_id)
{ {
let (actual_num, generics_span) = match kind.generics() { let (actual_num, generics_span) = match kind.generics() {
Some(generics) => (generics.params.len(), generics.span), Some(generics) => (generics.params.len(), generics.span),
@ -191,7 +191,7 @@ impl<'tcx> LanguageItemCollector<'tcx> {
} }
} }
self.collect_item(lang_item, item_def_id); self.collect_item(lang_item, item_def_id.to_def_id());
} }
} }
@ -211,13 +211,14 @@ fn get_lang_items(tcx: TyCtxt<'_>, (): ()) -> LanguageItems {
let crate_items = tcx.hir_crate_items(()); let crate_items = tcx.hir_crate_items(());
for id in crate_items.items() { for id in crate_items.items() {
collector.check_for_lang(Target::from_def_kind(tcx.def_kind(id.owner_id)), id.hir_id()); collector
.check_for_lang(Target::from_def_kind(tcx.def_kind(id.owner_id)), id.owner_id.def_id);
if matches!(tcx.def_kind(id.owner_id), DefKind::Enum) { if matches!(tcx.def_kind(id.owner_id), DefKind::Enum) {
let item = tcx.hir().item(id); let item = tcx.hir().item(id);
if let hir::ItemKind::Enum(def, ..) = &item.kind { if let hir::ItemKind::Enum(def, ..) = &item.kind {
for variant in def.variants { for variant in def.variants {
collector.check_for_lang(Target::Variant, variant.hir_id); collector.check_for_lang(Target::Variant, variant.def_id);
} }
} }
} }
@ -226,13 +227,13 @@ fn get_lang_items(tcx: TyCtxt<'_>, (): ()) -> LanguageItems {
// FIXME: avoid calling trait_item() when possible // FIXME: avoid calling trait_item() when possible
for id in crate_items.trait_items() { for id in crate_items.trait_items() {
let item = tcx.hir().trait_item(id); let item = tcx.hir().trait_item(id);
collector.check_for_lang(Target::from_trait_item(item), item.hir_id()) collector.check_for_lang(Target::from_trait_item(item), item.owner_id.def_id)
} }
// FIXME: avoid calling impl_item() when possible // FIXME: avoid calling impl_item() when possible
for id in crate_items.impl_items() { for id in crate_items.impl_items() {
let item = tcx.hir().impl_item(id); let item = tcx.hir().impl_item(id);
collector.check_for_lang(target_from_impl_item(tcx, item), item.hir_id()) collector.check_for_lang(target_from_impl_item(tcx, item), item.owner_id.def_id)
} }
// Extract out the found lang items. // Extract out the found lang items.

View file

@ -855,7 +855,8 @@ impl<'tcx> Visitor<'tcx> for Checker<'tcx> {
/// See issue #94972 for details on why this is a special case /// See issue #94972 for details on why this is a special case
fn is_unstable_reexport(tcx: TyCtxt<'_>, id: hir::HirId) -> bool { fn is_unstable_reexport(tcx: TyCtxt<'_>, id: hir::HirId) -> bool {
// Get the LocalDefId so we can lookup the item to check the kind. // Get the LocalDefId so we can lookup the item to check the kind.
let Some(def_id) = tcx.hir().opt_local_def_id(id) else { return false; }; let Some(owner) = id.as_owner() else { return false; };
let def_id = owner.def_id;
let Some(stab) = tcx.stability().local_stability(def_id) else { let Some(stab) = tcx.stability().local_stability(def_id) else {
return false; return false;

View file

@ -18,7 +18,7 @@ use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::intern::Interned; use rustc_data_structures::intern::Interned;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res}; use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{DefId, LocalDefId, LocalDefIdSet, CRATE_DEF_ID}; use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{AssocItemKind, HirIdSet, ItemId, Node, PatKind}; use rustc_hir::{AssocItemKind, HirIdSet, ItemId, Node, PatKind};
use rustc_middle::bug; use rustc_middle::bug;
@ -1879,7 +1879,7 @@ impl<'tcx> DefIdVisitor<'tcx> for SearchInterfaceForPrivateItemsVisitor<'tcx> {
struct PrivateItemsInPublicInterfacesChecker<'tcx> { struct PrivateItemsInPublicInterfacesChecker<'tcx> {
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
old_error_set_ancestry: LocalDefIdSet, old_error_set_ancestry: HirIdSet,
} }
impl<'tcx> PrivateItemsInPublicInterfacesChecker<'tcx> { impl<'tcx> PrivateItemsInPublicInterfacesChecker<'tcx> {
@ -1892,7 +1892,9 @@ impl<'tcx> PrivateItemsInPublicInterfacesChecker<'tcx> {
tcx: self.tcx, tcx: self.tcx,
item_def_id: def_id, item_def_id: def_id,
required_visibility, required_visibility,
has_old_errors: self.old_error_set_ancestry.contains(&def_id), has_old_errors: self
.old_error_set_ancestry
.contains(&self.tcx.hir().local_def_id_to_hir_id(def_id)),
in_assoc_ty: false, in_assoc_ty: false,
} }
} }
@ -2158,15 +2160,7 @@ fn check_private_in_public(tcx: TyCtxt<'_>, (): ()) {
} }
// Check for private types and traits in public interfaces. // Check for private types and traits in public interfaces.
let mut checker = PrivateItemsInPublicInterfacesChecker { let mut checker = PrivateItemsInPublicInterfacesChecker { tcx, old_error_set_ancestry };
tcx,
// Only definition IDs are ever searched in `old_error_set_ancestry`,
// so we can filter away all non-definition IDs at this point.
old_error_set_ancestry: old_error_set_ancestry
.into_iter()
.filter_map(|hir_id| tcx.hir().opt_local_def_id(hir_id))
.collect(),
};
for id in tcx.hir().items() { for id in tcx.hir().items() {
checker.check_item(id); checker.check_item(id);

View file

@ -211,7 +211,7 @@ impl<'tcx> DumpVisitor<'tcx> {
None => continue, None => continue,
}; };
if !self.span.filter_generated(ident.span) { if !self.span.filter_generated(ident.span) {
let id = id_from_hir_id(hir_id, &self.save_ctxt); let id = id_from_hir_id(hir_id);
let span = self.span_from_span(ident.span); let span = self.span_from_span(ident.span);
self.dumper.dump_def( self.dumper.dump_def(
@ -240,17 +240,17 @@ impl<'tcx> DumpVisitor<'tcx> {
&mut self, &mut self,
sig: &'tcx hir::FnSig<'tcx>, sig: &'tcx hir::FnSig<'tcx>,
body: Option<hir::BodyId>, body: Option<hir::BodyId>,
def_id: LocalDefId, owner_id: hir::OwnerId,
ident: Ident, ident: Ident,
generics: &'tcx hir::Generics<'tcx>, generics: &'tcx hir::Generics<'tcx>,
span: Span, span: Span,
) { ) {
debug!("process_method: {:?}:{}", def_id, ident); debug!("process_method: {:?}:{}", owner_id, ident);
let map = self.tcx.hir(); let map = self.tcx.hir();
let hir_id = map.local_def_id_to_hir_id(def_id); let hir_id: hir::HirId = owner_id.into();
self.nest_typeck_results(def_id, |v| { self.nest_typeck_results(owner_id.def_id, |v| {
if let Some(mut method_data) = v.save_ctxt.get_method_data(hir_id, ident, span) { if let Some(mut method_data) = v.save_ctxt.get_method_data(owner_id, ident, span) {
if let Some(body) = body { if let Some(body) = body {
v.process_formals(map.body(body).params, &method_data.qualname); v.process_formals(map.body(body).params, &method_data.qualname);
} }
@ -258,9 +258,10 @@ impl<'tcx> DumpVisitor<'tcx> {
method_data.value = method_data.value =
fn_to_string(sig.decl, sig.header, Some(ident.name), generics, &[], None); fn_to_string(sig.decl, sig.header, Some(ident.name), generics, &[], None);
method_data.sig = sig::method_signature(hir_id, ident, generics, sig, &v.save_ctxt); method_data.sig =
sig::method_signature(owner_id, ident, generics, sig, &v.save_ctxt);
v.dumper.dump_def(&access_from!(v.save_ctxt, def_id), method_data); v.dumper.dump_def(&access_from!(v.save_ctxt, owner_id.def_id), method_data);
} }
// walk arg and return types // walk arg and return types
@ -282,14 +283,11 @@ impl<'tcx> DumpVisitor<'tcx> {
fn process_struct_field_def( fn process_struct_field_def(
&mut self, &mut self,
field: &'tcx hir::FieldDef<'tcx>, field: &'tcx hir::FieldDef<'tcx>,
parent_id: hir::HirId, parent_id: LocalDefId,
) { ) {
let field_data = self.save_ctxt.get_field_data(field, parent_id); let field_data = self.save_ctxt.get_field_data(field, parent_id);
if let Some(field_data) = field_data { if let Some(field_data) = field_data {
self.dumper.dump_def( self.dumper.dump_def(&access_from!(self.save_ctxt, field.def_id), field_data);
&access_from!(self.save_ctxt, self.tcx.hir().local_def_id(field.hir_id)),
field_data,
);
} }
} }
@ -309,7 +307,7 @@ impl<'tcx> DumpVisitor<'tcx> {
// Append $id to name to make sure each one is unique. // Append $id to name to make sure each one is unique.
let qualname = format!("{}::{}${}", prefix, name, id); let qualname = format!("{}::{}${}", prefix, name, id);
if !self.span.filter_generated(param_ss) { if !self.span.filter_generated(param_ss) {
let id = id_from_hir_id(param.hir_id, &self.save_ctxt); let id = id_from_def_id(param.def_id.to_def_id());
let span = self.span_from_span(param_ss); let span = self.span_from_span(param_ss);
self.dumper.dump_def( self.dumper.dump_def(
@ -387,25 +385,24 @@ impl<'tcx> DumpVisitor<'tcx> {
fn process_assoc_const( fn process_assoc_const(
&mut self, &mut self,
def_id: LocalDefId, owner_id: hir::OwnerId,
ident: Ident, ident: Ident,
typ: &'tcx hir::Ty<'tcx>, typ: &'tcx hir::Ty<'tcx>,
expr: Option<&'tcx hir::Expr<'tcx>>, expr: Option<&'tcx hir::Expr<'tcx>>,
parent_id: DefId, parent_id: DefId,
attrs: &'tcx [ast::Attribute], attrs: &'tcx [ast::Attribute],
) { ) {
let qualname = format!("::{}", self.tcx.def_path_str(def_id.to_def_id())); let qualname = format!("::{}", self.tcx.def_path_str(owner_id.to_def_id()));
if !self.span.filter_generated(ident.span) { if !self.span.filter_generated(ident.span) {
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id); let sig = sig::assoc_const_signature(owner_id, ident.name, typ, expr, &self.save_ctxt);
let sig = sig::assoc_const_signature(hir_id, ident.name, typ, expr, &self.save_ctxt);
let span = self.span_from_span(ident.span); let span = self.span_from_span(ident.span);
self.dumper.dump_def( self.dumper.dump_def(
&access_from!(self.save_ctxt, def_id), &access_from!(self.save_ctxt, owner_id.def_id),
Def { Def {
kind: DefKind::Const, kind: DefKind::Const,
id: id_from_hir_id(hir_id, &self.save_ctxt), id: id_from_def_id(owner_id.to_def_id()),
span, span,
name: ident.name.to_string(), name: ident.name.to_string(),
qualname, qualname,
@ -421,7 +418,7 @@ impl<'tcx> DumpVisitor<'tcx> {
} }
// walk type and init value // walk type and init value
self.nest_typeck_results(def_id, |v| { self.nest_typeck_results(owner_id.def_id, |v| {
v.visit_ty(typ); v.visit_ty(typ);
if let Some(expr) = expr { if let Some(expr) = expr {
v.visit_expr(expr); v.visit_expr(expr);
@ -456,8 +453,7 @@ impl<'tcx> DumpVisitor<'tcx> {
if include_priv_fields { if include_priv_fields {
return Some(f.ident.to_string()); return Some(f.ident.to_string());
} }
let def_id = self.save_ctxt.tcx.hir().local_def_id(f.hir_id); if self.save_ctxt.tcx.visibility(f.def_id).is_public() {
if self.save_ctxt.tcx.visibility(def_id).is_public() {
Some(f.ident.to_string()) Some(f.ident.to_string())
} else { } else {
None None
@ -466,7 +462,7 @@ impl<'tcx> DumpVisitor<'tcx> {
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(", "); .join(", ");
let value = format!("{} {{ {} }}", name, fields_str); let value = format!("{} {{ {} }}", name, fields_str);
(value, fields.iter().map(|f| id_from_hir_id(f.hir_id, &self.save_ctxt)).collect()) (value, fields.iter().map(|f| id_from_def_id(f.def_id.to_def_id())).collect())
} }
_ => (String::new(), vec![]), _ => (String::new(), vec![]),
}; };
@ -495,7 +491,7 @@ impl<'tcx> DumpVisitor<'tcx> {
self.nest_typeck_results(item.owner_id.def_id, |v| { self.nest_typeck_results(item.owner_id.def_id, |v| {
for field in def.fields() { for field in def.fields() {
v.process_struct_field_def(field, item.hir_id()); v.process_struct_field_def(field, item.owner_id.def_id);
v.visit_ty(&field.ty); v.visit_ty(&field.ty);
} }
@ -529,7 +525,7 @@ impl<'tcx> DumpVisitor<'tcx> {
let value = format!("{}::{} {{ {} }}", enum_data.name, name, fields_str); let value = format!("{}::{} {{ {} }}", enum_data.name, name, fields_str);
if !self.span.filter_generated(name_span) { if !self.span.filter_generated(name_span) {
let span = self.span_from_span(name_span); let span = self.span_from_span(name_span);
let id = id_from_hir_id(variant.hir_id, &self.save_ctxt); let id = id_from_def_id(variant.def_id.to_def_id());
let parent = Some(id_from_def_id(item.owner_id.to_def_id())); let parent = Some(id_from_def_id(item.owner_id.to_def_id()));
let attrs = self.tcx.hir().attrs(variant.hir_id); let attrs = self.tcx.hir().attrs(variant.hir_id);
@ -567,7 +563,7 @@ impl<'tcx> DumpVisitor<'tcx> {
} }
if !self.span.filter_generated(name_span) { if !self.span.filter_generated(name_span) {
let span = self.span_from_span(name_span); let span = self.span_from_span(name_span);
let id = id_from_hir_id(variant.hir_id, &self.save_ctxt); let id = id_from_def_id(variant.def_id.to_def_id());
let parent = Some(id_from_def_id(item.owner_id.to_def_id())); let parent = Some(id_from_def_id(item.owner_id.to_def_id()));
let attrs = self.tcx.hir().attrs(variant.hir_id); let attrs = self.tcx.hir().attrs(variant.hir_id);
@ -593,7 +589,7 @@ impl<'tcx> DumpVisitor<'tcx> {
} }
for field in variant.data.fields() { for field in variant.data.fields() {
self.process_struct_field_def(field, variant.hir_id); self.process_struct_field_def(field, variant.def_id);
self.visit_ty(field.ty); self.visit_ty(field.ty);
} }
} }
@ -883,7 +879,7 @@ impl<'tcx> DumpVisitor<'tcx> {
// Rust uses the id of the pattern for var lookups, so we'll use it too. // Rust uses the id of the pattern for var lookups, so we'll use it too.
if !self.span.filter_generated(ident.span) { if !self.span.filter_generated(ident.span) {
let qualname = format!("{}${}", ident, hir_id); let qualname = format!("{}${}", ident, hir_id);
let id = id_from_hir_id(hir_id, &self.save_ctxt); let id = id_from_hir_id(hir_id);
let span = self.span_from_span(ident.span); let span = self.span_from_span(ident.span);
self.dumper.dump_def( self.dumper.dump_def(
@ -983,7 +979,7 @@ impl<'tcx> DumpVisitor<'tcx> {
let body = body.map(|b| self.tcx.hir().body(b).value); let body = body.map(|b| self.tcx.hir().body(b).value);
let attrs = self.tcx.hir().attrs(trait_item.hir_id()); let attrs = self.tcx.hir().attrs(trait_item.hir_id());
self.process_assoc_const( self.process_assoc_const(
trait_item.owner_id.def_id, trait_item.owner_id,
trait_item.ident, trait_item.ident,
&ty, &ty,
body, body,
@ -997,7 +993,7 @@ impl<'tcx> DumpVisitor<'tcx> {
self.process_method( self.process_method(
sig, sig,
body, body,
trait_item.owner_id.def_id, trait_item.owner_id,
trait_item.ident, trait_item.ident,
&trait_item.generics, &trait_item.generics,
trait_item.span, trait_item.span,
@ -1028,7 +1024,7 @@ impl<'tcx> DumpVisitor<'tcx> {
decl_id: None, decl_id: None,
docs: self.save_ctxt.docs_for_attrs(attrs), docs: self.save_ctxt.docs_for_attrs(attrs),
sig: sig::assoc_type_signature( sig: sig::assoc_type_signature(
trait_item.hir_id(), trait_item.owner_id,
trait_item.ident, trait_item.ident,
Some(bounds), Some(bounds),
default_ty.as_deref(), default_ty.as_deref(),
@ -1053,7 +1049,7 @@ impl<'tcx> DumpVisitor<'tcx> {
let body = self.tcx.hir().body(body); let body = self.tcx.hir().body(body);
let attrs = self.tcx.hir().attrs(impl_item.hir_id()); let attrs = self.tcx.hir().attrs(impl_item.hir_id());
self.process_assoc_const( self.process_assoc_const(
impl_item.owner_id.def_id, impl_item.owner_id,
impl_item.ident, impl_item.ident,
&ty, &ty,
Some(&body.value), Some(&body.value),
@ -1065,7 +1061,7 @@ impl<'tcx> DumpVisitor<'tcx> {
self.process_method( self.process_method(
sig, sig,
Some(body), Some(body),
impl_item.owner_id.def_id, impl_item.owner_id,
impl_item.ident, impl_item.ident,
&impl_item.generics, &impl_item.generics,
impl_item.span, impl_item.span,
@ -1081,18 +1077,16 @@ impl<'tcx> DumpVisitor<'tcx> {
} }
pub(crate) fn process_crate(&mut self) { pub(crate) fn process_crate(&mut self) {
let id = hir::CRATE_HIR_ID; let qualname = format!("::{}", self.tcx.def_path_str(CRATE_DEF_ID.to_def_id()));
let qualname =
format!("::{}", self.tcx.def_path_str(self.tcx.hir().local_def_id(id).to_def_id()));
let sm = self.tcx.sess.source_map(); let sm = self.tcx.sess.source_map();
let krate_mod = self.tcx.hir().root_module(); let krate_mod = self.tcx.hir().root_module();
let filename = sm.span_to_filename(krate_mod.spans.inner_span); let filename = sm.span_to_filename(krate_mod.spans.inner_span);
let data_id = id_from_hir_id(id, &self.save_ctxt); let data_id = id_from_def_id(CRATE_DEF_ID.to_def_id());
let children = let children =
krate_mod.item_ids.iter().map(|i| id_from_def_id(i.owner_id.to_def_id())).collect(); krate_mod.item_ids.iter().map(|i| id_from_def_id(i.owner_id.to_def_id())).collect();
let span = self.span_from_span(krate_mod.spans.inner_span); let span = self.span_from_span(krate_mod.spans.inner_span);
let attrs = self.tcx.hir().attrs(id); let attrs = self.tcx.hir().attrs(hir::CRATE_HIR_ID);
self.dumper.dump_def( self.dumper.dump_def(
&Access { public: true, reachable: true }, &Access { public: true, reachable: true },
@ -1319,7 +1313,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
// output the inferred type here? :shrug: // output the inferred type here? :shrug:
hir::ArrayLen::Infer(..) => {} hir::ArrayLen::Infer(..) => {}
hir::ArrayLen::Body(anon_const) => self hir::ArrayLen::Body(anon_const) => self
.nest_typeck_results(self.tcx.hir().local_def_id(anon_const.hir_id), |v| { .nest_typeck_results(anon_const.def_id, |v| {
v.visit_expr(&map.body(anon_const.body).value) v.visit_expr(&map.body(anon_const.body).value)
}), }),
} }
@ -1361,7 +1355,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
} }
} }
} }
hir::ExprKind::Closure(&hir::Closure { ref fn_decl, body, .. }) => { hir::ExprKind::Closure(&hir::Closure { ref fn_decl, body, def_id, .. }) => {
let id = format!("${}", ex.hir_id); let id = format!("${}", ex.hir_id);
// walk arg and return types // walk arg and return types
@ -1375,7 +1369,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
// walk the body // walk the body
let map = self.tcx.hir(); let map = self.tcx.hir();
self.nest_typeck_results(self.tcx.hir().local_def_id(ex.hir_id), |v| { self.nest_typeck_results(def_id, |v| {
let body = map.body(body); let body = map.body(body);
v.process_formals(body.params, &id); v.process_formals(body.params, &id);
v.visit_expr(&body.value) v.visit_expr(&body.value)
@ -1389,7 +1383,7 @@ impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
// output the inferred type here? :shrug: // output the inferred type here? :shrug:
hir::ArrayLen::Infer(..) => {} hir::ArrayLen::Infer(..) => {}
hir::ArrayLen::Body(anon_const) => self hir::ArrayLen::Body(anon_const) => self
.nest_typeck_results(self.tcx.hir().local_def_id(anon_const.hir_id), |v| { .nest_typeck_results(anon_const.def_id, |v| {
v.visit_expr(&map.body(anon_const.body).value) v.visit_expr(&map.body(anon_const.body).value)
}), }),
} }

View file

@ -21,7 +21,7 @@ use rustc_ast::util::comments::beautify_doc_string;
use rustc_ast_pretty::pprust::attribute_to_string; use rustc_ast_pretty::pprust::attribute_to_string;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::{DefKind as HirDefKind, Res}; use rustc_hir::def::{DefKind as HirDefKind, Res};
use rustc_hir::def_id::{DefId, LOCAL_CRATE}; use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::Node; use rustc_hir::Node;
use rustc_hir_pretty::{enum_def_to_string, fn_to_string, ty_to_string}; use rustc_hir_pretty::{enum_def_to_string, fn_to_string, ty_to_string};
@ -318,7 +318,7 @@ impl<'tcx> SaveContext<'tcx> {
qualname, qualname,
value, value,
parent: None, parent: None,
children: def.variants.iter().map(|v| id_from_hir_id(v.hir_id, self)).collect(), children: def.variants.iter().map(|v| id_from_def_id(v.def_id.to_def_id())).collect(),
decl_id: None, decl_id: None,
docs: self.docs_for_attrs(attrs), docs: self.docs_for_attrs(attrs),
sig: sig::item_signature(item, self), sig: sig::item_signature(item, self),
@ -379,12 +379,11 @@ impl<'tcx> SaveContext<'tcx> {
} }
} }
pub fn get_field_data(&self, field: &hir::FieldDef<'_>, scope: hir::HirId) -> Option<Def> { pub fn get_field_data(&self, field: &hir::FieldDef<'_>, scope: LocalDefId) -> Option<Def> {
let name = field.ident.to_string(); let name = field.ident.to_string();
let scope_def_id = self.tcx.hir().local_def_id(scope).to_def_id(); let qualname = format!("::{}::{}", self.tcx.def_path_str(scope.to_def_id()), field.ident);
let qualname = format!("::{}::{}", self.tcx.def_path_str(scope_def_id), field.ident);
filter!(self.span_utils, field.ident.span); filter!(self.span_utils, field.ident.span);
let field_def_id = self.tcx.hir().local_def_id(field.hir_id).to_def_id(); let field_def_id = field.def_id.to_def_id();
let typ = self.tcx.type_of(field_def_id).to_string(); let typ = self.tcx.type_of(field_def_id).to_string();
let id = id_from_def_id(field_def_id); let id = id_from_def_id(field_def_id);
@ -398,7 +397,7 @@ impl<'tcx> SaveContext<'tcx> {
name, name,
qualname, qualname,
value: typ, value: typ,
parent: Some(id_from_def_id(scope_def_id)), parent: Some(id_from_def_id(scope.to_def_id())),
children: vec![], children: vec![],
decl_id: None, decl_id: None,
docs: self.docs_for_attrs(attrs), docs: self.docs_for_attrs(attrs),
@ -409,12 +408,11 @@ impl<'tcx> SaveContext<'tcx> {
// FIXME would be nice to take a MethodItem here, but the ast provides both // FIXME would be nice to take a MethodItem here, but the ast provides both
// trait and impl flavours, so the caller must do the disassembly. // trait and impl flavours, so the caller must do the disassembly.
pub fn get_method_data(&self, hir_id: hir::HirId, ident: Ident, span: Span) -> Option<Def> { pub fn get_method_data(&self, owner_id: hir::OwnerId, ident: Ident, span: Span) -> Option<Def> {
// The qualname for a method is the trait name or name of the struct in an impl in // The qualname for a method is the trait name or name of the struct in an impl in
// which the method is declared in, followed by the method's name. // which the method is declared in, followed by the method's name.
let def_id = self.tcx.hir().local_def_id(hir_id).to_def_id();
let (qualname, parent_scope, decl_id, docs, attributes) = let (qualname, parent_scope, decl_id, docs, attributes) =
match self.tcx.impl_of_method(def_id) { match self.tcx.impl_of_method(owner_id.to_def_id()) {
Some(impl_id) => match self.tcx.hir().get_if_local(impl_id) { Some(impl_id) => match self.tcx.hir().get_if_local(impl_id) {
Some(Node::Item(item)) => match item.kind { Some(Node::Item(item)) => match item.kind {
hir::ItemKind::Impl(hir::Impl { ref self_ty, .. }) => { hir::ItemKind::Impl(hir::Impl { ref self_ty, .. }) => {
@ -427,8 +425,8 @@ impl<'tcx> SaveContext<'tcx> {
let trait_id = self.tcx.trait_id_of_impl(impl_id); let trait_id = self.tcx.trait_id_of_impl(impl_id);
let mut docs = String::new(); let mut docs = String::new();
let mut attrs = vec![]; let mut attrs = vec![];
if let Some(Node::ImplItem(_)) = hir.find(hir_id) { if let Some(Node::ImplItem(_)) = hir.find(owner_id.into()) {
attrs = self.tcx.hir().attrs(hir_id).to_vec(); attrs = self.tcx.hir().attrs(owner_id.into()).to_vec();
docs = self.docs_for_attrs(&attrs); docs = self.docs_for_attrs(&attrs);
} }
@ -452,29 +450,29 @@ impl<'tcx> SaveContext<'tcx> {
_ => { _ => {
span_bug!( span_bug!(
span, span,
"Container {:?} for method {} not an impl?", "Container {:?} for method {:?} not an impl?",
impl_id, impl_id,
hir_id owner_id,
); );
} }
}, },
r => { r => {
span_bug!( span_bug!(
span, span,
"Container {:?} for method {} is not a node item {:?}", "Container {:?} for method {:?} is not a node item {:?}",
impl_id, impl_id,
hir_id, owner_id,
r r
); );
} }
}, },
None => match self.tcx.trait_of_item(def_id) { None => match self.tcx.trait_of_item(owner_id.to_def_id()) {
Some(def_id) => { Some(def_id) => {
let mut docs = String::new(); let mut docs = String::new();
let mut attrs = vec![]; let mut attrs = vec![];
if let Some(Node::TraitItem(_)) = self.tcx.hir().find(hir_id) { if let Some(Node::TraitItem(_)) = self.tcx.hir().find(owner_id.into()) {
attrs = self.tcx.hir().attrs(hir_id).to_vec(); attrs = self.tcx.hir().attrs(owner_id.into()).to_vec();
docs = self.docs_for_attrs(&attrs); docs = self.docs_for_attrs(&attrs);
} }
@ -487,7 +485,7 @@ impl<'tcx> SaveContext<'tcx> {
) )
} }
None => { None => {
debug!("could not find container for method {} at {:?}", hir_id, span); debug!("could not find container for method {:?} at {:?}", owner_id, span);
// This is not necessarily a bug, if there was a compilation error, // This is not necessarily a bug, if there was a compilation error,
// the typeck results we need might not exist. // the typeck results we need might not exist.
return None; return None;
@ -501,7 +499,7 @@ impl<'tcx> SaveContext<'tcx> {
Some(Def { Some(Def {
kind: DefKind::Method, kind: DefKind::Method,
id: id_from_def_id(def_id), id: id_from_def_id(owner_id.to_def_id()),
span: self.span_from_span(ident.span), span: self.span_from_span(ident.span),
name: ident.name.to_string(), name: ident.name.to_string(),
qualname, qualname,
@ -669,7 +667,7 @@ impl<'tcx> SaveContext<'tcx> {
match res { match res {
Res::Local(id) => { Res::Local(id) => {
Some(Ref { kind: RefKind::Variable, span, ref_id: id_from_hir_id(id, self) }) Some(Ref { kind: RefKind::Variable, span, ref_id: id_from_hir_id(id) })
} }
Res::Def(HirDefKind::Trait, def_id) if fn_type(path_seg) => { Res::Def(HirDefKind::Trait, def_id) if fn_type(path_seg) => {
Some(Ref { kind: RefKind::Type, span, ref_id: id_from_def_id(def_id) }) Some(Ref { kind: RefKind::Type, span, ref_id: id_from_def_id(def_id) })
@ -1033,18 +1031,15 @@ fn id_from_def_id(id: DefId) -> rls_data::Id {
rls_data::Id { krate: id.krate.as_u32(), index: id.index.as_u32() } rls_data::Id { krate: id.krate.as_u32(), index: id.index.as_u32() }
} }
fn id_from_hir_id(id: hir::HirId, scx: &SaveContext<'_>) -> rls_data::Id { fn id_from_hir_id(id: hir::HirId) -> rls_data::Id {
let def_id = scx.tcx.hir().opt_local_def_id(id); // Create a *fake* `DefId` out of a `HirId` by combining the owner
def_id.map(|id| id_from_def_id(id.to_def_id())).unwrap_or_else(|| { // `local_def_index` and the `local_id`.
// Create a *fake* `DefId` out of a `HirId` by combining the owner // This will work unless you have *billions* of definitions in a single
// `local_def_index` and the `local_id`. // crate (very unlikely to actually happen).
// This will work unless you have *billions* of definitions in a single rls_data::Id {
// crate (very unlikely to actually happen). krate: LOCAL_CRATE.as_u32(),
rls_data::Id { index: id.owner.def_id.local_def_index.as_u32() | id.local_id.as_u32().reverse_bits(),
krate: LOCAL_CRATE.as_u32(), }
index: id.owner.def_id.local_def_index.as_u32() | id.local_id.as_u32().reverse_bits(),
}
})
} }
fn null_id() -> rls_data::Id { fn null_id() -> rls_data::Id {

View file

@ -25,7 +25,7 @@
// //
// FIXME where clauses need implementing, defs/refs in generics are mostly missing. // FIXME where clauses need implementing, defs/refs in generics are mostly missing.
use crate::{id_from_def_id, id_from_hir_id, SaveContext}; use crate::{id_from_def_id, SaveContext};
use rls_data::{SigElement, Signature}; use rls_data::{SigElement, Signature};
@ -34,6 +34,7 @@ use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res}; use rustc_hir::def::{DefKind, Res};
use rustc_hir_pretty::id_to_string; use rustc_hir_pretty::id_to_string;
use rustc_hir_pretty::{bounds_to_string, path_segment_to_string, path_to_string, ty_to_string}; use rustc_hir_pretty::{bounds_to_string, path_segment_to_string, path_to_string, ty_to_string};
use rustc_span::def_id::LocalDefId;
use rustc_span::symbol::{Ident, Symbol}; use rustc_span::symbol::{Ident, Symbol};
pub fn item_signature(item: &hir::Item<'_>, scx: &SaveContext<'_>) -> Option<Signature> { pub fn item_signature(item: &hir::Item<'_>, scx: &SaveContext<'_>) -> Option<Signature> {
@ -71,7 +72,7 @@ pub fn variant_signature(variant: &hir::Variant<'_>, scx: &SaveContext<'_>) -> O
} }
pub fn method_signature( pub fn method_signature(
id: hir::HirId, id: hir::OwnerId,
ident: Ident, ident: Ident,
generics: &hir::Generics<'_>, generics: &hir::Generics<'_>,
m: &hir::FnSig<'_>, m: &hir::FnSig<'_>,
@ -84,7 +85,7 @@ pub fn method_signature(
} }
pub fn assoc_const_signature( pub fn assoc_const_signature(
id: hir::HirId, id: hir::OwnerId,
ident: Symbol, ident: Symbol,
ty: &hir::Ty<'_>, ty: &hir::Ty<'_>,
default: Option<&hir::Expr<'_>>, default: Option<&hir::Expr<'_>>,
@ -97,7 +98,7 @@ pub fn assoc_const_signature(
} }
pub fn assoc_type_signature( pub fn assoc_type_signature(
id: hir::HirId, id: hir::OwnerId,
ident: Ident, ident: Ident,
bounds: Option<hir::GenericBounds<'_>>, bounds: Option<hir::GenericBounds<'_>>,
default: Option<&hir::Ty<'_>>, default: Option<&hir::Ty<'_>>,
@ -112,7 +113,8 @@ pub fn assoc_type_signature(
type Result = std::result::Result<Signature, &'static str>; type Result = std::result::Result<Signature, &'static str>;
trait Sig { trait Sig {
fn make(&self, offset: usize, id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result; type Parent;
fn make(&self, offset: usize, id: Option<Self::Parent>, scx: &SaveContext<'_>) -> Result;
} }
fn extend_sig( fn extend_sig(
@ -148,6 +150,7 @@ fn text_sig(text: String) -> Signature {
} }
impl<'hir> Sig for hir::Ty<'hir> { impl<'hir> Sig for hir::Ty<'hir> {
type Parent = hir::HirId;
fn make(&self, offset: usize, _parent_id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result { fn make(&self, offset: usize, _parent_id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result {
let id = Some(self.hir_id); let id = Some(self.hir_id);
match self.kind { match self.kind {
@ -326,6 +329,7 @@ impl<'hir> Sig for hir::Ty<'hir> {
} }
impl<'hir> Sig for hir::Item<'hir> { impl<'hir> Sig for hir::Item<'hir> {
type Parent = hir::HirId;
fn make(&self, offset: usize, _parent_id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result { fn make(&self, offset: usize, _parent_id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result {
let id = Some(self.hir_id()); let id = Some(self.hir_id());
@ -391,7 +395,7 @@ impl<'hir> Sig for hir::Item<'hir> {
text.push_str("fn "); text.push_str("fn ");
let mut sig = let mut sig =
name_and_generics(text, offset, generics, self.hir_id(), self.ident, scx)?; name_and_generics(text, offset, generics, self.owner_id, self.ident, scx)?;
sig.text.push('('); sig.text.push('(');
for i in decl.inputs { for i in decl.inputs {
@ -441,7 +445,7 @@ impl<'hir> Sig for hir::Item<'hir> {
hir::ItemKind::TyAlias(ref ty, ref generics) => { hir::ItemKind::TyAlias(ref ty, ref generics) => {
let text = "type ".to_owned(); let text = "type ".to_owned();
let mut sig = let mut sig =
name_and_generics(text, offset, generics, self.hir_id(), self.ident, scx)?; name_and_generics(text, offset, generics, self.owner_id, self.ident, scx)?;
sig.text.push_str(" = "); sig.text.push_str(" = ");
let ty = ty.make(offset + sig.text.len(), id, scx)?; let ty = ty.make(offset + sig.text.len(), id, scx)?;
@ -453,21 +457,21 @@ impl<'hir> Sig for hir::Item<'hir> {
hir::ItemKind::Enum(_, ref generics) => { hir::ItemKind::Enum(_, ref generics) => {
let text = "enum ".to_owned(); let text = "enum ".to_owned();
let mut sig = let mut sig =
name_and_generics(text, offset, generics, self.hir_id(), self.ident, scx)?; name_and_generics(text, offset, generics, self.owner_id, self.ident, scx)?;
sig.text.push_str(" {}"); sig.text.push_str(" {}");
Ok(sig) Ok(sig)
} }
hir::ItemKind::Struct(_, ref generics) => { hir::ItemKind::Struct(_, ref generics) => {
let text = "struct ".to_owned(); let text = "struct ".to_owned();
let mut sig = let mut sig =
name_and_generics(text, offset, generics, self.hir_id(), self.ident, scx)?; name_and_generics(text, offset, generics, self.owner_id, self.ident, scx)?;
sig.text.push_str(" {}"); sig.text.push_str(" {}");
Ok(sig) Ok(sig)
} }
hir::ItemKind::Union(_, ref generics) => { hir::ItemKind::Union(_, ref generics) => {
let text = "union ".to_owned(); let text = "union ".to_owned();
let mut sig = let mut sig =
name_and_generics(text, offset, generics, self.hir_id(), self.ident, scx)?; name_and_generics(text, offset, generics, self.owner_id, self.ident, scx)?;
sig.text.push_str(" {}"); sig.text.push_str(" {}");
Ok(sig) Ok(sig)
} }
@ -483,7 +487,7 @@ impl<'hir> Sig for hir::Item<'hir> {
} }
text.push_str("trait "); text.push_str("trait ");
let mut sig = let mut sig =
name_and_generics(text, offset, generics, self.hir_id(), self.ident, scx)?; name_and_generics(text, offset, generics, self.owner_id, self.ident, scx)?;
if !bounds.is_empty() { if !bounds.is_empty() {
sig.text.push_str(": "); sig.text.push_str(": ");
@ -498,7 +502,7 @@ impl<'hir> Sig for hir::Item<'hir> {
let mut text = String::new(); let mut text = String::new();
text.push_str("trait "); text.push_str("trait ");
let mut sig = let mut sig =
name_and_generics(text, offset, generics, self.hir_id(), self.ident, scx)?; name_and_generics(text, offset, generics, self.owner_id, self.ident, scx)?;
if !bounds.is_empty() { if !bounds.is_empty() {
sig.text.push_str(" = "); sig.text.push_str(" = ");
@ -532,7 +536,8 @@ impl<'hir> Sig for hir::Item<'hir> {
text.push_str(" const"); text.push_str(" const");
} }
let generics_sig = generics.make(offset + text.len(), id, scx)?; let generics_sig =
generics.make(offset + text.len(), Some(self.owner_id.def_id), scx)?;
text.push_str(&generics_sig.text); text.push_str(&generics_sig.text);
text.push(' '); text.push(' ');
@ -575,6 +580,7 @@ impl<'hir> Sig for hir::Item<'hir> {
} }
impl<'hir> Sig for hir::Path<'hir> { impl<'hir> Sig for hir::Path<'hir> {
type Parent = hir::HirId;
fn make(&self, offset: usize, id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result { fn make(&self, offset: usize, id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result {
let res = scx.get_path_res(id.ok_or("Missing id for Path")?); let res = scx.get_path_res(id.ok_or("Missing id for Path")?);
@ -609,7 +615,8 @@ impl<'hir> Sig for hir::Path<'hir> {
// This does not cover the where clause, which must be processed separately. // This does not cover the where clause, which must be processed separately.
impl<'hir> Sig for hir::Generics<'hir> { impl<'hir> Sig for hir::Generics<'hir> {
fn make(&self, offset: usize, _parent_id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result { type Parent = LocalDefId;
fn make(&self, offset: usize, _parent_id: Option<LocalDefId>, scx: &SaveContext<'_>) -> Result {
if self.params.is_empty() { if self.params.is_empty() {
return Ok(text_sig(String::new())); return Ok(text_sig(String::new()));
} }
@ -624,7 +631,7 @@ impl<'hir> Sig for hir::Generics<'hir> {
} }
param_text.push_str(param.name.ident().as_str()); param_text.push_str(param.name.ident().as_str());
defs.push(SigElement { defs.push(SigElement {
id: id_from_hir_id(param.hir_id, scx), id: id_from_def_id(param.def_id.to_def_id()),
start: offset + text.len(), start: offset + text.len(),
end: offset + text.len() + param_text.as_str().len(), end: offset + text.len() + param_text.as_str().len(),
}); });
@ -646,12 +653,13 @@ impl<'hir> Sig for hir::Generics<'hir> {
} }
impl<'hir> Sig for hir::FieldDef<'hir> { impl<'hir> Sig for hir::FieldDef<'hir> {
fn make(&self, offset: usize, _parent_id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result { type Parent = LocalDefId;
fn make(&self, offset: usize, _parent_id: Option<LocalDefId>, scx: &SaveContext<'_>) -> Result {
let mut text = String::new(); let mut text = String::new();
text.push_str(&self.ident.to_string()); text.push_str(&self.ident.to_string());
let defs = Some(SigElement { let defs = Some(SigElement {
id: id_from_hir_id(self.hir_id, scx), id: id_from_def_id(self.def_id.to_def_id()),
start: offset, start: offset,
end: offset + text.len(), end: offset + text.len(),
}); });
@ -666,13 +674,14 @@ impl<'hir> Sig for hir::FieldDef<'hir> {
} }
impl<'hir> Sig for hir::Variant<'hir> { impl<'hir> Sig for hir::Variant<'hir> {
fn make(&self, offset: usize, parent_id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result { type Parent = LocalDefId;
fn make(&self, offset: usize, parent_id: Option<LocalDefId>, scx: &SaveContext<'_>) -> Result {
let mut text = self.ident.to_string(); let mut text = self.ident.to_string();
match self.data { match self.data {
hir::VariantData::Struct(fields, r) => { hir::VariantData::Struct(fields, r) => {
let id = parent_id.ok_or("Missing id for Variant's parent")?; let id = parent_id.ok_or("Missing id for Variant's parent")?;
let name_def = SigElement { let name_def = SigElement {
id: id_from_hir_id(id, scx), id: id_from_def_id(id.to_def_id()),
start: offset, start: offset,
end: offset + text.len(), end: offset + text.len(),
}; };
@ -693,9 +702,9 @@ impl<'hir> Sig for hir::Variant<'hir> {
text.push('}'); text.push('}');
Ok(Signature { text, defs, refs }) Ok(Signature { text, defs, refs })
} }
hir::VariantData::Tuple(fields, id, _) => { hir::VariantData::Tuple(fields, _, def_id) => {
let name_def = SigElement { let name_def = SigElement {
id: id_from_hir_id(id, scx), id: id_from_def_id(def_id.to_def_id()),
start: offset, start: offset,
end: offset + text.len(), end: offset + text.len(),
}; };
@ -703,7 +712,7 @@ impl<'hir> Sig for hir::Variant<'hir> {
let mut defs = vec![name_def]; let mut defs = vec![name_def];
let mut refs = vec![]; let mut refs = vec![];
for f in fields { for f in fields {
let field_sig = f.make(offset + text.len(), Some(id), scx)?; let field_sig = f.make(offset + text.len(), Some(def_id), scx)?;
text.push_str(&field_sig.text); text.push_str(&field_sig.text);
text.push_str(", "); text.push_str(", ");
defs.extend(field_sig.defs.into_iter()); defs.extend(field_sig.defs.into_iter());
@ -712,9 +721,9 @@ impl<'hir> Sig for hir::Variant<'hir> {
text.push(')'); text.push(')');
Ok(Signature { text, defs, refs }) Ok(Signature { text, defs, refs })
} }
hir::VariantData::Unit(id, _) => { hir::VariantData::Unit(_, def_id) => {
let name_def = SigElement { let name_def = SigElement {
id: id_from_hir_id(id, scx), id: id_from_def_id(def_id.to_def_id()),
start: offset, start: offset,
end: offset + text.len(), end: offset + text.len(),
}; };
@ -725,6 +734,7 @@ impl<'hir> Sig for hir::Variant<'hir> {
} }
impl<'hir> Sig for hir::ForeignItem<'hir> { impl<'hir> Sig for hir::ForeignItem<'hir> {
type Parent = hir::HirId;
fn make(&self, offset: usize, _parent_id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result { fn make(&self, offset: usize, _parent_id: Option<hir::HirId>, scx: &SaveContext<'_>) -> Result {
let id = Some(self.hir_id()); let id = Some(self.hir_id());
match self.kind { match self.kind {
@ -733,7 +743,7 @@ impl<'hir> Sig for hir::ForeignItem<'hir> {
text.push_str("fn "); text.push_str("fn ");
let mut sig = let mut sig =
name_and_generics(text, offset, generics, self.hir_id(), self.ident, scx)?; name_and_generics(text, offset, generics, self.owner_id, self.ident, scx)?;
sig.text.push('('); sig.text.push('(');
for i in decl.inputs { for i in decl.inputs {
@ -797,25 +807,25 @@ fn name_and_generics(
mut text: String, mut text: String,
offset: usize, offset: usize,
generics: &hir::Generics<'_>, generics: &hir::Generics<'_>,
id: hir::HirId, id: hir::OwnerId,
name: Ident, name: Ident,
scx: &SaveContext<'_>, scx: &SaveContext<'_>,
) -> Result { ) -> Result {
let name = name.to_string(); let name = name.to_string();
let def = SigElement { let def = SigElement {
id: id_from_hir_id(id, scx), id: id_from_def_id(id.to_def_id()),
start: offset + text.len(), start: offset + text.len(),
end: offset + text.len() + name.len(), end: offset + text.len() + name.len(),
}; };
text.push_str(&name); text.push_str(&name);
let generics: Signature = generics.make(offset + text.len(), Some(id), scx)?; let generics: Signature = generics.make(offset + text.len(), Some(id.def_id), scx)?;
// FIXME where clause // FIXME where clause
let text = format!("{}{}", text, generics.text); let text = format!("{}{}", text, generics.text);
Ok(extend_sig(generics, text, vec![def], vec![])) Ok(extend_sig(generics, text, vec![def], vec![]))
} }
fn make_assoc_type_signature( fn make_assoc_type_signature(
id: hir::HirId, id: hir::OwnerId,
ident: Ident, ident: Ident,
bounds: Option<hir::GenericBounds<'_>>, bounds: Option<hir::GenericBounds<'_>>,
default: Option<&hir::Ty<'_>>, default: Option<&hir::Ty<'_>>,
@ -824,7 +834,7 @@ fn make_assoc_type_signature(
let mut text = "type ".to_owned(); let mut text = "type ".to_owned();
let name = ident.to_string(); let name = ident.to_string();
let mut defs = vec![SigElement { let mut defs = vec![SigElement {
id: id_from_hir_id(id, scx), id: id_from_def_id(id.to_def_id()),
start: text.len(), start: text.len(),
end: text.len() + name.len(), end: text.len() + name.len(),
}]; }];
@ -837,7 +847,7 @@ fn make_assoc_type_signature(
} }
if let Some(default) = default { if let Some(default) = default {
text.push_str(" = "); text.push_str(" = ");
let ty_sig = default.make(text.len(), Some(id), scx)?; let ty_sig = default.make(text.len(), Some(id.into()), scx)?;
text.push_str(&ty_sig.text); text.push_str(&ty_sig.text);
defs.extend(ty_sig.defs.into_iter()); defs.extend(ty_sig.defs.into_iter());
refs.extend(ty_sig.refs.into_iter()); refs.extend(ty_sig.refs.into_iter());
@ -847,7 +857,7 @@ fn make_assoc_type_signature(
} }
fn make_assoc_const_signature( fn make_assoc_const_signature(
id: hir::HirId, id: hir::OwnerId,
ident: Symbol, ident: Symbol,
ty: &hir::Ty<'_>, ty: &hir::Ty<'_>,
default: Option<&hir::Expr<'_>>, default: Option<&hir::Expr<'_>>,
@ -856,7 +866,7 @@ fn make_assoc_const_signature(
let mut text = "const ".to_owned(); let mut text = "const ".to_owned();
let name = ident.to_string(); let name = ident.to_string();
let mut defs = vec![SigElement { let mut defs = vec![SigElement {
id: id_from_hir_id(id, scx), id: id_from_def_id(id.to_def_id()),
start: text.len(), start: text.len(),
end: text.len() + name.len(), end: text.len() + name.len(),
}]; }];
@ -864,7 +874,7 @@ fn make_assoc_const_signature(
text.push_str(&name); text.push_str(&name);
text.push_str(": "); text.push_str(": ");
let ty_sig = ty.make(text.len(), Some(id), scx)?; let ty_sig = ty.make(text.len(), Some(id.into()), scx)?;
text.push_str(&ty_sig.text); text.push_str(&ty_sig.text);
defs.extend(ty_sig.defs.into_iter()); defs.extend(ty_sig.defs.into_iter());
refs.extend(ty_sig.refs.into_iter()); refs.extend(ty_sig.refs.into_iter());
@ -878,7 +888,7 @@ fn make_assoc_const_signature(
} }
fn make_method_signature( fn make_method_signature(
id: hir::HirId, id: hir::OwnerId,
ident: Ident, ident: Ident,
generics: &hir::Generics<'_>, generics: &hir::Generics<'_>,
m: &hir::FnSig<'_>, m: &hir::FnSig<'_>,

View file

@ -399,7 +399,7 @@ fn predicate_constraint(generics: &hir::Generics<'_>, pred: ty::Predicate<'_>) -
/// param for cleaner code. /// param for cleaner code.
fn suggest_restriction<'tcx>( fn suggest_restriction<'tcx>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
hir_id: HirId, item_id: LocalDefId,
hir_generics: &hir::Generics<'tcx>, hir_generics: &hir::Generics<'tcx>,
msg: &str, msg: &str,
err: &mut Diagnostic, err: &mut Diagnostic,
@ -418,7 +418,6 @@ fn suggest_restriction<'tcx>(
{ {
return; return;
} }
let Some(item_id) = hir_id.as_owner() else { return; };
let generics = tcx.generics_of(item_id); let generics = tcx.generics_of(item_id);
// Given `fn foo(t: impl Trait)` where `Trait` requires assoc type `A`... // Given `fn foo(t: impl Trait)` where `Trait` requires assoc type `A`...
if let Some((param, bound_str, fn_sig)) = if let Some((param, bound_str, fn_sig)) =
@ -523,7 +522,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
mut err: &mut Diagnostic, mut err: &mut Diagnostic,
trait_pred: ty::PolyTraitPredicate<'tcx>, trait_pred: ty::PolyTraitPredicate<'tcx>,
associated_ty: Option<(&'static str, Ty<'tcx>)>, associated_ty: Option<(&'static str, Ty<'tcx>)>,
body_id: LocalDefId, mut body_id: LocalDefId,
) { ) {
let trait_pred = self.resolve_numeric_literals_with_default(trait_pred); let trait_pred = self.resolve_numeric_literals_with_default(trait_pred);
@ -536,9 +535,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
// FIXME: Add check for trait bound that is already present, particularly `?Sized` so we // FIXME: Add check for trait bound that is already present, particularly `?Sized` so we
// don't suggest `T: Sized + ?Sized`. // don't suggest `T: Sized + ?Sized`.
let mut body_id = body_id;
while let Some(node) = self.tcx.hir().find_by_def_id(body_id) { while let Some(node) = self.tcx.hir().find_by_def_id(body_id) {
let hir_id = self.tcx.hir().local_def_id_to_hir_id(body_id);
match node { match node {
hir::Node::Item(hir::Item { hir::Node::Item(hir::Item {
ident, ident,
@ -549,7 +546,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
// Restricting `Self` for a single method. // Restricting `Self` for a single method.
suggest_restriction( suggest_restriction(
self.tcx, self.tcx,
hir_id, body_id,
&generics, &generics,
"`Self`", "`Self`",
err, err,
@ -569,7 +566,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
assert!(param_ty); assert!(param_ty);
// Restricting `Self` for a single method. // Restricting `Self` for a single method.
suggest_restriction( suggest_restriction(
self.tcx, hir_id, &generics, "`Self`", err, None, projection, trait_pred, self.tcx, body_id, &generics, "`Self`", err, None, projection, trait_pred,
None, None,
); );
return; return;
@ -591,7 +588,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
// Missing restriction on associated type of type parameter (unmet projection). // Missing restriction on associated type of type parameter (unmet projection).
suggest_restriction( suggest_restriction(
self.tcx, self.tcx,
hir_id, body_id,
&generics, &generics,
"the associated type", "the associated type",
err, err,
@ -611,7 +608,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
// Missing restriction on associated type of type parameter (unmet projection). // Missing restriction on associated type of type parameter (unmet projection).
suggest_restriction( suggest_restriction(
self.tcx, self.tcx,
hir_id, body_id,
&generics, &generics,
"the associated type", "the associated type",
err, err,

View file

@ -383,13 +383,10 @@ fn clean_middle_term<'tcx>(
fn clean_hir_term<'tcx>(term: &hir::Term<'tcx>, cx: &mut DocContext<'tcx>) -> Term { fn clean_hir_term<'tcx>(term: &hir::Term<'tcx>, cx: &mut DocContext<'tcx>) -> Term {
match term { match term {
hir::Term::Ty(ty) => Term::Type(clean_ty(ty, cx)), hir::Term::Ty(ty) => Term::Type(clean_ty(ty, cx)),
hir::Term::Const(c) => { hir::Term::Const(c) => Term::Constant(clean_middle_const(
let def_id = cx.tcx.hir().local_def_id(c.hir_id); ty::Binder::dummy(ty::Const::from_anon_const(cx.tcx, c.def_id)),
Term::Constant(clean_middle_const( cx,
ty::Binder::dummy(ty::Const::from_anon_const(cx.tcx, def_id)), )),
cx,
))
}
} }
} }
@ -524,12 +521,11 @@ fn clean_generic_param<'tcx>(
generics: Option<&hir::Generics<'tcx>>, generics: Option<&hir::Generics<'tcx>>,
param: &hir::GenericParam<'tcx>, param: &hir::GenericParam<'tcx>,
) -> GenericParamDef { ) -> GenericParamDef {
let did = cx.tcx.hir().local_def_id(param.hir_id);
let (name, kind) = match param.kind { let (name, kind) = match param.kind {
hir::GenericParamKind::Lifetime { .. } => { hir::GenericParamKind::Lifetime { .. } => {
let outlives = if let Some(generics) = generics { let outlives = if let Some(generics) = generics {
generics generics
.outlives_for_param(did) .outlives_for_param(param.def_id)
.filter(|bp| !bp.in_where_clause) .filter(|bp| !bp.in_where_clause)
.flat_map(|bp| bp.bounds) .flat_map(|bp| bp.bounds)
.map(|bound| match bound { .map(|bound| match bound {
@ -545,7 +541,7 @@ fn clean_generic_param<'tcx>(
hir::GenericParamKind::Type { ref default, synthetic } => { hir::GenericParamKind::Type { ref default, synthetic } => {
let bounds = if let Some(generics) = generics { let bounds = if let Some(generics) = generics {
generics generics
.bounds_for_param(did) .bounds_for_param(param.def_id)
.filter(|bp| bp.origin != PredicateOrigin::WhereClause) .filter(|bp| bp.origin != PredicateOrigin::WhereClause)
.flat_map(|bp| bp.bounds) .flat_map(|bp| bp.bounds)
.filter_map(|x| clean_generic_bound(x, cx)) .filter_map(|x| clean_generic_bound(x, cx))
@ -556,7 +552,7 @@ fn clean_generic_param<'tcx>(
( (
param.name.ident().name, param.name.ident().name,
GenericParamDefKind::Type { GenericParamDefKind::Type {
did: did.to_def_id(), did: param.def_id.to_def_id(),
bounds, bounds,
default: default.map(|t| clean_ty(t, cx)).map(Box::new), default: default.map(|t| clean_ty(t, cx)).map(Box::new),
synthetic, synthetic,
@ -566,12 +562,10 @@ fn clean_generic_param<'tcx>(
hir::GenericParamKind::Const { ty, default } => ( hir::GenericParamKind::Const { ty, default } => (
param.name.ident().name, param.name.ident().name,
GenericParamDefKind::Const { GenericParamDefKind::Const {
did: did.to_def_id(), did: param.def_id.to_def_id(),
ty: Box::new(clean_ty(ty, cx)), ty: Box::new(clean_ty(ty, cx)),
default: default.map(|ct| { default: default
let def_id = cx.tcx.hir().local_def_id(ct.hir_id); .map(|ct| Box::new(ty::Const::from_anon_const(cx.tcx, ct.def_id).to_string())),
Box::new(ty::Const::from_anon_const(cx.tcx, def_id).to_string())
}),
}, },
), ),
}; };
@ -1548,18 +1542,16 @@ fn maybe_expand_private_type_alias<'tcx>(
_ => None, _ => None,
}); });
if let Some(lt) = lifetime { if let Some(lt) = lifetime {
let lt_def_id = cx.tcx.hir().local_def_id(param.hir_id);
let cleaned = if !lt.is_anonymous() { let cleaned = if !lt.is_anonymous() {
clean_lifetime(lt, cx) clean_lifetime(lt, cx)
} else { } else {
Lifetime::elided() Lifetime::elided()
}; };
substs.insert(lt_def_id.to_def_id(), SubstParam::Lifetime(cleaned)); substs.insert(param.def_id.to_def_id(), SubstParam::Lifetime(cleaned));
} }
indices.lifetimes += 1; indices.lifetimes += 1;
} }
hir::GenericParamKind::Type { ref default, .. } => { hir::GenericParamKind::Type { ref default, .. } => {
let ty_param_def_id = cx.tcx.hir().local_def_id(param.hir_id);
let mut j = 0; let mut j = 0;
let type_ = generic_args.args.iter().find_map(|arg| match arg { let type_ = generic_args.args.iter().find_map(|arg| match arg {
hir::GenericArg::Type(ty) => { hir::GenericArg::Type(ty) => {
@ -1572,17 +1564,14 @@ fn maybe_expand_private_type_alias<'tcx>(
_ => None, _ => None,
}); });
if let Some(ty) = type_ { if let Some(ty) = type_ {
substs.insert(ty_param_def_id.to_def_id(), SubstParam::Type(clean_ty(ty, cx))); substs.insert(param.def_id.to_def_id(), SubstParam::Type(clean_ty(ty, cx)));
} else if let Some(default) = *default { } else if let Some(default) = *default {
substs.insert( substs
ty_param_def_id.to_def_id(), .insert(param.def_id.to_def_id(), SubstParam::Type(clean_ty(default, cx)));
SubstParam::Type(clean_ty(default, cx)),
);
} }
indices.types += 1; indices.types += 1;
} }
hir::GenericParamKind::Const { .. } => { hir::GenericParamKind::Const { .. } => {
let const_param_def_id = cx.tcx.hir().local_def_id(param.hir_id);
let mut j = 0; let mut j = 0;
let const_ = generic_args.args.iter().find_map(|arg| match arg { let const_ = generic_args.args.iter().find_map(|arg| match arg {
hir::GenericArg::Const(ct) => { hir::GenericArg::Const(ct) => {
@ -1596,7 +1585,7 @@ fn maybe_expand_private_type_alias<'tcx>(
}); });
if let Some(ct) = const_ { if let Some(ct) = const_ {
substs.insert( substs.insert(
const_param_def_id.to_def_id(), param.def_id.to_def_id(),
SubstParam::Constant(clean_const(ct, cx)), SubstParam::Constant(clean_const(ct, cx)),
); );
} }
@ -1624,7 +1613,6 @@ pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> T
let length = match length { let length = match length {
hir::ArrayLen::Infer(_, _) => "_".to_string(), hir::ArrayLen::Infer(_, _) => "_".to_string(),
hir::ArrayLen::Body(anon_const) => { hir::ArrayLen::Body(anon_const) => {
let def_id = cx.tcx.hir().local_def_id(anon_const.hir_id);
// NOTE(min_const_generics): We can't use `const_eval_poly` for constants // NOTE(min_const_generics): We can't use `const_eval_poly` for constants
// as we currently do not supply the parent generics to anonymous constants // as we currently do not supply the parent generics to anonymous constants
// but do allow `ConstKind::Param`. // but do allow `ConstKind::Param`.
@ -1632,8 +1620,8 @@ pub(crate) fn clean_ty<'tcx>(ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> T
// `const_eval_poly` tries to first substitute generic parameters which // `const_eval_poly` tries to first substitute generic parameters which
// results in an ICE while manually constructing the constant and using `eval` // results in an ICE while manually constructing the constant and using `eval`
// does nothing for `ConstKind::Param`. // does nothing for `ConstKind::Param`.
let ct = ty::Const::from_anon_const(cx.tcx, def_id); let ct = ty::Const::from_anon_const(cx.tcx, anon_const.def_id);
let param_env = cx.tcx.param_env(def_id); let param_env = cx.tcx.param_env(anon_const.def_id);
print_const(cx, ct.eval(cx.tcx, param_env)) print_const(cx, ct.eval(cx.tcx, param_env))
} }
}; };
@ -1930,8 +1918,7 @@ fn clean_middle_opaque_bounds<'tcx>(
} }
pub(crate) fn clean_field<'tcx>(field: &hir::FieldDef<'tcx>, cx: &mut DocContext<'tcx>) -> Item { pub(crate) fn clean_field<'tcx>(field: &hir::FieldDef<'tcx>, cx: &mut DocContext<'tcx>) -> Item {
let def_id = cx.tcx.hir().local_def_id(field.hir_id).to_def_id(); clean_field_with_def_id(field.def_id.to_def_id(), field.ident.name, clean_ty(field.ty, cx), cx)
clean_field_with_def_id(def_id, field.ident.name, clean_ty(field.ty, cx), cx)
} }
pub(crate) fn clean_middle_field<'tcx>(field: &ty::FieldDef, cx: &mut DocContext<'tcx>) -> Item { pub(crate) fn clean_middle_field<'tcx>(field: &ty::FieldDef, cx: &mut DocContext<'tcx>) -> Item {
@ -1981,10 +1968,8 @@ fn clean_variant_data<'tcx>(
disr_expr: &Option<hir::AnonConst>, disr_expr: &Option<hir::AnonConst>,
cx: &mut DocContext<'tcx>, cx: &mut DocContext<'tcx>,
) -> Variant { ) -> Variant {
let discriminant = disr_expr.map(|disr| Discriminant { let discriminant = disr_expr
expr: Some(disr.body), .map(|disr| Discriminant { expr: Some(disr.body), value: disr.def_id.to_def_id() });
value: cx.tcx.hir().local_def_id(disr.hir_id).to_def_id(),
});
let kind = match variant { let kind = match variant {
hir::VariantData::Struct(..) => VariantKind::Struct(VariantStruct { hir::VariantData::Struct(..) => VariantKind::Struct(VariantStruct {

View file

@ -6,9 +6,10 @@ use if_chain::if_chain;
use rustc_ast::ast::LitKind; use rustc_ast::ast::LitKind;
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::intravisit::{walk_expr, FnKind, Visitor}; use rustc_hir::intravisit::{walk_expr, FnKind, Visitor};
use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, HirId, UnOp}; use rustc_hir::{BinOpKind, Body, Expr, ExprKind, FnDecl, UnOp};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::def_id::LocalDefId;
use rustc_span::source_map::Span; use rustc_span::source_map::Span;
use rustc_span::sym; use rustc_span::sym;
@ -82,7 +83,7 @@ impl<'tcx> LateLintPass<'tcx> for NonminimalBool {
_: &'tcx FnDecl<'_>, _: &'tcx FnDecl<'_>,
body: &'tcx Body<'_>, body: &'tcx Body<'_>,
_: Span, _: Span,
_: HirId, _: LocalDefId,
) { ) {
NonminimalBoolVisitor { cx }.visit_body(body); NonminimalBoolVisitor { cx }.visit_body(body);
} }

View file

@ -8,9 +8,10 @@ use clippy_utils::{get_async_fn_body, is_async_fn, LimitStack};
use core::ops::ControlFlow; use core::ops::ControlFlow;
use rustc_ast::ast::Attribute; use rustc_ast::ast::Attribute;
use rustc_hir::intravisit::FnKind; use rustc_hir::intravisit::FnKind;
use rustc_hir::{Body, Expr, ExprKind, FnDecl, HirId}; use rustc_hir::{Body, Expr, ExprKind, FnDecl};
use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::def_id::LocalDefId;
use rustc_span::source_map::Span; use rustc_span::source_map::Span;
use rustc_span::{sym, BytePos}; use rustc_span::{sym, BytePos};
@ -140,9 +141,8 @@ impl<'tcx> LateLintPass<'tcx> for CognitiveComplexity {
decl: &'tcx FnDecl<'_>, decl: &'tcx FnDecl<'_>,
body: &'tcx Body<'_>, body: &'tcx Body<'_>,
span: Span, span: Span,
hir_id: HirId, def_id: LocalDefId,
) { ) {
let def_id = cx.tcx.hir().local_def_id(hir_id);
if !cx.tcx.has_attr(def_id.to_def_id(), sym::test) { if !cx.tcx.has_attr(def_id.to_def_id(), sym::test) {
let expr = if is_async_fn(kind) { let expr = if is_async_fn(kind) {
match get_async_fn_body(cx.tcx, body) { match get_async_fn_body(cx.tcx, body) {

View file

@ -778,20 +778,20 @@ fn walk_parents<'tcx>(
Node::Expr(parent) if parent.span.ctxt() == ctxt => match parent.kind { Node::Expr(parent) if parent.span.ctxt() == ctxt => match parent.kind {
ExprKind::Ret(_) => { ExprKind::Ret(_) => {
let owner_id = cx.tcx.hir().body_owner(cx.enclosing_body.unwrap()); let owner_id = cx.tcx.hir().body_owner_def_id(cx.enclosing_body.unwrap());
Some( Some(
if let Node::Expr( if let Node::Expr(
closure_expr @ Expr { closure_expr @ Expr {
kind: ExprKind::Closure(closure), kind: ExprKind::Closure(closure),
.. ..
}, },
) = cx.tcx.hir().get(owner_id) ) = cx.tcx.hir().get_by_def_id(owner_id)
{ {
closure_result_position(cx, closure, cx.typeck_results().expr_ty(closure_expr), precedence) closure_result_position(cx, closure, cx.typeck_results().expr_ty(closure_expr), precedence)
} else { } else {
let output = cx let output = cx
.tcx .tcx
.erase_late_bound_regions(cx.tcx.fn_sig(cx.tcx.hir().local_def_id(owner_id)).subst_identity().output()); .erase_late_bound_regions(cx.tcx.fn_sig(owner_id).subst_identity().output());
ty_auto_deref_stability(cx, output, precedence).position_for_result(cx) ty_auto_deref_stability(cx, output, precedence).position_for_result(cx)
}, },
) )

View file

@ -7,7 +7,7 @@ use rustc_errors::Applicability;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_hir::intravisit::{walk_expr, walk_fn, walk_item, FnKind, Visitor}; use rustc_hir::intravisit::{walk_expr, walk_fn, walk_item, FnKind, Visitor};
use rustc_hir::{ use rustc_hir::{
self as hir, BlockCheckMode, BodyId, Constness, Expr, ExprKind, FnDecl, HirId, Impl, Item, ItemKind, UnsafeSource, self as hir, BlockCheckMode, BodyId, Constness, Expr, ExprKind, FnDecl, Impl, Item, ItemKind, UnsafeSource,
Unsafety, Unsafety,
}; };
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
@ -18,6 +18,7 @@ use rustc_middle::ty::{
TraitPredicate, Ty, TyCtxt, TraitPredicate, Ty, TyCtxt,
}; };
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::def_id::LocalDefId;
use rustc_span::source_map::Span; use rustc_span::source_map::Span;
use rustc_span::sym; use rustc_span::sym;
@ -425,7 +426,7 @@ struct UnsafeVisitor<'a, 'tcx> {
impl<'tcx> Visitor<'tcx> for UnsafeVisitor<'_, 'tcx> { impl<'tcx> Visitor<'tcx> for UnsafeVisitor<'_, 'tcx> {
type NestedFilter = nested_filter::All; type NestedFilter = nested_filter::All;
fn visit_fn(&mut self, kind: FnKind<'tcx>, decl: &'tcx FnDecl<'_>, body_id: BodyId, _: Span, id: HirId) { fn visit_fn(&mut self, kind: FnKind<'tcx>, decl: &'tcx FnDecl<'_>, body_id: BodyId, _: Span, id: LocalDefId) {
if self.has_unsafe { if self.has_unsafe {
return; return;
} }

View file

@ -23,7 +23,6 @@ use rustc_parse::maybe_new_parser_from_source_str;
use rustc_parse::parser::ForceCollect; use rustc_parse::parser::ForceCollect;
use rustc_session::parse::ParseSess; use rustc_session::parse::ParseSess;
use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::def_id::LocalDefId;
use rustc_span::edition::Edition; use rustc_span::edition::Edition;
use rustc_span::source_map::{BytePos, FilePathMapping, SourceMap, Span}; use rustc_span::source_map::{BytePos, FilePathMapping, SourceMap, Span};
use rustc_span::{sym, FileName, Pos}; use rustc_span::{sym, FileName, Pos};
@ -302,7 +301,7 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
panic_span: None, panic_span: None,
}; };
fpu.visit_expr(body.value); fpu.visit_expr(body.value);
lint_for_missing_headers(cx, item.owner_id.def_id, sig, headers, Some(body_id), fpu.panic_span); lint_for_missing_headers(cx, item.owner_id, sig, headers, Some(body_id), fpu.panic_span);
} }
}, },
hir::ItemKind::Impl(impl_) => { hir::ItemKind::Impl(impl_) => {
@ -338,7 +337,7 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
let Some(headers) = check_attrs(cx, &self.valid_idents, attrs) else { return }; let Some(headers) = check_attrs(cx, &self.valid_idents, attrs) else { return };
if let hir::TraitItemKind::Fn(ref sig, ..) = item.kind { if let hir::TraitItemKind::Fn(ref sig, ..) = item.kind {
if !in_external_macro(cx.tcx.sess, item.span) { if !in_external_macro(cx.tcx.sess, item.span) {
lint_for_missing_headers(cx, item.owner_id.def_id, sig, headers, None, None); lint_for_missing_headers(cx, item.owner_id, sig, headers, None, None);
} }
} }
} }
@ -357,20 +356,20 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
panic_span: None, panic_span: None,
}; };
fpu.visit_expr(body.value); fpu.visit_expr(body.value);
lint_for_missing_headers(cx, item.owner_id.def_id, sig, headers, Some(body_id), fpu.panic_span); lint_for_missing_headers(cx, item.owner_id, sig, headers, Some(body_id), fpu.panic_span);
} }
} }
} }
fn lint_for_missing_headers( fn lint_for_missing_headers(
cx: &LateContext<'_>, cx: &LateContext<'_>,
def_id: LocalDefId, owner_id: hir::OwnerId,
sig: &hir::FnSig<'_>, sig: &hir::FnSig<'_>,
headers: DocHeaders, headers: DocHeaders,
body_id: Option<hir::BodyId>, body_id: Option<hir::BodyId>,
panic_span: Option<Span>, panic_span: Option<Span>,
) { ) {
if !cx.effective_visibilities.is_exported(def_id) { if !cx.effective_visibilities.is_exported(owner_id.def_id) {
return; // Private functions do not require doc comments return; // Private functions do not require doc comments
} }
@ -378,13 +377,13 @@ fn lint_for_missing_headers(
if cx if cx
.tcx .tcx
.hir() .hir()
.parent_iter(cx.tcx.hir().local_def_id_to_hir_id(def_id)) .parent_iter(owner_id.into())
.any(|(id, _node)| is_doc_hidden(cx.tcx.hir().attrs(id))) .any(|(id, _node)| is_doc_hidden(cx.tcx.hir().attrs(id)))
{ {
return; return;
} }
let span = cx.tcx.def_span(def_id); let span = cx.tcx.def_span(owner_id);
match (headers.safety, sig.header.unsafety) { match (headers.safety, sig.header.unsafety) {
(false, hir::Unsafety::Unsafe) => span_lint( (false, hir::Unsafety::Unsafe) => span_lint(
cx, cx,
@ -411,8 +410,7 @@ fn lint_for_missing_headers(
); );
} }
if !headers.errors { if !headers.errors {
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id); if is_type_diagnostic_item(cx, return_ty(cx, owner_id), sym::Result) {
if is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym::Result) {
span_lint( span_lint(
cx, cx,
MISSING_ERRORS_DOC, MISSING_ERRORS_DOC,

View file

@ -8,6 +8,7 @@ use rustc_middle::mir::FakeReadCause;
use rustc_middle::ty::layout::LayoutOf; use rustc_middle::ty::layout::LayoutOf;
use rustc_middle::ty::{self, TraitRef, Ty}; use rustc_middle::ty::{self, TraitRef, Ty};
use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::def_id::LocalDefId;
use rustc_span::source_map::Span; use rustc_span::source_map::Span;
use rustc_span::symbol::kw; use rustc_span::symbol::kw;
use rustc_target::spec::abi::Abi; use rustc_target::spec::abi::Abi;
@ -63,7 +64,7 @@ impl<'tcx> LateLintPass<'tcx> for BoxedLocal {
_: &'tcx FnDecl<'_>, _: &'tcx FnDecl<'_>,
body: &'tcx Body<'_>, body: &'tcx Body<'_>,
_: Span, _: Span,
hir_id: HirId, fn_def_id: LocalDefId,
) { ) {
if let Some(header) = fn_kind.header() { if let Some(header) = fn_kind.header() {
if header.abi != Abi::Rust { if header.abi != Abi::Rust {
@ -71,7 +72,11 @@ impl<'tcx> LateLintPass<'tcx> for BoxedLocal {
} }
} }
let parent_id = cx.tcx.hir().get_parent_item(hir_id).def_id; let parent_id = cx
.tcx
.hir()
.get_parent_item(cx.tcx.hir().local_def_id_to_hir_id(fn_def_id))
.def_id;
let parent_node = cx.tcx.hir().find_by_def_id(parent_id); let parent_node = cx.tcx.hir().find_by_def_id(parent_id);
let mut trait_self_ty = None; let mut trait_self_ty = None;
@ -84,7 +89,7 @@ impl<'tcx> LateLintPass<'tcx> for BoxedLocal {
// find `self` ty for this trait if relevant // find `self` ty for this trait if relevant
if let ItemKind::Trait(_, _, _, _, items) = item.kind { if let ItemKind::Trait(_, _, _, _, items) = item.kind {
for trait_item in items { for trait_item in items {
if trait_item.id.hir_id() == hir_id { if trait_item.id.owner_id.def_id == fn_def_id {
// be sure we have `self` parameter in this function // be sure we have `self` parameter in this function
if trait_item.kind == (AssocItemKind::Fn { has_self: true }) { if trait_item.kind == (AssocItemKind::Fn { has_self: true }) {
trait_self_ty = Some( trait_self_ty = Some(
@ -105,7 +110,6 @@ impl<'tcx> LateLintPass<'tcx> for BoxedLocal {
too_large_for_stack: self.too_large_for_stack, too_large_for_stack: self.too_large_for_stack,
}; };
let fn_def_id = cx.tcx.hir().local_def_id(hir_id);
let infcx = cx.tcx.infer_ctxt().build(); let infcx = cx.tcx.infer_ctxt().build();
ExprUseVisitor::new(&mut v, &infcx, fn_def_id, cx.param_env, cx.typeck_results()).consume_body(body); ExprUseVisitor::new(&mut v, &infcx, fn_def_id, cx.param_env, cx.typeck_results()).consume_body(body);

View file

@ -1,10 +1,11 @@
use clippy_utils::diagnostics::span_lint_and_help; use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::{get_parent_as_impl, has_repr_attr, is_bool}; use clippy_utils::{get_parent_as_impl, has_repr_attr, is_bool};
use rustc_hir::intravisit::FnKind; use rustc_hir::intravisit::FnKind;
use rustc_hir::{Body, FnDecl, HirId, Item, ItemKind, TraitFn, TraitItem, TraitItemKind, Ty}; use rustc_hir::{Body, FnDecl, Item, ItemKind, TraitFn, TraitItem, TraitItemKind, Ty};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::Span; use rustc_span::Span;
use rustc_span::def_id::LocalDefId;
use rustc_target::spec::abi::Abi; use rustc_target::spec::abi::Abi;
declare_clippy_lint! { declare_clippy_lint! {
@ -168,8 +169,9 @@ impl<'tcx> LateLintPass<'tcx> for ExcessiveBools {
fn_decl: &'tcx FnDecl<'tcx>, fn_decl: &'tcx FnDecl<'tcx>,
_: &'tcx Body<'tcx>, _: &'tcx Body<'tcx>,
span: Span, span: Span,
hir_id: HirId, def_id: LocalDefId,
) { ) {
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id);
if let Some(fn_header) = fn_kind.header() if let Some(fn_header) = fn_kind.header()
&& fn_header.abi == Abi::Rust && fn_header.abi == Abi::Rust
&& get_parent_as_impl(cx.tcx, hir_id) && get_parent_as_impl(cx.tcx, hir_id)

View file

@ -79,8 +79,7 @@ impl LateLintPass<'_> for ExhaustiveItems {
then { then {
let (lint, msg) = if let ItemKind::Struct(ref v, ..) = item.kind { let (lint, msg) = if let ItemKind::Struct(ref v, ..) = item.kind {
if v.fields().iter().any(|f| { if v.fields().iter().any(|f| {
let def_id = cx.tcx.hir().local_def_id(f.hir_id); !cx.tcx.visibility(f.def_id).is_public()
!cx.tcx.visibility(def_id).is_public()
}) { }) {
// skip structs with private fields // skip structs with private fields
return; return;

View file

@ -1,7 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::snippet; use clippy_utils::source::snippet;
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::{intravisit::FnKind, Body, ExprKind, FnDecl, HirId, ImplicitSelfKind, Unsafety}; use rustc_hir::{intravisit::FnKind, Body, ExprKind, FnDecl, ImplicitSelfKind, Unsafety};
use rustc_lint::LateContext; use rustc_lint::LateContext;
use rustc_middle::ty; use rustc_middle::ty;
use rustc_span::Span; use rustc_span::Span;
@ -16,7 +16,6 @@ pub fn check_fn(
decl: &FnDecl<'_>, decl: &FnDecl<'_>,
body: &Body<'_>, body: &Body<'_>,
span: Span, span: Span,
_hir_id: HirId,
) { ) {
let FnKind::Method(ref ident, sig) = kind else { let FnKind::Method(ref ident, sig) = kind else {
return; return;

View file

@ -9,6 +9,7 @@ use rustc_hir as hir;
use rustc_hir::intravisit; use rustc_hir::intravisit;
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::def_id::LocalDefId;
use rustc_span::Span; use rustc_span::Span;
declare_clippy_lint! { declare_clippy_lint! {
@ -363,12 +364,13 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
decl: &'tcx hir::FnDecl<'_>, decl: &'tcx hir::FnDecl<'_>,
body: &'tcx hir::Body<'_>, body: &'tcx hir::Body<'_>,
span: Span, span: Span,
hir_id: hir::HirId, def_id: LocalDefId,
) { ) {
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id);
too_many_arguments::check_fn(cx, kind, decl, span, hir_id, self.too_many_arguments_threshold); too_many_arguments::check_fn(cx, kind, decl, span, hir_id, self.too_many_arguments_threshold);
too_many_lines::check_fn(cx, kind, span, body, self.too_many_lines_threshold); too_many_lines::check_fn(cx, kind, span, body, self.too_many_lines_threshold);
not_unsafe_ptr_arg_deref::check_fn(cx, kind, decl, body, hir_id); not_unsafe_ptr_arg_deref::check_fn(cx, kind, decl, body, def_id);
misnamed_getters::check_fn(cx, kind, decl, body, span, hir_id); misnamed_getters::check_fn(cx, kind, decl, body, span);
} }
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {

View file

@ -1,6 +1,6 @@
use rustc_ast::ast::Attribute; use rustc_ast::ast::Attribute;
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::def_id::{DefIdSet, LocalDefId}; use rustc_hir::def_id::DefIdSet;
use rustc_hir::{self as hir, def::Res, QPath}; use rustc_hir::{self as hir, def::Res, QPath};
use rustc_lint::{LateContext, LintContext}; use rustc_lint::{LateContext, LintContext};
use rustc_middle::{ use rustc_middle::{
@ -27,14 +27,14 @@ pub(super) fn check_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>
let is_public = cx.effective_visibilities.is_exported(item.owner_id.def_id); let is_public = cx.effective_visibilities.is_exported(item.owner_id.def_id);
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi()); let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
if let Some(attr) = attr { if let Some(attr) = attr {
check_needless_must_use(cx, sig.decl, item.hir_id(), item.span, fn_header_span, attr); check_needless_must_use(cx, sig.decl, item.owner_id, item.span, fn_header_span, attr);
} else if is_public && !is_proc_macro(cx.sess(), attrs) && !attrs.iter().any(|a| a.has_name(sym::no_mangle)) { } else if is_public && !is_proc_macro(cx.sess(), attrs) && !attrs.iter().any(|a| a.has_name(sym::no_mangle)) {
check_must_use_candidate( check_must_use_candidate(
cx, cx,
sig.decl, sig.decl,
cx.tcx.hir().body(*body_id), cx.tcx.hir().body(*body_id),
item.span, item.span,
item.owner_id.def_id, item.owner_id,
item.span.with_hi(sig.decl.output.span().hi()), item.span.with_hi(sig.decl.output.span().hi()),
"this function could have a `#[must_use]` attribute", "this function could have a `#[must_use]` attribute",
); );
@ -49,7 +49,7 @@ pub(super) fn check_impl_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Imp
let attrs = cx.tcx.hir().attrs(item.hir_id()); let attrs = cx.tcx.hir().attrs(item.hir_id());
let attr = cx.tcx.get_attr(item.owner_id.to_def_id(), sym::must_use); let attr = cx.tcx.get_attr(item.owner_id.to_def_id(), sym::must_use);
if let Some(attr) = attr { if let Some(attr) = attr {
check_needless_must_use(cx, sig.decl, item.hir_id(), item.span, fn_header_span, attr); check_needless_must_use(cx, sig.decl, item.owner_id, item.span, fn_header_span, attr);
} else if is_public } else if is_public
&& !is_proc_macro(cx.sess(), attrs) && !is_proc_macro(cx.sess(), attrs)
&& trait_ref_of_method(cx, item.owner_id.def_id).is_none() && trait_ref_of_method(cx, item.owner_id.def_id).is_none()
@ -59,7 +59,7 @@ pub(super) fn check_impl_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Imp
sig.decl, sig.decl,
cx.tcx.hir().body(*body_id), cx.tcx.hir().body(*body_id),
item.span, item.span,
item.owner_id.def_id, item.owner_id,
item.span.with_hi(sig.decl.output.span().hi()), item.span.with_hi(sig.decl.output.span().hi()),
"this method could have a `#[must_use]` attribute", "this method could have a `#[must_use]` attribute",
); );
@ -75,7 +75,7 @@ pub(super) fn check_trait_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Tr
let attrs = cx.tcx.hir().attrs(item.hir_id()); let attrs = cx.tcx.hir().attrs(item.hir_id());
let attr = cx.tcx.get_attr(item.owner_id.to_def_id(), sym::must_use); let attr = cx.tcx.get_attr(item.owner_id.to_def_id(), sym::must_use);
if let Some(attr) = attr { if let Some(attr) = attr {
check_needless_must_use(cx, sig.decl, item.hir_id(), item.span, fn_header_span, attr); check_needless_must_use(cx, sig.decl, item.owner_id, item.span, fn_header_span, attr);
} else if let hir::TraitFn::Provided(eid) = *eid { } else if let hir::TraitFn::Provided(eid) = *eid {
let body = cx.tcx.hir().body(eid); let body = cx.tcx.hir().body(eid);
if attr.is_none() && is_public && !is_proc_macro(cx.sess(), attrs) { if attr.is_none() && is_public && !is_proc_macro(cx.sess(), attrs) {
@ -84,7 +84,7 @@ pub(super) fn check_trait_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Tr
sig.decl, sig.decl,
body, body,
item.span, item.span,
item.owner_id.def_id, item.owner_id,
item.span.with_hi(sig.decl.output.span().hi()), item.span.with_hi(sig.decl.output.span().hi()),
"this method could have a `#[must_use]` attribute", "this method could have a `#[must_use]` attribute",
); );
@ -96,7 +96,7 @@ pub(super) fn check_trait_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Tr
fn check_needless_must_use( fn check_needless_must_use(
cx: &LateContext<'_>, cx: &LateContext<'_>,
decl: &hir::FnDecl<'_>, decl: &hir::FnDecl<'_>,
item_id: hir::HirId, item_id: hir::OwnerId,
item_span: Span, item_span: Span,
fn_header_span: Span, fn_header_span: Span,
attr: &Attribute, attr: &Attribute,
@ -131,7 +131,7 @@ fn check_must_use_candidate<'tcx>(
decl: &'tcx hir::FnDecl<'_>, decl: &'tcx hir::FnDecl<'_>,
body: &'tcx hir::Body<'_>, body: &'tcx hir::Body<'_>,
item_span: Span, item_span: Span,
item_id: LocalDefId, item_id: hir::OwnerId,
fn_span: Span, fn_span: Span,
msg: &str, msg: &str,
) { ) {
@ -139,8 +139,8 @@ fn check_must_use_candidate<'tcx>(
|| mutates_static(cx, body) || mutates_static(cx, body)
|| in_external_macro(cx.sess(), item_span) || in_external_macro(cx.sess(), item_span)
|| returns_unit(decl) || returns_unit(decl)
|| !cx.effective_visibilities.is_exported(item_id) || !cx.effective_visibilities.is_exported(item_id.def_id)
|| is_must_use_ty(cx, return_ty(cx, cx.tcx.hir().local_def_id_to_hir_id(item_id))) || is_must_use_ty(cx, return_ty(cx, item_id))
{ {
return; return;
} }

View file

@ -17,7 +17,7 @@ pub(super) fn check_fn<'tcx>(
kind: intravisit::FnKind<'tcx>, kind: intravisit::FnKind<'tcx>,
decl: &'tcx hir::FnDecl<'tcx>, decl: &'tcx hir::FnDecl<'tcx>,
body: &'tcx hir::Body<'tcx>, body: &'tcx hir::Body<'tcx>,
hir_id: hir::HirId, def_id: LocalDefId,
) { ) {
let unsafety = match kind { let unsafety = match kind {
intravisit::FnKind::ItemFn(_, _, hir::FnHeader { unsafety, .. }) => unsafety, intravisit::FnKind::ItemFn(_, _, hir::FnHeader { unsafety, .. }) => unsafety,
@ -25,7 +25,7 @@ pub(super) fn check_fn<'tcx>(
intravisit::FnKind::Closure => return, intravisit::FnKind::Closure => return,
}; };
check_raw_ptr(cx, unsafety, decl, body, cx.tcx.hir().local_def_id(hir_id)); check_raw_ptr(cx, unsafety, decl, body, def_id)
} }
pub(super) fn check_trait_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'_>) { pub(super) fn check_trait_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'_>) {

View file

@ -1,11 +1,12 @@
use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::return_ty; use clippy_utils::return_ty;
use rustc_hir::intravisit::FnKind; use rustc_hir::intravisit::FnKind;
use rustc_hir::{Body, FnDecl, HirId}; use rustc_hir::{Body, FnDecl};
use rustc_infer::infer::TyCtxtInferExt; use rustc_infer::infer::TyCtxtInferExt;
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::{self, AliasTy, Clause, EarlyBinder, PredicateKind}; use rustc_middle::ty::{self, AliasTy, Clause, EarlyBinder, PredicateKind};
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::def_id::LocalDefId;
use rustc_span::{sym, Span}; use rustc_span::{sym, Span};
use rustc_trait_selection::traits::error_reporting::suggestions::TypeErrCtxtExt; use rustc_trait_selection::traits::error_reporting::suggestions::TypeErrCtxtExt;
use rustc_trait_selection::traits::{self, FulfillmentError}; use rustc_trait_selection::traits::{self, FulfillmentError};
@ -56,12 +57,12 @@ impl<'tcx> LateLintPass<'tcx> for FutureNotSend {
decl: &'tcx FnDecl<'tcx>, decl: &'tcx FnDecl<'tcx>,
_: &'tcx Body<'tcx>, _: &'tcx Body<'tcx>,
_: Span, _: Span,
hir_id: HirId, fn_def_id: LocalDefId,
) { ) {
if let FnKind::Closure = kind { if let FnKind::Closure = kind {
return; return;
} }
let ret_ty = return_ty(cx, hir_id); let ret_ty = return_ty(cx, cx.tcx.hir().local_def_id_to_hir_id(fn_def_id).expect_owner());
if let ty::Alias(ty::Opaque, AliasTy { def_id, substs, .. }) = *ret_ty.kind() { if let ty::Alias(ty::Opaque, AliasTy { def_id, substs, .. }) = *ret_ty.kind() {
let preds = cx.tcx.explicit_item_bounds(def_id); let preds = cx.tcx.explicit_item_bounds(def_id);
let mut is_future = false; let mut is_future = false;
@ -78,8 +79,7 @@ impl<'tcx> LateLintPass<'tcx> for FutureNotSend {
let send_trait = cx.tcx.get_diagnostic_item(sym::Send).unwrap(); let send_trait = cx.tcx.get_diagnostic_item(sym::Send).unwrap();
let span = decl.output.span(); let span = decl.output.span();
let infcx = cx.tcx.infer_ctxt().build(); let infcx = cx.tcx.infer_ctxt().build();
let def_id = cx.tcx.hir().local_def_id(hir_id); let cause = traits::ObligationCause::misc(span, fn_def_id);
let cause = traits::ObligationCause::misc(span, def_id);
let send_errors = traits::fully_solve_bound(&infcx, cause, cx.param_env, ret_ty, send_trait); let send_errors = traits::fully_solve_bound(&infcx, cause, cx.param_env, ret_ty, send_trait);
if !send_errors.is_empty() { if !send_errors.is_empty() {
span_lint_and_then( span_lint_and_then(

View file

@ -11,6 +11,7 @@ use rustc_hir::{Block, Body, Expr, ExprKind, FnDecl, FnRetTy, HirId};
use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro; use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::def_id::LocalDefId;
use rustc_span::{Span, SyntaxContext}; use rustc_span::{Span, SyntaxContext};
declare_clippy_lint! { declare_clippy_lint! {
@ -223,7 +224,7 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitReturn {
decl: &'tcx FnDecl<'_>, decl: &'tcx FnDecl<'_>,
body: &'tcx Body<'_>, body: &'tcx Body<'_>,
span: Span, span: Span,
_: HirId, _: LocalDefId,
) { ) {
if (!matches!(kind, FnKind::Closure) && matches!(decl.output, FnRetTy::DefaultReturn(_))) if (!matches!(kind, FnKind::Closure) && matches!(decl.output, FnRetTy::DefaultReturn(_)))
|| span.ctxt() != body.value.span.ctxt() || span.ctxt() != body.value.span.ctxt()

View file

@ -105,7 +105,7 @@ impl<'tcx> LateLintPass<'tcx> for InherentToString {
if impl_item.generics.params.iter().all(|p| matches!(p.kind, GenericParamKind::Lifetime { .. })); if impl_item.generics.params.iter().all(|p| matches!(p.kind, GenericParamKind::Lifetime { .. }));
// Check if return type is String // Check if return type is String
if is_type_lang_item(cx, return_ty(cx, impl_item.hir_id()), LangItem::String); if is_type_lang_item(cx, return_ty(cx, impl_item.owner_id), LangItem::String);
// Filters instances of to_string which are required by a trait // Filters instances of to_string which are required by a trait
if trait_ref_of_method(cx, impl_item.owner_id.def_id).is_none(); if trait_ref_of_method(cx, impl_item.owner_id.def_id).is_none();

View file

@ -15,7 +15,6 @@ use rustc_hir::{
}; };
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::hir::nested_filter as middle_nested_filter; use rustc_middle::hir::nested_filter as middle_nested_filter;
use rustc_middle::ty::TyCtxt;
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::def_id::LocalDefId; use rustc_span::def_id::LocalDefId;
use rustc_span::source_map::Span; use rustc_span::source_map::Span;
@ -154,7 +153,7 @@ fn check_fn_inner<'tcx>(
.filter(|param| matches!(param.kind, GenericParamKind::Type { .. })); .filter(|param| matches!(param.kind, GenericParamKind::Type { .. }));
for typ in types { for typ in types {
for pred in generics.bounds_for_param(cx.tcx.hir().local_def_id(typ.hir_id)) { for pred in generics.bounds_for_param(typ.def_id) {
if pred.origin == PredicateOrigin::WhereClause { if pred.origin == PredicateOrigin::WhereClause {
// has_where_lifetimes checked that this predicate contains no lifetime. // has_where_lifetimes checked that this predicate contains no lifetime.
continue; continue;
@ -251,7 +250,7 @@ fn could_use_elision<'tcx>(
// level of the current item. // level of the current item.
// check named LTs // check named LTs
let allowed_lts = allowed_lts_from(cx.tcx, named_generics); let allowed_lts = allowed_lts_from(named_generics);
// these will collect all the lifetimes for references in arg/return types // these will collect all the lifetimes for references in arg/return types
let mut input_visitor = RefVisitor::new(cx); let mut input_visitor = RefVisitor::new(cx);
@ -360,11 +359,11 @@ fn could_use_elision<'tcx>(
} }
} }
fn allowed_lts_from(tcx: TyCtxt<'_>, named_generics: &[GenericParam<'_>]) -> FxHashSet<RefLt> { fn allowed_lts_from(named_generics: &[GenericParam<'_>]) -> FxHashSet<RefLt> {
let mut allowed_lts = FxHashSet::default(); let mut allowed_lts = FxHashSet::default();
for par in named_generics.iter() { for par in named_generics.iter() {
if let GenericParamKind::Lifetime { .. } = par.kind { if let GenericParamKind::Lifetime { .. } = par.kind {
allowed_lts.insert(RefLt::Named(tcx.hir().local_def_id(par.hir_id))); allowed_lts.insert(RefLt::Named(par.def_id));
} }
} }
allowed_lts.insert(RefLt::Unnamed); allowed_lts.insert(RefLt::Unnamed);
@ -516,7 +515,7 @@ fn has_where_lifetimes<'tcx>(cx: &LateContext<'tcx>, generics: &'tcx Generics<'_
return true; return true;
} }
// if the bounds define new lifetimes, they are fine to occur // if the bounds define new lifetimes, they are fine to occur
let allowed_lts = allowed_lts_from(cx.tcx, pred.bound_generic_params); let allowed_lts = allowed_lts_from(pred.bound_generic_params);
// now walk the bounds // now walk the bounds
for bound in pred.bounds.iter() { for bound in pred.bounds.iter() {
walk_param_bound(&mut visitor, bound); walk_param_bound(&mut visitor, bound);

View file

@ -6,10 +6,11 @@ use rustc_errors::Applicability;
use rustc_hir::intravisit::FnKind; use rustc_hir::intravisit::FnKind;
use rustc_hir::{ use rustc_hir::{
AsyncGeneratorKind, Block, Body, Closure, Expr, ExprKind, FnDecl, FnRetTy, GeneratorKind, GenericArg, GenericBound, AsyncGeneratorKind, Block, Body, Closure, Expr, ExprKind, FnDecl, FnRetTy, GeneratorKind, GenericArg, GenericBound,
HirId, ItemKind, LifetimeName, Term, TraitRef, Ty, TyKind, TypeBindingKind, ItemKind, LifetimeName, Term, TraitRef, Ty, TyKind, TypeBindingKind,
}; };
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::def_id::LocalDefId;
use rustc_span::{sym, Span}; use rustc_span::{sym, Span};
declare_clippy_lint! { declare_clippy_lint! {
@ -45,7 +46,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualAsyncFn {
decl: &'tcx FnDecl<'_>, decl: &'tcx FnDecl<'_>,
body: &'tcx Body<'_>, body: &'tcx Body<'_>,
span: Span, span: Span,
_: HirId, _: LocalDefId,
) { ) {
if_chain! { if_chain! {
if let Some(header) = kind.header(); if let Some(header) = kind.header();

View file

@ -157,11 +157,10 @@ impl<'tcx> LateLintPass<'tcx> for ManualNonExhaustiveEnum {
&& def.variants.len() > 1 && def.variants.len() > 1
{ {
let mut iter = def.variants.iter().filter_map(|v| { let mut iter = def.variants.iter().filter_map(|v| {
let id = cx.tcx.hir().local_def_id(v.hir_id); (matches!(v.data, hir::VariantData::Unit(_, _))
(matches!(v.data, hir::VariantData::Unit(..))
&& v.ident.as_str().starts_with('_') && v.ident.as_str().starts_with('_')
&& is_doc_hidden(cx.tcx.hir().attrs(v.hir_id))) && is_doc_hidden(cx.tcx.hir().attrs(v.hir_id)))
.then_some((id, v.span)) .then_some((v.def_id, v.span))
}); });
if let Some((id, span)) = iter.next() if let Some((id, span)) = iter.next()
&& iter.next().is_none() && iter.next().is_none()

View file

@ -3412,7 +3412,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
} }
if let hir::ImplItemKind::Fn(_, _) = impl_item.kind { if let hir::ImplItemKind::Fn(_, _) = impl_item.kind {
let ret_ty = return_ty(cx, impl_item.hir_id()); let ret_ty = return_ty(cx, impl_item.owner_id);
if contains_ty_adt_constructor_opaque(cx, ret_ty, self_ty) { if contains_ty_adt_constructor_opaque(cx, ret_ty, self_ty) {
return; return;
@ -3460,7 +3460,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
if_chain! { if_chain! {
if item.ident.name == sym::new; if item.ident.name == sym::new;
if let TraitItemKind::Fn(_, _) = item.kind; if let TraitItemKind::Fn(_, _) = item.kind;
let ret_ty = return_ty(cx, item.hir_id()); let ret_ty = return_ty(cx, item.owner_id);
let self_ty = TraitRef::identity(cx.tcx, item.owner_id.to_def_id()) let self_ty = TraitRef::identity(cx.tcx, item.owner_id.to_def_id())
.self_ty() .self_ty()
.skip_binder(); .skip_binder();

View file

@ -11,10 +11,8 @@ use super::SUSPICIOUS_MAP;
pub fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, count_recv: &hir::Expr<'_>, map_arg: &hir::Expr<'_>) { pub fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, count_recv: &hir::Expr<'_>, map_arg: &hir::Expr<'_>) {
if_chain! { if_chain! {
if is_trait_method(cx, count_recv, sym::Iterator); if is_trait_method(cx, count_recv, sym::Iterator);
let closure = expr_or_init(cx, map_arg); if let hir::ExprKind::Closure(closure) = expr_or_init(cx, map_arg).kind;
if let Some(def_id) = cx.tcx.hir().opt_local_def_id(closure.hir_id); let closure_body = cx.tcx.hir().body(closure.body);
if let Some(body_id) = cx.tcx.hir().maybe_body_owned_by(def_id);
let closure_body = cx.tcx.hir().body(body_id);
if !cx.typeck_results().expr_ty(closure_body.value).is_unit(); if !cx.typeck_results().expr_ty(closure_body.value).is_unit();
then { then {
if let Some(map_mutated_vars) = mutated_variables(closure_body.value, cx) { if let Some(map_mutated_vars) = mutated_variables(closure_body.value, cx) {

View file

@ -368,10 +368,9 @@ fn can_change_type<'a>(cx: &LateContext<'a>, mut expr: &'a Expr<'a>, mut ty: Ty<
Node::Block(..) => continue, Node::Block(..) => continue,
Node::Item(item) => { Node::Item(item) => {
if let ItemKind::Fn(_, _, body_id) = &item.kind if let ItemKind::Fn(_, _, body_id) = &item.kind
&& let output_ty = return_ty(cx, item.hir_id()) && let output_ty = return_ty(cx, item.owner_id)
&& let local_def_id = cx.tcx.hir().local_def_id(item.hir_id()) && Inherited::build(cx.tcx, item.owner_id.def_id).enter(|inherited| {
&& Inherited::build(cx.tcx, local_def_id).enter(|inherited| { let fn_ctxt = FnCtxt::new(inherited, cx.param_env, item.owner_id.def_id);
let fn_ctxt = FnCtxt::new(inherited, cx.param_env, local_def_id);
fn_ctxt.can_coerce(ty, output_ty) fn_ctxt.can_coerce(ty, output_ty)
}) { }) {
if has_lifetime(output_ty) && has_lifetime(ty) { if has_lifetime(output_ty) && has_lifetime(ty) {

View file

@ -4,12 +4,13 @@ use if_chain::if_chain;
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::intravisit::FnKind; use rustc_hir::intravisit::FnKind;
use rustc_hir::{ use rustc_hir::{
self as hir, def, BinOpKind, BindingAnnotation, Body, ByRef, Expr, ExprKind, FnDecl, HirId, Mutability, PatKind, self as hir, def, BinOpKind, BindingAnnotation, Body, ByRef, Expr, ExprKind, FnDecl, Mutability, PatKind, Stmt,
Stmt, StmtKind, TyKind, StmtKind, TyKind,
}; };
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::lint::in_external_macro; use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::def_id::LocalDefId;
use rustc_span::hygiene::DesugaringKind; use rustc_span::hygiene::DesugaringKind;
use rustc_span::source_map::{ExpnKind, Span}; use rustc_span::source_map::{ExpnKind, Span};
@ -151,7 +152,7 @@ impl<'tcx> LateLintPass<'tcx> for LintPass {
decl: &'tcx FnDecl<'_>, decl: &'tcx FnDecl<'_>,
body: &'tcx Body<'_>, body: &'tcx Body<'_>,
span: Span, span: Span,
_: HirId, _: LocalDefId,
) { ) {
if let FnKind::Closure = k { if let FnKind::Closure = k {
// Does not apply to closures // Does not apply to closures

View file

@ -6,11 +6,12 @@ use clippy_utils::{fn_has_unsatisfiable_preds, is_entrypoint_fn, is_from_proc_ma
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def_id::CRATE_DEF_ID; use rustc_hir::def_id::CRATE_DEF_ID;
use rustc_hir::intravisit::FnKind; use rustc_hir::intravisit::FnKind;
use rustc_hir::{Body, Constness, FnDecl, GenericParamKind, HirId}; use rustc_hir::{Body, Constness, FnDecl, GenericParamKind};
use rustc_hir_analysis::hir_ty_to_ty; use rustc_hir_analysis::hir_ty_to_ty;
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::lint::in_external_macro; use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::def_id::LocalDefId;
use rustc_span::Span; use rustc_span::Span;
declare_clippy_lint! { declare_clippy_lint! {
@ -91,14 +92,12 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
_: &FnDecl<'_>, _: &FnDecl<'_>,
body: &Body<'tcx>, body: &Body<'tcx>,
span: Span, span: Span,
hir_id: HirId, def_id: LocalDefId,
) { ) {
if !self.msrv.meets(msrvs::CONST_IF_MATCH) { if !self.msrv.meets(msrvs::CONST_IF_MATCH) {
return; return;
} }
let def_id = cx.tcx.hir().local_def_id(hir_id);
if in_external_macro(cx.tcx.sess, span) || is_entrypoint_fn(cx, def_id.to_def_id()) { if in_external_macro(cx.tcx.sess, span) || is_entrypoint_fn(cx, def_id.to_def_id()) {
return; return;
} }
@ -132,6 +131,8 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
FnKind::Closure => return, FnKind::Closure => return,
} }
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id);
// Const fns are not allowed as methods in a trait. // Const fns are not allowed as methods in a trait.
{ {
let parent = cx.tcx.hir().get_parent_item(hir_id).def_id; let parent = cx.tcx.hir().get_parent_item(hir_id).def_id;

View file

@ -6,6 +6,7 @@ use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::TypeVisitable; use rustc_middle::ty::TypeVisitable;
use rustc_middle::ty::{Adt, Array, Ref, Slice, Tuple, Ty}; use rustc_middle::ty::{Adt, Array, Ref, Slice, Tuple, Ty};
use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::def_id::LocalDefId;
use rustc_span::source_map::Span; use rustc_span::source_map::Span;
use rustc_span::symbol::sym; use rustc_span::symbol::sym;
use std::iter; use std::iter;
@ -102,21 +103,21 @@ impl<'tcx> LateLintPass<'tcx> for MutableKeyType {
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) { fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
if let hir::ItemKind::Fn(ref sig, ..) = item.kind { if let hir::ItemKind::Fn(ref sig, ..) = item.kind {
self.check_sig(cx, item.hir_id(), sig.decl); self.check_sig(cx, item.owner_id.def_id, sig.decl);
} }
} }
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'tcx>) { fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'tcx>) {
if let hir::ImplItemKind::Fn(ref sig, ..) = item.kind { if let hir::ImplItemKind::Fn(ref sig, ..) = item.kind {
if trait_ref_of_method(cx, item.owner_id.def_id).is_none() { if trait_ref_of_method(cx, item.owner_id.def_id).is_none() {
self.check_sig(cx, item.hir_id(), sig.decl); self.check_sig(cx, item.owner_id.def_id, sig.decl);
} }
} }
} }
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'tcx>) { fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'tcx>) {
if let hir::TraitItemKind::Fn(ref sig, ..) = item.kind { if let hir::TraitItemKind::Fn(ref sig, ..) = item.kind {
self.check_sig(cx, item.hir_id(), sig.decl); self.check_sig(cx, item.owner_id.def_id, sig.decl);
} }
} }
@ -136,8 +137,7 @@ impl MutableKeyType {
} }
} }
fn check_sig(&self, cx: &LateContext<'_>, item_hir_id: hir::HirId, decl: &hir::FnDecl<'_>) { fn check_sig(&self, cx: &LateContext<'_>, fn_def_id: LocalDefId, decl: &hir::FnDecl<'_>) {
let fn_def_id = cx.tcx.hir().local_def_id(item_hir_id);
let fn_sig = cx.tcx.fn_sig(fn_def_id).subst_identity(); let fn_sig = cx.tcx.fn_sig(fn_def_id).subst_identity();
for (hir_ty, ty) in iter::zip(decl.inputs, fn_sig.inputs().skip_binder()) { for (hir_ty, ty) in iter::zip(decl.inputs, fn_sig.inputs().skip_binder()) {
self.check_ty_(cx, hir_ty.span, *ty); self.check_ty_(cx, hir_ty.span, *ty);

View file

@ -20,6 +20,7 @@ use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::mir::FakeReadCause; use rustc_middle::mir::FakeReadCause;
use rustc_middle::ty::{self, TypeVisitable}; use rustc_middle::ty::{self, TypeVisitable};
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::def_id::LocalDefId;
use rustc_span::symbol::kw; use rustc_span::symbol::kw;
use rustc_span::{sym, Span}; use rustc_span::{sym, Span};
use rustc_target::spec::abi::Abi; use rustc_target::spec::abi::Abi;
@ -82,12 +83,14 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
decl: &'tcx FnDecl<'_>, decl: &'tcx FnDecl<'_>,
body: &'tcx Body<'_>, body: &'tcx Body<'_>,
span: Span, span: Span,
hir_id: HirId, fn_def_id: LocalDefId,
) { ) {
if span.from_expansion() { if span.from_expansion() {
return; return;
} }
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(fn_def_id);
match kind { match kind {
FnKind::ItemFn(.., header) => { FnKind::ItemFn(.., header) => {
let attrs = cx.tcx.hir().attrs(hir_id); let attrs = cx.tcx.hir().attrs(hir_id);
@ -119,8 +122,6 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
let sized_trait = need!(cx.tcx.lang_items().sized_trait()); let sized_trait = need!(cx.tcx.lang_items().sized_trait());
let fn_def_id = cx.tcx.hir().local_def_id(hir_id);
let preds = traits::elaborate_predicates(cx.tcx, cx.param_env.caller_bounds().iter()) let preds = traits::elaborate_predicates(cx.tcx, cx.param_env.caller_bounds().iter())
.filter(|p| !p.is_global()) .filter(|p| !p.is_global())
.filter_map(|obligation| { .filter_map(|obligation| {

View file

@ -75,7 +75,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
} }
if let hir::ImplItemKind::Fn(ref sig, _) = impl_item.kind { if let hir::ImplItemKind::Fn(ref sig, _) = impl_item.kind {
let name = impl_item.ident.name; let name = impl_item.ident.name;
let id = impl_item.hir_id(); let id = impl_item.owner_id;
if sig.header.constness == hir::Constness::Const { if sig.header.constness == hir::Constness::Const {
// can't be implemented by default // can't be implemented by default
return; return;
@ -97,7 +97,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
if sig.decl.inputs.is_empty(); if sig.decl.inputs.is_empty();
if name == sym::new; if name == sym::new;
if cx.effective_visibilities.is_reachable(impl_item.owner_id.def_id); if cx.effective_visibilities.is_reachable(impl_item.owner_id.def_id);
let self_def_id = cx.tcx.hir().get_parent_item(id); let self_def_id = cx.tcx.hir().get_parent_item(id.into());
let self_ty = cx.tcx.type_of(self_def_id); let self_ty = cx.tcx.type_of(self_def_id);
if self_ty == return_ty(cx, id); if self_ty == return_ty(cx, id);
if let Some(default_trait_id) = cx.tcx.get_diagnostic_item(sym::Default); if let Some(default_trait_id) = cx.tcx.get_diagnostic_item(sym::Default);
@ -133,7 +133,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
span_lint_hir_and_then( span_lint_hir_and_then(
cx, cx,
NEW_WITHOUT_DEFAULT, NEW_WITHOUT_DEFAULT,
id, id.into(),
impl_item.span, impl_item.span,
&format!( &format!(
"you should consider adding a `Default` implementation for `{self_type_snip}`" "you should consider adding a `Default` implementation for `{self_type_snip}`"

View file

@ -209,7 +209,8 @@ impl<'tcx> LateLintPass<'tcx> for ArithmeticSideEffects {
fn check_body(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) { fn check_body(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) {
let body_owner = cx.tcx.hir().body_owner(body.id()); let body_owner = cx.tcx.hir().body_owner(body.id());
let body_owner_def_id = cx.tcx.hir().local_def_id(body_owner); let body_owner_def_id = cx.tcx.hir().body_owner_def_id(body.id());
let body_owner_kind = cx.tcx.hir().body_owner_kind(body_owner_def_id); let body_owner_kind = cx.tcx.hir().body_owner_kind(body_owner_def_id);
if let hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_) = body_owner_kind { if let hir::BodyOwnerKind::Const | hir::BodyOwnerKind::Static(_) = body_owner_kind {
let body_span = cx.tcx.hir().span_with_body(body_owner); let body_span = cx.tcx.hir().span_with_body(body_owner);

View file

@ -96,7 +96,7 @@ impl Context {
pub fn enter_body(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) { pub fn enter_body(&mut self, cx: &LateContext<'_>, body: &hir::Body<'_>) {
let body_owner = cx.tcx.hir().body_owner(body.id()); let body_owner = cx.tcx.hir().body_owner(body.id());
let body_owner_def_id = cx.tcx.hir().local_def_id(body_owner); let body_owner_def_id = cx.tcx.hir().body_owner_def_id(body.id());
match cx.tcx.hir().body_owner_kind(body_owner_def_id) { match cx.tcx.hir().body_owner_kind(body_owner_def_id) {
hir::BodyOwnerKind::Static(_) | hir::BodyOwnerKind::Const => { hir::BodyOwnerKind::Static(_) | hir::BodyOwnerKind::Const => {

View file

@ -8,6 +8,7 @@ use rustc_hir as hir;
use rustc_hir::intravisit::FnKind; use rustc_hir::intravisit::FnKind;
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::def_id::LocalDefId;
use rustc_span::{sym, Span}; use rustc_span::{sym, Span};
declare_clippy_lint! { declare_clippy_lint! {
@ -49,9 +50,13 @@ impl<'tcx> LateLintPass<'tcx> for PanicInResultFn {
_: &'tcx hir::FnDecl<'tcx>, _: &'tcx hir::FnDecl<'tcx>,
body: &'tcx hir::Body<'tcx>, body: &'tcx hir::Body<'tcx>,
span: Span, span: Span,
hir_id: hir::HirId, def_id: LocalDefId,
) { ) {
if !matches!(fn_kind, FnKind::Closure) && is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym::Result) { if matches!(fn_kind, FnKind::Closure) {
return;
}
let owner = cx.tcx.hir().local_def_id_to_hir_id(def_id).expect_owner();
if is_type_diagnostic_item(cx, return_ty(cx, owner), sym::Result) {
lint_impl_body(cx, span, body); lint_impl_body(cx, span, body);
} }
} }

View file

@ -12,7 +12,7 @@ use rustc_data_structures::fx::FxHashSet;
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::intravisit::FnKind; use rustc_hir::intravisit::FnKind;
use rustc_hir::{BindingAnnotation, Body, FnDecl, HirId, Impl, ItemKind, MutTy, Mutability, Node, PatKind}; use rustc_hir::{BindingAnnotation, Body, FnDecl, Impl, ItemKind, MutTy, Mutability, Node, PatKind};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::adjustment::{Adjust, PointerCast}; use rustc_middle::ty::adjustment::{Adjust, PointerCast};
use rustc_middle::ty::layout::LayoutOf; use rustc_middle::ty::layout::LayoutOf;
@ -272,12 +272,13 @@ impl<'tcx> LateLintPass<'tcx> for PassByRefOrValue {
decl: &'tcx FnDecl<'_>, decl: &'tcx FnDecl<'_>,
_body: &'tcx Body<'_>, _body: &'tcx Body<'_>,
span: Span, span: Span,
hir_id: HirId, def_id: LocalDefId,
) { ) {
if span.from_expansion() { if span.from_expansion() {
return; return;
} }
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id);
match kind { match kind {
FnKind::ItemFn(.., header) => { FnKind::ItemFn(.., header) => {
if header.abi != Abi::Rust { if header.abi != Abi::Rust {
@ -308,6 +309,6 @@ impl<'tcx> LateLintPass<'tcx> for PassByRefOrValue {
} }
} }
self.check_poly_fn(cx, cx.tcx.hir().local_def_id(hir_id), decl, Some(span)); self.check_poly_fn(cx, def_id, decl, Some(span));
} }
} }

View file

@ -1,11 +1,10 @@
use clippy_utils::diagnostics::span_lint_and_help; use clippy_utils::diagnostics::span_lint_and_help;
use rustc_hir::{ use rustc_hir::{intravisit, Body, Expr, ExprKind, FnDecl, Let, LocalSource, Mutability, Pat, PatKind, Stmt, StmtKind};
intravisit, Body, Expr, ExprKind, FnDecl, HirId, Let, LocalSource, Mutability, Pat, PatKind, Stmt, StmtKind,
};
use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro; use rustc_middle::lint::in_external_macro;
use rustc_middle::ty; use rustc_middle::ty;
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::def_id::LocalDefId;
use rustc_span::source_map::Span; use rustc_span::source_map::Span;
declare_clippy_lint! { declare_clippy_lint! {
@ -116,7 +115,7 @@ impl<'tcx> LateLintPass<'tcx> for PatternTypeMismatch {
_: &'tcx FnDecl<'_>, _: &'tcx FnDecl<'_>,
body: &'tcx Body<'_>, body: &'tcx Body<'_>,
_: Span, _: Span,
_: HirId, _: LocalDefId,
) { ) {
for param in body.params { for param in body.params {
apply_lint(cx, param.pat, DerefPossible::Impossible); apply_lint(cx, param.pat, DerefPossible::Impossible);

View file

@ -6,11 +6,12 @@ use clippy_utils::{fn_has_unsatisfiable_preds, match_def_path, paths};
use if_chain::if_chain; use if_chain::if_chain;
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::intravisit::FnKind; use rustc_hir::intravisit::FnKind;
use rustc_hir::{def_id, Body, FnDecl, HirId, LangItem}; use rustc_hir::{def_id, Body, FnDecl, LangItem};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::mir; use rustc_middle::mir;
use rustc_middle::ty::{self, Ty}; use rustc_middle::ty::{self, Ty};
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::def_id::LocalDefId;
use rustc_span::source_map::{BytePos, Span}; use rustc_span::source_map::{BytePos, Span};
use rustc_span::sym; use rustc_span::sym;
@ -69,12 +70,10 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClone {
cx: &LateContext<'tcx>, cx: &LateContext<'tcx>,
_: FnKind<'tcx>, _: FnKind<'tcx>,
_: &'tcx FnDecl<'_>, _: &'tcx FnDecl<'_>,
body: &'tcx Body<'_>, _: &'tcx Body<'_>,
_: Span, _: Span,
_: HirId, def_id: LocalDefId,
) { ) {
let def_id = cx.tcx.hir().body_owner_def_id(body.id());
// Building MIR for `fn`s with unsatisfiable preds results in ICE. // Building MIR for `fn`s with unsatisfiable preds results in ICE.
if fn_has_unsatisfiable_preds(cx, def_id.to_def_id()) { if fn_has_unsatisfiable_preds(cx, def_id.to_def_id()) {
return; return;

View file

@ -3,7 +3,7 @@ use clippy_utils::ty::is_must_use_ty;
use clippy_utils::{nth_arg, return_ty}; use clippy_utils::{nth_arg, return_ty};
use rustc_hir::def_id::LocalDefId; use rustc_hir::def_id::LocalDefId;
use rustc_hir::intravisit::FnKind; use rustc_hir::intravisit::FnKind;
use rustc_hir::{Body, FnDecl, HirId, TraitItem, TraitItemKind}; use rustc_hir::{Body, FnDecl, OwnerId, TraitItem, TraitItemKind};
use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro; use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
@ -68,7 +68,7 @@ declare_clippy_lint! {
declare_lint_pass!(ReturnSelfNotMustUse => [RETURN_SELF_NOT_MUST_USE]); declare_lint_pass!(ReturnSelfNotMustUse => [RETURN_SELF_NOT_MUST_USE]);
fn check_method(cx: &LateContext<'_>, decl: &FnDecl<'_>, fn_def: LocalDefId, span: Span, hir_id: HirId) { fn check_method(cx: &LateContext<'_>, decl: &FnDecl<'_>, fn_def: LocalDefId, span: Span, owner_id: OwnerId) {
if_chain! { if_chain! {
// If it comes from an external macro, better ignore it. // If it comes from an external macro, better ignore it.
if !in_external_macro(cx.sess(), span); if !in_external_macro(cx.sess(), span);
@ -76,10 +76,10 @@ fn check_method(cx: &LateContext<'_>, decl: &FnDecl<'_>, fn_def: LocalDefId, spa
// We only show this warning for public exported methods. // We only show this warning for public exported methods.
if cx.effective_visibilities.is_exported(fn_def); if cx.effective_visibilities.is_exported(fn_def);
// We don't want to emit this lint if the `#[must_use]` attribute is already there. // We don't want to emit this lint if the `#[must_use]` attribute is already there.
if !cx.tcx.hir().attrs(hir_id).iter().any(|attr| attr.has_name(sym::must_use)); if !cx.tcx.hir().attrs(owner_id.into()).iter().any(|attr| attr.has_name(sym::must_use));
if cx.tcx.visibility(fn_def.to_def_id()).is_public(); if cx.tcx.visibility(fn_def.to_def_id()).is_public();
let ret_ty = return_ty(cx, hir_id); let ret_ty = return_ty(cx, owner_id.into());
let self_arg = nth_arg(cx, hir_id, 0); let self_arg = nth_arg(cx, owner_id.into(), 0);
// If `Self` has the same type as the returned type, then we want to warn. // If `Self` has the same type as the returned type, then we want to warn.
// //
// For this check, we don't want to remove the reference on the returned type because if // For this check, we don't want to remove the reference on the returned type because if
@ -109,26 +109,26 @@ impl<'tcx> LateLintPass<'tcx> for ReturnSelfNotMustUse {
decl: &'tcx FnDecl<'tcx>, decl: &'tcx FnDecl<'tcx>,
_: &'tcx Body<'tcx>, _: &'tcx Body<'tcx>,
span: Span, span: Span,
hir_id: HirId, fn_def: LocalDefId,
) { ) {
if_chain! { if_chain! {
// We are only interested in methods, not in functions or associated functions. // We are only interested in methods, not in functions or associated functions.
if matches!(kind, FnKind::Method(_, _)); if matches!(kind, FnKind::Method(_, _));
if let Some(fn_def) = cx.tcx.hir().opt_local_def_id(hir_id);
if let Some(impl_def) = cx.tcx.impl_of_method(fn_def.to_def_id()); if let Some(impl_def) = cx.tcx.impl_of_method(fn_def.to_def_id());
// We don't want this method to be te implementation of a trait because the // We don't want this method to be te implementation of a trait because the
// `#[must_use]` should be put on the trait definition directly. // `#[must_use]` should be put on the trait definition directly.
if cx.tcx.trait_id_of_impl(impl_def).is_none(); if cx.tcx.trait_id_of_impl(impl_def).is_none();
then { then {
check_method(cx, decl, fn_def, span, hir_id); let hir_id = cx.tcx.hir().local_def_id_to_hir_id(fn_def);
check_method(cx, decl, fn_def, span, hir_id.expect_owner());
} }
} }
} }
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'tcx>) { fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'tcx>) {
if let TraitItemKind::Fn(ref sig, _) = item.kind { if let TraitItemKind::Fn(ref sig, _) = item.kind {
check_method(cx, sig.decl, item.owner_id.def_id, item.span, item.hir_id()); check_method(cx, sig.decl, item.owner_id.def_id, item.span, item.owner_id);
} }
} }
} }

View file

@ -6,11 +6,12 @@ use core::ops::ControlFlow;
use if_chain::if_chain; use if_chain::if_chain;
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::intravisit::FnKind; use rustc_hir::intravisit::FnKind;
use rustc_hir::{Block, Body, Expr, ExprKind, FnDecl, HirId, LangItem, MatchSource, PatKind, QPath, StmtKind}; use rustc_hir::{Block, Body, Expr, ExprKind, FnDecl, LangItem, MatchSource, PatKind, QPath, StmtKind};
use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro; use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::subst::GenericArgKind; use rustc_middle::ty::subst::GenericArgKind;
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::def_id::LocalDefId;
use rustc_span::source_map::Span; use rustc_span::source_map::Span;
use rustc_span::{BytePos, Pos}; use rustc_span::{BytePos, Pos};
@ -152,7 +153,7 @@ impl<'tcx> LateLintPass<'tcx> for Return {
_: &'tcx FnDecl<'tcx>, _: &'tcx FnDecl<'tcx>,
body: &'tcx Body<'tcx>, body: &'tcx Body<'tcx>,
sp: Span, sp: Span,
_: HirId, _: LocalDefId,
) { ) {
match kind { match kind {
FnKind::Closure => { FnKind::Closure => {

View file

@ -54,7 +54,7 @@ impl<'tcx> LateLintPass<'tcx> for SelfNamedConstructors {
let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id()).def_id; let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id()).def_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.owner_id); let self_ty = cx.tcx.type_of(item.owner_id);
let ret_ty = return_ty(cx, impl_item.hir_id()); let ret_ty = return_ty(cx, impl_item.owner_id);
// Do not check trait impls // Do not check trait impls
if matches!(item.kind, ItemKind::Impl(Impl { of_trait: Some(_), .. })) { if matches!(item.kind, ItemKind::Impl(Impl { of_trait: Some(_), .. })) {

View file

@ -61,8 +61,7 @@ fn is_struct_with_trailing_zero_sized_array(cx: &LateContext<'_>, item: &Item<'_
if let rustc_hir::TyKind::Array(_, rustc_hir::ArrayLen::Body(length)) = last_field.ty.kind; if let rustc_hir::TyKind::Array(_, rustc_hir::ArrayLen::Body(length)) = last_field.ty.kind;
// Then check if that that array zero-sized // Then check if that that array zero-sized
let length_ldid = cx.tcx.hir().local_def_id(length.hir_id); let length = Const::from_anon_const(cx.tcx, length.def_id);
let length = Const::from_anon_const(cx.tcx, length_ldid);
let length = length.try_eval_usize(cx.tcx, cx.param_env); let length = length.try_eval_usize(cx.tcx, cx.param_env);
if let Some(length) = length; if let Some(length) = length;
then { then {

View file

@ -12,11 +12,12 @@ mod vec_box;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::intravisit::FnKind; use rustc_hir::intravisit::FnKind;
use rustc_hir::{ use rustc_hir::{
Body, FnDecl, FnRetTy, GenericArg, HirId, ImplItem, ImplItemKind, Item, ItemKind, Local, MutTy, QPath, TraitItem, Body, FnDecl, FnRetTy, GenericArg, ImplItem, ImplItemKind, Item, ItemKind, Local, MutTy, QPath, TraitItem,
TraitItemKind, TyKind, TraitItemKind, TyKind,
}; };
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::def_id::LocalDefId;
use rustc_span::source_map::Span; use rustc_span::source_map::Span;
declare_clippy_lint! { declare_clippy_lint! {
@ -311,15 +312,27 @@ pub struct Types {
impl_lint_pass!(Types => [BOX_COLLECTION, VEC_BOX, OPTION_OPTION, LINKEDLIST, BORROWED_BOX, REDUNDANT_ALLOCATION, RC_BUFFER, RC_MUTEX, TYPE_COMPLEXITY]); impl_lint_pass!(Types => [BOX_COLLECTION, VEC_BOX, OPTION_OPTION, LINKEDLIST, BORROWED_BOX, REDUNDANT_ALLOCATION, RC_BUFFER, RC_MUTEX, TYPE_COMPLEXITY]);
impl<'tcx> LateLintPass<'tcx> for Types { impl<'tcx> LateLintPass<'tcx> for Types {
fn check_fn(&mut self, cx: &LateContext<'_>, _: FnKind<'_>, decl: &FnDecl<'_>, _: &Body<'_>, _: Span, id: HirId) { fn check_fn(
let is_in_trait_impl = &mut self,
if let Some(hir::Node::Item(item)) = cx.tcx.hir().find_by_def_id(cx.tcx.hir().get_parent_item(id).def_id) { cx: &LateContext<'_>,
matches!(item.kind, ItemKind::Impl(hir::Impl { of_trait: Some(_), .. })) _: FnKind<'_>,
} else { decl: &FnDecl<'_>,
false _: &Body<'_>,
}; _: Span,
def_id: LocalDefId,
) {
let is_in_trait_impl = if let Some(hir::Node::Item(item)) = cx.tcx.hir().find_by_def_id(
cx.tcx
.hir()
.get_parent_item(cx.tcx.hir().local_def_id_to_hir_id(def_id))
.def_id,
) {
matches!(item.kind, ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }))
} else {
false
};
let is_exported = cx.effective_visibilities.is_exported(cx.tcx.hir().local_def_id(id)); let is_exported = cx.effective_visibilities.is_exported(def_id);
self.check_fn_decl( self.check_fn_decl(
cx, cx,
@ -381,7 +394,7 @@ impl<'tcx> LateLintPass<'tcx> for Types {
fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) { fn check_field_def(&mut self, cx: &LateContext<'_>, field: &hir::FieldDef<'_>) {
let is_exported = cx let is_exported = cx
.effective_visibilities .effective_visibilities
.is_exported(cx.tcx.hir().local_def_id(field.hir_id)); .is_exported(field.def_id);
self.check_ty( self.check_ty(
cx, cx,

View file

@ -5,10 +5,11 @@ use if_chain::if_chain;
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::intravisit::FnKind; use rustc_hir::intravisit::FnKind;
use rustc_hir::LangItem::{OptionSome, ResultOk}; use rustc_hir::LangItem::{OptionSome, ResultOk};
use rustc_hir::{Body, ExprKind, FnDecl, HirId, Impl, ItemKind, Node}; use rustc_hir::{Body, ExprKind, FnDecl, Impl, ItemKind, Node};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty; use rustc_middle::ty;
use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::def_id::LocalDefId;
use rustc_span::symbol::sym; use rustc_span::symbol::sym;
use rustc_span::Span; use rustc_span::Span;
@ -77,12 +78,11 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps {
fn_decl: &FnDecl<'tcx>, fn_decl: &FnDecl<'tcx>,
body: &Body<'tcx>, body: &Body<'tcx>,
span: Span, span: Span,
hir_id: HirId, def_id: LocalDefId,
) { ) {
// Abort if public function/method or closure. // Abort if public function/method or closure.
match fn_kind { match fn_kind {
FnKind::ItemFn(..) | FnKind::Method(..) => { FnKind::ItemFn(..) | FnKind::Method(..) => {
let def_id = cx.tcx.hir().local_def_id(hir_id);
if self.avoid_breaking_exported_api && cx.effective_visibilities.is_exported(def_id) { if self.avoid_breaking_exported_api && cx.effective_visibilities.is_exported(def_id) {
return; return;
} }
@ -91,6 +91,7 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps {
} }
// Abort if the method is implementing a trait or of it a trait method. // Abort if the method is implementing a trait or of it a trait method.
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(def_id);
if let Some(Node::Item(item)) = cx.tcx.hir().find_parent(hir_id) { if let Some(Node::Item(item)) = cx.tcx.hir().find_parent(hir_id) {
if matches!( if matches!(
item.kind, item.kind,
@ -101,17 +102,18 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps {
} }
// Get the wrapper and inner types, if can't, abort. // Get the wrapper and inner types, if can't, abort.
let (return_type_label, lang_item, inner_type) = if let ty::Adt(adt_def, subst) = return_ty(cx, hir_id).kind() { let (return_type_label, lang_item, inner_type) =
if cx.tcx.is_diagnostic_item(sym::Option, adt_def.did()) { if let ty::Adt(adt_def, subst) = return_ty(cx, hir_id.expect_owner()).kind() {
("Option", OptionSome, subst.type_at(0)) if cx.tcx.is_diagnostic_item(sym::Option, adt_def.did()) {
} else if cx.tcx.is_diagnostic_item(sym::Result, adt_def.did()) { ("Option", OptionSome, subst.type_at(0))
("Result", ResultOk, subst.type_at(0)) } else if cx.tcx.is_diagnostic_item(sym::Result, adt_def.did()) {
("Result", ResultOk, subst.type_at(0))
} else {
return;
}
} else { } else {
return; return;
} };
} else {
return;
};
// Check if all return expression respect the following condition and collect them. // Check if all return expression respect the following condition and collect them.
let mut suggs = Vec::new(); let mut suggs = Vec::new();

View file

@ -1,9 +1,10 @@
use clippy_utils::diagnostics::span_lint_and_help; use clippy_utils::diagnostics::span_lint_and_help;
use rustc_hir::intravisit::{walk_expr, walk_fn, FnKind, Visitor}; use rustc_hir::intravisit::{walk_expr, walk_fn, FnKind, Visitor};
use rustc_hir::{Body, Expr, ExprKind, FnDecl, HirId, YieldSource}; use rustc_hir::{Body, Expr, ExprKind, FnDecl, YieldSource};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::hir::nested_filter; use rustc_middle::hir::nested_filter;
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::def_id::LocalDefId;
use rustc_span::Span; use rustc_span::Span;
declare_clippy_lint! { declare_clippy_lint! {
@ -66,11 +67,11 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAsync {
fn_decl: &'tcx FnDecl<'tcx>, fn_decl: &'tcx FnDecl<'tcx>,
body: &Body<'tcx>, body: &Body<'tcx>,
span: Span, span: Span,
hir_id: HirId, def_id: LocalDefId,
) { ) {
if !span.from_expansion() && fn_kind.asyncness().is_async() { if !span.from_expansion() && fn_kind.asyncness().is_async() {
let mut visitor = AsyncFnVisitor { cx, found_await: false }; let mut visitor = AsyncFnVisitor { cx, found_await: false };
walk_fn(&mut visitor, fn_kind, fn_decl, body.id(), hir_id); walk_fn(&mut visitor, fn_kind, fn_decl, body.id(), def_id);
if !visitor.found_await { if !visitor.found_await {
span_lint_and_help( span_lint_and_help(
cx, cx,

View file

@ -11,6 +11,7 @@ use rustc_middle::hir::nested_filter;
use rustc_middle::lint::in_external_macro; use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::Ty; use rustc_middle::ty::Ty;
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::def_id::LocalDefId;
use rustc_span::source_map::Span; use rustc_span::source_map::Span;
use rustc_span::sym; use rustc_span::sym;
@ -312,7 +313,7 @@ impl<'tcx> LateLintPass<'tcx> for Unwrap {
decl: &'tcx FnDecl<'_>, decl: &'tcx FnDecl<'_>,
body: &'tcx Body<'_>, body: &'tcx Body<'_>,
span: Span, span: Span,
fn_id: HirId, fn_id: LocalDefId,
) { ) {
if span.from_expansion() { if span.from_expansion() {
return; return;

View file

@ -64,8 +64,8 @@ impl<'tcx> LateLintPass<'tcx> for UnwrapInResult {
// first check if it's a method or function // first check if it's a method or function
if let hir::ImplItemKind::Fn(ref _signature, _) = impl_item.kind; if let hir::ImplItemKind::Fn(ref _signature, _) = impl_item.kind;
// checking if its return type is `result` or `option` // checking if its return type is `result` or `option`
if is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id()), sym::Result) if is_type_diagnostic_item(cx, return_ty(cx, impl_item.owner_id), sym::Result)
|| is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id()), sym::Option); || is_type_diagnostic_item(cx, return_ty(cx, impl_item.owner_id), sym::Option);
then { then {
lint_impl_body(cx, impl_item.span, impl_item); lint_impl_body(cx, impl_item.span, impl_item);
} }

View file

@ -1118,9 +1118,8 @@ pub fn can_move_expr_to_closure<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'
self.captures.entry(l).and_modify(|e| *e |= cap).or_insert(cap); self.captures.entry(l).and_modify(|e| *e |= cap).or_insert(cap);
} }
}, },
ExprKind::Closure { .. } => { ExprKind::Closure(closure) => {
let closure_id = self.cx.tcx.hir().local_def_id(e.hir_id); for capture in self.cx.typeck_results().closure_min_captures_flattened(closure.def_id) {
for capture in self.cx.typeck_results().closure_min_captures_flattened(closure_id) {
let local_id = match capture.place.base { let local_id = match capture.place.base {
PlaceBase::Local(id) => id, PlaceBase::Local(id) => id,
PlaceBase::Upvar(var) => var.var_path.hir_id, PlaceBase::Upvar(var) => var.var_path.hir_id,
@ -1577,15 +1576,13 @@ pub fn is_direct_expn_of(span: Span, name: &str) -> Option<Span> {
} }
/// Convenience function to get the return type of a function. /// Convenience function to get the return type of a function.
pub fn return_ty<'tcx>(cx: &LateContext<'tcx>, fn_item: hir::HirId) -> Ty<'tcx> { pub fn return_ty<'tcx>(cx: &LateContext<'tcx>, fn_def_id: hir::OwnerId) -> Ty<'tcx> {
let fn_def_id = cx.tcx.hir().local_def_id(fn_item);
let ret_ty = cx.tcx.fn_sig(fn_def_id).subst_identity().output(); let ret_ty = cx.tcx.fn_sig(fn_def_id).subst_identity().output();
cx.tcx.erase_late_bound_regions(ret_ty) cx.tcx.erase_late_bound_regions(ret_ty)
} }
/// Convenience function to get the nth argument type of a function. /// Convenience function to get the nth argument type of a function.
pub fn nth_arg<'tcx>(cx: &LateContext<'tcx>, fn_item: hir::HirId, nth: usize) -> Ty<'tcx> { pub fn nth_arg<'tcx>(cx: &LateContext<'tcx>, fn_def_id: hir::OwnerId, nth: usize) -> Ty<'tcx> {
let fn_def_id = cx.tcx.hir().local_def_id(fn_item);
let arg = cx.tcx.fn_sig(fn_def_id).subst_identity().input(nth); let arg = cx.tcx.fn_sig(fn_def_id).subst_identity().input(nth);
cx.tcx.erase_late_bound_regions(arg) cx.tcx.erase_late_bound_regions(arg)
} }

View file

@ -809,7 +809,7 @@ pub struct DerefClosure {
/// ///
/// note: this only works on single line immutable closures with exactly one input parameter. /// note: this only works on single line immutable closures with exactly one input parameter.
pub fn deref_closure_args(cx: &LateContext<'_>, closure: &hir::Expr<'_>) -> Option<DerefClosure> { pub fn deref_closure_args(cx: &LateContext<'_>, closure: &hir::Expr<'_>) -> Option<DerefClosure> {
if let hir::ExprKind::Closure(&Closure { fn_decl, body, .. }) = closure.kind { if let hir::ExprKind::Closure(&Closure { fn_decl, def_id, body, .. }) = closure.kind {
let closure_body = cx.tcx.hir().body(body); let closure_body = cx.tcx.hir().body(body);
// is closure arg a type annotated double reference (i.e.: `|x: &&i32| ...`) // is closure arg a type annotated double reference (i.e.: `|x: &&i32| ...`)
// a type annotation is present if param `kind` is different from `TyKind::Infer` // a type annotation is present if param `kind` is different from `TyKind::Infer`
@ -829,10 +829,8 @@ pub fn deref_closure_args(cx: &LateContext<'_>, closure: &hir::Expr<'_>) -> Opti
applicability: Applicability::MachineApplicable, applicability: Applicability::MachineApplicable,
}; };
let fn_def_id = cx.tcx.hir().local_def_id(closure.hir_id);
let infcx = cx.tcx.infer_ctxt().build(); let infcx = cx.tcx.infer_ctxt().build();
ExprUseVisitor::new(&mut visitor, &infcx, fn_def_id, cx.param_env, cx.typeck_results()) ExprUseVisitor::new(&mut visitor, &infcx, def_id, cx.param_env, cx.typeck_results()).consume_body(closure_body);
.consume_body(closure_body);
if !visitor.suggestion_start.is_empty() { if !visitor.suggestion_start.is_empty() {
return Some(DerefClosure { return Some(DerefClosure {

View file

@ -16,6 +16,7 @@ use rustc_hir as hir;
use rustc_hir::intravisit; use rustc_hir::intravisit;
use rustc_hir::Node; use rustc_hir::Node;
use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_span::def_id::LocalDefId;
use rustc_span::source_map; use rustc_span::source_map;
#[no_mangle] #[no_mangle]
@ -40,8 +41,9 @@ impl<'tcx> LateLintPass<'tcx> for MissingAllowedAttrPass {
_: &'tcx hir::FnDecl, _: &'tcx hir::FnDecl,
_: &'tcx hir::Body, _: &'tcx hir::Body,
span: source_map::Span, span: source_map::Span,
id: hir::HirId, def_id: LocalDefId,
) { ) {
let id = cx.tcx.hir().local_def_id_to_hir_id(def_id);
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).def_id), _ => cx.tcx.hir().expect_item(cx.tcx.hir().get_parent_item(id).def_id),