Skip to content

Commit 6b33f47

Browse files
committed
remove usize: DepGraphRead and add Untracked
The idea is that a `usize` is sort of ambiguous: in this case, it represents indices that do not need tracking, but it could as easily be some data read out from a tracked location, and hence represent tracked data. Therefore, we add an `Untracked` type that lets user assert that value is not tracked. Also correct various typos.
1 parent 9daea5b commit 6b33f47

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

src/librustc_metadata/encoder.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use rustc::hir::intravisit::Visitor;
5353
use rustc::hir::intravisit;
5454
use rustc::hir::map::DefKey;
5555

56-
use super::index_builder::{FromId, IndexBuilder, ItemContentBuilder, XRef};
56+
use super::index_builder::{FromId, IndexBuilder, ItemContentBuilder, Untracked, XRef};
5757

5858
pub struct EncodeContext<'a, 'tcx: 'a> {
5959
pub diag: &'a Handler,
@@ -206,15 +206,20 @@ impl<'a, 'tcx, 'encoder> IndexBuilder<'a, 'tcx, 'encoder> {
206206
for (i, variant) in def.variants.iter().enumerate() {
207207
self.record(variant.did,
208208
ItemContentBuilder::encode_enum_variant_info,
209-
(enum_did, i));
209+
(enum_did, Untracked(i)));
210210
}
211211
}
212212
}
213213

214214
impl<'a, 'tcx, 'encoder> ItemContentBuilder<'a, 'tcx, 'encoder> {
215+
/// Encode data for the given variant of the given ADT. The
216+
/// index of the variant is untracked: this is ok because we
217+
/// will have to lookup the adt-def by its id, and that gives us
218+
/// the right to access any information in the adt-def (including,
219+
/// e.g., the length of the various vectors).
215220
fn encode_enum_variant_info(&mut self,
216-
(enum_did, index):
217-
(DefId, usize)) {
221+
(enum_did, Untracked(index)):
222+
(DefId, Untracked<usize>)) {
218223
let ecx = self.ecx;
219224
let def = ecx.tcx.lookup_adt_def(enum_did);
220225
let variant = &def.variants[index];
@@ -420,16 +425,22 @@ impl<'a, 'tcx, 'encoder> IndexBuilder<'a, 'tcx, 'encoder> {
420425
for (field_index, field) in variant.fields.iter().enumerate() {
421426
self.record(field.did,
422427
ItemContentBuilder::encode_field,
423-
(adt_def_id, variant_index, field_index));
428+
(adt_def_id, Untracked((variant_index, field_index))));
424429
}
425430
}
426431
}
427432
}
428433

429434
impl<'a, 'tcx, 'encoder> ItemContentBuilder<'a, 'tcx, 'encoder> {
435+
/// Encode data for the given field of the given variant of the
436+
/// given ADT. The indices of the variant/field are untracked:
437+
/// this is ok because we will have to lookup the adt-def by its
438+
/// id, and that gives us the right to access any information in
439+
/// the adt-def (including, e.g., the length of the various
440+
/// vectors).
430441
fn encode_field(&mut self,
431-
(adt_def_id, variant_index, field_index):
432-
(DefId, usize, usize)) {
442+
(adt_def_id, Untracked((variant_index, field_index))):
443+
(DefId, Untracked<(usize, usize)>)) {
433444
let ecx = self.ecx();
434445
let def = ecx.tcx.lookup_adt_def(adt_def_id);
435446
let variant = &def.variants[variant_index];

src/librustc_metadata/index_builder.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
//!
2525
//! In addition to the offset, we need to track the data that was used
2626
//! 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
27+
//! can figure out which HIR nodes contributed to that data for
2828
//! incremental compilation purposes.
2929
//!
30-
//! The `IndexBuilder` facilitates with both of these. It is created
30+
//! The `IndexBuilder` facilitates both of these. It is created
3131
//! with an RBML encoder isntance (`rbml_w`) along with an
3232
//! `EncodingContext` (`ecx`), which it encapsulates. It has one main
3333
//! method, `record()`. You invoke `record` like so to create a new
@@ -166,10 +166,6 @@ pub trait DepGraphRead {
166166
fn read(&self, tcx: TyCtxt);
167167
}
168168

169-
impl DepGraphRead for usize {
170-
fn read(&self, _tcx: TyCtxt) { }
171-
}
172-
173169
impl DepGraphRead for DefId {
174170
fn read(&self, _tcx: TyCtxt) { }
175171
}
@@ -229,6 +225,21 @@ read_hir!(hir::ImplItem);
229225
read_hir!(hir::TraitItem);
230226
read_hir!(hir::ForeignItem);
231227

228+
/// Leaks access to a value of type T without any tracking. This is
229+
/// suitable for ambiguous types like `usize`, which *could* represent
230+
/// tracked data (e.g., if you read it out of a HIR node) or might not
231+
/// (e.g., if it's an index). Adding in an `Untracked` is an
232+
/// assertion, essentially, that the data does not need to be tracked
233+
/// (or that read edges will be added by some other way).
234+
///
235+
/// A good idea is to add to each use of `Untracked` an explanation of
236+
/// why this value is ok.
237+
pub struct Untracked<T>(pub T);
238+
239+
impl<T> DepGraphRead for Untracked<T> {
240+
fn read(&self, _tcx: TyCtxt) { }
241+
}
242+
232243
/// Newtype that can be used to package up misc data extracted from a
233244
/// HIR node that doesn't carry its own id. This will allow an
234245
/// arbitrary `T` to be passed in, but register a read on the given

0 commit comments

Comments
 (0)