Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move hir_id_validation to rustc_passes + simplify hir::map code #70349

Merged
merged 2 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 73 additions & 135 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ use rustc_target::spec::abi::Abi;

pub mod blocks;
mod collector;
mod hir_id_validator;
pub use hir_id_validator::check_crate;

/// Represents an entry and its parent `HirId`.
#[derive(Copy, Clone, Debug)]
Expand All @@ -44,79 +42,42 @@ impl<'hir> Entry<'hir> {

fn fn_decl<'hir>(node: Node<'hir>) -> Option<&'hir FnDecl<'hir>> {
match node {
Node::Item(ref item) => match item.kind {
ItemKind::Fn(ref sig, _, _) => Some(&sig.decl),
_ => None,
},

Node::TraitItem(ref item) => match item.kind {
TraitItemKind::Fn(ref sig, _) => Some(&sig.decl),
_ => None,
},

Node::ImplItem(ref item) => match item.kind {
ImplItemKind::Fn(ref sig, _) => Some(&sig.decl),
_ => None,
},

Node::Expr(ref expr) => match expr.kind {
ExprKind::Closure(_, ref fn_decl, ..) => Some(fn_decl),
_ => None,
},

Node::Item(Item { kind: ItemKind::Fn(sig, _, _), .. })
| Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(sig, _), .. })
| Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(sig, _), .. }) => Some(&sig.decl),
Node::Expr(Expr { kind: ExprKind::Closure(_, fn_decl, ..), .. }) => Some(fn_decl),
_ => None,
}
}

fn fn_sig<'hir>(node: Node<'hir>) -> Option<&'hir FnSig<'hir>> {
match &node {
Node::Item(item) => match &item.kind {
ItemKind::Fn(sig, _, _) => Some(sig),
_ => None,
},

Node::TraitItem(item) => match &item.kind {
TraitItemKind::Fn(sig, _) => Some(sig),
_ => None,
},

Node::ImplItem(item) => match &item.kind {
ImplItemKind::Fn(sig, _) => Some(sig),
_ => None,
},

Node::Item(Item { kind: ItemKind::Fn(sig, _, _), .. })
| Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(sig, _), .. })
| Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(sig, _), .. }) => Some(sig),
_ => None,
}
}

fn associated_body<'hir>(node: Node<'hir>) -> Option<BodyId> {
match node {
Node::Item(item) => match item.kind {
ItemKind::Const(_, body) | ItemKind::Static(.., body) | ItemKind::Fn(.., body) => {
Some(body)
}
_ => None,
},

Node::TraitItem(item) => match item.kind {
TraitItemKind::Const(_, Some(body)) | TraitItemKind::Fn(_, TraitFn::Provided(body)) => {
Some(body)
}
_ => None,
},

Node::ImplItem(item) => match item.kind {
ImplItemKind::Const(_, body) | ImplItemKind::Fn(_, body) => Some(body),
_ => None,
},
Node::Item(Item {
kind: ItemKind::Const(_, body) | ItemKind::Static(.., body) | ItemKind::Fn(.., body),
..
})
| Node::TraitItem(TraitItem {
kind:
TraitItemKind::Const(_, Some(body)) | TraitItemKind::Fn(_, TraitFn::Provided(body)),
..
})
| Node::ImplItem(ImplItem {
kind: ImplItemKind::Const(_, body) | ImplItemKind::Fn(_, body),
..
})
| Node::Expr(Expr { kind: ExprKind::Closure(.., body, _, _), .. }) => Some(*body),

Node::AnonConst(constant) => Some(constant.body),

Node::Expr(expr) => match expr.kind {
ExprKind::Closure(.., body, _, _) => Some(body),
_ => None,
},

_ => None,
}
}
Expand Down Expand Up @@ -520,20 +481,21 @@ impl<'hir> Map<'hir> {
}

pub fn get_generics(&self, id: DefId) -> Option<&'hir Generics<'hir>> {
self.get_if_local(id).and_then(|node| match node {
Node::ImplItem(ref impl_item) => Some(&impl_item.generics),
Node::TraitItem(ref trait_item) => Some(&trait_item.generics),
Node::Item(ref item) => match item.kind {
ItemKind::Fn(_, ref generics, _)
| ItemKind::TyAlias(_, ref generics)
| ItemKind::Enum(_, ref generics)
| ItemKind::Struct(_, ref generics)
| ItemKind::Union(_, ref generics)
| ItemKind::Trait(_, _, ref generics, ..)
| ItemKind::TraitAlias(ref generics, _)
| ItemKind::Impl { ref generics, .. } => Some(generics),
_ => None,
},
self.get_if_local(id).and_then(|node| match &node {
Node::ImplItem(impl_item) => Some(&impl_item.generics),
Node::TraitItem(trait_item) => Some(&trait_item.generics),
Node::Item(Item {
kind:
ItemKind::Fn(_, generics, _)
| ItemKind::TyAlias(_, generics)
| ItemKind::Enum(_, generics)
| ItemKind::Struct(_, generics)
| ItemKind::Union(_, generics)
| ItemKind::Trait(_, _, generics, ..)
| ItemKind::TraitAlias(generics, _)
| ItemKind::Impl { generics, .. },
..
}) => Some(generics),
_ => None,
})
}
Expand Down Expand Up @@ -573,11 +535,12 @@ impl<'hir> Map<'hir> {
_ => return false,
}
match self.find(self.get_parent_node(id)) {
Some(Node::Item(_)) | Some(Node::TraitItem(_)) | Some(Node::ImplItem(_)) => true,
Some(Node::Expr(e)) => match e.kind {
ExprKind::Closure(..) => true,
_ => false,
},
Some(
Node::Item(_)
| Node::TraitItem(_)
| Node::ImplItem(_)
| Node::Expr(Expr { kind: ExprKind::Closure(..), .. }),
) => true,
_ => false,
}
}
Expand Down Expand Up @@ -644,12 +607,8 @@ impl<'hir> Map<'hir> {
if let (Some((_, next_node)), false) = (iter.peek(), ignore_tail) {
match next_node {
Node::Block(Block { expr: None, .. }) => return None,
Node::Block(Block { expr: Some(expr), .. }) => {
if hir_id != expr.hir_id {
// The current node is not the tail expression of its parent.
return None;
}
}
// The current node is not the tail expression of its parent.
Node::Block(Block { expr: Some(e), .. }) if hir_id != e.hir_id => return None,
_ => {}
}
}
Expand All @@ -659,14 +618,11 @@ impl<'hir> Map<'hir> {
| Node::TraitItem(_)
| Node::Expr(Expr { kind: ExprKind::Closure(..), .. })
| Node::ImplItem(_) => return Some(hir_id),
Node::Expr(ref expr) => {
match expr.kind {
// Ignore `return`s on the first iteration
ExprKind::Loop(..) | ExprKind::Ret(..) => return None,
_ => {}
}
// Ignore `return`s on the first iteration
Node::Expr(Expr { kind: ExprKind::Loop(..) | ExprKind::Ret(..), .. })
| Node::Local(_) => {
return None;
}
Node::Local(_) => return None,
_ => {}
}
}
Expand Down Expand Up @@ -710,17 +666,12 @@ impl<'hir> Map<'hir> {
pub fn get_match_if_cause(&self, hir_id: HirId) -> Option<&'hir Expr<'hir>> {
for (_, node) in self.parent_iter(hir_id) {
match node {
Node::Item(_) | Node::ForeignItem(_) | Node::TraitItem(_) | Node::ImplItem(_) => {
break;
}
Node::Expr(expr) => match expr.kind {
ExprKind::Match(_, _, _) => return Some(expr),
_ => {}
},
Node::Stmt(stmt) => match stmt.kind {
StmtKind::Local(_) => break,
_ => {}
},
Node::Item(_)
| Node::ForeignItem(_)
| Node::TraitItem(_)
| Node::ImplItem(_)
| Node::Stmt(Stmt { kind: StmtKind::Local(_), .. }) => break,
Node::Expr(expr @ Expr { kind: ExprKind::Match(..), .. }) => return Some(expr),
_ => {}
}
}
Expand All @@ -730,32 +681,22 @@ impl<'hir> Map<'hir> {
/// Returns the nearest enclosing scope. A scope is roughly an item or block.
pub fn get_enclosing_scope(&self, hir_id: HirId) -> Option<HirId> {
for (hir_id, node) in self.parent_iter(hir_id) {
if match node {
Node::Item(i) => match i.kind {
if let Node::Item(Item {
kind:
ItemKind::Fn(..)
| ItemKind::Mod(..)
| ItemKind::Enum(..)
| ItemKind::Struct(..)
| ItemKind::Union(..)
| ItemKind::Trait(..)
| ItemKind::Impl { .. } => true,
_ => false,
},
Node::ForeignItem(fi) => match fi.kind {
ForeignItemKind::Fn(..) => true,
_ => false,
},
Node::TraitItem(ti) => match ti.kind {
TraitItemKind::Fn(..) => true,
_ => false,
},
Node::ImplItem(ii) => match ii.kind {
ImplItemKind::Fn(..) => true,
_ => false,
},
Node::Block(_) => true,
_ => false,
} {
| ItemKind::Impl { .. },
..
})
| Node::ForeignItem(ForeignItem { kind: ForeignItemKind::Fn(..), .. })
| Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(..), .. })
| Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(..), .. })
| Node::Block(_) = node
{
return Some(hir_id);
}
}
Expand All @@ -771,11 +712,11 @@ impl<'hir> Map<'hir> {
return CRATE_HIR_ID;
}
match self.get(scope) {
Node::Item(i) => match i.kind {
ItemKind::OpaqueTy(OpaqueTy { impl_trait_fn: None, .. }) => {}
_ => break,
},
Node::Block(_) => {}
Node::Item(Item {
kind: ItemKind::OpaqueTy(OpaqueTy { impl_trait_fn: None, .. }),
..
})
| Node::Block(_) => {}
_ => break,
}
}
Expand Down Expand Up @@ -823,14 +764,11 @@ impl<'hir> Map<'hir> {

pub fn expect_variant_data(&self, id: HirId) -> &'hir VariantData<'hir> {
match self.find(id) {
Some(Node::Item(i)) => match i.kind {
ItemKind::Struct(ref struct_def, _) | ItemKind::Union(ref struct_def, _) => {
struct_def
}
_ => bug!("struct ID bound to non-struct {}", self.node_to_string(id)),
},
Some(
Node::Ctor(vd)
| Node::Item(Item { kind: ItemKind::Struct(vd, _) | ItemKind::Union(vd, _), .. }),
) => vd,
Some(Node::Variant(variant)) => &variant.data,
Some(Node::Ctor(data)) => data,
_ => bug!("expected struct or variant, found {}", self.node_to_string(id)),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_interface/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ pub fn create_global_ctxt<'tcx>(
fn analysis(tcx: TyCtxt<'_>, cnum: CrateNum) -> Result<()> {
assert_eq!(cnum, LOCAL_CRATE);

rustc::hir::map::check_crate(tcx);
rustc_passes::hir_id_validator::check_crate(tcx);

let sess = tcx.sess;
let mut entry_point = None;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::hir::map::Map;
use crate::ty::TyCtxt;
use rustc::hir::map::Map;
use rustc::ty::TyCtxt;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe put this file in a hir folder if you plan to move the collector there too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm; how about doing it if/when the collector is moved? I'm not sure whether I will move it atm.

use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::sync::{par_iter, Lock, ParallelIterator};
use rustc_hir as hir;
Expand Down
1 change: 1 addition & 0 deletions src/librustc_passes/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod check_const;
pub mod dead;
mod diagnostic_items;
pub mod entry;
pub mod hir_id_validator;
pub mod hir_stats;
mod intrinsicck;
mod lang_items;
Expand Down