Skip to content

Commit ea13456

Browse files
committed
rustc_metadata: use NonZeroUsize for the position of a Lazy.
1 parent e505857 commit ea13456

File tree

4 files changed

+38
-25
lines changed

4 files changed

+38
-25
lines changed

src/librustc_metadata/decoder.rs

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

2626
use std::io;
2727
use std::mem;
28+
use std::num::NonZeroUsize;
2829
use std::u32;
2930

3031
use rustc_serialize::{Decodable, Decoder, SpecializedDecoder, opaque};
@@ -131,7 +132,7 @@ impl<'a, 'tcx> Metadata<'a, 'tcx> for (&'a CrateMetadata, TyCtxt<'tcx>) {
131132

132133
impl<'a, 'tcx, T: Decodable> Lazy<T> {
133134
crate fn decode<M: Metadata<'a, 'tcx>>(self, meta: M) -> T {
134-
let mut dcx = meta.decoder(self.position);
135+
let mut dcx = meta.decoder(self.position.get());
135136
dcx.lazy_state = LazyState::NodeStart(self.position);
136137
T::decode(&mut dcx).unwrap()
137138
}
@@ -142,7 +143,7 @@ impl<'a: 'x, 'tcx: 'x, 'x, T: Decodable> Lazy<[T]> {
142143
self,
143144
meta: M,
144145
) -> impl ExactSizeIterator<Item = T> + Captures<'a> + Captures<'tcx> + 'x {
145-
let mut dcx = meta.decoder(self.position);
146+
let mut dcx = meta.decoder(self.position.get());
146147
dcx.lazy_state = LazyState::NodeStart(self.position);
147148
(0..self.meta).map(move |_| T::decode(&mut dcx).unwrap())
148149
}
@@ -166,13 +167,14 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> {
166167
let position = match self.lazy_state {
167168
LazyState::NoNode => bug!("read_lazy_with_meta: outside of a metadata node"),
168169
LazyState::NodeStart(start) => {
170+
let start = start.get();
169171
assert!(distance + min_size <= start);
170172
start - distance - min_size
171173
}
172-
LazyState::Previous(last_min_end) => last_min_end + distance,
174+
LazyState::Previous(last_min_end) => last_min_end.get() + distance,
173175
};
174-
self.lazy_state = LazyState::Previous(position + min_size);
175-
Ok(Lazy::from_position_and_meta(position, meta))
176+
self.lazy_state = LazyState::Previous(NonZeroUsize::new(position + min_size).unwrap());
177+
Ok(Lazy::from_position_and_meta(NonZeroUsize::new(position).unwrap(), meta))
176178
}
177179
}
178180

@@ -384,7 +386,9 @@ impl<'tcx> MetadataBlob {
384386
}
385387

386388
crate fn get_rustc_version(&self) -> String {
387-
Lazy::<String>::from_position(METADATA_HEADER.len() + 4).decode(self)
389+
Lazy::<String>::from_position(
390+
NonZeroUsize::new(METADATA_HEADER.len() + 4).unwrap(),
391+
).decode(self)
388392
}
389393

390394
crate fn get_root(&self) -> CrateRoot<'tcx> {
@@ -393,7 +397,9 @@ impl<'tcx> MetadataBlob {
393397
let pos = (((slice[offset + 0] as u32) << 24) | ((slice[offset + 1] as u32) << 16) |
394398
((slice[offset + 2] as u32) << 8) |
395399
((slice[offset + 3] as u32) << 0)) as usize;
396-
Lazy::<CrateRoot<'tcx>>::from_position(pos).decode(self)
400+
Lazy::<CrateRoot<'tcx>>::from_position(
401+
NonZeroUsize::new(pos).unwrap(),
402+
).decode(self)
397403
}
398404

399405
crate fn list_crate_metadata(&self,

src/librustc_metadata/encoder.rs

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

2525
use rustc_data_structures::stable_hasher::StableHasher;
26+
use rustc_data_structures::sync::Lrc;
2627
use rustc_serialize::{Encodable, Encoder, SpecializedEncoder, opaque};
2728

2829
use std::hash::Hash;
30+
use std::num::NonZeroUsize;
2931
use std::path::Path;
30-
use rustc_data_structures::sync::Lrc;
3132
use std::u32;
3233
use syntax::ast;
3334
use syntax::attr;
@@ -271,10 +272,11 @@ impl<'tcx> EncodeContext<'tcx> {
271272
&mut self,
272273
lazy: Lazy<T>,
273274
) -> Result<(), <Self as Encoder>::Error> {
274-
let min_end = lazy.position + T::min_size(lazy.meta);
275+
let min_end = lazy.position.get() + T::min_size(lazy.meta);
275276
let distance = match self.lazy_state {
276277
LazyState::NoNode => bug!("emit_lazy_distance: outside of a metadata node"),
277278
LazyState::NodeStart(start) => {
279+
let start = start.get();
278280
assert!(min_end <= start);
279281
start - min_end
280282
}
@@ -284,25 +286,25 @@ impl<'tcx> EncodeContext<'tcx> {
284286
"make sure that the calls to `lazy*` \
285287
are in the same order as the metadata fields",
286288
);
287-
lazy.position - last_min_end
289+
lazy.position.get() - last_min_end.get()
288290
}
289291
};
290-
self.lazy_state = LazyState::Previous(min_end);
292+
self.lazy_state = LazyState::Previous(NonZeroUsize::new(min_end).unwrap());
291293
self.emit_usize(distance)
292294
}
293295

294296
fn lazy<T: ?Sized + LazyMeta>(
295297
&mut self,
296298
value: impl EncodeContentsForLazy<T>,
297299
) -> Lazy<T> {
298-
let pos = self.position();
300+
let pos = NonZeroUsize::new(self.position()).unwrap();
299301

300302
assert_eq!(self.lazy_state, LazyState::NoNode);
301303
self.lazy_state = LazyState::NodeStart(pos);
302304
let meta = value.encode_contents_for_lazy(self);
303305
self.lazy_state = LazyState::NoNode;
304306

305-
assert!(pos + <T>::min_size(meta) <= self.position());
307+
assert!(pos.get() + <T>::min_size(meta) <= self.position());
306308

307309
Lazy::from_position_and_meta(pos, meta)
308310
}
@@ -1934,7 +1936,7 @@ crate fn encode_metadata(tcx: TyCtxt<'_>) -> EncodedMetadata {
19341936

19351937
// Encode the root position.
19361938
let header = METADATA_HEADER.len();
1937-
let pos = root.position;
1939+
let pos = root.position.get();
19381940
result[header + 0] = (pos >> 24) as u8;
19391941
result[header + 1] = (pos >> 16) as u8;
19401942
result[header + 2] = (pos >> 8) as u8;

src/librustc_metadata/index.rs

Lines changed: 9 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};
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

@@ -94,8 +95,8 @@ impl Index<'tcx> {
9495
}
9596

9697
fn record_index(&mut self, item: DefIndex, entry: Lazy<Entry<'tcx>>) {
97-
assert!(entry.position < (u32::MAX as usize));
98-
let position = entry.position as u32;
98+
assert!(entry.position.get() < (u32::MAX as usize));
99+
let position = entry.position.get() as u32;
99100
let array_index = item.index();
100101

101102
let positions = &mut self.positions;
@@ -111,7 +112,10 @@ impl Index<'tcx> {
111112
crate fn write_index(&self, buf: &mut Encoder) -> Lazy<[Self]> {
112113
let pos = buf.position();
113114
buf.emit_raw_bytes(&self.positions);
114-
Lazy::from_position_and_meta(pos as usize, self.positions.len() / 4)
115+
Lazy::from_position_and_meta(
116+
NonZeroUsize::new(pos as usize).unwrap(),
117+
self.positions.len() / 4,
118+
)
115119
}
116120
}
117121

@@ -124,14 +128,14 @@ impl Lazy<[Index<'tcx>]> {
124128
def_index,
125129
self.meta);
126130

127-
let bytes = &bytes[self.position..][..self.meta * 4];
131+
let bytes = &bytes[self.position.get()..][..self.meta * 4];
128132
let position = u32::read_from_bytes_at(bytes, def_index.index());
129133
if position == u32::MAX {
130134
debug!("Index::lookup: position=u32::MAX");
131135
None
132136
} else {
133137
debug!("Index::lookup: position={:?}", position);
134-
Some(Lazy::from_position(position as usize))
138+
Some(Lazy::from_position(NonZeroUsize::new(position as usize).unwrap()))
135139
}
136140
}
137141
}

src/librustc_metadata/schema.rs

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

2222
use std::marker::PhantomData;
23+
use std::num::NonZeroUsize;
2324

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

110111
impl<T: ?Sized + LazyMeta> Lazy<T> {
111-
crate fn from_position_and_meta(position: usize, meta: T::Meta) -> Lazy<T> {
112+
crate fn from_position_and_meta(position: NonZeroUsize, meta: T::Meta) -> Lazy<T> {
112113
Lazy {
113114
position,
114115
meta,
@@ -118,14 +119,14 @@ impl<T: ?Sized + LazyMeta> Lazy<T> {
118119
}
119120

120121
impl<T> Lazy<T> {
121-
crate fn from_position(position: usize) -> Lazy<T> {
122+
crate fn from_position(position: NonZeroUsize) -> Lazy<T> {
122123
Lazy::from_position_and_meta(position, ())
123124
}
124125
}
125126

126127
impl<T> Lazy<[T]> {
127128
crate fn empty() -> Lazy<[T]> {
128-
Lazy::from_position_and_meta(0, 0)
129+
Lazy::from_position_and_meta(NonZeroUsize::new(1).unwrap(), 0)
129130
}
130131
}
131132

@@ -147,12 +148,12 @@ crate enum LazyState {
147148

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

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

158159
#[derive(RustcEncodable, RustcDecodable)]

0 commit comments

Comments
 (0)