impl<'hir> Entry<'hir> {
fn parent_node(self) -> Option<HirId> {
match self.node {
- Node::Crate(_) | Node::MacroDef(_) => None,
+ Node::Crate(_) => None,
_ => Some(self.parent),
}
}
}
}
-fn fn_sig<'hir>(node: Node<'hir>) -> Option<&'hir FnSig<'hir>> {
+pub fn fn_sig<'hir>(node: Node<'hir>) -> Option<&'hir FnSig<'hir>> {
match &node {
Node::Item(Item { kind: ItemKind::Fn(sig, _, _), .. })
| Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(sig, _), .. })
ItemKind::TraitAlias(..) => DefKind::TraitAlias,
ItemKind::ExternCrate(_) => DefKind::ExternCrate,
ItemKind::Use(..) => DefKind::Use,
- ItemKind::ForeignMod(..) => DefKind::ForeignMod,
+ ItemKind::ForeignMod { .. } => DefKind::ForeignMod,
ItemKind::GlobalAsm(..) => DefKind::GlobalAsm,
ItemKind::Impl { .. } => DefKind::Impl,
},
}
}
+ pub fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir> {
+ match self.find(id.hir_id).unwrap() {
+ Node::ForeignItem(item) => item,
+ _ => bug!(),
+ }
+ }
+
pub fn body(&self, id: BodyId) -> &'hir Body<'hir> {
self.tcx.hir_owner_nodes(id.hir_id.owner).unwrap().bodies.get(&id.hir_id.local_id).unwrap()
}
pub fn body_param_names(&self, id: BodyId) -> impl Iterator<Item = Ident> + 'hir {
self.body(id).params.iter().map(|arg| match arg.pat.kind {
PatKind::Binding(_, _, ident, _) => ident,
- _ => Ident::new(kw::Invalid, rustc_span::DUMMY_SP),
+ _ => Ident::new(kw::Empty, rustc_span::DUMMY_SP),
})
}
for id in &module.impl_items {
visitor.visit_impl_item(self.expect_impl_item(id.hir_id));
}
+
+ for id in &module.foreign_items {
+ visitor.visit_foreign_item(self.expect_foreign_item(id.hir_id));
+ }
}
/// Retrieves the `Node` corresponding to `id`, panicking if it cannot be found.
let mut scope = id;
loop {
scope = self.get_enclosing_scope(scope).unwrap_or(CRATE_HIR_ID);
- if scope == CRATE_HIR_ID {
- return CRATE_HIR_ID;
- }
- match self.get(scope) {
- Node::Block(_) => {}
- _ => break,
+ if scope == CRATE_HIR_ID || !matches!(self.get(scope), Node::Block(_)) {
+ return scope;
}
}
- scope
}
pub fn get_parent_did(&self, id: HirId) -> LocalDefId {
let parent = self.get_parent_item(hir_id);
if let Some(entry) = self.find_entry(parent) {
if let Entry {
- node: Node::Item(Item { kind: ItemKind::ForeignMod(ref nm), .. }), ..
+ node: Node::Item(Item { kind: ItemKind::ForeignMod { abi, .. }, .. }),
+ ..
} = entry
{
- return nm.abi;
+ return *abi;
}
}
bug!("expected foreign mod or inlined parent, found {}", self.node_to_string(parent))
/// Given a node ID, gets a list of attributes associated with the AST
/// corresponding to the node-ID.
pub fn attrs(&self, id: HirId) -> &'hir [ast::Attribute] {
- let attrs = match self.find_entry(id).map(|entry| entry.node) {
- Some(Node::Param(a)) => Some(&a.attrs[..]),
- Some(Node::Local(l)) => Some(&l.attrs[..]),
- Some(Node::Item(i)) => Some(&i.attrs[..]),
- Some(Node::ForeignItem(fi)) => Some(&fi.attrs[..]),
- Some(Node::TraitItem(ref ti)) => Some(&ti.attrs[..]),
- Some(Node::ImplItem(ref ii)) => Some(&ii.attrs[..]),
- Some(Node::Variant(ref v)) => Some(&v.attrs[..]),
- Some(Node::Field(ref f)) => Some(&f.attrs[..]),
- Some(Node::Expr(ref e)) => Some(&*e.attrs),
- Some(Node::Stmt(ref s)) => Some(s.kind.attrs(|id| self.item(id.id))),
- Some(Node::Arm(ref a)) => Some(&*a.attrs),
- Some(Node::GenericParam(param)) => Some(¶m.attrs[..]),
+ let attrs = self.find_entry(id).map(|entry| match entry.node {
+ Node::Param(a) => &a.attrs[..],
+ Node::Local(l) => &l.attrs[..],
+ Node::Item(i) => &i.attrs[..],
+ Node::ForeignItem(fi) => &fi.attrs[..],
+ Node::TraitItem(ref ti) => &ti.attrs[..],
+ Node::ImplItem(ref ii) => &ii.attrs[..],
+ Node::Variant(ref v) => &v.attrs[..],
+ Node::Field(ref f) => &f.attrs[..],
+ Node::Expr(ref e) => &*e.attrs,
+ Node::Stmt(ref s) => s.kind.attrs(|id| self.item(id.id)),
+ Node::Arm(ref a) => &*a.attrs,
+ Node::GenericParam(param) => ¶m.attrs[..],
// Unit/tuple structs/variants take the attributes straight from
// the struct/variant definition.
- Some(Node::Ctor(..)) => return self.attrs(self.get_parent_item(id)),
- Some(Node::Crate(item)) => Some(&item.attrs[..]),
- _ => None,
- };
+ Node::Ctor(..) => self.attrs(self.get_parent_item(id)),
+ Node::Crate(item) => &item.attrs[..],
+ Node::MacroDef(def) => def.attrs,
+ Node::AnonConst(..)
+ | Node::PathSegment(..)
+ | Node::Ty(..)
+ | Node::Pat(..)
+ | Node::Binding(..)
+ | Node::TraitRef(..)
+ | Node::Block(..)
+ | Node::Lifetime(..)
+ | Node::Visibility(..) => &[],
+ });
attrs.unwrap_or(&[])
}
fn impl_item(&self, id: ImplItemId) -> &'hir ImplItem<'hir> {
self.impl_item(id)
}
+
+ fn foreign_item(&self, id: ForeignItemId) -> &'hir ForeignItem<'hir> {
+ self.foreign_item(id)
+ }
}
trait Named {
ItemKind::Const(..) => "const",
ItemKind::Fn(..) => "fn",
ItemKind::Mod(..) => "mod",
- ItemKind::ForeignMod(..) => "foreign mod",
+ ItemKind::ForeignMod { .. } => "foreign mod",
ItemKind::GlobalAsm(..) => "global asm",
ItemKind::TyAlias(..) => "ty",
ItemKind::OpaqueTy(..) => "opaque type",