if let Some(entry) = self.map[id.as_usize()] {
self.dep_graph.read_index(entry.dep_node);
} else {
- bug!("called `HirMap::read()` with invalid `NodeId`")
+ bug!("called `HirMap::read()` with invalid `NodeId`: {:?}", id)
}
}
};
match item.node {
- ItemKind::Static(_, m, _) => Some(Def::Static(def_id(),
- m == MutMutable)),
+ ItemKind::Static(_, m, _) => Some(Def::Static(def_id(), m == MutMutable)),
ItemKind::Const(..) => Some(Def::Const(def_id())),
ItemKind::Fn(..) => Some(Def::Fn(def_id())),
ItemKind::Mod(..) => Some(Def::Mod(def_id())),
Node::AnonConst(_) |
Node::Expr(_) |
Node::Stmt(_) |
+ Node::PathSegment(_) |
Node::Ty(_) |
Node::TraitRef(_) |
Node::Pat(_) |
pub fn fn_decl(&self, node_id: ast::NodeId) -> Option<FnDecl> {
if let Some(entry) = self.find_entry(node_id) {
- entry.fn_decl().map(|fd| fd.clone())
+ entry.fn_decl().cloned()
} else {
bug!("no entry for node_id `{}`", node_id)
}
match self.get(id) {
Node::Item(&Item { node: ItemKind::Trait(..), .. }) => id,
Node::GenericParam(_) => self.get_parent_node(id),
- _ => {
- bug!("ty_param_owner: {} not a type parameter",
- self.node_to_string(id))
- }
+ _ => bug!("ty_param_owner: {} not a type parameter", self.node_to_string(id))
}
}
pub fn ty_param_name(&self, id: NodeId) -> Name {
match self.get(id) {
- Node::Item(&Item { node: ItemKind::Trait(..), .. }) => {
- keywords::SelfType.name()
- }
+ Node::Item(&Item { node: ItemKind::Trait(..), .. }) => keywords::SelfType.name(),
Node::GenericParam(param) => param.name.ident().name,
_ => bug!("ty_param_name: {} not a type parameter", self.node_to_string(id)),
}
/// Retrieve the Node corresponding to `id`, panicking if it cannot
/// be found.
pub fn get(&self, id: NodeId) -> Node<'hir> {
- match self.find(id) {
- Some(node) => node, // read recorded by `find`
- None => bug!("couldn't find node id {} in the AST map", id)
- }
+ // read recorded by `find`
+ self.find(id).unwrap_or_else(|| bug!("couldn't find node id {} in the AST map", id))
}
pub fn get_if_local(&self, id: DefId) -> Option<Node<'hir>> {
/// }
/// ```
pub fn get_return_block(&self, id: NodeId) -> Option<NodeId> {
- let match_fn = |node: &Node| {
+ let match_fn = |node: &Node<'_>| {
match *node {
Node::Item(_) |
Node::ForeignItem(_) |
_ => false,
}
};
- let match_non_returning_block = |node: &Node| {
+ let match_non_returning_block = |node: &Node<'_>| {
match *node {
Node::Expr(ref expr) => {
match expr.node {
}
};
- match self.walk_parent_nodes(id, match_fn, match_non_returning_block) {
- Ok(id) => Some(id),
- Err(_) => None,
- }
+ self.walk_parent_nodes(id, match_fn, match_non_returning_block).ok()
}
/// Retrieve the NodeId for `id`'s parent item, or `id` itself if no
}
}
- /// Returns the NodeId of `id`'s nearest module parent, or `id` itself if no
+ /// Returns the DefId of `id`'s nearest module parent, or `id` itself if no
/// module parent is in this map.
pub fn get_module_parent(&self, id: NodeId) -> DefId {
- let id = match self.walk_parent_nodes(id, |node| match *node {
+ self.local_def_id(self.get_module_parent_node(id))
+ }
+
+ /// Returns the NodeId of `id`'s nearest module parent, or `id` itself if no
+ /// module parent is in this map.
+ pub fn get_module_parent_node(&self, id: NodeId) -> NodeId {
+ match self.walk_parent_nodes(id, |node| match *node {
Node::Item(&Item { node: ItemKind::Mod(_), .. }) => true,
_ => false,
}, |_| false) {
Ok(id) => id,
Err(id) => id,
- };
- self.local_def_id(id)
+ }
}
/// Returns the nearest enclosing scope. A scope is an item or block.
/// and associated types probably shouldn't, for example. Behavior in this
/// regard should be expected to be highly unstable.
pub fn get_enclosing_scope(&self, id: NodeId) -> Option<NodeId> {
- match self.walk_parent_nodes(id, |node| match *node {
+ self.walk_parent_nodes(id, |node| match *node {
Node::Item(_) |
Node::ForeignItem(_) |
Node::TraitItem(_) |
Node::ImplItem(_) |
Node::Block(_) => true,
_ => false,
- }, |_| false) {
- Ok(id) => Some(id),
- Err(_) => None,
- }
+ }, |_| false).ok()
}
pub fn get_parent_did(&self, id: NodeId) -> DefId {
pub fn get_foreign_abi(&self, id: NodeId) -> Abi {
let parent = self.get_parent(id);
if let Some(entry) = self.find_entry(parent) {
- match entry {
- Entry { node: Node::Item(Item { node: ItemKind::ForeignMod(ref nm), .. }), .. }
- => {
- self.read(id); // reveals some of the content of a node
- return nm.abi;
- }
- _ => {}
+ if let Entry {
+ node: Node::Item(Item { node: ItemKind::ForeignMod(ref nm), .. }), .. } = entry
+ {
+ self.read(id); // reveals some of the content of a node
+ return nm.abi;
}
}
bug!("expected foreign mod or inlined parent, found {}", self.node_to_string(parent))
match i.node {
ItemKind::Struct(ref struct_def, _) |
ItemKind::Union(ref struct_def, _) => struct_def,
- _ => {
- bug!("struct ID bound to non-struct {}",
- self.node_to_string(id));
- }
+ _ => bug!("struct ID bound to non-struct {}", self.node_to_string(id))
}
}
Some(Node::StructCtor(data)) => data,
Some(Node::Variant(variant)) => &variant.node.data,
- _ => {
- bug!("expected struct or variant, found {}",
- self.node_to_string(id));
- }
+ _ => bug!("expected struct or variant, found {}", self.node_to_string(id))
}
}
Some(Node::GenericParam(param)) => Some(¶m.attrs[..]),
// unit/tuple structs take the attributes straight from
// the struct definition.
- Some(Node::StructCtor(_)) => {
- return self.attrs(self.get_parent(id));
- }
+ Some(Node::StructCtor(_)) => return self.attrs(self.get_parent(id)),
_ => None
};
attrs.unwrap_or(&[])
Some(Node::AnonConst(constant)) => self.body(constant.body).value.span,
Some(Node::Expr(expr)) => expr.span,
Some(Node::Stmt(stmt)) => stmt.span,
+ Some(Node::PathSegment(seg)) => seg.ident.span,
Some(Node::Ty(ty)) => ty.span,
Some(Node::TraitRef(tr)) => tr.path.span,
Some(Node::Binding(pat)) => pat.span,
Some(Node::Visibility(v)) => bug!("unexpected Visibility {:?}", v),
Some(Node::Local(local)) => local.span,
Some(Node::MacroDef(macro_def)) => macro_def.span,
-
Some(Node::Crate) => self.forest.krate.span,
None => bug!("hir::map::Map::span: id not in map: {:?}", id),
}
// If `id` itself is a mod named `m` with parent `p`, then
// returns `Some(id, m, p)`. If `id` has no mod in its parent
// chain, then returns `None`.
- fn find_first_mod_parent<'a>(map: &'a Map, mut id: NodeId) -> Option<(NodeId, Name)> {
+ fn find_first_mod_parent<'a>(map: &'a Map<'_>, mut id: NodeId) -> Option<(NodeId, Name)> {
loop {
- match map.find(id)? {
- Node::Item(item) if item_is_mod(&item) =>
- return Some((id, item.name)),
- _ => {}
+ if let Node::Item(item) = map.find(id)? {
+ if item_is_mod(&item) {
+ return Some((id, item.name))
+ }
}
let parent = map.get_parent(id);
if parent == id { return None }
impl Named for TraitItem { fn name(&self) -> Name { self.ident.name } }
impl Named for ImplItem { fn name(&self) -> Name { self.ident.name } }
-
pub fn map_crate<'hir>(sess: &::session::Session,
cstore: &dyn CrateStore,
forest: &'hir mut Forest,
/// Identical to the `PpAnn` implementation for `hir::Crate`,
/// except it avoids creating a dependency on the whole crate.
impl<'hir> print::PpAnn for Map<'hir> {
- fn nested(&self, state: &mut print::State, nested: print::Nested) -> io::Result<()> {
+ fn nested(&self, state: &mut print::State<'_>, nested: print::Nested) -> io::Result<()> {
match nested {
Nested::Item(id) => state.print_item(self.expect_item(id.id)),
Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)),
}
impl<'a> print::State<'a> {
- pub fn print_node(&mut self, node: Node) -> io::Result<()> {
+ pub fn print_node(&mut self, node: Node<'_>) -> io::Result<()> {
match node {
Node::Item(a) => self.print_item(&a),
Node::ForeignItem(a) => self.print_foreign_item(&a),
Node::AnonConst(a) => self.print_anon_const(&a),
Node::Expr(a) => self.print_expr(&a),
Node::Stmt(a) => self.print_stmt(&a),
+ Node::PathSegment(a) => self.print_path_segment(&a),
Node::Ty(a) => self.print_type(&a),
Node::TraitRef(a) => self.print_trait_ref(&a),
Node::Binding(a) |
Node::StructCtor(_) => bug!("cannot print isolated StructCtor"),
Node::Local(a) => self.print_local_decl(&a),
Node::MacroDef(_) => bug!("cannot print MacroDef"),
- Node::Crate => bug!("cannot print Crate"),
+ Node::Crate => bug!("cannot print Crate"),
}
}
}
-fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
+fn node_id_to_string(map: &Map<'_>, id: NodeId, include_id: bool) -> String {
let id_str = format!(" (id={})", id);
let id_str = if include_id { &id_str[..] } else { "" };
Some(Node::Stmt(_)) => {
format!("stmt {}{}", map.node_to_pretty_string(id), id_str)
}
+ Some(Node::PathSegment(_)) => {
+ format!("path segment {}{}", map.node_to_pretty_string(id), id_str)
+ }
Some(Node::Ty(_)) => {
format!("type {}{}", map.node_to_pretty_string(id), id_str)
}
}
}
-pub fn describe_def(tcx: TyCtxt, def_id: DefId) -> Option<Def> {
+pub fn describe_def(tcx: TyCtxt<'_, '_, '_>, def_id: DefId) -> Option<Def> {
if let Some(node_id) = tcx.hir.as_local_node_id(def_id) {
tcx.hir.describe_def(node_id)
} else {