Directly encode ImplPolarity in metadata.

This commit is contained in:
Camille GILLOT 2022-04-09 18:59:21 +02:00
parent dd38eea722
commit 81bac88e93
5 changed files with 46 additions and 2 deletions

View file

@ -5,6 +5,7 @@
#![feature(nll)]
#![feature(once_cell)]
#![feature(proc_macro_internals)]
#![feature(macro_metavar_expr)]
#![feature(min_specialization)]
#![feature(try_blocks)]
#![feature(never_type)]

View file

@ -292,6 +292,12 @@ trait LazyQueryDecodable<'a, 'tcx, T> {
) -> T;
}
impl<'a, 'tcx, T> LazyQueryDecodable<'a, 'tcx, T> for Option<T> {
fn decode_query(self, _: CrateMetadataRef<'a>, _: TyCtxt<'tcx>, err: impl FnOnce() -> !) -> T {
if let Some(l) = self { l } else { err() }
}
}
impl<'a, 'tcx, T> LazyQueryDecodable<'a, 'tcx, T> for Option<Lazy<T>>
where
T: Decodable<DecodeContext<'a, 'tcx>>,

View file

@ -1472,7 +1472,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
}
let polarity = self.tcx.impl_polarity(def_id);
record!(self.tables.impl_polarity[def_id] <- polarity);
self.tables.impl_polarity.set(def_id.index, polarity);
EntryKind::Impl
}

View file

@ -310,7 +310,7 @@ define_tables! {
promoted_mir: Table<DefIndex, Lazy!(IndexVec<mir::Promoted, mir::Body<'tcx>>)>,
thir_abstract_const: Table<DefIndex, Lazy!(&'tcx [thir::abstract_const::Node<'tcx>])>,
impl_parent: Table<DefIndex, Lazy!(DefId)>,
impl_polarity: Table<DefIndex, Lazy!(ty::ImplPolarity)>,
impl_polarity: Table<DefIndex, ty::ImplPolarity>,
impl_constness: Table<DefIndex, Lazy!(hir::Constness)>,
impl_defaultness: Table<DefIndex, Lazy!(hir::Defaultness)>,
// FIXME(eddyb) perhaps compute this on the fly if cheap enough?

View file

@ -76,6 +76,43 @@ impl FixedSizeEncoding for u32 {
}
}
macro_rules! fixed_size_enum {
($ty:ty { $(($($pat:tt)*))* }) => {
impl FixedSizeEncoding for Option<$ty> {
fixed_size_encoding_byte_len_and_defaults!(1);
#[inline]
fn from_bytes(b: &[u8]) -> Self {
use $ty::*;
if b[0] == 0 {
return None;
}
match b[0] - 1 {
$(${index()} => Some($($pat)*),)*
_ => panic!("Unexpected ImplPolarity code: {:?}", b[0]),
}
}
#[inline]
fn write_to_bytes(self, b: &mut [u8]) {
use $ty::*;
b[0] = match self {
None => 0,
$(Some($($pat)*) => 1 + ${index()},)*
}
}
}
}
}
fixed_size_enum! {
ty::ImplPolarity {
( Positive )
( Negative )
( Reservation )
}
}
// NOTE(eddyb) there could be an impl for `usize`, which would enable a more
// generic `Lazy<T>` impl, but in the general case we might not need / want to
// fit every `usize` in `u32`.