Skip to content

Commit 81bac88

Browse files
committed
Directly encode ImplPolarity in metadata.
1 parent dd38eea commit 81bac88

File tree

5 files changed

+46
-2
lines changed

5 files changed

+46
-2
lines changed

compiler/rustc_metadata/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![feature(nll)]
66
#![feature(once_cell)]
77
#![feature(proc_macro_internals)]
8+
#![feature(macro_metavar_expr)]
89
#![feature(min_specialization)]
910
#![feature(try_blocks)]
1011
#![feature(never_type)]

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ trait LazyQueryDecodable<'a, 'tcx, T> {
292292
) -> T;
293293
}
294294

295+
impl<'a, 'tcx, T> LazyQueryDecodable<'a, 'tcx, T> for Option<T> {
296+
fn decode_query(self, _: CrateMetadataRef<'a>, _: TyCtxt<'tcx>, err: impl FnOnce() -> !) -> T {
297+
if let Some(l) = self { l } else { err() }
298+
}
299+
}
300+
295301
impl<'a, 'tcx, T> LazyQueryDecodable<'a, 'tcx, T> for Option<Lazy<T>>
296302
where
297303
T: Decodable<DecodeContext<'a, 'tcx>>,

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14721472
}
14731473

14741474
let polarity = self.tcx.impl_polarity(def_id);
1475-
record!(self.tables.impl_polarity[def_id] <- polarity);
1475+
self.tables.impl_polarity.set(def_id.index, polarity);
14761476

14771477
EntryKind::Impl
14781478
}

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ define_tables! {
310310
promoted_mir: Table<DefIndex, Lazy!(IndexVec<mir::Promoted, mir::Body<'tcx>>)>,
311311
thir_abstract_const: Table<DefIndex, Lazy!(&'tcx [thir::abstract_const::Node<'tcx>])>,
312312
impl_parent: Table<DefIndex, Lazy!(DefId)>,
313-
impl_polarity: Table<DefIndex, Lazy!(ty::ImplPolarity)>,
313+
impl_polarity: Table<DefIndex, ty::ImplPolarity>,
314314
impl_constness: Table<DefIndex, Lazy!(hir::Constness)>,
315315
impl_defaultness: Table<DefIndex, Lazy!(hir::Defaultness)>,
316316
// FIXME(eddyb) perhaps compute this on the fly if cheap enough?

compiler/rustc_metadata/src/rmeta/table.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,43 @@ impl FixedSizeEncoding for u32 {
7676
}
7777
}
7878

79+
macro_rules! fixed_size_enum {
80+
($ty:ty { $(($($pat:tt)*))* }) => {
81+
impl FixedSizeEncoding for Option<$ty> {
82+
fixed_size_encoding_byte_len_and_defaults!(1);
83+
84+
#[inline]
85+
fn from_bytes(b: &[u8]) -> Self {
86+
use $ty::*;
87+
if b[0] == 0 {
88+
return None;
89+
}
90+
match b[0] - 1 {
91+
$(${index()} => Some($($pat)*),)*
92+
_ => panic!("Unexpected ImplPolarity code: {:?}", b[0]),
93+
}
94+
}
95+
96+
#[inline]
97+
fn write_to_bytes(self, b: &mut [u8]) {
98+
use $ty::*;
99+
b[0] = match self {
100+
None => 0,
101+
$(Some($($pat)*) => 1 + ${index()},)*
102+
}
103+
}
104+
}
105+
}
106+
}
107+
108+
fixed_size_enum! {
109+
ty::ImplPolarity {
110+
( Positive )
111+
( Negative )
112+
( Reservation )
113+
}
114+
}
115+
79116
// NOTE(eddyb) there could be an impl for `usize`, which would enable a more
80117
// generic `Lazy<T>` impl, but in the general case we might not need / want to
81118
// fit every `usize` in `u32`.

0 commit comments

Comments
 (0)