Skip to content

Commit

Permalink
rustc_metadata: replace predicates_defined_on with explicit_predicate…
Browse files Browse the repository at this point in the history
…s_of and inferred_outlives_of.
  • Loading branch information
eddyb committed Apr 9, 2019
1 parent d1191bf commit 76166f6
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 35 deletions.
61 changes: 54 additions & 7 deletions src/librustc/ty/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,26 @@ pub fn encode_with_shorthand<E, T, M>(encoder: &mut E,
Ok(())
}

pub fn encode_predicates<'tcx, E, C>(encoder: &mut E,
predicates: &ty::GenericPredicates<'tcx>,
cache: C)
-> Result<(), E::Error>
pub fn encode_predicates<'tcx, E, C>(
encoder: &mut E,
predicates: &[ty::Predicate<'tcx>],
cache: C,
) -> Result<(), E::Error>
where E: TyEncoder,
C: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap<ty::Predicate<'tcx>, usize>,
{
predicates.len().encode(encoder)?;
for predicate in predicates {
encode_with_shorthand(encoder, predicate, &cache)?;
}
Ok(())
}

pub fn encode_generic_predicates<'tcx, E, C>(
encoder: &mut E,
predicates: &ty::GenericPredicates<'tcx>,
cache: C,
) -> Result<(), E::Error>
where E: TyEncoder,
C: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap<ty::Predicate<'tcx>, usize>,
{
Expand Down Expand Up @@ -160,8 +176,31 @@ pub fn decode_ty<'a, 'tcx, D>(decoder: &mut D) -> Result<Ty<'tcx>, D::Error>
}

#[inline]
pub fn decode_predicates<'a, 'tcx, D>(decoder: &mut D)
-> Result<ty::GenericPredicates<'tcx>, D::Error>
pub fn decode_predicates<'a, 'tcx, D>(
decoder: &mut D,
) -> Result<Vec<ty::Predicate<'tcx>>, D::Error>
where D: TyDecoder<'a, 'tcx>,
'tcx: 'a,
{
(0..decoder.read_usize()?).map(|_| {
// Handle shorthands first, if we have an usize > 0x80.
if decoder.positioned_at_shorthand() {
let pos = decoder.read_usize()?;
assert!(pos >= SHORTHAND_OFFSET);
let shorthand = pos - SHORTHAND_OFFSET;

decoder.with_position(shorthand, ty::Predicate::decode)
} else {
ty::Predicate::decode(decoder)
}
})
.collect()
}

#[inline]
pub fn decode_generic_predicates<'a, 'tcx, D>(
decoder: &mut D,
) -> Result<ty::GenericPredicates<'tcx>, D::Error>
where D: TyDecoder<'a, 'tcx>,
'tcx: 'a,
{
Expand Down Expand Up @@ -336,11 +375,19 @@ macro_rules! implement_ty_decoder {
}
}

impl<$($typaram),*> SpecializedDecoder<Vec<ty::Predicate<'tcx>>>
for $DecoderName<$($typaram),*> {
fn specialized_decode(&mut self)
-> Result<Vec<ty::Predicate<'tcx>>, Self::Error> {
decode_predicates(self)
}
}

impl<$($typaram),*> SpecializedDecoder<ty::GenericPredicates<'tcx>>
for $DecoderName<$($typaram),*> {
fn specialized_decode(&mut self)
-> Result<ty::GenericPredicates<'tcx>, Self::Error> {
decode_predicates(self)
decode_generic_predicates(self)
}
}

Expand Down
15 changes: 14 additions & 1 deletion src/librustc/ty/query/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,19 @@ impl<'enc, 'a, 'tcx, E> SpecializedEncoder<ty::Ty<'tcx>> for CacheEncoder<'enc,
}
}

impl<'enc, 'a, 'tcx, E> SpecializedEncoder<Vec<ty::Predicate<'tcx>>>
for CacheEncoder<'enc, 'a, 'tcx, E>
where E: 'enc + ty_codec::TyEncoder
{
#[inline]
fn specialized_encode(&mut self,
predicates: &Vec<ty::Predicate<'tcx>>)
-> Result<(), Self::Error> {
ty_codec::encode_predicates(self, predicates,
|encoder| &mut encoder.predicate_shorthands)
}
}

impl<'enc, 'a, 'tcx, E> SpecializedEncoder<ty::GenericPredicates<'tcx>>
for CacheEncoder<'enc, 'a, 'tcx, E>
where E: 'enc + ty_codec::TyEncoder
Expand All @@ -899,7 +912,7 @@ impl<'enc, 'a, 'tcx, E> SpecializedEncoder<ty::GenericPredicates<'tcx>>
fn specialized_encode(&mut self,
predicates: &ty::GenericPredicates<'tcx>)
-> Result<(), Self::Error> {
ty_codec::encode_predicates(self, predicates,
ty_codec::encode_generic_predicates(self, predicates,
|encoder| &mut encoder.predicate_shorthands)
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_metadata/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ provide! { <'tcx> tcx, def_id, other, cdata,
generics_of => {
tcx.alloc_generics(cdata.get_generics(def_id.index, tcx.sess))
}
predicates_defined_on => { Lrc::new(cdata.get_predicates_defined_on(def_id.index, tcx)) }
explicit_predicates_of => { Lrc::new(cdata.get_explicit_predicates(def_id.index, tcx)) }
inferred_outlives_of => { Lrc::new(cdata.get_inferred_outlives(def_id.index, tcx)) }
super_predicates_of => { Lrc::new(cdata.get_super_predicates(def_id.index, tcx)) }
trait_def => {
tcx.alloc_trait_def(cdata.get_trait_def(def_id.index, tcx.sess))
Expand Down
19 changes: 14 additions & 5 deletions src/librustc_metadata/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,11 +617,20 @@ impl<'a, 'tcx> CrateMetadata {
tcx.alloc_adt_def(did, kind, variants, repr)
}

pub fn get_predicates_defined_on(&self,
item_id: DefIndex,
tcx: TyCtxt<'a, 'tcx, 'tcx>)
-> ty::GenericPredicates<'tcx> {
self.entry(item_id).predicates_defined_on.unwrap().decode((self, tcx))
pub fn get_explicit_predicates(
&self,
item_id: DefIndex,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
) -> ty::GenericPredicates<'tcx> {
self.entry(item_id).explicit_predicates.unwrap().decode((self, tcx))
}

pub fn get_inferred_outlives(
&self,
item_id: DefIndex,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
) -> Vec<ty::Predicate<'tcx>> {
self.entry(item_id).inferred_outlives.unwrap().decode((self, tcx))
}

pub fn get_super_predicates(&self,
Expand Down
76 changes: 58 additions & 18 deletions src/librustc_metadata/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,19 @@ impl<'a, 'tcx> SpecializedEncoder<interpret::AllocId> for EncodeContext<'a, 'tcx
}
}

impl<'a, 'tcx> SpecializedEncoder<Vec<ty::Predicate<'tcx>>> for EncodeContext<'a, 'tcx> {
fn specialized_encode(&mut self,
predicates: &Vec<ty::Predicate<'tcx>>)
-> Result<(), Self::Error> {
ty_codec::encode_predicates(self, predicates, |ecx| &mut ecx.predicate_shorthands)
}
}

impl<'a, 'tcx> SpecializedEncoder<ty::GenericPredicates<'tcx>> for EncodeContext<'a, 'tcx> {
fn specialized_encode(&mut self,
predicates: &ty::GenericPredicates<'tcx>)
-> Result<(), Self::Error> {
ty_codec::encode_predicates(self, predicates, |ecx| &mut ecx.predicate_shorthands)
ty_codec::encode_generic_predicates(self, predicates, |ecx| &mut ecx.predicate_shorthands)
}
}

Expand Down Expand Up @@ -613,7 +621,8 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
LazySeq::empty()
},
generics: Some(self.encode_generics(def_id)),
predicates_defined_on: Some(self.encode_predicates_defined_on(def_id)),
explicit_predicates: Some(self.encode_explicit_predicates(def_id)),
inferred_outlives: Some(self.encode_inferred_outlives(def_id)),

mir: self.encode_optimized_mir(def_id),
}
Expand Down Expand Up @@ -668,7 +677,8 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
LazySeq::empty()
},
generics: Some(self.encode_generics(def_id)),
predicates_defined_on: Some(self.encode_predicates_defined_on(def_id)),
explicit_predicates: Some(self.encode_explicit_predicates(def_id)),
inferred_outlives: Some(self.encode_inferred_outlives(def_id)),

mir: self.encode_optimized_mir(def_id),
}
Expand Down Expand Up @@ -705,7 +715,8 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
inherent_impls: LazySeq::empty(),
variances: LazySeq::empty(),
generics: None,
predicates_defined_on: None,
explicit_predicates: None,
inferred_outlives: None,

mir: None
}
Expand Down Expand Up @@ -745,7 +756,8 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
inherent_impls: LazySeq::empty(),
variances: LazySeq::empty(),
generics: Some(self.encode_generics(def_id)),
predicates_defined_on: Some(self.encode_predicates_defined_on(def_id)),
explicit_predicates: Some(self.encode_explicit_predicates(def_id)),
inferred_outlives: Some(self.encode_inferred_outlives(def_id)),

mir: None,
}
Expand Down Expand Up @@ -804,7 +816,8 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
LazySeq::empty()
},
generics: Some(self.encode_generics(def_id)),
predicates_defined_on: Some(self.encode_predicates_defined_on(def_id)),
explicit_predicates: Some(self.encode_explicit_predicates(def_id)),
inferred_outlives: Some(self.encode_inferred_outlives(def_id)),

mir: self.encode_optimized_mir(def_id),
}
Expand All @@ -816,10 +829,16 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
self.lazy(tcx.generics_of(def_id))
}

fn encode_predicates_defined_on(&mut self, def_id: DefId) -> Lazy<ty::GenericPredicates<'tcx>> {
debug!("IsolatedEncoder::encode_predicates_defined_on({:?})", def_id);
fn encode_explicit_predicates(&mut self, def_id: DefId) -> Lazy<ty::GenericPredicates<'tcx>> {
debug!("IsolatedEncoder::encode_explicit_predicates({:?})", def_id);
let tcx = self.tcx;
self.lazy(&tcx.predicates_defined_on(def_id))
self.lazy(&tcx.explicit_predicates_of(def_id))
}

fn encode_inferred_outlives(&mut self, def_id: DefId) -> Lazy<Vec<ty::Predicate<'tcx>>> {
debug!("IsolatedEncoder::encode_inferred_outlives({:?})", def_id);
let tcx = self.tcx;
self.lazy(&tcx.inferred_outlives_of(def_id))
}

fn encode_info_for_trait_item(&mut self, def_id: DefId) -> Entry<'tcx> {
Expand Down Expand Up @@ -913,7 +932,8 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
LazySeq::empty()
},
generics: Some(self.encode_generics(def_id)),
predicates_defined_on: Some(self.encode_predicates_defined_on(def_id)),
explicit_predicates: Some(self.encode_explicit_predicates(def_id)),
inferred_outlives: Some(self.encode_inferred_outlives(def_id)),

mir: self.encode_optimized_mir(def_id),
}
Expand Down Expand Up @@ -1011,7 +1031,8 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
LazySeq::empty()
},
generics: Some(self.encode_generics(def_id)),
predicates_defined_on: Some(self.encode_predicates_defined_on(def_id)),
explicit_predicates: Some(self.encode_explicit_predicates(def_id)),
inferred_outlives: Some(self.encode_inferred_outlives(def_id)),

mir: if mir { self.encode_optimized_mir(def_id) } else { None },
}
Expand Down Expand Up @@ -1267,7 +1288,21 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
hir::ItemKind::TraitAlias(..) => Some(self.encode_generics(def_id)),
_ => None,
},
predicates_defined_on: match item.node {
explicit_predicates: match item.node {
hir::ItemKind::Static(..) |
hir::ItemKind::Const(..) |
hir::ItemKind::Fn(..) |
hir::ItemKind::Ty(..) |
hir::ItemKind::Enum(..) |
hir::ItemKind::Struct(..) |
hir::ItemKind::Union(..) |
hir::ItemKind::Impl(..) |
hir::ItemKind::Existential(..) |
hir::ItemKind::Trait(..) |
hir::ItemKind::TraitAlias(..) => Some(self.encode_explicit_predicates(def_id)),
_ => None,
},
inferred_outlives: match item.node {
hir::ItemKind::Static(..) |
hir::ItemKind::Const(..) |
hir::ItemKind::Fn(..) |
Expand All @@ -1278,7 +1313,7 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
hir::ItemKind::Impl(..) |
hir::ItemKind::Existential(..) |
hir::ItemKind::Trait(..) |
hir::ItemKind::TraitAlias(..) => Some(self.encode_predicates_defined_on(def_id)),
hir::ItemKind::TraitAlias(..) => Some(self.encode_inferred_outlives(def_id)),
_ => None,
},

Expand Down Expand Up @@ -1328,7 +1363,8 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
inherent_impls: LazySeq::empty(),
variances: LazySeq::empty(),
generics: None,
predicates_defined_on: None,
explicit_predicates: None,
inferred_outlives: None,
mir: None,
}
}
Expand All @@ -1352,7 +1388,8 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
inherent_impls: LazySeq::empty(),
variances: LazySeq::empty(),
generics: None,
predicates_defined_on: None,
explicit_predicates: None,
inferred_outlives: None,

mir: None,
}
Expand Down Expand Up @@ -1411,7 +1448,8 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
inherent_impls: LazySeq::empty(),
variances: LazySeq::empty(),
generics: Some(self.encode_generics(def_id)),
predicates_defined_on: None,
explicit_predicates: None,
inferred_outlives: None,

mir: self.encode_optimized_mir(def_id),
}
Expand All @@ -1438,7 +1476,8 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
inherent_impls: LazySeq::empty(),
variances: LazySeq::empty(),
generics: Some(self.encode_generics(def_id)),
predicates_defined_on: Some(self.encode_predicates_defined_on(def_id)),
explicit_predicates: Some(self.encode_explicit_predicates(def_id)),
inferred_outlives: Some(self.encode_inferred_outlives(def_id)),

mir: self.encode_optimized_mir(def_id),
}
Expand Down Expand Up @@ -1640,7 +1679,8 @@ impl<'a, 'b: 'a, 'tcx: 'b> IsolatedEncoder<'a, 'b, 'tcx> {
_ => LazySeq::empty(),
},
generics: Some(self.encode_generics(def_id)),
predicates_defined_on: Some(self.encode_predicates_defined_on(def_id)),
explicit_predicates: Some(self.encode_explicit_predicates(def_id)),
inferred_outlives: Some(self.encode_inferred_outlives(def_id)),

mir: None,
}
Expand Down
10 changes: 8 additions & 2 deletions src/librustc_metadata/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,12 @@ pub struct Entry<'tcx> {
pub inherent_impls: LazySeq<DefIndex>,
pub variances: LazySeq<ty::Variance>,
pub generics: Option<Lazy<ty::Generics>>,
pub predicates_defined_on: Option<Lazy<ty::GenericPredicates<'tcx>>>,
pub explicit_predicates: Option<Lazy<ty::GenericPredicates<'tcx>>>,
// FIXME(eddyb) this would ideally be `LazySeq` but `ty::Predicate`
// doesn't handle shorthands in its own (de)serialization impls,
// as it's an `enum` for which we want to derive (de)serialization,
// so the `ty::codec` APIs handle the whole `Vec` at once.
pub inferred_outlives: Option<Lazy<Vec<ty::Predicate<'tcx>>>>,

pub mir: Option<Lazy<mir::Mir<'tcx>>>,
}
Expand All @@ -282,7 +287,8 @@ impl_stable_hash_for!(struct Entry<'tcx> {
inherent_impls,
variances,
generics,
predicates_defined_on,
explicit_predicates,
inferred_outlives,
mir
});

Expand Down
4 changes: 3 additions & 1 deletion src/libserialize/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ impl<T: UseSpecializedDecodable> Decodable for T {
}
}

// Can't avoid specialization for &T and Box<T> impls,
// Can't avoid specialization for &T, Box<T> and Vec<T> impls,
// as proxy impls on them are blankets that conflict
// with the Encodable and Decodable impls above,
// which only have `default` on their methods
Expand All @@ -910,5 +910,7 @@ impl<T: UseSpecializedDecodable> Decodable for T {
// more complex lattice model for specialization.
impl<'a, T: ?Sized + Encodable> UseSpecializedEncodable for &'a T {}
impl<T: ?Sized + Encodable> UseSpecializedEncodable for Box<T> {}
impl<T: Encodable> UseSpecializedEncodable for Vec<T> {}
impl<T: Decodable> UseSpecializedDecodable for Box<T> {}
impl<T: Decodable> UseSpecializedDecodable for Vec<T> {}

0 comments on commit 76166f6

Please sign in to comment.