Skip to content

Commit f64e73b

Browse files
committed
rustc: simplify constant cross-crate loading and rustc_passes::consts.
1 parent f89856b commit f64e73b

File tree

24 files changed

+390
-993
lines changed

24 files changed

+390
-993
lines changed

src/librustc/hir/map/collector.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use super::*;
1212

1313
use hir::intravisit::{Visitor, NestedVisitorMap};
14-
use middle::cstore::InlinedItem;
1514
use std::iter::repeat;
1615
use syntax::ast::{NodeId, CRATE_NODE_ID};
1716
use syntax_pos::Span;
@@ -21,7 +20,7 @@ pub struct NodeCollector<'ast> {
2120
/// The crate
2221
pub krate: &'ast Crate,
2322
/// The node map
24-
pub map: Vec<MapEntry<'ast>>,
23+
pub(super) map: Vec<MapEntry<'ast>>,
2524
/// The parent of this node
2625
pub parent_node: NodeId,
2726
/// If true, completely ignore nested items. We set this when loading
@@ -43,11 +42,11 @@ impl<'ast> NodeCollector<'ast> {
4342
collector
4443
}
4544

46-
pub fn extend(krate: &'ast Crate,
47-
parent: &'ast InlinedItem,
48-
parent_node: NodeId,
49-
map: Vec<MapEntry<'ast>>)
50-
-> NodeCollector<'ast> {
45+
pub(super) fn extend(krate: &'ast Crate,
46+
parent: &'ast InlinedItem,
47+
parent_node: NodeId,
48+
map: Vec<MapEntry<'ast>>)
49+
-> NodeCollector<'ast> {
5150
let mut collector = NodeCollector {
5251
krate: krate,
5352
map: map,

src/librustc/hir/map/mod.rs

Lines changed: 92 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ pub use self::definitions::{Definitions, DefKey, DefPath, DefPathData,
1717

1818
use dep_graph::{DepGraph, DepNode};
1919

20-
use middle::cstore::InlinedItem;
2120
use hir::def_id::{CRATE_DEF_INDEX, DefId, DefIndex};
2221

2322
use syntax::abi::Abi;
@@ -26,6 +25,7 @@ use syntax::codemap::Spanned;
2625
use syntax_pos::Span;
2726

2827
use hir::*;
28+
use hir::intravisit::Visitor;
2929
use hir::print as pprust;
3030

3131
use arena::TypedArena;
@@ -38,6 +38,15 @@ mod collector;
3838
mod def_collector;
3939
pub mod definitions;
4040

41+
/// The data we save and restore about an inlined item or method. This is not
42+
/// part of the AST that we parse from a file, but it becomes part of the tree
43+
/// that we trans.
44+
#[derive(Debug)]
45+
struct InlinedItem {
46+
def_id: DefId,
47+
body: Body,
48+
}
49+
4150
#[derive(Copy, Clone, Debug)]
4251
pub enum Node<'ast> {
4352
NodeItem(&'ast Item),
@@ -60,14 +69,12 @@ pub enum Node<'ast> {
6069
NodeLifetime(&'ast Lifetime),
6170
NodeTyParam(&'ast TyParam),
6271
NodeVisibility(&'ast Visibility),
63-
64-
NodeInlinedItem(&'ast InlinedItem),
6572
}
6673

6774
/// Represents an entry and its parent NodeID.
6875
/// The odd layout is to bring down the total size.
6976
#[derive(Copy, Debug)]
70-
pub enum MapEntry<'ast> {
77+
enum MapEntry<'ast> {
7178
/// Placeholder for holes in the map.
7279
NotPresent,
7380

@@ -121,8 +128,6 @@ impl<'ast> MapEntry<'ast> {
121128
NodeLifetime(n) => EntryLifetime(p, n),
122129
NodeTyParam(n) => EntryTyParam(p, n),
123130
NodeVisibility(n) => EntryVisibility(p, n),
124-
125-
NodeInlinedItem(n) => RootInlinedParent(n),
126131
}
127132
}
128133

@@ -171,10 +176,49 @@ impl<'ast> MapEntry<'ast> {
171176
EntryLifetime(_, n) => NodeLifetime(n),
172177
EntryTyParam(_, n) => NodeTyParam(n),
173178
EntryVisibility(_, n) => NodeVisibility(n),
174-
RootInlinedParent(n) => NodeInlinedItem(n),
175179
_ => return None
176180
})
177181
}
182+
183+
fn is_body_owner(self, node_id: NodeId) -> bool {
184+
match self {
185+
EntryItem(_, item) => {
186+
match item.node {
187+
ItemConst(_, body) |
188+
ItemStatic(.., body) |
189+
ItemFn(_, _, _, _, _, body) => body.node_id == node_id,
190+
_ => false
191+
}
192+
}
193+
194+
EntryTraitItem(_, item) => {
195+
match item.node {
196+
TraitItemKind::Const(_, Some(body)) |
197+
TraitItemKind::Method(_, TraitMethod::Provided(body)) => {
198+
body.node_id == node_id
199+
}
200+
_ => false
201+
}
202+
}
203+
204+
EntryImplItem(_, item) => {
205+
match item.node {
206+
ImplItemKind::Const(_, body) |
207+
ImplItemKind::Method(_, body) => body.node_id == node_id,
208+
_ => false
209+
}
210+
}
211+
212+
EntryExpr(_, expr) => {
213+
match expr.node {
214+
ExprClosure(.., body, _) => body.node_id == node_id,
215+
_ => false
216+
}
217+
}
218+
219+
_ => false
220+
}
221+
}
178222
}
179223

180224
/// Stores a crate and any number of inlined items from other crates.
@@ -250,42 +294,19 @@ impl<'ast> Map<'ast> {
250294
if !self.is_inlined_node_id(id) {
251295
let mut last_expr = None;
252296
loop {
253-
match map[id.as_usize()] {
254-
EntryItem(_, item) => {
255-
assert_eq!(id, item.id);
256-
let def_id = self.local_def_id(id);
257-
258-
if let Some(last_id) = last_expr {
259-
// The body of the item may have a separate dep node
260-
if self.is_item_body(last_id, item) {
261-
return DepNode::HirBody(def_id);
262-
}
263-
}
264-
return DepNode::Hir(def_id);
265-
}
266-
267-
EntryTraitItem(_, item) => {
268-
let def_id = self.local_def_id(id);
269-
270-
if let Some(last_id) = last_expr {
271-
// The body of the item may have a separate dep node
272-
if self.is_trait_item_body(last_id, item) {
273-
return DepNode::HirBody(def_id);
274-
}
275-
}
276-
return DepNode::Hir(def_id);
277-
}
278-
279-
EntryImplItem(_, item) => {
280-
let def_id = self.local_def_id(id);
281-
297+
let entry = map[id.as_usize()];
298+
match entry {
299+
EntryItem(..) |
300+
EntryTraitItem(..) |
301+
EntryImplItem(..) => {
282302
if let Some(last_id) = last_expr {
283-
// The body of the item may have a separate dep node
284-
if self.is_impl_item_body(last_id, item) {
303+
// The body may have a separate dep node
304+
if entry.is_body_owner(last_id) {
305+
let def_id = self.local_def_id(id);
285306
return DepNode::HirBody(def_id);
286307
}
287308
}
288-
return DepNode::Hir(def_id);
309+
return DepNode::Hir(self.local_def_id(id));
289310
}
290311

291312
EntryVariant(p, v) => {
@@ -377,33 +398,6 @@ impl<'ast> Map<'ast> {
377398
}
378399
}
379400

380-
fn is_item_body(&self, node_id: NodeId, item: &Item) -> bool {
381-
match item.node {
382-
ItemConst(_, body) |
383-
ItemStatic(.., body) |
384-
ItemFn(_, _, _, _, _, body) => body.node_id == node_id,
385-
_ => false
386-
}
387-
}
388-
389-
fn is_trait_item_body(&self, node_id: NodeId, item: &TraitItem) -> bool {
390-
match item.node {
391-
TraitItemKind::Const(_, Some(body)) |
392-
TraitItemKind::Method(_, TraitMethod::Provided(body)) => {
393-
body.node_id == node_id
394-
}
395-
_ => false
396-
}
397-
}
398-
399-
fn is_impl_item_body(&self, node_id: NodeId, item: &ImplItem) -> bool {
400-
match item.node {
401-
ImplItemKind::Const(_, body) |
402-
ImplItemKind::Method(_, body) => body.node_id == node_id,
403-
_ => false
404-
}
405-
}
406-
407401
pub fn num_local_def_ids(&self) -> usize {
408402
self.definitions.len()
409403
}
@@ -483,6 +477,23 @@ impl<'ast> Map<'ast> {
483477
self.forest.krate.body(id)
484478
}
485479

480+
/// Returns the `NodeId` that corresponds to the definition of
481+
/// which this is the body of, i.e. a `fn`, `const` or `static`
482+
/// item (possibly associated), or a closure, or the body itself
483+
/// for embedded constant expressions (e.g. `N` in `[T; N]`).
484+
pub fn body_owner(&self, BodyId { node_id }: BodyId) -> NodeId {
485+
let parent = self.get_parent_node(node_id);
486+
if self.map.borrow()[parent.as_usize()].is_body_owner(node_id) {
487+
parent
488+
} else {
489+
node_id
490+
}
491+
}
492+
493+
pub fn body_owner_def_id(&self, id: BodyId) -> DefId {
494+
self.local_def_id(self.body_owner(id))
495+
}
496+
486497
/// Get the attributes on the krate. This is preferable to
487498
/// invoking `krate.attrs` because it registers a tighter
488499
/// dep-graph access.
@@ -726,9 +737,9 @@ impl<'ast> Map<'ast> {
726737
}
727738
}
728739

729-
pub fn expect_inlined_item(&self, id: NodeId) -> &'ast InlinedItem {
740+
pub fn expect_inlined_body(&self, id: NodeId) -> &'ast Body {
730741
match self.find_entry(id) {
731-
Some(RootInlinedParent(inlined_item)) => inlined_item,
742+
Some(RootInlinedParent(inlined_item)) => &inlined_item.body,
732743
_ => bug!("expected inlined item, found {}", self.node_to_string(id)),
733744
}
734745
}
@@ -969,24 +980,28 @@ pub fn map_crate<'ast>(forest: &'ast mut Forest,
969980
}
970981
}
971982

972-
/// Used for items loaded from external crate that are being inlined into this
983+
/// Used for bodies loaded from external crate that are being inlined into this
973984
/// crate.
974-
pub fn map_decoded_item<'ast>(map: &Map<'ast>,
975-
ii: InlinedItem,
976-
ii_parent_id: NodeId)
977-
-> &'ast InlinedItem {
985+
pub fn map_decoded_body<'ast>(map: &Map<'ast>,
986+
def_id: DefId,
987+
body: Body,
988+
parent_id: NodeId)
989+
-> &'ast Body {
978990
let _ignore = map.forest.dep_graph.in_ignore();
979991

980-
let ii = map.forest.inlined_items.alloc(ii);
992+
let ii = map.forest.inlined_items.alloc(InlinedItem {
993+
def_id: def_id,
994+
body: body
995+
});
981996

982997
let mut collector = NodeCollector::extend(map.krate(),
983998
ii,
984-
ii_parent_id,
999+
parent_id,
9851000
mem::replace(&mut *map.map.borrow_mut(), vec![]));
986-
ii.visit(&mut collector);
1001+
collector.visit_body(&ii.body);
9871002
*map.map.borrow_mut() = collector.map;
9881003

989-
ii
1004+
&ii.body
9901005
}
9911006

9921007
pub trait NodePrinter {
@@ -1016,8 +1031,6 @@ impl<'a> NodePrinter for pprust::State<'a> {
10161031
// printing.
10171032
NodeLocal(_) => bug!("cannot print isolated Local"),
10181033
NodeStructCtor(_) => bug!("cannot print isolated StructCtor"),
1019-
1020-
NodeInlinedItem(_) => bug!("cannot print inlined item"),
10211034
}
10221035
}
10231036
}
@@ -1131,9 +1144,6 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
11311144
Some(NodeVisibility(ref vis)) => {
11321145
format!("visibility {:?}{}", vis, id_str)
11331146
}
1134-
Some(NodeInlinedItem(_)) => {
1135-
format!("inlined item {}", id_str)
1136-
}
11371147
None => {
11381148
format!("unknown node{}", id_str)
11391149
}

src/librustc/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#![cfg_attr(stage0, feature(item_like_imports))]
3434
#![feature(libc)]
3535
#![feature(nonzero)]
36+
#![feature(pub_restricted)]
3637
#![feature(quote)]
3738
#![feature(rustc_diagnostic_macros)]
3839
#![feature(rustc_private)]
@@ -80,9 +81,8 @@ pub mod lint;
8081

8182
pub mod middle {
8283
pub mod astconv_util;
83-
pub mod expr_use_visitor; // STAGE0: increase glitch immunity
84+
pub mod expr_use_visitor;
8485
pub mod const_val;
85-
pub mod const_qualif;
8686
pub mod cstore;
8787
pub mod dataflow;
8888
pub mod dead;

src/librustc/middle/const_qualif.rs

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)