Skip to content

Commit eb5f2d3

Browse files
committed
rustc_metadata: Support encoding/decoding LazyArray without an Option
1 parent 8cc5aa5 commit eb5f2d3

File tree

4 files changed

+66
-30
lines changed

4 files changed

+66
-30
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ impl<'a, 'tcx, T> Decodable<DecodeContext<'a, 'tcx>> for LazyValue<T> {
654654
impl<'a, 'tcx, T> Decodable<DecodeContext<'a, 'tcx>> for LazyArray<T> {
655655
fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> Self {
656656
let len = decoder.read_usize();
657-
if len == 0 { LazyArray::empty() } else { decoder.read_lazy_array(len) }
657+
if len == 0 { LazyArray::default() } else { decoder.read_lazy_array(len) }
658658
}
659659
}
660660

@@ -864,7 +864,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
864864
.tables
865865
.children
866866
.get(self, index)
867-
.unwrap_or_else(LazyArray::empty)
867+
.unwrap_or_else(LazyArray::default)
868868
.decode(self)
869869
.map(|index| ty::FieldDef {
870870
did: self.local_def_id(index),
@@ -896,7 +896,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
896896
.tables
897897
.children
898898
.get(self, item_id)
899-
.unwrap_or_else(LazyArray::empty)
899+
.unwrap_or_else(LazyArray::default)
900900
.decode(self)
901901
.filter_map(|index| {
902902
let kind = self.def_kind(index);
@@ -1045,7 +1045,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10451045
.tables
10461046
.fn_arg_names
10471047
.get(self, id)
1048-
.unwrap_or_else(LazyArray::empty)
1048+
.unwrap_or_else(LazyArray::default)
10491049
.decode((self, sess))
10501050
.nth(0)
10511051
.map_or(false, |ident| ident.name == kw::SelfLower)
@@ -1060,7 +1060,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
10601060
.tables
10611061
.children
10621062
.get(self, id)
1063-
.unwrap_or_else(LazyArray::empty)
1063+
.unwrap_or_else(LazyArray::default)
10641064
.decode((self, sess))
10651065
.map(move |child_index| self.local_def_id(child_index))
10661066
}
@@ -1131,7 +1131,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11311131
.tables
11321132
.children
11331133
.get(self, id)
1134-
.unwrap_or_else(LazyArray::empty)
1134+
.unwrap_or_else(LazyArray::default)
11351135
.decode(self)
11361136
.map(move |index| respan(self.get_span(index, sess), self.item_name(index)))
11371137
}
@@ -1144,7 +1144,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11441144
.tables
11451145
.children
11461146
.get(self, id)
1147-
.unwrap_or_else(LazyArray::empty)
1147+
.unwrap_or_else(LazyArray::default)
11481148
.decode(self)
11491149
.map(move |field_index| self.get_visibility(field_index))
11501150
}
@@ -1159,7 +1159,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11591159
.tables
11601160
.inherent_impls
11611161
.get(self, id)
1162-
.unwrap_or_else(LazyArray::empty)
1162+
.unwrap_or_else(LazyArray::default)
11631163
.decode(self)
11641164
.map(|index| self.local_def_id(index)),
11651165
)
@@ -1174,7 +1174,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11741174
.tables
11751175
.inherent_impls
11761176
.get(self, ty_index)
1177-
.unwrap_or_else(LazyArray::empty)
1177+
.unwrap_or_else(LazyArray::default)
11781178
.decode(self)
11791179
.map(move |impl_index| (ty_def_id, self.local_def_id(impl_index)))
11801180
})

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ pub(super) struct EncodeContext<'a, 'tcx> {
7676
symbol_table: FxHashMap<Symbol, usize>,
7777
}
7878

79-
/// If the current crate is a proc-macro, returns early with `LazyArray::empty()`.
79+
/// If the current crate is a proc-macro, returns early with `LazyArray::default()`.
8080
/// This is useful for skipping the encoding of things that aren't needed
8181
/// for proc-macro crates.
8282
macro_rules! empty_proc_macro {
8383
($self:ident) => {
8484
if $self.is_proc_macro {
85-
return LazyArray::empty();
85+
return LazyArray::default();
8686
}
8787
};
8888
}
@@ -1961,7 +1961,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
19611961
Linkage::Static => Some(LinkagePreference::RequireStatic),
19621962
}));
19631963
}
1964-
LazyArray::empty()
1964+
LazyArray::default()
19651965
}
19661966

19671967
fn encode_info_for_foreign_item(&mut self, def_id: DefId, nitem: &hir::ForeignItem<'_>) {

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,16 @@ impl<T: ParameterizedOverTcx> ParameterizedOverTcx for LazyArray<T> {
115115
type Value<'tcx> = LazyArray<T::Value<'tcx>>;
116116
}
117117

118+
impl<T> Default for LazyArray<T> {
119+
fn default() -> LazyArray<T> {
120+
LazyArray::from_position_and_num_elems(NonZeroUsize::new(1).unwrap(), 0)
121+
}
122+
}
123+
118124
impl<T> LazyArray<T> {
119125
fn from_position_and_num_elems(position: NonZeroUsize, num_elems: usize) -> LazyArray<T> {
120126
LazyArray { position, num_elems, _marker: PhantomData }
121127
}
122-
123-
fn empty() -> LazyArray<T> {
124-
LazyArray::from_position_and_num_elems(NonZeroUsize::new(1).unwrap(), 0)
125-
}
126128
}
127129

128130
/// A list of lazily-decoded values, with the added capability of random access.

compiler/rustc_metadata/src/rmeta/table.rs

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ impl IsDefault for u32 {
3838
}
3939
}
4040

41+
impl<T> IsDefault for LazyArray<T> {
42+
fn is_default(&self) -> bool {
43+
self.num_elems == 0
44+
}
45+
}
46+
4147
/// Helper trait, for encoding to, and decoding from, a fixed number of bytes.
4248
/// Used mainly for Lazy positions and lengths.
4349
/// Unchecked invariant: `Self::default()` should encode as `[0; BYTE_LEN]`,
@@ -286,32 +292,60 @@ impl<T> FixedSizeEncoding for Option<LazyValue<T>> {
286292
}
287293
}
288294

295+
impl<T> LazyArray<T> {
296+
#[inline]
297+
fn write_to_bytes_impl(self, b: &mut [u8; 8]) {
298+
let ([position_bytes, meta_bytes],[])= b.as_chunks_mut::<4>() else { panic!() };
299+
300+
let position = self.position.get();
301+
let position: u32 = position.try_into().unwrap();
302+
position.write_to_bytes(position_bytes);
303+
304+
let len = self.num_elems;
305+
let len: u32 = len.try_into().unwrap();
306+
len.write_to_bytes(meta_bytes);
307+
}
308+
309+
fn from_bytes_impl(position_bytes: &[u8; 4], meta_bytes: &[u8; 4]) -> Option<LazyArray<T>> {
310+
let position = NonZeroUsize::new(u32::from_bytes(position_bytes) as usize)?;
311+
let len = u32::from_bytes(meta_bytes) as usize;
312+
Some(LazyArray::from_position_and_num_elems(position, len))
313+
}
314+
}
315+
316+
impl<T> FixedSizeEncoding for LazyArray<T> {
317+
type ByteArray = [u8; 8];
318+
319+
#[inline]
320+
fn from_bytes(b: &[u8; 8]) -> Self {
321+
let ([position_bytes, meta_bytes],[])= b.as_chunks::<4>() else { panic!() };
322+
if *meta_bytes == [0; 4] {
323+
return Default::default();
324+
}
325+
LazyArray::from_bytes_impl(position_bytes, meta_bytes).unwrap()
326+
}
327+
328+
#[inline]
329+
fn write_to_bytes(self, b: &mut [u8; 8]) {
330+
assert!(!self.is_default());
331+
self.write_to_bytes_impl(b)
332+
}
333+
}
334+
289335
impl<T> FixedSizeEncoding for Option<LazyArray<T>> {
290336
type ByteArray = [u8; 8];
291337

292338
#[inline]
293339
fn from_bytes(b: &[u8; 8]) -> Self {
294340
let ([position_bytes, meta_bytes],[])= b.as_chunks::<4>() else { panic!() };
295-
let position = NonZeroUsize::new(u32::from_bytes(position_bytes) as usize)?;
296-
let len = u32::from_bytes(meta_bytes) as usize;
297-
Some(LazyArray::from_position_and_num_elems(position, len))
341+
LazyArray::from_bytes_impl(position_bytes, meta_bytes)
298342
}
299343

300344
#[inline]
301345
fn write_to_bytes(self, b: &mut [u8; 8]) {
302346
match self {
303347
None => unreachable!(),
304-
Some(lazy) => {
305-
let ([position_bytes, meta_bytes],[])= b.as_chunks_mut::<4>() else { panic!() };
306-
307-
let position = lazy.position.get();
308-
let position: u32 = position.try_into().unwrap();
309-
position.write_to_bytes(position_bytes);
310-
311-
let len = lazy.num_elems;
312-
let len: u32 = len.try_into().unwrap();
313-
len.write_to_bytes(meta_bytes);
314-
}
348+
Some(lazy) => lazy.write_to_bytes_impl(b),
315349
}
316350
}
317351
}

0 commit comments

Comments
 (0)