Skip to content

Commit 9daea5b

Browse files
committed
Add a comment, remove Deref/DerefMut
The comment explains the `index-builder` pattern. We no longer need the `Deref/DerefMut` relationship, and it seems nicer without it.
1 parent 1a91e67 commit 9daea5b

File tree

2 files changed

+79
-33
lines changed

2 files changed

+79
-33
lines changed

src/librustc_metadata/encoder.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ fn encode_item_sort(rbml_w: &mut Encoder, sort: char) {
415415
impl<'a, 'tcx, 'encoder> IndexBuilder<'a, 'tcx, 'encoder> {
416416
fn encode_fields(&mut self,
417417
adt_def_id: DefId) {
418-
let def = self.ecx.tcx.lookup_adt_def(adt_def_id);
418+
let def = self.ecx().tcx.lookup_adt_def(adt_def_id);
419419
for (variant_index, variant) in def.variants.iter().enumerate() {
420420
for (field_index, field) in variant.fields.iter().enumerate() {
421421
self.record(field.did,
@@ -1155,7 +1155,7 @@ impl<'a, 'tcx, 'encoder> IndexBuilder<'a, 'tcx, 'encoder> {
11551155
/// normally in the visitor walk.
11561156
fn encode_addl_info_for_item(&mut self,
11571157
item: &hir::Item) {
1158-
let def_id = self.ecx.tcx.map.local_def_id(item.id);
1158+
let def_id = self.ecx().tcx.map.local_def_id(item.id);
11591159
match item.node {
11601160
hir::ItemStatic(..) |
11611161
hir::ItemConst(..) |
@@ -1187,7 +1187,7 @@ impl<'a, 'tcx, 'encoder> IndexBuilder<'a, 'tcx, 'encoder> {
11871187
def_id: DefId,
11881188
struct_node_id: ast::NodeId,
11891189
item: &hir::Item) {
1190-
let ecx = self.ecx;
1190+
let ecx = self.ecx();
11911191
let def = ecx.tcx.lookup_adt_def(def_id);
11921192
let variant = def.struct_variant();
11931193

@@ -1213,7 +1213,7 @@ impl<'a, 'tcx, 'encoder> IndexBuilder<'a, 'tcx, 'encoder> {
12131213
def_id: DefId,
12141214
impl_id: ast::NodeId,
12151215
ast_items: &[hir::ImplItem]) {
1216-
let ecx = self.ecx;
1216+
let ecx = self.ecx();
12171217
let impl_items = ecx.tcx.impl_items.borrow();
12181218
let items = &impl_items[&def_id];
12191219

@@ -1240,7 +1240,7 @@ impl<'a, 'tcx, 'encoder> IndexBuilder<'a, 'tcx, 'encoder> {
12401240
def_id: DefId,
12411241
trait_items: &[hir::TraitItem]) {
12421242
// Now output the trait item info for each trait item.
1243-
let tcx = self.ecx.tcx;
1243+
let tcx = self.ecx().tcx;
12441244
let r = tcx.trait_item_def_ids(def_id);
12451245
for (item_def_id, trait_item) in r.iter().zip(trait_items) {
12461246
let item_def_id = item_def_id.def_id();
@@ -1311,7 +1311,7 @@ impl<'a, 'ecx, 'tcx, 'encoder> Visitor<'tcx> for EncodeVisitor<'a, 'ecx, 'tcx, '
13111311
}
13121312
fn visit_item(&mut self, item: &'tcx hir::Item) {
13131313
intravisit::walk_item(self, item);
1314-
let def_id = self.index.ecx.tcx.map.local_def_id(item.id);
1314+
let def_id = self.index.ecx().tcx.map.local_def_id(item.id);
13151315
match item.node {
13161316
hir::ItemExternCrate(_) | hir::ItemUse(_) => (), // ignore these
13171317
_ => self.index.record(def_id,
@@ -1322,7 +1322,7 @@ impl<'a, 'ecx, 'tcx, 'encoder> Visitor<'tcx> for EncodeVisitor<'a, 'ecx, 'tcx, '
13221322
}
13231323
fn visit_foreign_item(&mut self, ni: &'tcx hir::ForeignItem) {
13241324
intravisit::walk_foreign_item(self, ni);
1325-
let def_id = self.index.ecx.tcx.map.local_def_id(ni.id);
1325+
let def_id = self.index.ecx().tcx.map.local_def_id(ni.id);
13261326
self.index.record(def_id,
13271327
ItemContentBuilder::encode_info_for_foreign_item,
13281328
(def_id, ni));

src/librustc_metadata/index_builder.rs

Lines changed: 72 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,60 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
//! Builder types for generating the "item data" section of the
12+
//! metadata. This section winds up looking like this:
13+
//!
14+
//! ```
15+
//! <common::data> // big list of item-like things...
16+
//! <common::data_item> // ...for most def-ids, there is an entry.
17+
//! </common::data_item>
18+
//! </common::data>
19+
//! ```
20+
//!
21+
//! As we generate this listing, we collect the offset of each
22+
//! `data_item` entry and store it in an index. Then, when we load the
23+
//! metadata, we can skip right to the metadata for a particular item.
24+
//!
25+
//! In addition to the offset, we need to track the data that was used
26+
//! to generate the contents of each `data_item`. This is so that we
27+
//! can figure out which HIR nodes contributors to that data for
28+
//! incremental compilation purposes.
29+
//!
30+
//! The `IndexBuilder` facilitates with both of these. It is created
31+
//! with an RBML encoder isntance (`rbml_w`) along with an
32+
//! `EncodingContext` (`ecx`), which it encapsulates. It has one main
33+
//! method, `record()`. You invoke `record` like so to create a new
34+
//! `data_item` element in the list:
35+
//!
36+
//! ```
37+
//! index.record(some_def_id, callback_fn, data)
38+
//! ```
39+
//!
40+
//! What record will do is to (a) record the current offset, (b) emit
41+
//! the `common::data_item` tag, and then call `callback_fn` with the
42+
//! given data as well as an `ItemContentBuilder`. Once `callback_fn`
43+
//! returns, the `common::data_item` tag will be closed.
44+
//!
45+
//! The `ItemContentBuilder` is another type that just offers access
46+
//! to the `ecx` and `rbml_w` that were given in, as well as
47+
//! maintaining a list of `xref` instances, which are used to extract
48+
//! common data so it is not re-serialized.
49+
//!
50+
//! `ItemContentBuilder` is a distinct type which does not offer the
51+
//! `record` method, so that we can ensure that `common::data_item` elements
52+
//! are never nested.
53+
//!
54+
//! In addition, while the `callback_fn` is executing, we will push a
55+
//! task `MetaData(some_def_id)`, which can then observe the
56+
//! reads/writes that occur in the task. For this reason, the `data`
57+
//! argument that is given to the `callback_fn` must implement the
58+
//! trait `DepGraphRead`, which indicates how to register reads on the
59+
//! data in this new task (note that many types of data, such as
60+
//! `DefId`, do not currently require any reads to be registered,
61+
//! since they are not derived from a HIR node). This is also why we
62+
//! give a callback fn, rather than taking a closure: it allows us to
63+
//! easily control precisely what data is given to that fn.
64+
1165
use common::tag_items_data_item;
1266
use encoder::EncodeContext;
1367
use index::IndexData;
@@ -18,7 +72,6 @@ use rustc::hir::def_id::DefId;
1872
use rustc::ty::{self, TyCtxt};
1973
use rustc_data_structures::fnv::FnvHashMap;
2074
use syntax::ast;
21-
use std::ops::{Deref, DerefMut};
2275

2376
/// Builder that can encode new items, adding them into the index.
2477
/// Item encoding cannot be nested.
@@ -53,6 +106,10 @@ impl<'a, 'tcx, 'encoder> IndexBuilder<'a, 'tcx, 'encoder> {
53106
}
54107
}
55108

109+
pub fn ecx(&self) -> &'a EncodeContext<'a, 'tcx> {
110+
self.builder.ecx()
111+
}
112+
56113
/// Emit the data for a def-id to the metadata. The function to
57114
/// emit the data is `op`, and it will be given `data` as
58115
/// arguments. This `record` function will start/end an RBML tag
@@ -76,34 +133,20 @@ impl<'a, 'tcx, 'encoder> IndexBuilder<'a, 'tcx, 'encoder> {
76133
data: DATA)
77134
where DATA: DepGraphRead
78135
{
79-
let position = self.rbml_w.mark_stable_position();
136+
let position = self.builder.rbml_w.mark_stable_position();
80137
self.items.record(id, position);
81-
let _task = self.ecx.tcx.dep_graph.in_task(DepNode::MetaData(id));
82-
self.rbml_w.start_tag(tag_items_data_item).unwrap();
83-
data.read(self.ecx.tcx);
84-
op(self, data);
85-
self.rbml_w.end_tag().unwrap();
138+
let _task = self.ecx().tcx.dep_graph.in_task(DepNode::MetaData(id));
139+
self.builder.rbml_w.start_tag(tag_items_data_item).unwrap();
140+
data.read(self.ecx().tcx);
141+
op(&mut self.builder, data);
142+
self.builder.rbml_w.end_tag().unwrap();
86143
}
87144

88145
pub fn into_fields(self) -> (IndexData, FnvHashMap<XRef<'tcx>, u32>) {
89146
(self.items, self.builder.xrefs)
90147
}
91148
}
92149

93-
impl<'a, 'tcx, 'encoder> Deref for IndexBuilder<'a, 'tcx, 'encoder> {
94-
type Target = ItemContentBuilder<'a, 'tcx, 'encoder>;
95-
96-
fn deref(&self) -> &Self::Target {
97-
&self.builder
98-
}
99-
}
100-
101-
impl<'a, 'tcx, 'encoder> DerefMut for IndexBuilder<'a, 'tcx, 'encoder> {
102-
fn deref_mut(&mut self) -> &mut Self::Target {
103-
&mut self.builder
104-
}
105-
}
106-
107150
impl<'a, 'tcx, 'encoder> ItemContentBuilder<'a, 'tcx, 'encoder> {
108151
pub fn ecx(&self) -> &'a EncodeContext<'a, 'tcx> {
109152
self.ecx
@@ -115,9 +158,10 @@ impl<'a, 'tcx, 'encoder> ItemContentBuilder<'a, 'tcx, 'encoder> {
115158
}
116159
}
117160

118-
/// Trait that registers reads for types that are tracked in the
119-
/// dep-graph. Mostly it is implemented for indices like DefId etc
120-
/// which do not need to register a read.
161+
/// Trait used for data that can be passed from outside a dep-graph
162+
/// task. The data must either be of some safe type, such as a
163+
/// `DefId` index, or implement the `read` method so that it can add
164+
/// a read of whatever dep-graph nodes are appropriate.
121165
pub trait DepGraphRead {
122166
fn read(&self, tcx: TyCtxt);
123167
}
@@ -185,8 +229,10 @@ read_hir!(hir::ImplItem);
185229
read_hir!(hir::TraitItem);
186230
read_hir!(hir::ForeignItem);
187231

188-
/// You can use `FromId(X, ...)` to indicate that `...` came from node
189-
/// `X`; so we will add a read from the suitable `Hir` node.
232+
/// Newtype that can be used to package up misc data extracted from a
233+
/// HIR node that doesn't carry its own id. This will allow an
234+
/// arbitrary `T` to be passed in, but register a read on the given
235+
/// node-id.
190236
pub struct FromId<T>(pub ast::NodeId, pub T);
191237

192238
impl<T> DepGraphRead for FromId<T> {

0 commit comments

Comments
 (0)