rustc_metadata: use NonZeroUsize for the position of a Lazy.
This commit is contained in:
parent
e505857914
commit
ea134563e7
@ -25,6 +25,7 @@
|
||||
|
||||
use std::io;
|
||||
use std::mem;
|
||||
use std::num::NonZeroUsize;
|
||||
use std::u32;
|
||||
|
||||
use rustc_serialize::{Decodable, Decoder, SpecializedDecoder, opaque};
|
||||
@ -131,7 +132,7 @@ fn tcx(self) -> Option<TyCtxt<'tcx>> {
|
||||
|
||||
impl<'a, 'tcx, T: Decodable> Lazy<T> {
|
||||
crate fn decode<M: Metadata<'a, 'tcx>>(self, meta: M) -> T {
|
||||
let mut dcx = meta.decoder(self.position);
|
||||
let mut dcx = meta.decoder(self.position.get());
|
||||
dcx.lazy_state = LazyState::NodeStart(self.position);
|
||||
T::decode(&mut dcx).unwrap()
|
||||
}
|
||||
@ -142,7 +143,7 @@ impl<'a: 'x, 'tcx: 'x, 'x, T: Decodable> Lazy<[T]> {
|
||||
self,
|
||||
meta: M,
|
||||
) -> impl ExactSizeIterator<Item = T> + Captures<'a> + Captures<'tcx> + 'x {
|
||||
let mut dcx = meta.decoder(self.position);
|
||||
let mut dcx = meta.decoder(self.position.get());
|
||||
dcx.lazy_state = LazyState::NodeStart(self.position);
|
||||
(0..self.meta).map(move |_| T::decode(&mut dcx).unwrap())
|
||||
}
|
||||
@ -166,13 +167,14 @@ fn read_lazy_with_meta<T: ?Sized + LazyMeta>(
|
||||
let position = match self.lazy_state {
|
||||
LazyState::NoNode => bug!("read_lazy_with_meta: outside of a metadata node"),
|
||||
LazyState::NodeStart(start) => {
|
||||
let start = start.get();
|
||||
assert!(distance + min_size <= start);
|
||||
start - distance - min_size
|
||||
}
|
||||
LazyState::Previous(last_min_end) => last_min_end + distance,
|
||||
LazyState::Previous(last_min_end) => last_min_end.get() + distance,
|
||||
};
|
||||
self.lazy_state = LazyState::Previous(position + min_size);
|
||||
Ok(Lazy::from_position_and_meta(position, meta))
|
||||
self.lazy_state = LazyState::Previous(NonZeroUsize::new(position + min_size).unwrap());
|
||||
Ok(Lazy::from_position_and_meta(NonZeroUsize::new(position).unwrap(), meta))
|
||||
}
|
||||
}
|
||||
|
||||
@ -384,7 +386,9 @@ impl<'tcx> MetadataBlob {
|
||||
}
|
||||
|
||||
crate fn get_rustc_version(&self) -> String {
|
||||
Lazy::<String>::from_position(METADATA_HEADER.len() + 4).decode(self)
|
||||
Lazy::<String>::from_position(
|
||||
NonZeroUsize::new(METADATA_HEADER.len() + 4).unwrap(),
|
||||
).decode(self)
|
||||
}
|
||||
|
||||
crate fn get_root(&self) -> CrateRoot<'tcx> {
|
||||
@ -393,7 +397,9 @@ impl<'tcx> MetadataBlob {
|
||||
let pos = (((slice[offset + 0] as u32) << 24) | ((slice[offset + 1] as u32) << 16) |
|
||||
((slice[offset + 2] as u32) << 8) |
|
||||
((slice[offset + 3] as u32) << 0)) as usize;
|
||||
Lazy::<CrateRoot<'tcx>>::from_position(pos).decode(self)
|
||||
Lazy::<CrateRoot<'tcx>>::from_position(
|
||||
NonZeroUsize::new(pos).unwrap(),
|
||||
).decode(self)
|
||||
}
|
||||
|
||||
crate fn list_crate_metadata(&self,
|
||||
|
@ -23,11 +23,12 @@
|
||||
use rustc::util::nodemap::FxHashMap;
|
||||
|
||||
use rustc_data_structures::stable_hasher::StableHasher;
|
||||
use rustc_data_structures::sync::Lrc;
|
||||
use rustc_serialize::{Encodable, Encoder, SpecializedEncoder, opaque};
|
||||
|
||||
use std::hash::Hash;
|
||||
use std::num::NonZeroUsize;
|
||||
use std::path::Path;
|
||||
use rustc_data_structures::sync::Lrc;
|
||||
use std::u32;
|
||||
use syntax::ast;
|
||||
use syntax::attr;
|
||||
@ -271,10 +272,11 @@ fn emit_lazy_distance<T: ?Sized + LazyMeta>(
|
||||
&mut self,
|
||||
lazy: Lazy<T>,
|
||||
) -> Result<(), <Self as Encoder>::Error> {
|
||||
let min_end = lazy.position + T::min_size(lazy.meta);
|
||||
let min_end = lazy.position.get() + T::min_size(lazy.meta);
|
||||
let distance = match self.lazy_state {
|
||||
LazyState::NoNode => bug!("emit_lazy_distance: outside of a metadata node"),
|
||||
LazyState::NodeStart(start) => {
|
||||
let start = start.get();
|
||||
assert!(min_end <= start);
|
||||
start - min_end
|
||||
}
|
||||
@ -284,10 +286,10 @@ fn emit_lazy_distance<T: ?Sized + LazyMeta>(
|
||||
"make sure that the calls to `lazy*` \
|
||||
are in the same order as the metadata fields",
|
||||
);
|
||||
lazy.position - last_min_end
|
||||
lazy.position.get() - last_min_end.get()
|
||||
}
|
||||
};
|
||||
self.lazy_state = LazyState::Previous(min_end);
|
||||
self.lazy_state = LazyState::Previous(NonZeroUsize::new(min_end).unwrap());
|
||||
self.emit_usize(distance)
|
||||
}
|
||||
|
||||
@ -295,14 +297,14 @@ fn lazy<T: ?Sized + LazyMeta>(
|
||||
&mut self,
|
||||
value: impl EncodeContentsForLazy<T>,
|
||||
) -> Lazy<T> {
|
||||
let pos = self.position();
|
||||
let pos = NonZeroUsize::new(self.position()).unwrap();
|
||||
|
||||
assert_eq!(self.lazy_state, LazyState::NoNode);
|
||||
self.lazy_state = LazyState::NodeStart(pos);
|
||||
let meta = value.encode_contents_for_lazy(self);
|
||||
self.lazy_state = LazyState::NoNode;
|
||||
|
||||
assert!(pos + <T>::min_size(meta) <= self.position());
|
||||
assert!(pos.get() + <T>::min_size(meta) <= self.position());
|
||||
|
||||
Lazy::from_position_and_meta(pos, meta)
|
||||
}
|
||||
@ -1934,7 +1936,7 @@ fn visit_impl_item(&mut self, _impl_item: &'v hir::ImplItem) {
|
||||
|
||||
// Encode the root position.
|
||||
let header = METADATA_HEADER.len();
|
||||
let pos = root.position;
|
||||
let pos = root.position.get();
|
||||
result[header + 0] = (pos >> 24) as u8;
|
||||
result[header + 1] = (pos >> 16) as u8;
|
||||
result[header + 2] = (pos >> 8) as u8;
|
||||
|
@ -3,6 +3,7 @@
|
||||
use rustc::hir::def_id::{DefId, DefIndex};
|
||||
use rustc_serialize::opaque::Encoder;
|
||||
use std::marker::PhantomData;
|
||||
use std::num::NonZeroUsize;
|
||||
use std::u32;
|
||||
use log::debug;
|
||||
|
||||
@ -94,8 +95,8 @@ impl Index<'tcx> {
|
||||
}
|
||||
|
||||
fn record_index(&mut self, item: DefIndex, entry: Lazy<Entry<'tcx>>) {
|
||||
assert!(entry.position < (u32::MAX as usize));
|
||||
let position = entry.position as u32;
|
||||
assert!(entry.position.get() < (u32::MAX as usize));
|
||||
let position = entry.position.get() as u32;
|
||||
let array_index = item.index();
|
||||
|
||||
let positions = &mut self.positions;
|
||||
@ -111,7 +112,10 @@ fn record_index(&mut self, item: DefIndex, entry: Lazy<Entry<'tcx>>) {
|
||||
crate fn write_index(&self, buf: &mut Encoder) -> Lazy<[Self]> {
|
||||
let pos = buf.position();
|
||||
buf.emit_raw_bytes(&self.positions);
|
||||
Lazy::from_position_and_meta(pos as usize, self.positions.len() / 4)
|
||||
Lazy::from_position_and_meta(
|
||||
NonZeroUsize::new(pos as usize).unwrap(),
|
||||
self.positions.len() / 4,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@ -124,14 +128,14 @@ impl Lazy<[Index<'tcx>]> {
|
||||
def_index,
|
||||
self.meta);
|
||||
|
||||
let bytes = &bytes[self.position..][..self.meta * 4];
|
||||
let bytes = &bytes[self.position.get()..][..self.meta * 4];
|
||||
let position = u32::read_from_bytes_at(bytes, def_index.index());
|
||||
if position == u32::MAX {
|
||||
debug!("Index::lookup: position=u32::MAX");
|
||||
None
|
||||
} else {
|
||||
debug!("Index::lookup: position={:?}", position);
|
||||
Some(Lazy::from_position(position as usize))
|
||||
Some(Lazy::from_position(NonZeroUsize::new(position as usize).unwrap()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -20,6 +20,7 @@
|
||||
use syntax_pos::{self, Span};
|
||||
|
||||
use std::marker::PhantomData;
|
||||
use std::num::NonZeroUsize;
|
||||
|
||||
crate fn rustc_version() -> String {
|
||||
format!("rustc {}",
|
||||
@ -102,13 +103,13 @@ fn min_size(len: usize) -> usize {
|
||||
where T: ?Sized + LazyMeta<Meta = Meta>,
|
||||
Meta: 'static + Copy,
|
||||
{
|
||||
pub position: usize,
|
||||
pub position: NonZeroUsize,
|
||||
pub meta: Meta,
|
||||
_marker: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T: ?Sized + LazyMeta> Lazy<T> {
|
||||
crate fn from_position_and_meta(position: usize, meta: T::Meta) -> Lazy<T> {
|
||||
crate fn from_position_and_meta(position: NonZeroUsize, meta: T::Meta) -> Lazy<T> {
|
||||
Lazy {
|
||||
position,
|
||||
meta,
|
||||
@ -118,14 +119,14 @@ impl<T: ?Sized + LazyMeta> Lazy<T> {
|
||||
}
|
||||
|
||||
impl<T> Lazy<T> {
|
||||
crate fn from_position(position: usize) -> Lazy<T> {
|
||||
crate fn from_position(position: NonZeroUsize) -> Lazy<T> {
|
||||
Lazy::from_position_and_meta(position, ())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Lazy<[T]> {
|
||||
crate fn empty() -> Lazy<[T]> {
|
||||
Lazy::from_position_and_meta(0, 0)
|
||||
Lazy::from_position_and_meta(NonZeroUsize::new(1).unwrap(), 0)
|
||||
}
|
||||
}
|
||||
|
||||
@ -147,12 +148,12 @@ impl<T: ?Sized + LazyMeta> rustc_serialize::UseSpecializedDecodable for Lazy<T>
|
||||
|
||||
/// Inside a metadata node, and before any `Lazy`.
|
||||
/// The position is that of the node itself.
|
||||
NodeStart(usize),
|
||||
NodeStart(NonZeroUsize),
|
||||
|
||||
/// Inside a metadata node, with a previous `Lazy`.
|
||||
/// The position is a conservative estimate of where that
|
||||
/// previous `Lazy` would end (see their comments).
|
||||
Previous(usize),
|
||||
Previous(NonZeroUsize),
|
||||
}
|
||||
|
||||
#[derive(RustcEncodable, RustcDecodable)]
|
||||
|
Loading…
Reference in New Issue
Block a user