X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=compiler%2Frustc_middle%2Fsrc%2Fty%2Fcodec.rs;h=234649f1056c71e55a38dac6ce018c4bf9d1fe85;hb=a7015fe8169ebfcd6707f34bf190cb4c4ae302f7;hp=23c377651cc6c8f1595e4f329052e8411eeb60a1;hpb=eacebb4f4374c77843c31d57d7226efeae2f56a6;p=rust.git diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index 23c377651cc..234649f1056 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -15,10 +15,12 @@ use crate::thir; use crate::traits; use crate::ty::subst::SubstsRef; -use crate::ty::{self, AdtDef, Ty, TyCtxt}; +use crate::ty::{self, AdtDef, Ty}; use rustc_data_structures::fx::FxHashMap; -use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; +use rustc_serialize::{Decodable, Encodable}; +use rustc_middle::ty::TyInterner; use rustc_span::Span; +pub use rustc_type_ir::{TyDecoder, TyEncoder}; use std::hash::Hash; use std::intrinsics; use std::marker::DiscriminantKind; @@ -28,13 +30,13 @@ /// This offset is also chosen so that the first byte is never < 0x80. pub const SHORTHAND_OFFSET: usize = 0x80; -pub trait EncodableWithShorthand<'tcx, E: TyEncoder<'tcx>>: Copy + Eq + Hash { +pub trait EncodableWithShorthand: Copy + Eq + Hash { type Variant: Encodable; fn variant(&self) -> &Self::Variant; } #[allow(rustc::usage_of_ty_tykind)] -impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for Ty<'tcx> { +impl<'tcx, E: TyEncoder>> EncodableWithShorthand for Ty<'tcx> { type Variant = ty::TyKind<'tcx>; #[inline] @@ -43,7 +45,9 @@ fn variant(&self) -> &Self::Variant { } } -impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for ty::PredicateKind<'tcx> { +impl<'tcx, E: TyEncoder>> EncodableWithShorthand + for ty::PredicateKind<'tcx> +{ type Variant = ty::PredicateKind<'tcx>; #[inline] @@ -52,15 +56,6 @@ fn variant(&self) -> &Self::Variant { } } -pub trait TyEncoder<'tcx>: Encoder { - const CLEAR_CROSS_CRATE: bool; - - fn position(&self) -> usize; - fn type_shorthands(&mut self) -> &mut FxHashMap, usize>; - fn predicate_shorthands(&mut self) -> &mut FxHashMap, usize>; - fn encode_alloc_id(&mut self, alloc_id: &AllocId) -> Result<(), Self::Error>; -} - /// Trait for decoding to a reference. /// /// This is a separate trait from `Decodable` so that we can implement it for @@ -71,7 +66,7 @@ pub trait TyEncoder<'tcx>: Encoder { /// /// `Decodable` can still be implemented in cases where `Decodable` is required /// by a trait bound. -pub trait RefDecodable<'tcx, D: TyDecoder<'tcx>> { +pub trait RefDecodable<'tcx, D: TyDecoder>> { fn decode(d: &mut D) -> &'tcx Self; } @@ -82,9 +77,9 @@ pub fn encode_with_shorthand<'tcx, E, T, M>( cache: M, ) -> Result<(), E::Error> where - E: TyEncoder<'tcx>, + E: TyEncoder>, M: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap, - T: EncodableWithShorthand<'tcx, E>, + T: EncodableWithShorthand, // The discriminant and shorthand must have the same size. T::Variant: DiscriminantKind, { @@ -119,50 +114,52 @@ pub fn encode_with_shorthand<'tcx, E, T, M>( Ok(()) } -impl<'tcx, E: TyEncoder<'tcx>> Encodable for Ty<'tcx> { +impl<'tcx, E: TyEncoder>> Encodable for Ty<'tcx> { fn encode(&self, e: &mut E) -> Result<(), E::Error> { encode_with_shorthand(e, self, TyEncoder::type_shorthands) } } -impl<'tcx, E: TyEncoder<'tcx>> Encodable for ty::Binder<'tcx, ty::PredicateKind<'tcx>> { +impl<'tcx, E: TyEncoder>> Encodable + for ty::Binder<'tcx, ty::PredicateKind<'tcx>> +{ fn encode(&self, e: &mut E) -> Result<(), E::Error> { self.bound_vars().encode(e)?; encode_with_shorthand(e, &self.skip_binder(), TyEncoder::predicate_shorthands) } } -impl<'tcx, E: TyEncoder<'tcx>> Encodable for ty::Predicate<'tcx> { +impl<'tcx, E: TyEncoder>> Encodable for ty::Predicate<'tcx> { fn encode(&self, e: &mut E) -> Result<(), E::Error> { self.kind().encode(e) } } -impl<'tcx, E: TyEncoder<'tcx>> Encodable for ty::Region<'tcx> { +impl<'tcx, E: TyEncoder>> Encodable for ty::Region<'tcx> { fn encode(&self, e: &mut E) -> Result<(), E::Error> { self.kind().encode(e) } } -impl<'tcx, E: TyEncoder<'tcx>> Encodable for ty::Const<'tcx> { +impl<'tcx, E: TyEncoder>> Encodable for ty::Const<'tcx> { fn encode(&self, e: &mut E) -> Result<(), E::Error> { self.0.0.encode(e) } } -impl<'tcx, E: TyEncoder<'tcx>> Encodable for ConstAllocation<'tcx> { +impl<'tcx, E: TyEncoder>> Encodable for ConstAllocation<'tcx> { fn encode(&self, e: &mut E) -> Result<(), E::Error> { self.inner().encode(e) } } -impl<'tcx, E: TyEncoder<'tcx>> Encodable for AdtDef<'tcx> { +impl<'tcx, E: TyEncoder>> Encodable for AdtDef<'tcx> { fn encode(&self, e: &mut E) -> Result<(), E::Error> { self.0.0.encode(e) } } -impl<'tcx, E: TyEncoder<'tcx>> Encodable for AllocId { +impl<'tcx, E: TyEncoder>> Encodable for AllocId { fn encode(&self, e: &mut E) -> Result<(), E::Error> { e.encode_alloc_id(self) } @@ -170,7 +167,7 @@ fn encode(&self, e: &mut E) -> Result<(), E::Error> { macro_rules! encodable_via_deref { ($($t:ty),+) => { - $(impl<'tcx, E: TyEncoder<'tcx>> Encodable for $t { + $(impl<'tcx, E: TyEncoder>> Encodable for $t { fn encode(&self, e: &mut E) -> Result<(), E::Error> { (**self).encode(e) } @@ -187,51 +184,35 @@ fn encode(&self, e: &mut E) -> Result<(), E::Error> { &'tcx mir::coverage::CodeRegion } -pub trait TyDecoder<'tcx>: Decoder { - const CLEAR_CROSS_CRATE: bool; - - fn tcx(&self) -> TyCtxt<'tcx>; - - fn peek_byte(&self) -> u8; - - fn position(&self) -> usize; - - fn cached_ty_for_shorthand(&mut self, shorthand: usize, or_insert_with: F) -> Ty<'tcx> - where - F: FnOnce(&mut Self) -> Ty<'tcx>; - - fn with_position(&mut self, pos: usize, f: F) -> R - where - F: FnOnce(&mut Self) -> R; - - fn positioned_at_shorthand(&self) -> bool { - (self.peek_byte() & (SHORTHAND_OFFSET as u8)) != 0 - } - - fn decode_alloc_id(&mut self) -> AllocId; -} - #[inline] -fn decode_arena_allocable<'tcx, D, T: ArenaAllocatable<'tcx> + Decodable>( +fn decode_arena_allocable< + 'tcx, + D: TyDecoder>, + T: ArenaAllocatable<'tcx> + Decodable, +>( decoder: &mut D, ) -> &'tcx T where - D: TyDecoder<'tcx>, + D: TyDecoder, { - decoder.tcx().arena.alloc(Decodable::decode(decoder)) + decoder.interner().tcx.arena.alloc(Decodable::decode(decoder)) } #[inline] -fn decode_arena_allocable_slice<'tcx, D, T: ArenaAllocatable<'tcx> + Decodable>( +fn decode_arena_allocable_slice< + 'tcx, + D: TyDecoder>, + T: ArenaAllocatable<'tcx> + Decodable, +>( decoder: &mut D, ) -> &'tcx [T] where - D: TyDecoder<'tcx>, + D: TyDecoder, { - decoder.tcx().arena.alloc_from_iter( as Decodable>::decode(decoder)) + decoder.interner().tcx.arena.alloc_from_iter( as Decodable>::decode(decoder)) } -impl<'tcx, D: TyDecoder<'tcx>> Decodable for Ty<'tcx> { +impl<'tcx, D: TyDecoder>> Decodable for Ty<'tcx> { #[allow(rustc::usage_of_ty_tykind)] fn decode(decoder: &mut D) -> Ty<'tcx> { // Handle shorthands first, if we have a usize > 0x80. @@ -244,13 +225,13 @@ fn decode(decoder: &mut D) -> Ty<'tcx> { decoder.with_position(shorthand, Ty::decode) }) } else { - let tcx = decoder.tcx(); - tcx.mk_ty(ty::TyKind::decode(decoder)) + let tcx = decoder.interner().tcx; + tcx.mk_ty(rustc_type_ir::TyKind::decode(decoder)) } } } -impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Binder<'tcx, ty::PredicateKind<'tcx>> { +impl<'tcx, D: TyDecoder>> Decodable for ty::Binder<'tcx, ty::PredicateKind<'tcx>> { fn decode(decoder: &mut D) -> ty::Binder<'tcx, ty::PredicateKind<'tcx>> { let bound_vars = Decodable::decode(decoder); // Handle shorthands first, if we have a usize > 0x80. @@ -269,64 +250,64 @@ fn decode(decoder: &mut D) -> ty::Binder<'tcx, ty::PredicateKind<'tcx>> { } } -impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Predicate<'tcx> { +impl<'tcx, D: TyDecoder>> Decodable for ty::Predicate<'tcx> { fn decode(decoder: &mut D) -> ty::Predicate<'tcx> { let predicate_kind = Decodable::decode(decoder); - decoder.tcx().mk_predicate(predicate_kind) + decoder.interner().tcx.mk_predicate(predicate_kind) } } -impl<'tcx, D: TyDecoder<'tcx>> Decodable for SubstsRef<'tcx> { +impl<'tcx, D: TyDecoder>> Decodable for SubstsRef<'tcx> { fn decode(decoder: &mut D) -> Self { let len = decoder.read_usize(); - let tcx = decoder.tcx(); + let tcx = decoder.interner().tcx; tcx.mk_substs( (0..len).map::, _>(|_| Decodable::decode(decoder)), ) } } -impl<'tcx, D: TyDecoder<'tcx>> Decodable for mir::Place<'tcx> { +impl<'tcx, D: TyDecoder>> Decodable for mir::Place<'tcx> { fn decode(decoder: &mut D) -> Self { let local: mir::Local = Decodable::decode(decoder); let len = decoder.read_usize(); - let projection = decoder.tcx().mk_place_elems( + let projection = decoder.interner().tcx.mk_place_elems( (0..len).map::, _>(|_| Decodable::decode(decoder)), ); mir::Place { local, projection } } } -impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Region<'tcx> { +impl<'tcx, D: TyDecoder>> Decodable for ty::Region<'tcx> { fn decode(decoder: &mut D) -> Self { - decoder.tcx().mk_region(Decodable::decode(decoder)) + decoder.interner().tcx.mk_region(Decodable::decode(decoder)) } } -impl<'tcx, D: TyDecoder<'tcx>> Decodable for CanonicalVarInfos<'tcx> { +impl<'tcx, D: TyDecoder>> Decodable for CanonicalVarInfos<'tcx> { fn decode(decoder: &mut D) -> Self { let len = decoder.read_usize(); let interned: Vec> = (0..len).map(|_| Decodable::decode(decoder)).collect(); - decoder.tcx().intern_canonical_var_infos(interned.as_slice()) + decoder.interner().tcx.intern_canonical_var_infos(interned.as_slice()) } } -impl<'tcx, D: TyDecoder<'tcx>> Decodable for AllocId { +impl<'tcx, D: TyDecoder>> Decodable for AllocId { fn decode(decoder: &mut D) -> Self { decoder.decode_alloc_id() } } -impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::SymbolName<'tcx> { +impl<'tcx, D: TyDecoder>> Decodable for ty::SymbolName<'tcx> { fn decode(decoder: &mut D) -> Self { - ty::SymbolName::new(decoder.tcx(), &decoder.read_str()) + ty::SymbolName::new(decoder.interner().tcx, &decoder.read_str()) } } macro_rules! impl_decodable_via_ref { ($($t:ty),+) => { - $(impl<'tcx, D: TyDecoder<'tcx>> Decodable for $t { + $(impl<'tcx, D: TyDecoder>> Decodable for $t { fn decode(decoder: &mut D) -> Self { RefDecodable::decode(decoder) } @@ -334,78 +315,78 @@ fn decode(decoder: &mut D) -> Self { } } -impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List> { +impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for ty::List> { fn decode(decoder: &mut D) -> &'tcx Self { let len = decoder.read_usize(); - decoder.tcx().mk_type_list((0..len).map::, _>(|_| Decodable::decode(decoder))) + decoder.interner().tcx.mk_type_list((0..len).map::, _>(|_| Decodable::decode(decoder))) } } -impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> +impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for ty::List>> { fn decode(decoder: &mut D) -> &'tcx Self { let len = decoder.read_usize(); - decoder.tcx().mk_poly_existential_predicates( + decoder.interner().tcx.mk_poly_existential_predicates( (0..len).map::, _>(|_| Decodable::decode(decoder)), ) } } -impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Const<'tcx> { +impl<'tcx, D: TyDecoder>> Decodable for ty::Const<'tcx> { fn decode(decoder: &mut D) -> Self { - decoder.tcx().mk_const(Decodable::decode(decoder)) + decoder.interner().tcx.mk_const(Decodable::decode(decoder)) } } -impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [ty::ValTree<'tcx>] { +impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for [ty::ValTree<'tcx>] { fn decode(decoder: &mut D) -> &'tcx Self { - decoder.tcx().arena.alloc_from_iter( + decoder.interner().tcx.arena.alloc_from_iter( (0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::>(), ) } } -impl<'tcx, D: TyDecoder<'tcx>> Decodable for ConstAllocation<'tcx> { +impl<'tcx, D: TyDecoder>> Decodable for ConstAllocation<'tcx> { fn decode(decoder: &mut D) -> Self { - decoder.tcx().intern_const_alloc(Decodable::decode(decoder)) + decoder.interner().tcx.intern_const_alloc(Decodable::decode(decoder)) } } -impl<'tcx, D: TyDecoder<'tcx>> Decodable for AdtDef<'tcx> { +impl<'tcx, D: TyDecoder>> Decodable for AdtDef<'tcx> { fn decode(decoder: &mut D) -> Self { - decoder.tcx().intern_adt_def(Decodable::decode(decoder)) + decoder.interner().tcx.intern_adt_def(Decodable::decode(decoder)) } } -impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [(ty::Predicate<'tcx>, Span)] { +impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for [(ty::Predicate<'tcx>, Span)] { fn decode(decoder: &mut D) -> &'tcx Self { - decoder.tcx().arena.alloc_from_iter( + decoder.interner().tcx.arena.alloc_from_iter( (0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::>(), ) } } -impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [thir::abstract_const::Node<'tcx>] { +impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for [thir::abstract_const::Node<'tcx>] { fn decode(decoder: &mut D) -> &'tcx Self { - decoder.tcx().arena.alloc_from_iter( + decoder.interner().tcx.arena.alloc_from_iter( (0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::>(), ) } } -impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [thir::abstract_const::NodeId] { +impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for [thir::abstract_const::NodeId] { fn decode(decoder: &mut D) -> &'tcx Self { - decoder.tcx().arena.alloc_from_iter( + decoder.interner().tcx.arena.alloc_from_iter( (0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::>(), ) } } -impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List { +impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for ty::List { fn decode(decoder: &mut D) -> &'tcx Self { let len = decoder.read_usize(); - decoder.tcx().mk_bound_variable_kinds( + decoder.interner().tcx.mk_bound_variable_kinds( (0..len).map::(|_| Decodable::decode(decoder)), ) } @@ -439,14 +420,14 @@ macro_rules! impl_arena_allocatable_decoder { ([]$args:tt) => {}; ([decode $(, $attrs:ident)*] [$name:ident: $ty:ty]) => { - impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for $ty { + impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for $ty { #[inline] fn decode(decoder: &mut D) -> &'tcx Self { decode_arena_allocable(decoder) } } - impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [$ty] { + impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for [$ty] { #[inline] fn decode(decoder: &mut D) -> &'tcx Self { decode_arena_allocable_slice(decoder) @@ -466,6 +447,33 @@ macro_rules! impl_arena_allocatable_decoders { rustc_hir::arena_types!(impl_arena_allocatable_decoders); arena_types!(impl_arena_allocatable_decoders); +macro_rules! impl_arena_copy_decoder { + (<$tcx:tt> $($ty:ty,)*) => { + $(impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for $ty { + #[inline] + fn decode(decoder: &mut D) -> &'tcx Self { + decoder.tcx().arena.alloc(Decodable::decode(decoder)) + } + } + + impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [$ty] { + #[inline] + fn decode(decoder: &mut D) -> &'tcx Self { + decoder.tcx().arena.alloc_from_iter( as Decodable>::decode(decoder)) + } + })* + }; +} + +impl_arena_copy_decoder! {<'tcx> + Span, + rustc_span::symbol::Ident, + ty::Variance, + rustc_span::def_id::DefId, + rustc_span::def_id::LocalDefId, + (rustc_middle::middle::exported_symbols::ExportedSymbol<'tcx>, rustc_middle::middle::exported_symbols::SymbolExportInfo), +} + #[macro_export] macro_rules! implement_ty_decoder { ($DecoderName:ident <$($typaram:tt),*>) => { @@ -510,13 +518,13 @@ fn read_raw_bytes(&mut self, len: usize) -> &[u8] { macro_rules! impl_binder_encode_decode { ($($t:ty),+ $(,)?) => { $( - impl<'tcx, E: TyEncoder<'tcx>> Encodable for ty::Binder<'tcx, $t> { + impl<'tcx, E: TyEncoder>> Encodable for ty::Binder<'tcx, $t> { fn encode(&self, e: &mut E) -> Result<(), E::Error> { self.bound_vars().encode(e)?; self.as_ref().skip_binder().encode(e) } } - impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Binder<'tcx, $t> { + impl<'tcx, D: TyDecoder>> Decodable for ty::Binder<'tcx, $t> { fn decode(decoder: &mut D) -> Self { let bound_vars = Decodable::decode(decoder); ty::Binder::bind_with_vars(Decodable::decode(decoder), bound_vars)