From 6438b9eca8fed1972627911121aee5c8b8906a97 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 13 Mar 2025 17:36:33 +0000 Subject: Use {Decodable,Encodable}_NoContext in type_ir --- compiler/rustc_macros/src/serialize.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'compiler/rustc_macros/src/serialize.rs') diff --git a/compiler/rustc_macros/src/serialize.rs b/compiler/rustc_macros/src/serialize.rs index 2c33a0ac0aa..77b2735064a 100644 --- a/compiler/rustc_macros/src/serialize.rs +++ b/compiler/rustc_macros/src/serialize.rs @@ -15,7 +15,7 @@ pub(super) fn type_decodable_derive( quote! {} }; - s.add_impl_generic(parse_quote! { #decoder_ty: ::rustc_type_ir::codec::TyDecoder #bound }); + s.add_impl_generic(parse_quote! { #decoder_ty: ::rustc_middle::ty::codec::TyDecoder #bound }); s.add_bounds(synstructure::AddBounds::Fields); s.underscore_const(true); @@ -45,12 +45,12 @@ pub(super) fn decodable_derive(mut s: synstructure::Structure<'_>) -> proc_macro decodable_body(s, decoder_ty) } -pub(super) fn decodable_generic_derive( +pub(super) fn decodable_nocontext_derive( mut s: synstructure::Structure<'_>, ) -> proc_macro2::TokenStream { let decoder_ty = quote! { __D }; s.add_impl_generic(parse_quote! { #decoder_ty: ::rustc_serialize::Decoder }); - s.add_bounds(synstructure::AddBounds::Generics); + s.add_bounds(synstructure::AddBounds::Fields); s.underscore_const(true); decodable_body(s, decoder_ty) @@ -141,7 +141,7 @@ pub(super) fn type_encodable_derive( }; let encoder_ty = quote! { __E }; - s.add_impl_generic(parse_quote! { #encoder_ty: ::rustc_type_ir::codec::TyEncoder #bound }); + s.add_impl_generic(parse_quote! { #encoder_ty: ::rustc_middle::ty::codec::TyEncoder #bound }); s.add_bounds(synstructure::AddBounds::Fields); s.underscore_const(true); @@ -171,12 +171,12 @@ pub(super) fn encodable_derive(mut s: synstructure::Structure<'_>) -> proc_macro encodable_body(s, encoder_ty, false) } -pub(super) fn encodable_generic_derive( +pub(super) fn encodable_nocontext_derive( mut s: synstructure::Structure<'_>, ) -> proc_macro2::TokenStream { let encoder_ty = quote! { __E }; s.add_impl_generic(parse_quote! { #encoder_ty: ::rustc_serialize::Encoder }); - s.add_bounds(synstructure::AddBounds::Generics); + s.add_bounds(synstructure::AddBounds::Fields); s.underscore_const(true); encodable_body(s, encoder_ty, false) -- cgit 1.4.1-3-g733a5 From 19c84c8812adb7fa0e7540b5de2541a5995e901a Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 13 Mar 2025 17:53:36 +0000 Subject: Move codec module back into middle --- compiler/rustc_macros/src/serialize.rs | 26 ++-- compiler/rustc_metadata/src/rmeta/decoder.rs | 6 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 4 +- .../rustc_middle/src/mir/interpret/allocation.rs | 11 +- .../src/mir/interpret/allocation/init_mask.rs | 13 +- compiler/rustc_middle/src/mir/interpret/mod.rs | 4 +- compiler/rustc_middle/src/mir/mod.rs | 4 +- compiler/rustc_middle/src/query/on_disk_cache.rs | 6 +- compiler/rustc_middle/src/ty/codec.rs | 155 ++++++++++++--------- compiler/rustc_middle/src/ty/generic_args.rs | 4 +- compiler/rustc_middle/src/ty/mod.rs | 5 +- compiler/rustc_query_impl/src/plumbing.rs | 3 +- compiler/rustc_type_ir/src/binder.rs | 4 +- compiler/rustc_type_ir/src/codec.rs | 61 -------- compiler/rustc_type_ir/src/lib.rs | 4 - compiler/rustc_type_ir/src/ty_kind.rs | 6 +- 16 files changed, 129 insertions(+), 187 deletions(-) delete mode 100644 compiler/rustc_type_ir/src/codec.rs (limited to 'compiler/rustc_macros/src/serialize.rs') diff --git a/compiler/rustc_macros/src/serialize.rs b/compiler/rustc_macros/src/serialize.rs index 77b2735064a..673e6cd618f 100644 --- a/compiler/rustc_macros/src/serialize.rs +++ b/compiler/rustc_macros/src/serialize.rs @@ -6,16 +6,11 @@ use syn::spanned::Spanned; pub(super) fn type_decodable_derive( mut s: synstructure::Structure<'_>, ) -> proc_macro2::TokenStream { + if !s.ast().generics.lifetimes().any(|lt| lt.lifetime.ident == "tcx") { + s.add_impl_generic(parse_quote! { 'tcx }); + } let decoder_ty = quote! { __D }; - let bound = if s.ast().generics.lifetimes().any(|lt| lt.lifetime.ident == "tcx") { - quote! { > } - } else if s.ast().generics.type_params().any(|ty| ty.ident == "I") { - quote! { } - } else { - quote! {} - }; - - s.add_impl_generic(parse_quote! { #decoder_ty: ::rustc_middle::ty::codec::TyDecoder #bound }); + s.add_impl_generic(parse_quote! { #decoder_ty: ::rustc_middle::ty::codec::TyDecoder<'tcx> }); s.add_bounds(synstructure::AddBounds::Fields); s.underscore_const(true); @@ -132,16 +127,11 @@ fn decode_field(field: &syn::Field) -> proc_macro2::TokenStream { pub(super) fn type_encodable_derive( mut s: synstructure::Structure<'_>, ) -> proc_macro2::TokenStream { - let bound = if s.ast().generics.lifetimes().any(|lt| lt.lifetime.ident == "tcx") { - quote! { > } - } else if s.ast().generics.type_params().any(|ty| ty.ident == "I") { - quote! { } - } else { - quote! {} - }; - let encoder_ty = quote! { __E }; - s.add_impl_generic(parse_quote! { #encoder_ty: ::rustc_middle::ty::codec::TyEncoder #bound }); + if !s.ast().generics.lifetimes().any(|lt| lt.lifetime.ident == "tcx") { + s.add_impl_generic(parse_quote! { 'tcx }); + } + s.add_impl_generic(parse_quote! { #encoder_ty: ::rustc_middle::ty::codec::TyEncoder<'tcx> }); s.add_bounds(synstructure::AddBounds::Fields); s.underscore_const(true); diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 6ffbebfffd5..e60904eebeb 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -386,13 +386,11 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> { } } -impl<'a, 'tcx> TyDecoder for DecodeContext<'a, 'tcx> { +impl<'a, 'tcx> TyDecoder<'tcx> for DecodeContext<'a, 'tcx> { const CLEAR_CROSS_CRATE: bool = true; - type I = TyCtxt<'tcx>; - #[inline] - fn interner(&self) -> Self::I { + fn interner(&self) -> TyCtxt<'tcx> { self.tcx() } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 57c941976e4..2b9113242aa 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -378,11 +378,9 @@ impl<'a, 'tcx> Encodable> for [u8] { } } -impl<'a, 'tcx> TyEncoder for EncodeContext<'a, 'tcx> { +impl<'a, 'tcx> TyEncoder<'tcx> for EncodeContext<'a, 'tcx> { const CLEAR_CROSS_CRATE: bool = true; - type I = TyCtxt<'tcx>; - fn position(&self) -> usize { self.opaque.position() } diff --git a/compiler/rustc_middle/src/mir/interpret/allocation.rs b/compiler/rustc_middle/src/mir/interpret/allocation.rs index aed38414ab1..57aafbb26bc 100644 --- a/compiler/rustc_middle/src/mir/interpret/allocation.rs +++ b/compiler/rustc_middle/src/mir/interpret/allocation.rs @@ -16,8 +16,7 @@ use rustc_abi::{Align, HasDataLayout, Size}; use rustc_ast::Mutability; use rustc_data_structures::intern::Interned; use rustc_macros::HashStable; -use rustc_serialize::{Decodable, Encodable}; -use rustc_type_ir::{TyDecoder, TyEncoder}; +use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use super::{ AllocId, BadBytesAccess, CtfeProvenance, InterpErrorKind, InterpResult, Pointer, @@ -112,7 +111,7 @@ struct AllocFlags { all_zero: bool, } -impl Encodable for AllocFlags { +impl Encodable for AllocFlags { fn encode(&self, encoder: &mut E) { // Make sure Align::MAX can be stored with the high 2 bits unset. const { @@ -131,7 +130,7 @@ impl Encodable for AllocFlags { } } -impl Decodable for AllocFlags { +impl Decodable for AllocFlags { fn decode(decoder: &mut D) -> Self { let flags: u8 = Decodable::decode(decoder); let align = flags & 0b0011_1111; @@ -173,7 +172,7 @@ fn all_zero(buf: &[u8]) -> bool { } /// Custom encoder for [`Allocation`] to more efficiently represent the case where all bytes are 0. -impl Encodable for Allocation +impl Encodable for Allocation where Bytes: AllocBytes, ProvenanceMap: Encodable, @@ -193,7 +192,7 @@ where } } -impl Decodable for Allocation +impl Decodable for Allocation where Bytes: AllocBytes, ProvenanceMap: Decodable, diff --git a/compiler/rustc_middle/src/mir/interpret/allocation/init_mask.rs b/compiler/rustc_middle/src/mir/interpret/allocation/init_mask.rs index fea5038e6dd..20492cda4e2 100644 --- a/compiler/rustc_middle/src/mir/interpret/allocation/init_mask.rs +++ b/compiler/rustc_middle/src/mir/interpret/allocation/init_mask.rs @@ -5,9 +5,8 @@ use std::ops::Range; use std::{hash, iter}; use rustc_abi::Size; -use rustc_macros::{HashStable, TyDecodable, TyEncodable}; -use rustc_serialize::{Decodable, Encodable}; -use rustc_type_ir::{TyDecoder, TyEncoder}; +use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable}; +use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use super::AllocRange; @@ -19,13 +18,13 @@ type Block = u64; /// possible. Currently, if all the blocks have the same value, then the mask represents either a /// fully initialized or fully uninitialized const allocation, so we can only store that single /// value. -#[derive(Clone, Debug, Eq, PartialEq, TyEncodable, TyDecodable, Hash, HashStable)] +#[derive(Clone, Debug, Eq, PartialEq, Encodable_NoContext, Decodable_NoContext, Hash, HashStable)] pub struct InitMask { blocks: InitMaskBlocks, len: Size, } -#[derive(Clone, Debug, Eq, PartialEq, TyEncodable, TyDecodable, Hash, HashStable)] +#[derive(Clone, Debug, Eq, PartialEq, Encodable_NoContext, Decodable_NoContext, Hash, HashStable)] enum InitMaskBlocks { Lazy { /// Whether the lazy init mask is fully initialized or uninitialized. @@ -194,7 +193,7 @@ struct InitMaskMaterialized { // and also produces more output when the high bits of each `u64` are occupied. // Note: There is probably a remaining optimization for masks that do not use an entire // `Block`. -impl Encodable for InitMaskMaterialized { +impl Encodable for InitMaskMaterialized { fn encode(&self, encoder: &mut E) { encoder.emit_usize(self.blocks.len()); for block in &self.blocks { @@ -204,7 +203,7 @@ impl Encodable for InitMaskMaterialized { } // This implementation is deliberately not derived, see the matching `Encodable` impl. -impl Decodable for InitMaskMaterialized { +impl Decodable for InitMaskMaterialized { fn decode(decoder: &mut D) -> Self { let num_blocks = decoder.read_usize(); let mut blocks = Vec::with_capacity(num_blocks); diff --git a/compiler/rustc_middle/src/mir/interpret/mod.rs b/compiler/rustc_middle/src/mir/interpret/mod.rs index effedf854e4..c2438af6a1e 100644 --- a/compiler/rustc_middle/src/mir/interpret/mod.rs +++ b/compiler/rustc_middle/src/mir/interpret/mod.rs @@ -105,7 +105,7 @@ enum AllocDiscriminant { Static, } -pub fn specialized_encode_alloc_id<'tcx, E: TyEncoder>>( +pub fn specialized_encode_alloc_id<'tcx, E: TyEncoder<'tcx>>( encoder: &mut E, tcx: TyCtxt<'tcx>, alloc_id: AllocId, @@ -175,7 +175,7 @@ impl<'s> AllocDecodingSession<'s> { /// Decodes an `AllocId` in a thread-safe way. pub fn decode_alloc_id<'tcx, D>(&self, decoder: &mut D) -> AllocId where - D: TyDecoder>, + D: TyDecoder<'tcx>, { // Read the index of the allocation. let idx = usize::try_from(decoder.read_u32()).unwrap(); diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index e6b92440e7b..7090e93549e 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -790,7 +790,7 @@ impl ClearCrossCrate { const TAG_CLEAR_CROSS_CRATE_CLEAR: u8 = 0; const TAG_CLEAR_CROSS_CRATE_SET: u8 = 1; -impl> Encodable for ClearCrossCrate { +impl<'tcx, E: TyEncoder<'tcx>, T: Encodable> Encodable for ClearCrossCrate { #[inline] fn encode(&self, e: &mut E) { if E::CLEAR_CROSS_CRATE { @@ -806,7 +806,7 @@ impl> Encodable for ClearCrossCrate { } } } -impl> Decodable for ClearCrossCrate { +impl<'tcx, D: TyDecoder<'tcx>, T: Decodable> Decodable for ClearCrossCrate { #[inline] fn decode(d: &mut D) -> ClearCrossCrate { if D::CLEAR_CROSS_CRATE { diff --git a/compiler/rustc_middle/src/query/on_disk_cache.rs b/compiler/rustc_middle/src/query/on_disk_cache.rs index d9035efaf56..e66958dfcb8 100644 --- a/compiler/rustc_middle/src/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/query/on_disk_cache.rs @@ -518,8 +518,7 @@ where value } -impl<'a, 'tcx> TyDecoder for CacheDecoder<'a, 'tcx> { - type I = TyCtxt<'tcx>; +impl<'a, 'tcx> TyDecoder<'tcx> for CacheDecoder<'a, 'tcx> { const CLEAR_CROSS_CRATE: bool = false; #[inline] @@ -943,8 +942,7 @@ impl<'a, 'tcx> SpanEncoder for CacheEncoder<'a, 'tcx> { } } -impl<'a, 'tcx> TyEncoder for CacheEncoder<'a, 'tcx> { - type I = TyCtxt<'tcx>; +impl<'a, 'tcx> TyEncoder<'tcx> for CacheEncoder<'a, 'tcx> { const CLEAR_CROSS_CRATE: bool = false; #[inline] diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index fe42a224d9f..74b34afe616 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -14,9 +14,8 @@ use rustc_abi::{FieldIdx, VariantIdx}; use rustc_data_structures::fx::FxHashMap; use rustc_hir::def_id::LocalDefId; use rustc_serialize::{Decodable, Encodable}; -use rustc_span::Span; use rustc_span::source_map::Spanned; -pub use rustc_type_ir::{TyDecoder, TyEncoder}; +use rustc_span::{Span, SpanDecoder, SpanEncoder}; use crate::arena::ArenaAllocatable; use crate::infer::canonical::{CanonicalVarInfo, CanonicalVarInfos}; @@ -31,13 +30,45 @@ use crate::ty::{self, AdtDef, GenericArgsRef, Ty, TyCtxt}; /// This offset is also chosen so that the first byte is never < 0x80. pub const SHORTHAND_OFFSET: usize = 0x80; -pub trait EncodableWithShorthand: Copy + Eq + Hash { +pub trait TyEncoder<'tcx>: SpanEncoder { + 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); +} + +pub trait TyDecoder<'tcx>: SpanDecoder { + const CLEAR_CROSS_CRATE: bool; + + fn interner(&self) -> TyCtxt<'tcx>; + + 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; +} + +pub trait EncodableWithShorthand<'tcx, E: TyEncoder<'tcx>>: Copy + Eq + Hash { type Variant: Encodable; fn variant(&self) -> &Self::Variant; } #[allow(rustc::usage_of_ty_tykind)] -impl<'tcx, E: TyEncoder>> EncodableWithShorthand for Ty<'tcx> { +impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for Ty<'tcx> { type Variant = ty::TyKind<'tcx>; #[inline] @@ -46,7 +77,7 @@ impl<'tcx, E: TyEncoder>> EncodableWithShorthand for Ty<'tcx } } -impl<'tcx, E: TyEncoder>> EncodableWithShorthand for ty::PredicateKind<'tcx> { +impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for ty::PredicateKind<'tcx> { type Variant = ty::PredicateKind<'tcx>; #[inline] @@ -65,16 +96,16 @@ impl<'tcx, E: TyEncoder>> EncodableWithShorthand for ty::Pre /// /// `Decodable` can still be implemented in cases where `Decodable` is required /// by a trait bound. -pub trait RefDecodable<'tcx, D: TyDecoder>> { +pub trait RefDecodable<'tcx, D: TyDecoder<'tcx>> { fn decode(d: &mut D) -> &'tcx Self; } /// Encode the given value or a previously cached shorthand. pub fn encode_with_shorthand<'tcx, E, T, M>(encoder: &mut E, value: &T, cache: M) where - E: TyEncoder>, + E: TyEncoder<'tcx>, M: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap, - T: EncodableWithShorthand, + T: EncodableWithShorthand<'tcx, E>, // The discriminant and shorthand must have the same size. T::Variant: DiscriminantKind, { @@ -108,13 +139,13 @@ where } } -impl<'tcx, E: TyEncoder>> Encodable for Ty<'tcx> { +impl<'tcx, E: TyEncoder<'tcx>> Encodable for Ty<'tcx> { fn encode(&self, e: &mut E) { encode_with_shorthand(e, self, TyEncoder::type_shorthands); } } -impl<'tcx, E: TyEncoder>> Encodable for ty::Predicate<'tcx> { +impl<'tcx, E: TyEncoder<'tcx>> Encodable for ty::Predicate<'tcx> { fn encode(&self, e: &mut E) { let kind = self.kind(); kind.bound_vars().encode(e); @@ -122,76 +153,72 @@ impl<'tcx, E: TyEncoder>> Encodable for ty::Predicate<'tcx> } } -impl<'tcx, E: TyEncoder>> Encodable for ty::Clause<'tcx> { +impl<'tcx, E: TyEncoder<'tcx>> Encodable for ty::Clause<'tcx> { fn encode(&self, e: &mut E) { self.as_predicate().encode(e); } } -impl<'tcx, E: TyEncoder>> Encodable for ty::Region<'tcx> { +impl<'tcx, E: TyEncoder<'tcx>> Encodable for ty::Region<'tcx> { fn encode(&self, e: &mut E) { self.kind().encode(e); } } -impl<'tcx, E: TyEncoder>> Encodable for ty::Const<'tcx> { +impl<'tcx, E: TyEncoder<'tcx>> Encodable for ty::Const<'tcx> { fn encode(&self, e: &mut E) { self.0.0.encode(e); } } -impl<'tcx, E: TyEncoder>> Encodable for ty::Pattern<'tcx> { +impl<'tcx, E: TyEncoder<'tcx>> Encodable for ty::Pattern<'tcx> { fn encode(&self, e: &mut E) { self.0.0.encode(e); } } -impl<'tcx, E: TyEncoder>> Encodable for ty::ValTree<'tcx> { +impl<'tcx, E: TyEncoder<'tcx>> Encodable for ty::ValTree<'tcx> { fn encode(&self, e: &mut E) { self.0.0.encode(e); } } -impl<'tcx, E: TyEncoder>> Encodable for ConstAllocation<'tcx> { +impl<'tcx, E: TyEncoder<'tcx>> Encodable for ConstAllocation<'tcx> { fn encode(&self, e: &mut E) { self.inner().encode(e) } } -impl<'tcx, E: TyEncoder>> Encodable for AdtDef<'tcx> { +impl<'tcx, E: TyEncoder<'tcx>> Encodable for AdtDef<'tcx> { fn encode(&self, e: &mut E) { self.0.0.encode(e) } } -impl<'tcx, E: TyEncoder>> Encodable for AllocId { +impl<'tcx, E: TyEncoder<'tcx>> Encodable for AllocId { fn encode(&self, e: &mut E) { e.encode_alloc_id(self) } } -impl<'tcx, E: TyEncoder>> Encodable for CtfeProvenance { +impl<'tcx, E: TyEncoder<'tcx>> Encodable for CtfeProvenance { fn encode(&self, e: &mut E) { self.into_parts().encode(e); } } -impl<'tcx, E: TyEncoder>> Encodable for ty::ParamEnv<'tcx> { +impl<'tcx, E: TyEncoder<'tcx>> Encodable for ty::ParamEnv<'tcx> { fn encode(&self, e: &mut E) { self.caller_bounds().encode(e); } } #[inline] -fn decode_arena_allocable< - 'tcx, - D: TyDecoder>, - T: ArenaAllocatable<'tcx> + Decodable, ->( +fn decode_arena_allocable<'tcx, D: TyDecoder<'tcx>, T: ArenaAllocatable<'tcx> + Decodable>( decoder: &mut D, ) -> &'tcx T where - D: TyDecoder, + D: TyDecoder<'tcx>, { decoder.interner().arena.alloc(Decodable::decode(decoder)) } @@ -199,18 +226,18 @@ where #[inline] fn decode_arena_allocable_slice< 'tcx, - D: TyDecoder>, + D: TyDecoder<'tcx>, T: ArenaAllocatable<'tcx> + Decodable, >( decoder: &mut D, ) -> &'tcx [T] where - D: TyDecoder, + D: TyDecoder<'tcx>, { decoder.interner().arena.alloc_from_iter( as Decodable>::decode(decoder)) } -impl<'tcx, D: TyDecoder>> Decodable for Ty<'tcx> { +impl<'tcx, D: TyDecoder<'tcx>> 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. @@ -229,7 +256,7 @@ impl<'tcx, D: TyDecoder>> Decodable for Ty<'tcx> { } } -impl<'tcx, D: TyDecoder>> Decodable for ty::Predicate<'tcx> { +impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Predicate<'tcx> { fn decode(decoder: &mut D) -> ty::Predicate<'tcx> { let bound_vars = Decodable::decode(decoder); // Handle shorthands first, if we have a usize > 0x80. @@ -249,14 +276,14 @@ impl<'tcx, D: TyDecoder>> Decodable for ty::Predicate<'tcx> } } -impl<'tcx, D: TyDecoder>> Decodable for ty::Clause<'tcx> { +impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Clause<'tcx> { fn decode(decoder: &mut D) -> ty::Clause<'tcx> { let pred: ty::Predicate<'tcx> = Decodable::decode(decoder); pred.expect_clause() } } -impl<'tcx, D: TyDecoder>> Decodable for GenericArgsRef<'tcx> { +impl<'tcx, D: TyDecoder<'tcx>> Decodable for GenericArgsRef<'tcx> { fn decode(decoder: &mut D) -> Self { let len = decoder.read_usize(); let tcx = decoder.interner(); @@ -266,7 +293,7 @@ impl<'tcx, D: TyDecoder>> Decodable for GenericArgsRef<'tcx> } } -impl<'tcx, D: TyDecoder>> Decodable for mir::Place<'tcx> { +impl<'tcx, D: TyDecoder<'tcx>> Decodable for mir::Place<'tcx> { fn decode(decoder: &mut D) -> Self { let local: mir::Local = Decodable::decode(decoder); let len = decoder.read_usize(); @@ -277,13 +304,13 @@ impl<'tcx, D: TyDecoder>> Decodable for mir::Place<'tcx> { } } -impl<'tcx, D: TyDecoder>> Decodable for ty::Region<'tcx> { +impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Region<'tcx> { fn decode(decoder: &mut D) -> Self { ty::Region::new_from_kind(decoder.interner(), Decodable::decode(decoder)) } } -impl<'tcx, D: TyDecoder>> Decodable for CanonicalVarInfos<'tcx> { +impl<'tcx, D: TyDecoder<'tcx>> Decodable for CanonicalVarInfos<'tcx> { fn decode(decoder: &mut D) -> Self { let len = decoder.read_usize(); decoder.interner().mk_canonical_var_infos_from_iter( @@ -292,26 +319,26 @@ impl<'tcx, D: TyDecoder>> Decodable for CanonicalVarInfos<'t } } -impl<'tcx, D: TyDecoder>> Decodable for AllocId { +impl<'tcx, D: TyDecoder<'tcx>> Decodable for AllocId { fn decode(decoder: &mut D) -> Self { decoder.decode_alloc_id() } } -impl<'tcx, D: TyDecoder>> Decodable for CtfeProvenance { +impl<'tcx, D: TyDecoder<'tcx>> Decodable for CtfeProvenance { fn decode(decoder: &mut D) -> Self { let parts = Decodable::decode(decoder); CtfeProvenance::from_parts(parts) } } -impl<'tcx, D: TyDecoder>> Decodable for ty::SymbolName<'tcx> { +impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::SymbolName<'tcx> { fn decode(decoder: &mut D) -> Self { ty::SymbolName::new(decoder.interner(), decoder.read_str()) } } -impl<'tcx, D: TyDecoder>> Decodable for ty::ParamEnv<'tcx> { +impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::ParamEnv<'tcx> { fn decode(d: &mut D) -> Self { let caller_bounds = Decodable::decode(d); ty::ParamEnv::new(caller_bounds) @@ -320,7 +347,7 @@ impl<'tcx, D: TyDecoder>> Decodable for ty::ParamEnv<'tcx> { macro_rules! impl_decodable_via_ref { ($($t:ty,)+) => { - $(impl<'tcx, D: TyDecoder>> Decodable for $t { + $(impl<'tcx, D: TyDecoder<'tcx>> Decodable for $t { fn decode(decoder: &mut D) -> Self { RefDecodable::decode(decoder) } @@ -328,7 +355,7 @@ macro_rules! impl_decodable_via_ref { } } -impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for ty::List> { +impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List> { fn decode(decoder: &mut D) -> &'tcx Self { let len = decoder.read_usize(); decoder @@ -337,7 +364,7 @@ impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for ty::List>> RefDecodable<'tcx, D> +impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List> { fn decode(decoder: &mut D) -> &'tcx Self { @@ -348,38 +375,38 @@ impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> } } -impl<'tcx, D: TyDecoder>> Decodable for ty::Const<'tcx> { +impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Const<'tcx> { fn decode(decoder: &mut D) -> Self { let kind: ty::ConstKind<'tcx> = Decodable::decode(decoder); decoder.interner().mk_ct_from_kind(kind) } } -impl<'tcx, D: TyDecoder>> Decodable for ty::Pattern<'tcx> { +impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Pattern<'tcx> { fn decode(decoder: &mut D) -> Self { decoder.interner().mk_pat(Decodable::decode(decoder)) } } -impl<'tcx, D: TyDecoder>> Decodable for ty::ValTree<'tcx> { +impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::ValTree<'tcx> { fn decode(decoder: &mut D) -> Self { decoder.interner().intern_valtree(Decodable::decode(decoder)) } } -impl<'tcx, D: TyDecoder>> Decodable for ConstAllocation<'tcx> { +impl<'tcx, D: TyDecoder<'tcx>> Decodable for ConstAllocation<'tcx> { fn decode(decoder: &mut D) -> Self { decoder.interner().mk_const_alloc(Decodable::decode(decoder)) } } -impl<'tcx, D: TyDecoder>> Decodable for AdtDef<'tcx> { +impl<'tcx, D: TyDecoder<'tcx>> Decodable for AdtDef<'tcx> { fn decode(decoder: &mut D) -> Self { decoder.interner().mk_adt_def_from_data(Decodable::decode(decoder)) } } -impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for [(ty::Clause<'tcx>, Span)] { +impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [(ty::Clause<'tcx>, Span)] { fn decode(decoder: &mut D) -> &'tcx Self { decoder .interner() @@ -388,9 +415,7 @@ impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for [(ty::Claus } } -impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> - for [(ty::PolyTraitRef<'tcx>, Span)] -{ +impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [(ty::PolyTraitRef<'tcx>, Span)] { fn decode(decoder: &mut D) -> &'tcx Self { decoder .interner() @@ -399,7 +424,7 @@ impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> } } -impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for [Spanned>] { +impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [Spanned>] { fn decode(decoder: &mut D) -> &'tcx Self { decoder .interner() @@ -408,9 +433,7 @@ impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for [Spanned>> RefDecodable<'tcx, D> - for ty::List -{ +impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List { fn decode(decoder: &mut D) -> &'tcx Self { let len = decoder.read_usize(); decoder.interner().mk_bound_variable_kinds_from_iter( @@ -419,7 +442,7 @@ impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> } } -impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for ty::List> { +impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List> { fn decode(decoder: &mut D) -> &'tcx Self { let len = decoder.read_usize(); decoder.interner().mk_const_list_from_iter( @@ -428,7 +451,7 @@ impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for ty::List>> RefDecodable<'tcx, D> +impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::ListWithCachedTypeInfo> { fn decode(decoder: &mut D) -> &'tcx Self { @@ -439,7 +462,7 @@ impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> } } -impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for ty::List { +impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List { fn decode(decoder: &mut D) -> &'tcx Self { let len = decoder.read_usize(); decoder @@ -448,7 +471,7 @@ impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for ty::List>> RefDecodable<'tcx, D> for ty::List { +impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List { fn decode(decoder: &mut D) -> &'tcx Self { let len = decoder.read_usize(); decoder.interner().mk_local_def_ids_from_iter( @@ -457,15 +480,13 @@ impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for ty::List>> Decodable for &'tcx ty::List { +impl<'tcx, D: TyDecoder<'tcx>> Decodable for &'tcx ty::List { fn decode(d: &mut D) -> Self { RefDecodable::decode(d) } } -impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> - for ty::List<(VariantIdx, FieldIdx)> -{ +impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List<(VariantIdx, FieldIdx)> { fn decode(decoder: &mut D) -> &'tcx Self { let len = decoder.read_usize(); decoder.interner().mk_offset_of_from_iter( @@ -503,14 +524,14 @@ macro_rules! impl_arena_allocatable_decoder { ([]$args:tt) => {}; ([decode $(, $attrs:ident)*] [$name:ident: $ty:ty]) => { - impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for $ty { + impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for $ty { #[inline] fn decode(decoder: &mut D) -> &'tcx Self { decode_arena_allocable(decoder) } } - impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for [$ty] { + impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [$ty] { #[inline] fn decode(decoder: &mut D) -> &'tcx Self { decode_arena_allocable_slice(decoder) @@ -532,14 +553,14 @@ arena_types!(impl_arena_allocatable_decoders); macro_rules! impl_arena_copy_decoder { (<$tcx:tt> $($ty:ty,)*) => { - $(impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for $ty { + $(impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for $ty { #[inline] fn decode(decoder: &mut D) -> &'tcx Self { decoder.interner().arena.alloc(Decodable::decode(decoder)) } } - impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for [$ty] { + impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [$ty] { #[inline] fn decode(decoder: &mut D) -> &'tcx Self { decoder.interner().arena.alloc_from_iter( as Decodable>::decode(decoder)) diff --git a/compiler/rustc_middle/src/ty/generic_args.rs b/compiler/rustc_middle/src/ty/generic_args.rs index 795ea2a1ea6..e87859a55ed 100644 --- a/compiler/rustc_middle/src/ty/generic_args.rs +++ b/compiler/rustc_middle/src/ty/generic_args.rs @@ -334,13 +334,13 @@ impl<'tcx> TypeVisitable> for GenericArg<'tcx> { } } -impl<'tcx, E: TyEncoder>> Encodable for GenericArg<'tcx> { +impl<'tcx, E: TyEncoder<'tcx>> Encodable for GenericArg<'tcx> { fn encode(&self, e: &mut E) { self.unpack().encode(e) } } -impl<'tcx, D: TyDecoder>> Decodable for GenericArg<'tcx> { +impl<'tcx, D: TyDecoder<'tcx>> Decodable for GenericArg<'tcx> { fn decode(d: &mut D) -> GenericArg<'tcx> { GenericArgKind::decode(d).pack() } diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index fd36c1f0085..29949f8b3e3 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -105,6 +105,7 @@ use crate::middle::privacy::EffectiveVisibilities; use crate::mir::{Body, CoroutineLayout}; use crate::query::{IntoQueryParam, Providers}; use crate::ty; +use crate::ty::codec::{TyDecoder, TyEncoder}; pub use crate::ty::diagnostics::*; use crate::ty::fast_reject::SimplifiedType; use crate::ty::util::Discr; @@ -549,13 +550,13 @@ impl<'tcx> TypeVisitable> for Term<'tcx> { } } -impl<'tcx, E: TyEncoder>> Encodable for Term<'tcx> { +impl<'tcx, E: TyEncoder<'tcx>> Encodable for Term<'tcx> { fn encode(&self, e: &mut E) { self.unpack().encode(e) } } -impl<'tcx, D: TyDecoder>> Decodable for Term<'tcx> { +impl<'tcx, D: TyDecoder<'tcx>> Decodable for Term<'tcx> { fn decode(d: &mut D) -> Self { let res: TermKind<'tcx> = Decodable::decode(d); res.pack() diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index f98d6421307..fa31ef5eeb0 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -19,9 +19,10 @@ use rustc_middle::query::Key; use rustc_middle::query::on_disk_cache::{ AbsoluteBytePos, CacheDecoder, CacheEncoder, EncodedDepNodeIndex, }; +use rustc_middle::ty::codec::TyEncoder; use rustc_middle::ty::print::with_reduced_queries; use rustc_middle::ty::tls::{self, ImplicitCtxt}; -use rustc_middle::ty::{self, TyCtxt, TyEncoder}; +use rustc_middle::ty::{self, TyCtxt}; use rustc_query_system::dep_graph::{DepNodeParams, HasDepContext}; use rustc_query_system::ich::StableHashingContext; use rustc_query_system::query::{ diff --git a/compiler/rustc_type_ir/src/binder.rs b/compiler/rustc_type_ir/src/binder.rs index 65d2614f44e..c8b71074de7 100644 --- a/compiler/rustc_type_ir/src/binder.rs +++ b/compiler/rustc_type_ir/src/binder.rs @@ -57,7 +57,7 @@ where macro_rules! impl_binder_encode_decode { ($($t:ty),+ $(,)?) => { $( - impl> rustc_serialize::Encodable for ty::Binder + impl rustc_serialize::Encodable for ty::Binder where $t: rustc_serialize::Encodable, I::BoundVarKinds: rustc_serialize::Encodable, @@ -67,7 +67,7 @@ macro_rules! impl_binder_encode_decode { self.as_ref().skip_binder().encode(e); } } - impl> rustc_serialize::Decodable for ty::Binder + impl rustc_serialize::Decodable for ty::Binder where $t: TypeVisitable + rustc_serialize::Decodable, I::BoundVarKinds: rustc_serialize::Decodable, diff --git a/compiler/rustc_type_ir/src/codec.rs b/compiler/rustc_type_ir/src/codec.rs deleted file mode 100644 index a1cffe3ca5a..00000000000 --- a/compiler/rustc_type_ir/src/codec.rs +++ /dev/null @@ -1,61 +0,0 @@ -use rustc_data_structures::fx::FxHashMap; -use rustc_span::{SpanDecoder, SpanEncoder}; - -use crate::{Interner, PredicateKind}; - -/// The shorthand encoding uses an enum's variant index `usize` -/// and is offset by this value so it never matches a real variant. -/// This offset is also chosen so that the first byte is never < 0x80. -pub const SHORTHAND_OFFSET: usize = 0x80; - -/// Trait for decoding to a reference. -/// -/// This is a separate trait from `Decodable` so that we can implement it for -/// upstream types, such as `FxHashSet`. -/// -/// The `Decodable_NoContext` derive macro will use this trait for fields that are -/// references (and don't use a type alias to hide that). -/// -/// `Decodable` can still be implemented in cases where `Decodable` is required -/// by a trait bound. -pub trait RefDecodable<'tcx, D: TyDecoder> { - fn decode(d: &mut D) -> &'tcx Self; -} - -pub trait TyEncoder: SpanEncoder { - type I: Interner; - const CLEAR_CROSS_CRATE: bool; - - fn position(&self) -> usize; - - fn type_shorthands(&mut self) -> &mut FxHashMap<::Ty, usize>; - - fn predicate_shorthands(&mut self) -> &mut FxHashMap, usize>; - - fn encode_alloc_id(&mut self, alloc_id: &::AllocId); -} - -pub trait TyDecoder: SpanDecoder { - type I: Interner; - const CLEAR_CROSS_CRATE: bool; - - fn interner(&self) -> Self::I; - - fn cached_ty_for_shorthand( - &mut self, - shorthand: usize, - or_insert_with: F, - ) -> ::Ty - where - F: FnOnce(&mut Self) -> ::Ty; - - 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; -} diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index 5e02bf29319..62912189509 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -17,8 +17,6 @@ use std::hash::Hash; use rustc_macros::{Decodable, Encodable, HashStable_NoContext}; // These modules are `pub` since they are not glob-imported. -#[cfg(feature = "nightly")] -pub mod codec; pub mod data_structures; pub mod elaborate; pub mod error; @@ -61,8 +59,6 @@ pub use TyKind::*; pub use Variance::*; pub use binder::*; pub use canonical::*; -#[cfg(feature = "nightly")] -pub use codec::*; pub use const_kind::*; pub use flags::*; pub use fold::*; diff --git a/compiler/rustc_type_ir/src/ty_kind.rs b/compiler/rustc_type_ir/src/ty_kind.rs index af423153587..9bea4482b55 100644 --- a/compiler/rustc_type_ir/src/ty_kind.rs +++ b/compiler/rustc_type_ir/src/ty_kind.rs @@ -1037,7 +1037,8 @@ impl Deref for UnsafeBinderInner { } #[cfg(feature = "nightly")] -impl> rustc_serialize::Encodable for UnsafeBinderInner +impl rustc_serialize::Encodable + for UnsafeBinderInner where I::Ty: rustc_serialize::Encodable, I::BoundVarKinds: rustc_serialize::Encodable, @@ -1049,7 +1050,8 @@ where } #[cfg(feature = "nightly")] -impl> rustc_serialize::Decodable for UnsafeBinderInner +impl rustc_serialize::Decodable + for UnsafeBinderInner where I::Ty: TypeVisitable + rustc_serialize::Decodable, I::BoundVarKinds: rustc_serialize::Decodable, -- cgit 1.4.1-3-g733a5