Skip to content

Commit 78fa21d

Browse files
committed
rustc_metadata: use NonZeroUsize for the position of a Lazy.
1 parent 66c8763 commit 78fa21d

File tree

4 files changed

+35
-25
lines changed

4 files changed

+35
-25
lines changed

src/librustc_metadata/decoder.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use rustc::util::captures::Captures;
2424

2525
use std::io;
2626
use std::mem;
27+
use std::num::NonZeroUsize;
2728
use std::u32;
2829

2930
use rustc_serialize::{Decodable, Decoder, SpecializedDecoder, opaque};
@@ -129,7 +130,7 @@ impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a CrateMetadata, TyCtxt<'a, 'tcx, 'tcx>
129130

130131
impl<'a, 'tcx: 'a, T: Decodable> Lazy<T> {
131132
pub fn decode<M: Metadata<'a, 'tcx>>(self, meta: M) -> T {
132-
let mut dcx = meta.decoder(self.position);
133+
let mut dcx = meta.decoder(self.position.get());
133134
dcx.lazy_state = LazyState::NodeStart(self.position);
134135
T::decode(&mut dcx).unwrap()
135136
}
@@ -140,7 +141,7 @@ impl<'a, 'tcx: 'a, T: Decodable> Lazy<[T]> {
140141
self,
141142
meta: M,
142143
) -> impl Iterator<Item = T> + Captures<'tcx> + 'a {
143-
let mut dcx = meta.decoder(self.position);
144+
let mut dcx = meta.decoder(self.position.get());
144145
dcx.lazy_state = LazyState::NodeStart(self.position);
145146
(0..self.meta).map(move |_| T::decode(&mut dcx).unwrap())
146147
}
@@ -164,13 +165,14 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
164165
let position = match self.lazy_state {
165166
LazyState::NoNode => bug!("read_lazy_with_meta: outside of a metadata node"),
166167
LazyState::NodeStart(start) => {
168+
let start = start.get();
167169
assert!(distance + min_size <= start);
168170
start - distance - min_size
169171
}
170-
LazyState::Previous(last_min_end) => last_min_end + distance,
172+
LazyState::Previous(last_min_end) => last_min_end.get() + distance,
171173
};
172-
self.lazy_state = LazyState::Previous(position + min_size);
173-
Ok(Lazy::from_position_and_meta(position, meta))
174+
self.lazy_state = LazyState::Previous(NonZeroUsize::new(position + min_size).unwrap());
175+
Ok(Lazy::from_position_and_meta(NonZeroUsize::new(position).unwrap(), meta))
174176
}
175177
}
176178

@@ -375,7 +377,9 @@ impl<'tcx> MetadataBlob {
375377
}
376378

377379
pub fn get_rustc_version(&self) -> String {
378-
Lazy::<String>::from_position(METADATA_HEADER.len() + 4).decode(self)
380+
Lazy::<String>::from_position(
381+
NonZeroUsize::new(METADATA_HEADER.len() + 4).unwrap(),
382+
).decode(self)
379383
}
380384

381385
pub fn get_root(&self) -> CrateRoot<'tcx> {
@@ -384,7 +388,9 @@ impl<'tcx> MetadataBlob {
384388
let pos = (((slice[offset + 0] as u32) << 24) | ((slice[offset + 1] as u32) << 16) |
385389
((slice[offset + 2] as u32) << 8) |
386390
((slice[offset + 3] as u32) << 0)) as usize;
387-
Lazy::<CrateRoot<'tcx>>::from_position(pos).decode(self)
391+
Lazy::<CrateRoot<'tcx>>::from_position(
392+
NonZeroUsize::new(pos).unwrap(),
393+
).decode(self)
388394
}
389395

390396
pub fn list_crate_metadata(&self,

src/librustc_metadata/encoder.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ use rustc::session::config::{self, CrateType};
2222
use rustc::util::nodemap::FxHashMap;
2323

2424
use rustc_data_structures::stable_hasher::StableHasher;
25+
use rustc_data_structures::sync::Lrc;
2526
use rustc_serialize::{Encodable, Encoder, SpecializedEncoder, opaque};
2627

2728
use std::hash::Hash;
29+
use std::num::NonZeroUsize;
2830
use std::path::Path;
29-
use rustc_data_structures::sync::Lrc;
3031
use std::u32;
3132
use syntax::ast;
3233
use syntax::attr;
@@ -263,10 +264,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
263264
&mut self,
264265
lazy: Lazy<T>,
265266
) -> Result<(), <Self as Encoder>::Error> {
266-
let min_end = lazy.position + T::min_size(lazy.meta);
267+
let min_end = lazy.position.get() + T::min_size(lazy.meta);
267268
let distance = match self.lazy_state {
268269
LazyState::NoNode => bug!("emit_lazy_distance: outside of a metadata node"),
269270
LazyState::NodeStart(start) => {
271+
let start = start.get();
270272
assert!(min_end <= start);
271273
start - min_end
272274
}
@@ -276,25 +278,25 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
276278
"make sure that the calls to `lazy*` \
277279
are in the same order as the metadata fields",
278280
);
279-
lazy.position - last_min_end
281+
lazy.position.get() - last_min_end.get()
280282
}
281283
};
282-
self.lazy_state = LazyState::Previous(min_end);
284+
self.lazy_state = LazyState::Previous(NonZeroUsize::new(min_end).unwrap());
283285
self.emit_usize(distance)
284286
}
285287

286288
fn lazy<T: ?Sized + LazyMeta>(
287289
&mut self,
288290
value: impl EncodeContentsForLazy<T>,
289291
) -> Lazy<T> {
290-
let pos = self.position();
292+
let pos = NonZeroUsize::new(self.position()).unwrap();
291293

292294
assert_eq!(self.lazy_state, LazyState::NoNode);
293295
self.lazy_state = LazyState::NodeStart(pos);
294296
let meta = value.encode_contents_for_lazy(self);
295297
self.lazy_state = LazyState::NoNode;
296298

297-
assert!(pos + <T>::min_size(meta) <= self.position());
299+
assert!(pos.get() + <T>::min_size(meta) <= self.position());
298300

299301
Lazy::from_position_and_meta(pos, meta)
300302
}
@@ -1893,7 +1895,7 @@ pub fn encode_metadata<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>)
18931895

18941896
// Encode the root position.
18951897
let header = METADATA_HEADER.len();
1896-
let pos = root.position;
1898+
let pos = root.position.get();
18971899
result[header + 0] = (pos >> 24) as u8;
18981900
result[header + 1] = (pos >> 16) as u8;
18991901
result[header + 2] = (pos >> 8) as u8;

src/librustc_metadata/index.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::schema::*;
33
use rustc::hir::def_id::{DefId, DefIndex, DefIndexAddressSpace};
44
use rustc_serialize::opaque::Encoder;
55
use std::marker::PhantomData;
6+
use std::num::NonZeroUsize;
67
use std::u32;
78
use log::debug;
89

@@ -95,8 +96,8 @@ impl Index<'tcx> {
9596
}
9697

9798
pub fn record_index(&mut self, item: DefIndex, entry: Lazy<Entry<'tcx>>) {
98-
assert!(entry.position < (u32::MAX as usize));
99-
let position = entry.position as u32;
99+
assert!(entry.position.get() < (u32::MAX as usize));
100+
let position = entry.position.get() as u32;
100101
let space_index = item.address_space().index();
101102
let array_index = item.as_array_index();
102103

@@ -120,7 +121,7 @@ impl Index<'tcx> {
120121
// ... then the values in the higher range.
121122
buf.emit_raw_bytes(&self.positions[1]);
122123
Lazy::from_position_and_meta(
123-
pos as usize,
124+
NonZeroUsize::new(pos as usize).unwrap(),
124125
(self.positions[0].len() + self.positions[1].len()) / 4 + 1,
125126
)
126127
}
@@ -131,7 +132,7 @@ impl Lazy<[Index<'tcx>]> {
131132
/// DefIndex (if any).
132133
#[inline(never)]
133134
pub fn lookup(&self, bytes: &[u8], def_index: DefIndex) -> Option<Lazy<Entry<'tcx>>> {
134-
let bytes = &bytes[self.position..];
135+
let bytes = &bytes[self.position.get()..];
135136
debug!("Index::lookup: index={:?} len={:?}",
136137
def_index,
137138
self.meta);
@@ -151,7 +152,7 @@ impl Lazy<[Index<'tcx>]> {
151152
None
152153
} else {
153154
debug!("Index::lookup: position={:?}", position);
154-
Some(Lazy::from_position(position as usize))
155+
Some(Lazy::from_position(NonZeroUsize::new(position as usize).unwrap()))
155156
}
156157
}
157158
}

src/librustc_metadata/schema.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use syntax::symbol::Symbol;
1919
use syntax_pos::{self, Span};
2020

2121
use std::marker::PhantomData;
22+
use std::num::NonZeroUsize;
2223

2324
pub fn rustc_version() -> String {
2425
format!("rustc {}",
@@ -101,13 +102,13 @@ pub struct Lazy<T, Meta = <T as LazyMeta>::Meta>
101102
where T: ?Sized + LazyMeta<Meta = Meta>,
102103
Meta: 'static + Copy,
103104
{
104-
pub position: usize,
105+
pub position: NonZeroUsize,
105106
pub meta: Meta,
106107
_marker: PhantomData<T>,
107108
}
108109

109110
impl<T: ?Sized + LazyMeta> Lazy<T> {
110-
pub fn from_position_and_meta(position: usize, meta: T::Meta) -> Lazy<T> {
111+
pub fn from_position_and_meta(position: NonZeroUsize, meta: T::Meta) -> Lazy<T> {
111112
Lazy {
112113
position,
113114
meta,
@@ -117,14 +118,14 @@ impl<T: ?Sized + LazyMeta> Lazy<T> {
117118
}
118119

119120
impl<T> Lazy<T> {
120-
pub fn from_position(position: usize) -> Lazy<T> {
121+
pub fn from_position(position: NonZeroUsize) -> Lazy<T> {
121122
Lazy::from_position_and_meta(position, ())
122123
}
123124
}
124125

125126
impl<T> Lazy<[T]> {
126127
pub fn empty() -> Lazy<[T]> {
127-
Lazy::from_position_and_meta(0, 0)
128+
Lazy::from_position_and_meta(NonZeroUsize::new(1).unwrap(), 0)
128129
}
129130
}
130131

@@ -146,12 +147,12 @@ pub enum LazyState {
146147

147148
/// Inside a metadata node, and before any `Lazy`.
148149
/// The position is that of the node itself.
149-
NodeStart(usize),
150+
NodeStart(NonZeroUsize),
150151

151152
/// Inside a metadata node, with a previous `Lazy`.
152153
/// The position is a conservative estimate of where that
153154
/// previous `Lazy` would end (see their comments).
154-
Previous(usize),
155+
Previous(NonZeroUsize),
155156
}
156157

157158
#[derive(RustcEncodable, RustcDecodable)]

0 commit comments

Comments
 (0)