Skip to content

Commit 8aafb8a

Browse files
authored
Rollup merge of #70349 - Centril:hir-outa-rustc, r=Zoxc
move `hir_id_validation` to `rustc_passes` + simplify `hir::map` code r? @Zoxc
2 parents d03c02a + 12e4f9f commit 8aafb8a

File tree

4 files changed

+77
-138
lines changed

4 files changed

+77
-138
lines changed

src/librustc/hir/map/mod.rs

+73-135
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ use rustc_target::spec::abi::Abi;
2323

2424
pub mod blocks;
2525
mod collector;
26-
mod hir_id_validator;
27-
pub use hir_id_validator::check_crate;
2826

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

4543
fn fn_decl<'hir>(node: Node<'hir>) -> Option<&'hir FnDecl<'hir>> {
4644
match node {
47-
Node::Item(ref item) => match item.kind {
48-
ItemKind::Fn(ref sig, _, _) => Some(&sig.decl),
49-
_ => None,
50-
},
51-
52-
Node::TraitItem(ref item) => match item.kind {
53-
TraitItemKind::Fn(ref sig, _) => Some(&sig.decl),
54-
_ => None,
55-
},
56-
57-
Node::ImplItem(ref item) => match item.kind {
58-
ImplItemKind::Fn(ref sig, _) => Some(&sig.decl),
59-
_ => None,
60-
},
61-
62-
Node::Expr(ref expr) => match expr.kind {
63-
ExprKind::Closure(_, ref fn_decl, ..) => Some(fn_decl),
64-
_ => None,
65-
},
66-
45+
Node::Item(Item { kind: ItemKind::Fn(sig, _, _), .. })
46+
| Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(sig, _), .. })
47+
| Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(sig, _), .. }) => Some(&sig.decl),
48+
Node::Expr(Expr { kind: ExprKind::Closure(_, fn_decl, ..), .. }) => Some(fn_decl),
6749
_ => None,
6850
}
6951
}
7052

7153
fn fn_sig<'hir>(node: Node<'hir>) -> Option<&'hir FnSig<'hir>> {
7254
match &node {
73-
Node::Item(item) => match &item.kind {
74-
ItemKind::Fn(sig, _, _) => Some(sig),
75-
_ => None,
76-
},
77-
78-
Node::TraitItem(item) => match &item.kind {
79-
TraitItemKind::Fn(sig, _) => Some(sig),
80-
_ => None,
81-
},
82-
83-
Node::ImplItem(item) => match &item.kind {
84-
ImplItemKind::Fn(sig, _) => Some(sig),
85-
_ => None,
86-
},
87-
55+
Node::Item(Item { kind: ItemKind::Fn(sig, _, _), .. })
56+
| Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(sig, _), .. })
57+
| Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(sig, _), .. }) => Some(sig),
8858
_ => None,
8959
}
9060
}
9161

9262
fn associated_body<'hir>(node: Node<'hir>) -> Option<BodyId> {
9363
match node {
94-
Node::Item(item) => match item.kind {
95-
ItemKind::Const(_, body) | ItemKind::Static(.., body) | ItemKind::Fn(.., body) => {
96-
Some(body)
97-
}
98-
_ => None,
99-
},
100-
101-
Node::TraitItem(item) => match item.kind {
102-
TraitItemKind::Const(_, Some(body)) | TraitItemKind::Fn(_, TraitFn::Provided(body)) => {
103-
Some(body)
104-
}
105-
_ => None,
106-
},
107-
108-
Node::ImplItem(item) => match item.kind {
109-
ImplItemKind::Const(_, body) | ImplItemKind::Fn(_, body) => Some(body),
110-
_ => None,
111-
},
64+
Node::Item(Item {
65+
kind: ItemKind::Const(_, body) | ItemKind::Static(.., body) | ItemKind::Fn(.., body),
66+
..
67+
})
68+
| Node::TraitItem(TraitItem {
69+
kind:
70+
TraitItemKind::Const(_, Some(body)) | TraitItemKind::Fn(_, TraitFn::Provided(body)),
71+
..
72+
})
73+
| Node::ImplItem(ImplItem {
74+
kind: ImplItemKind::Const(_, body) | ImplItemKind::Fn(_, body),
75+
..
76+
})
77+
| Node::Expr(Expr { kind: ExprKind::Closure(.., body, _, _), .. }) => Some(*body),
11278

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

115-
Node::Expr(expr) => match expr.kind {
116-
ExprKind::Closure(.., body, _, _) => Some(body),
117-
_ => None,
118-
},
119-
12081
_ => None,
12182
}
12283
}
@@ -520,20 +481,21 @@ impl<'hir> Map<'hir> {
520481
}
521482

522483
pub fn get_generics(&self, id: DefId) -> Option<&'hir Generics<'hir>> {
523-
self.get_if_local(id).and_then(|node| match node {
524-
Node::ImplItem(ref impl_item) => Some(&impl_item.generics),
525-
Node::TraitItem(ref trait_item) => Some(&trait_item.generics),
526-
Node::Item(ref item) => match item.kind {
527-
ItemKind::Fn(_, ref generics, _)
528-
| ItemKind::TyAlias(_, ref generics)
529-
| ItemKind::Enum(_, ref generics)
530-
| ItemKind::Struct(_, ref generics)
531-
| ItemKind::Union(_, ref generics)
532-
| ItemKind::Trait(_, _, ref generics, ..)
533-
| ItemKind::TraitAlias(ref generics, _)
534-
| ItemKind::Impl { ref generics, .. } => Some(generics),
535-
_ => None,
536-
},
484+
self.get_if_local(id).and_then(|node| match &node {
485+
Node::ImplItem(impl_item) => Some(&impl_item.generics),
486+
Node::TraitItem(trait_item) => Some(&trait_item.generics),
487+
Node::Item(Item {
488+
kind:
489+
ItemKind::Fn(_, generics, _)
490+
| ItemKind::TyAlias(_, generics)
491+
| ItemKind::Enum(_, generics)
492+
| ItemKind::Struct(_, generics)
493+
| ItemKind::Union(_, generics)
494+
| ItemKind::Trait(_, _, generics, ..)
495+
| ItemKind::TraitAlias(generics, _)
496+
| ItemKind::Impl { generics, .. },
497+
..
498+
}) => Some(generics),
537499
_ => None,
538500
})
539501
}
@@ -573,11 +535,12 @@ impl<'hir> Map<'hir> {
573535
_ => return false,
574536
}
575537
match self.find(self.get_parent_node(id)) {
576-
Some(Node::Item(_)) | Some(Node::TraitItem(_)) | Some(Node::ImplItem(_)) => true,
577-
Some(Node::Expr(e)) => match e.kind {
578-
ExprKind::Closure(..) => true,
579-
_ => false,
580-
},
538+
Some(
539+
Node::Item(_)
540+
| Node::TraitItem(_)
541+
| Node::ImplItem(_)
542+
| Node::Expr(Expr { kind: ExprKind::Closure(..), .. }),
543+
) => true,
581544
_ => false,
582545
}
583546
}
@@ -644,12 +607,8 @@ impl<'hir> Map<'hir> {
644607
if let (Some((_, next_node)), false) = (iter.peek(), ignore_tail) {
645608
match next_node {
646609
Node::Block(Block { expr: None, .. }) => return None,
647-
Node::Block(Block { expr: Some(expr), .. }) => {
648-
if hir_id != expr.hir_id {
649-
// The current node is not the tail expression of its parent.
650-
return None;
651-
}
652-
}
610+
// The current node is not the tail expression of its parent.
611+
Node::Block(Block { expr: Some(e), .. }) if hir_id != e.hir_id => return None,
653612
_ => {}
654613
}
655614
}
@@ -659,14 +618,11 @@ impl<'hir> Map<'hir> {
659618
| Node::TraitItem(_)
660619
| Node::Expr(Expr { kind: ExprKind::Closure(..), .. })
661620
| Node::ImplItem(_) => return Some(hir_id),
662-
Node::Expr(ref expr) => {
663-
match expr.kind {
664-
// Ignore `return`s on the first iteration
665-
ExprKind::Loop(..) | ExprKind::Ret(..) => return None,
666-
_ => {}
667-
}
621+
// Ignore `return`s on the first iteration
622+
Node::Expr(Expr { kind: ExprKind::Loop(..) | ExprKind::Ret(..), .. })
623+
| Node::Local(_) => {
624+
return None;
668625
}
669-
Node::Local(_) => return None,
670626
_ => {}
671627
}
672628
}
@@ -710,17 +666,12 @@ impl<'hir> Map<'hir> {
710666
pub fn get_match_if_cause(&self, hir_id: HirId) -> Option<&'hir Expr<'hir>> {
711667
for (_, node) in self.parent_iter(hir_id) {
712668
match node {
713-
Node::Item(_) | Node::ForeignItem(_) | Node::TraitItem(_) | Node::ImplItem(_) => {
714-
break;
715-
}
716-
Node::Expr(expr) => match expr.kind {
717-
ExprKind::Match(_, _, _) => return Some(expr),
718-
_ => {}
719-
},
720-
Node::Stmt(stmt) => match stmt.kind {
721-
StmtKind::Local(_) => break,
722-
_ => {}
723-
},
669+
Node::Item(_)
670+
| Node::ForeignItem(_)
671+
| Node::TraitItem(_)
672+
| Node::ImplItem(_)
673+
| Node::Stmt(Stmt { kind: StmtKind::Local(_), .. }) => break,
674+
Node::Expr(expr @ Expr { kind: ExprKind::Match(..), .. }) => return Some(expr),
724675
_ => {}
725676
}
726677
}
@@ -730,32 +681,22 @@ impl<'hir> Map<'hir> {
730681
/// Returns the nearest enclosing scope. A scope is roughly an item or block.
731682
pub fn get_enclosing_scope(&self, hir_id: HirId) -> Option<HirId> {
732683
for (hir_id, node) in self.parent_iter(hir_id) {
733-
if match node {
734-
Node::Item(i) => match i.kind {
684+
if let Node::Item(Item {
685+
kind:
735686
ItemKind::Fn(..)
736687
| ItemKind::Mod(..)
737688
| ItemKind::Enum(..)
738689
| ItemKind::Struct(..)
739690
| ItemKind::Union(..)
740691
| ItemKind::Trait(..)
741-
| ItemKind::Impl { .. } => true,
742-
_ => false,
743-
},
744-
Node::ForeignItem(fi) => match fi.kind {
745-
ForeignItemKind::Fn(..) => true,
746-
_ => false,
747-
},
748-
Node::TraitItem(ti) => match ti.kind {
749-
TraitItemKind::Fn(..) => true,
750-
_ => false,
751-
},
752-
Node::ImplItem(ii) => match ii.kind {
753-
ImplItemKind::Fn(..) => true,
754-
_ => false,
755-
},
756-
Node::Block(_) => true,
757-
_ => false,
758-
} {
692+
| ItemKind::Impl { .. },
693+
..
694+
})
695+
| Node::ForeignItem(ForeignItem { kind: ForeignItemKind::Fn(..), .. })
696+
| Node::TraitItem(TraitItem { kind: TraitItemKind::Fn(..), .. })
697+
| Node::ImplItem(ImplItem { kind: ImplItemKind::Fn(..), .. })
698+
| Node::Block(_) = node
699+
{
759700
return Some(hir_id);
760701
}
761702
}
@@ -771,11 +712,11 @@ impl<'hir> Map<'hir> {
771712
return CRATE_HIR_ID;
772713
}
773714
match self.get(scope) {
774-
Node::Item(i) => match i.kind {
775-
ItemKind::OpaqueTy(OpaqueTy { impl_trait_fn: None, .. }) => {}
776-
_ => break,
777-
},
778-
Node::Block(_) => {}
715+
Node::Item(Item {
716+
kind: ItemKind::OpaqueTy(OpaqueTy { impl_trait_fn: None, .. }),
717+
..
718+
})
719+
| Node::Block(_) => {}
779720
_ => break,
780721
}
781722
}
@@ -823,14 +764,11 @@ impl<'hir> Map<'hir> {
823764

824765
pub fn expect_variant_data(&self, id: HirId) -> &'hir VariantData<'hir> {
825766
match self.find(id) {
826-
Some(Node::Item(i)) => match i.kind {
827-
ItemKind::Struct(ref struct_def, _) | ItemKind::Union(ref struct_def, _) => {
828-
struct_def
829-
}
830-
_ => bug!("struct ID bound to non-struct {}", self.node_to_string(id)),
831-
},
767+
Some(
768+
Node::Ctor(vd)
769+
| Node::Item(Item { kind: ItemKind::Struct(vd, _) | ItemKind::Union(vd, _), .. }),
770+
) => vd,
832771
Some(Node::Variant(variant)) => &variant.data,
833-
Some(Node::Ctor(data)) => data,
834772
_ => bug!("expected struct or variant, found {}", self.node_to_string(id)),
835773
}
836774
}

src/librustc_interface/passes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ pub fn create_global_ctxt<'tcx>(
776776
fn analysis(tcx: TyCtxt<'_>, cnum: CrateNum) -> Result<()> {
777777
assert_eq!(cnum, LOCAL_CRATE);
778778

779-
rustc::hir::map::check_crate(tcx);
779+
rustc_passes::hir_id_validator::check_crate(tcx);
780780

781781
let sess = tcx.sess;
782782
let mut entry_point = None;

src/librustc/hir/map/hir_id_validator.rs src/librustc_passes/hir_id_validator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::hir::map::Map;
2-
use crate::ty::TyCtxt;
1+
use rustc::hir::map::Map;
2+
use rustc::ty::TyCtxt;
33
use rustc_data_structures::fx::FxHashSet;
44
use rustc_data_structures::sync::{par_iter, Lock, ParallelIterator};
55
use rustc_hir as hir;

src/librustc_passes/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ mod check_const;
2121
pub mod dead;
2222
mod diagnostic_items;
2323
pub mod entry;
24+
pub mod hir_id_validator;
2425
pub mod hir_stats;
2526
mod intrinsicck;
2627
mod lang_items;

0 commit comments

Comments
 (0)