From 46389159403ca0973bf88a4c153a2707cdb257d2 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Fri, 27 May 2022 20:03:57 -0700 Subject: [PATCH] Make TyCtxt implement Interner, make HashStable generic and move to rustc_type_ir --- compiler/rustc_macros/src/serialize.rs | 4 +- compiler/rustc_metadata/src/rmeta/decoder.rs | 6 +- compiler/rustc_metadata/src/rmeta/encoder.rs | 4 +- .../rustc_middle/src/mir/interpret/mod.rs | 14 +- compiler/rustc_middle/src/ty/codec.rs | 129 ++++++++--------- compiler/rustc_middle/src/ty/context.rs | 11 +- compiler/rustc_middle/src/ty/mod.rs | 27 +--- compiler/rustc_middle/src/ty/sty.rs | 2 +- compiler/rustc_middle/src/ty/subst.rs | 6 +- .../rustc_mir_transform/src/coverage/tests.rs | 4 +- .../rustc_query_impl/src/on_disk_cache.rs | 10 +- compiler/rustc_query_system/src/ich/mod.rs | 122 ---------------- compiler/rustc_type_ir/src/lib.rs | 1 - compiler/rustc_type_ir/src/sty.rs | 135 +++++++++++++++++- compiler/rustc_typeck/src/check/op.rs | 2 - 15 files changed, 229 insertions(+), 248 deletions(-) diff --git a/compiler/rustc_macros/src/serialize.rs b/compiler/rustc_macros/src/serialize.rs index 653c728ee42..a39b4413981 100644 --- a/compiler/rustc_macros/src/serialize.rs +++ b/compiler/rustc_macros/src/serialize.rs @@ -8,7 +8,7 @@ pub fn type_decodable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2: if !s.ast().generics.lifetimes().any(|lt| lt.lifetime.ident == "tcx") { s.add_impl_generic(parse_quote! { 'tcx }); } - s.add_impl_generic(parse_quote! {#decoder_ty: ::rustc_type_ir::codec::TyDecoder>}); + s.add_impl_generic(parse_quote! {#decoder_ty: ::rustc_type_ir::codec::TyDecoder>}); s.add_bounds(synstructure::AddBounds::Generics); decodable_body(s, decoder_ty) @@ -95,7 +95,7 @@ pub fn type_encodable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2: s.add_impl_generic(parse_quote! {'tcx}); } let encoder_ty = quote! { __E }; - s.add_impl_generic(parse_quote! {#encoder_ty: ::rustc_type_ir::codec::TyEncoder>}); + s.add_impl_generic(parse_quote! {#encoder_ty: ::rustc_type_ir::codec::TyEncoder>}); s.add_bounds(synstructure::AddBounds::Generics); encodable_body(s, encoder_ty, false) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index d9f21fa9bb9..775ebb48402 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -24,8 +24,8 @@ use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState}; use rustc_middle::thir; use rustc_middle::ty::codec::TyDecoder; use rustc_middle::ty::fast_reject::SimplifiedType; +use rustc_middle::ty::GeneratorDiagnosticData; use rustc_middle::ty::{self, ParameterizedOverTcx, Ty, TyCtxt, Visibility}; -use rustc_middle::ty::{GeneratorDiagnosticData, TyInterner}; use rustc_serialize::{opaque, Decodable, Decoder}; use rustc_session::cstore::{ CrateSource, ExternCrate, ForeignModule, LinkagePreference, NativeLib, @@ -380,11 +380,11 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> { impl<'a, 'tcx> TyDecoder for DecodeContext<'a, 'tcx> { const CLEAR_CROSS_CRATE: bool = true; - type I = TyInterner<'tcx>; + type I = TyCtxt<'tcx>; #[inline] fn interner(&self) -> Self::I { - TyInterner { tcx: self.tcx() } + self.tcx() } #[inline] diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 2a862502f38..5a40986307c 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -26,7 +26,7 @@ use rustc_middle::traits::specialization_graph; use rustc_middle::ty::codec::TyEncoder; use rustc_middle::ty::fast_reject::{self, SimplifiedType, TreatParams}; use rustc_middle::ty::query::Providers; -use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt, TyInterner}; +use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt}; use rustc_serialize::{opaque, Encodable, Encoder}; use rustc_session::config::CrateType; use rustc_session::cstore::{ForeignModule, LinkagePreference, NativeLib}; @@ -316,7 +316,7 @@ impl<'a, 'tcx> Encodable> for Span { impl<'a, 'tcx> TyEncoder for EncodeContext<'a, 'tcx> { const CLEAR_CROSS_CRATE: bool = true; - type I = TyInterner<'tcx>; + type I = TyCtxt<'tcx>; fn position(&self) -> usize { self.opaque.position() diff --git a/compiler/rustc_middle/src/mir/interpret/mod.rs b/compiler/rustc_middle/src/mir/interpret/mod.rs index 926afa8ccb6..06cd6a66e39 100644 --- a/compiler/rustc_middle/src/mir/interpret/mod.rs +++ b/compiler/rustc_middle/src/mir/interpret/mod.rs @@ -115,7 +115,7 @@ use rustc_target::abi::Endian; use crate::mir; use crate::ty::codec::{TyDecoder, TyEncoder}; use crate::ty::subst::GenericArgKind; -use crate::ty::{self, Instance, Ty, TyCtxt, TyInterner}; +use crate::ty::{self, Instance, Ty, TyCtxt}; pub use self::error::{ struct_error, CheckInAllocMsg, ErrorHandled, EvalToAllocationRawResult, EvalToConstValueResult, @@ -203,7 +203,7 @@ enum AllocDiscriminant { Static, } -pub fn specialized_encode_alloc_id<'tcx, E: TyEncoder>>( +pub fn specialized_encode_alloc_id<'tcx, E: TyEncoder>>( encoder: &mut E, tcx: TyCtxt<'tcx>, alloc_id: AllocId, @@ -277,7 +277,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>, { // Read the index of the allocation. let idx = usize::try_from(decoder.read_u32()).unwrap(); @@ -305,7 +305,7 @@ impl<'s> AllocDecodingSession<'s> { AllocDiscriminant::Alloc => { // If this is an allocation, we need to reserve an // `AllocId` so we can decode cyclic graphs. - let alloc_id = decoder.interner().tcx.reserve_alloc_id(); + let alloc_id = decoder.interner().reserve_alloc_id(); *entry = State::InProgress(TinyList::new_single(self.session_id), alloc_id); Some(alloc_id) @@ -349,7 +349,7 @@ impl<'s> AllocDecodingSession<'s> { // We already have a reserved `AllocId`. let alloc_id = alloc_id.unwrap(); trace!("decoded alloc {:?}: {:#?}", alloc_id, alloc); - decoder.interner().tcx.set_alloc_id_same_memory(alloc_id, alloc); + decoder.interner().set_alloc_id_same_memory(alloc_id, alloc); alloc_id } AllocDiscriminant::Fn => { @@ -357,7 +357,7 @@ impl<'s> AllocDecodingSession<'s> { trace!("creating fn alloc ID"); let instance = ty::Instance::decode(decoder); trace!("decoded fn alloc instance: {:?}", instance); - let alloc_id = decoder.interner().tcx.create_fn_alloc(instance); + let alloc_id = decoder.interner().create_fn_alloc(instance); alloc_id } AllocDiscriminant::Static => { @@ -365,7 +365,7 @@ impl<'s> AllocDecodingSession<'s> { trace!("creating extern static alloc ID"); let did = >::decode(decoder); trace!("decoded static def-ID: {:?}", did); - let alloc_id = decoder.interner().tcx.create_static_alloc(did); + let alloc_id = decoder.interner().create_static_alloc(did); alloc_id } } diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index 2298cb4098b..1e2d1fbeb4b 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -17,7 +17,7 @@ use crate::traits; use crate::ty::subst::SubstsRef; use crate::ty::{self, AdtDef, Ty}; use rustc_data_structures::fx::FxHashMap; -use rustc_middle::ty::TyInterner; +use rustc_middle::ty::TyCtxt; use rustc_serialize::{Decodable, Encodable}; use rustc_span::Span; pub use rustc_type_ir::{TyDecoder, TyEncoder}; @@ -36,7 +36,7 @@ pub trait EncodableWithShorthand: Copy + Eq + Hash { } #[allow(rustc::usage_of_ty_tykind)] -impl<'tcx, E: TyEncoder>> EncodableWithShorthand for Ty<'tcx> { +impl<'tcx, E: TyEncoder>> EncodableWithShorthand for Ty<'tcx> { type Variant = ty::TyKind<'tcx>; #[inline] @@ -45,9 +45,7 @@ impl<'tcx, E: TyEncoder>> EncodableWithShorthand for Ty< } } -impl<'tcx, E: TyEncoder>> EncodableWithShorthand - for ty::PredicateKind<'tcx> -{ +impl<'tcx, E: TyEncoder>> EncodableWithShorthand for ty::PredicateKind<'tcx> { type Variant = ty::PredicateKind<'tcx>; #[inline] @@ -66,7 +64,7 @@ impl<'tcx, E: TyEncoder>> EncodableWithShorthand /// /// `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>> { fn decode(d: &mut D) -> &'tcx Self; } @@ -77,7 +75,7 @@ pub fn encode_with_shorthand<'tcx, E, T, M>( cache: M, ) -> Result<(), E::Error> where - E: TyEncoder>, + E: TyEncoder>, M: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap, T: EncodableWithShorthand, // The discriminant and shorthand must have the same size. @@ -114,13 +112,13 @@ where Ok(()) } -impl<'tcx, E: TyEncoder>> 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>> Encodable +impl<'tcx, E: TyEncoder>> Encodable for ty::Binder<'tcx, ty::PredicateKind<'tcx>> { fn encode(&self, e: &mut E) -> Result<(), E::Error> { @@ -129,37 +127,37 @@ impl<'tcx, E: TyEncoder>> Encodable } } -impl<'tcx, E: TyEncoder>> 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>> 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>> 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>> 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>> 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>> Encodable for AllocId { +impl<'tcx, E: TyEncoder>> Encodable for AllocId { fn encode(&self, e: &mut E) -> Result<(), E::Error> { e.encode_alloc_id(self) } @@ -168,7 +166,7 @@ impl<'tcx, E: TyEncoder>> Encodable for AllocId { #[inline] fn decode_arena_allocable< 'tcx, - D: TyDecoder>, + D: TyDecoder>, T: ArenaAllocatable<'tcx> + Decodable, >( decoder: &mut D, @@ -176,13 +174,13 @@ fn decode_arena_allocable< where D: TyDecoder, { - decoder.interner().tcx.arena.alloc(Decodable::decode(decoder)) + decoder.interner().arena.alloc(Decodable::decode(decoder)) } #[inline] fn decode_arena_allocable_slice< 'tcx, - D: TyDecoder>, + D: TyDecoder>, T: ArenaAllocatable<'tcx> + Decodable, >( decoder: &mut D, @@ -190,10 +188,10 @@ fn decode_arena_allocable_slice< where D: TyDecoder, { - decoder.interner().tcx.arena.alloc_from_iter( as Decodable>::decode(decoder)) + decoder.interner().arena.alloc_from_iter( as Decodable>::decode(decoder)) } -impl<'tcx, D: TyDecoder>> 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. @@ -206,13 +204,13 @@ impl<'tcx, D: TyDecoder>> Decodable for Ty<'tcx> { decoder.with_position(shorthand, Ty::decode) }) } else { - let tcx = decoder.interner().tcx; + let tcx = decoder.interner(); tcx.mk_ty(rustc_type_ir::TyKind::decode(decoder)) } } } -impl<'tcx, D: TyDecoder>> Decodable +impl<'tcx, D: TyDecoder>> Decodable for ty::Binder<'tcx, ty::PredicateKind<'tcx>> { fn decode(decoder: &mut D) -> ty::Binder<'tcx, ty::PredicateKind<'tcx>> { @@ -233,64 +231,64 @@ impl<'tcx, D: TyDecoder>> Decodable } } -impl<'tcx, D: TyDecoder>> 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.interner().tcx.mk_predicate(predicate_kind) + decoder.interner().mk_predicate(predicate_kind) } } -impl<'tcx, D: TyDecoder>> 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.interner().tcx; + let tcx = decoder.interner(); tcx.mk_substs( (0..len).map::, _>(|_| Decodable::decode(decoder)), ) } } -impl<'tcx, D: TyDecoder>> 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.interner().tcx.mk_place_elems( + let projection = decoder.interner().mk_place_elems( (0..len).map::, _>(|_| Decodable::decode(decoder)), ); mir::Place { local, projection } } } -impl<'tcx, D: TyDecoder>> Decodable for ty::Region<'tcx> { +impl<'tcx, D: TyDecoder>> Decodable for ty::Region<'tcx> { fn decode(decoder: &mut D) -> Self { - decoder.interner().tcx.mk_region(Decodable::decode(decoder)) + decoder.interner().mk_region(Decodable::decode(decoder)) } } -impl<'tcx, D: TyDecoder>> 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.interner().tcx.intern_canonical_var_infos(interned.as_slice()) + decoder.interner().intern_canonical_var_infos(interned.as_slice()) } } -impl<'tcx, D: TyDecoder>> Decodable for AllocId { +impl<'tcx, D: TyDecoder>> Decodable for AllocId { fn decode(decoder: &mut D) -> Self { decoder.decode_alloc_id() } } -impl<'tcx, D: TyDecoder>> Decodable for ty::SymbolName<'tcx> { +impl<'tcx, D: TyDecoder>> Decodable for ty::SymbolName<'tcx> { fn decode(decoder: &mut D) -> Self { - ty::SymbolName::new(decoder.interner().tcx, &decoder.read_str()) + ty::SymbolName::new(decoder.interner(), &decoder.read_str()) } } macro_rules! impl_decodable_via_ref { ($($t:ty),+) => { - $(impl<'tcx, D: TyDecoder>> Decodable for $t { + $(impl<'tcx, D: TyDecoder>> Decodable for $t { fn decode(decoder: &mut D) -> Self { RefDecodable::decode(decoder) } @@ -298,89 +296,86 @@ macro_rules! impl_decodable_via_ref { } } -impl<'tcx, D: TyDecoder>> 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 - .interner() - .tcx - .mk_type_list((0..len).map::, _>(|_| Decodable::decode(decoder))) + decoder.interner().mk_type_list((0..len).map::, _>(|_| Decodable::decode(decoder))) } } -impl<'tcx, D: TyDecoder>> 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.interner().tcx.mk_poly_existential_predicates( + decoder.interner().mk_poly_existential_predicates( (0..len).map::, _>(|_| Decodable::decode(decoder)), ) } } -impl<'tcx, D: TyDecoder>> Decodable for ty::Const<'tcx> { +impl<'tcx, D: TyDecoder>> Decodable for ty::Const<'tcx> { fn decode(decoder: &mut D) -> Self { - decoder.interner().tcx.mk_const(Decodable::decode(decoder)) + decoder.interner().mk_const(Decodable::decode(decoder)) } } -impl<'tcx, D: TyDecoder>> 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.interner().tcx.arena.alloc_from_iter( + decoder.interner().arena.alloc_from_iter( (0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::>(), ) } } -impl<'tcx, D: TyDecoder>> Decodable for ConstAllocation<'tcx> { +impl<'tcx, D: TyDecoder>> Decodable for ConstAllocation<'tcx> { fn decode(decoder: &mut D) -> Self { - decoder.interner().tcx.intern_const_alloc(Decodable::decode(decoder)) + decoder.interner().intern_const_alloc(Decodable::decode(decoder)) } } -impl<'tcx, D: TyDecoder>> Decodable for AdtDef<'tcx> { +impl<'tcx, D: TyDecoder>> Decodable for AdtDef<'tcx> { fn decode(decoder: &mut D) -> Self { - decoder.interner().tcx.intern_adt_def(Decodable::decode(decoder)) + decoder.interner().intern_adt_def(Decodable::decode(decoder)) } } -impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> +impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for [(ty::Predicate<'tcx>, Span)] { fn decode(decoder: &mut D) -> &'tcx Self { - decoder.interner().tcx.arena.alloc_from_iter( + decoder.interner().arena.alloc_from_iter( (0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::>(), ) } } -impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> +impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for [thir::abstract_const::Node<'tcx>] { fn decode(decoder: &mut D) -> &'tcx Self { - decoder.interner().tcx.arena.alloc_from_iter( + decoder.interner().arena.alloc_from_iter( (0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::>(), ) } } -impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> +impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for [thir::abstract_const::NodeId] { fn decode(decoder: &mut D) -> &'tcx Self { - decoder.interner().tcx.arena.alloc_from_iter( + decoder.interner().arena.alloc_from_iter( (0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::>(), ) } } -impl<'tcx, D: TyDecoder>> 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.interner().tcx.mk_bound_variable_kinds( + decoder.interner().mk_bound_variable_kinds( (0..len).map::(|_| Decodable::decode(decoder)), ) } @@ -414,14 +409,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>> 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>> RefDecodable<'tcx, D> for [$ty] { #[inline] fn decode(decoder: &mut D) -> &'tcx Self { decode_arena_allocable_slice(decoder) @@ -443,17 +438,17 @@ 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>> RefDecodable<'tcx, D> for $ty { #[inline] fn decode(decoder: &mut D) -> &'tcx Self { - decoder.interner().tcx.arena.alloc(Decodable::decode(decoder)) + decoder.interner().arena.alloc(Decodable::decode(decoder)) } } - impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for [$ty] { + impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for [$ty] { #[inline] fn decode(decoder: &mut D) -> &'tcx Self { - decoder.interner().tcx.arena.alloc_from_iter( as Decodable>::decode(decoder)) + decoder.interner().arena.alloc_from_iter( as Decodable>::decode(decoder)) } })* }; @@ -512,13 +507,13 @@ macro_rules! implement_ty_decoder { macro_rules! impl_binder_encode_decode { ($($t:ty),+ $(,)?) => { $( - impl<'tcx, E: TyEncoder>> 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>> 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) diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index c1c7e1389cb..a0d92e2a5dd 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -90,12 +90,8 @@ pub trait OnDiskCache<'tcx>: rustc_data_structures::sync::Sync { fn serialize(&self, tcx: TyCtxt<'tcx>, encoder: &mut FileEncoder) -> FileEncodeResult; } -pub struct TyInterner<'tcx> { - pub tcx: TyCtxt<'tcx>, -} - #[allow(rustc::usage_of_ty_tykind)] -impl<'tcx> Interner for TyInterner<'tcx> { +impl<'tcx> Interner for TyCtxt<'tcx> { type AdtDef = ty::AdtDef<'tcx>; type SubstsRef = ty::SubstsRef<'tcx>; type DefId = DefId; @@ -1103,11 +1099,6 @@ pub struct GlobalCtxt<'tcx> { } impl<'tcx> TyCtxt<'tcx> { - #[inline] - pub fn interner(self) -> TyInterner<'tcx> { - TyInterner { tcx: self } - } - /// Expects a body and returns its codegen attributes. /// /// Unlike `codegen_fn_attrs`, this returns `CodegenFnAttrs::EMPTY` for diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 03e8a9cb96c..14e5dc0dd77 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -68,8 +68,8 @@ pub use self::consts::{ pub use self::context::{ tls, CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, CtxtInterners, DelaySpanBugEmitted, FreeRegionInfo, GeneratorDiagnosticData, - GeneratorInteriorTypeCause, GlobalCtxt, Lift, OnDiskCache, TyCtxt, TyInterner, TypeckResults, - UserType, UserTypeAnnotationIndex, + GeneratorInteriorTypeCause, GlobalCtxt, Lift, OnDiskCache, TyCtxt, TypeckResults, UserType, + UserTypeAnnotationIndex, }; pub use self::instance::{Instance, InstanceDef}; pub use self::list::List; @@ -463,30 +463,17 @@ static_assert_size!(WithStableHash>, 56); #[rustc_pass_by_value] pub struct Ty<'tcx>(Interned<'tcx, WithStableHash>>); -static LEAKED_BOOL_TY_ALREADY: std::sync::atomic::AtomicBool = - std::sync::atomic::AtomicBool::new(false); - -/// "Static" bool only used for internal testing. -/// -/// FIXME(rustc_type_ir): This really should be replaced with something that doesn't leak. -/// however, since it's used for testing, it's not _that_ bad. -pub fn leak_bool_ty_for_unit_testing<'tcx>() -> Ty<'tcx> { - use std::sync::atomic::*; - - if LEAKED_BOOL_TY_ALREADY.load(Ordering::Acquire) { - panic!("Can only leak one bool type, since its equality depends on its address"); - } else { - LEAKED_BOOL_TY_ALREADY.store(true, Ordering::Release); - } - - Ty(Interned::new_unchecked(Box::leak(Box::new(WithStableHash { +impl<'tcx> TyCtxt<'tcx> { + /// A "bool" type used in rustc_mir_transform unit tests when we + /// have not spun up a TyCtxt. + pub const BOOL_TY_FOR_UNIT_TESTING: Ty<'tcx> = Ty(Interned::new_unchecked(&WithStableHash { internee: TyS { kind: ty::Bool, flags: TypeFlags::empty(), outer_exclusive_binder: DebruijnIndex::from_usize(0), }, stable_hash: Fingerprint::ZERO, - })))) + })); } impl<'a, 'tcx> HashStable> for TyS<'tcx> { diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 9410166ac20..faeb729c884 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -32,7 +32,7 @@ use rustc_type_ir::TyKind as IrTyKind; // Re-export the `TyKind` from `rustc_type_ir` here for convenience #[rustc_diagnostic_item = "TyKind"] -pub type TyKind<'tcx> = IrTyKind>; +pub type TyKind<'tcx> = IrTyKind>; #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, TyEncodable, TyDecodable)] #[derive(HashStable, TypeFoldable, Lift)] diff --git a/compiler/rustc_middle/src/ty/subst.rs b/compiler/rustc_middle/src/ty/subst.rs index 8494d5c71fe..290485ab5fe 100644 --- a/compiler/rustc_middle/src/ty/subst.rs +++ b/compiler/rustc_middle/src/ty/subst.rs @@ -4,7 +4,7 @@ use crate::mir; use crate::ty::codec::{TyDecoder, TyEncoder}; use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeFolder, TypeVisitor}; use crate::ty::sty::{ClosureSubsts, GeneratorSubsts, InlineConstSubsts}; -use crate::ty::{self, Lift, List, ParamConst, Ty, TyCtxt, TyInterner}; +use crate::ty::{self, Lift, List, ParamConst, Ty, TyCtxt}; use rustc_data_structures::intern::{Interned, WithStableHash}; use rustc_hir::def_id::DefId; @@ -216,13 +216,13 @@ impl<'tcx> TypeFoldable<'tcx> for GenericArg<'tcx> { } } -impl<'tcx, E: TyEncoder>> Encodable for GenericArg<'tcx> { +impl<'tcx, E: TyEncoder>> Encodable for GenericArg<'tcx> { fn encode(&self, e: &mut E) -> Result<(), E::Error> { self.unpack().encode(e) } } -impl<'tcx, D: TyDecoder>> Decodable for GenericArg<'tcx> { +impl<'tcx, D: TyDecoder>> Decodable for GenericArg<'tcx> { fn decode(d: &mut D) -> GenericArg<'tcx> { GenericArgKind::decode(d).pack() } diff --git a/compiler/rustc_mir_transform/src/coverage/tests.rs b/compiler/rustc_mir_transform/src/coverage/tests.rs index 41f004b0c00..213bb6608e1 100644 --- a/compiler/rustc_mir_transform/src/coverage/tests.rs +++ b/compiler/rustc_mir_transform/src/coverage/tests.rs @@ -37,7 +37,7 @@ use rustc_data_structures::graph::WithSuccessors; use rustc_index::vec::{Idx, IndexVec}; use rustc_middle::mir::coverage::CoverageKind; use rustc_middle::mir::*; -use rustc_middle::ty::{self, Ty}; +use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_span::{self, BytePos, Pos, Span, DUMMY_SP}; // All `TEMP_BLOCK` targets should be replaced before calling `to_body() -> mir::Body`. @@ -56,7 +56,7 @@ impl<'tcx> MockBlocks<'tcx> { blocks: IndexVec::new(), dummy_place: Place { local: RETURN_PLACE, projection: ty::List::empty() }, next_local: 0, - bool_ty: ty::leak_bool_ty_for_unit_testing(), + bool_ty: TyCtxt::BOOL_TY_FOR_UNIT_TESTING, } } diff --git a/compiler/rustc_query_impl/src/on_disk_cache.rs b/compiler/rustc_query_impl/src/on_disk_cache.rs index 7dc6921a569..8e4b3269402 100644 --- a/compiler/rustc_query_impl/src/on_disk_cache.rs +++ b/compiler/rustc_query_impl/src/on_disk_cache.rs @@ -11,7 +11,7 @@ use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState}; use rustc_middle::mir::{self, interpret}; use rustc_middle::thir; use rustc_middle::ty::codec::{RefDecodable, TyDecoder, TyEncoder}; -use rustc_middle::ty::{self, Ty, TyCtxt, TyInterner}; +use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_query_system::dep_graph::DepContext; use rustc_query_system::query::{QueryCache, QueryContext, QuerySideEffects}; use rustc_serialize::{ @@ -548,12 +548,12 @@ where } impl<'a, 'tcx> TyDecoder for CacheDecoder<'a, 'tcx> { - type I = TyInterner<'tcx>; + type I = TyCtxt<'tcx>; const CLEAR_CROSS_CRATE: bool = false; #[inline] - fn interner(&self) -> TyInterner<'tcx> { - TyInterner { tcx: self.tcx } + fn interner(&self) -> TyCtxt<'tcx> { + self.tcx } #[inline] @@ -932,7 +932,7 @@ impl<'a, 'tcx, E> TyEncoder for CacheEncoder<'a, 'tcx, E> where E: 'a + OpaqueEncoder, { - type I = TyInterner<'tcx>; + type I = TyCtxt<'tcx>; const CLEAR_CROSS_CRATE: bool = false; fn position(&self) -> usize { diff --git a/compiler/rustc_query_system/src/ich/mod.rs b/compiler/rustc_query_system/src/ich/mod.rs index ddaac1c78a1..0a1c350b2db 100644 --- a/compiler/rustc_query_system/src/ich/mod.rs +++ b/compiler/rustc_query_system/src/ich/mod.rs @@ -1,7 +1,6 @@ //! ICH - Incremental Compilation Hash pub use self::hcx::StableHashingContext; -use rustc_data_structures::stable_hasher::HashStable; use rustc_span::symbol::{sym, Symbol}; mod hcx; @@ -18,124 +17,3 @@ pub const IGNORED_ATTRIBUTES: &[Symbol] = &[ sym::rustc_partition_codegened, sym::rustc_expected_cgu_reuse, ]; - -#[allow(rustc::usage_of_ty_tykind)] -impl<'__ctx, I: rustc_type_ir::Interner> HashStable> - for rustc_type_ir::TyKind -where - I::AdtDef: HashStable>, - I::DefId: HashStable>, - I::SubstsRef: HashStable>, - I::Ty: HashStable>, - I::Const: HashStable>, - I::TypeAndMut: HashStable>, - I::PolyFnSig: HashStable>, - I::ListBinderExistentialPredicate: HashStable>, - I::Region: HashStable>, - I::Movability: HashStable>, - I::Mutability: HashStable>, - I::BinderListTy: HashStable>, - I::ListTy: HashStable>, - I::ProjectionTy: HashStable>, - I::BoundTy: HashStable>, - I::ParamTy: HashStable>, - I::PlaceholderType: HashStable>, - I::InferTy: HashStable>, - I::DelaySpanBugEmitted: HashStable>, -{ - #[inline] - fn hash_stable( - &self, - __hcx: &mut crate::ich::StableHashingContext<'__ctx>, - __hasher: &mut rustc_data_structures::stable_hasher::StableHasher, - ) { - std::mem::discriminant(self).hash_stable(__hcx, __hasher); - use rustc_type_ir::TyKind::*; - match self { - Bool => {} - Char => {} - Int(i) => { - i.hash_stable(__hcx, __hasher); - } - Uint(u) => { - u.hash_stable(__hcx, __hasher); - } - Float(f) => { - f.hash_stable(__hcx, __hasher); - } - Adt(adt, substs) => { - adt.hash_stable(__hcx, __hasher); - substs.hash_stable(__hcx, __hasher); - } - Foreign(def_id) => { - def_id.hash_stable(__hcx, __hasher); - } - Str => {} - Array(t, c) => { - t.hash_stable(__hcx, __hasher); - c.hash_stable(__hcx, __hasher); - } - Slice(t) => { - t.hash_stable(__hcx, __hasher); - } - RawPtr(tam) => { - tam.hash_stable(__hcx, __hasher); - } - Ref(r, t, m) => { - r.hash_stable(__hcx, __hasher); - t.hash_stable(__hcx, __hasher); - m.hash_stable(__hcx, __hasher); - } - FnDef(def_id, substs) => { - def_id.hash_stable(__hcx, __hasher); - substs.hash_stable(__hcx, __hasher); - } - FnPtr(polyfnsig) => { - polyfnsig.hash_stable(__hcx, __hasher); - } - Dynamic(l, r) => { - l.hash_stable(__hcx, __hasher); - r.hash_stable(__hcx, __hasher); - } - Closure(def_id, substs) => { - def_id.hash_stable(__hcx, __hasher); - substs.hash_stable(__hcx, __hasher); - } - Generator(def_id, substs, m) => { - def_id.hash_stable(__hcx, __hasher); - substs.hash_stable(__hcx, __hasher); - m.hash_stable(__hcx, __hasher); - } - GeneratorWitness(b) => { - b.hash_stable(__hcx, __hasher); - } - Never => {} - Tuple(substs) => { - substs.hash_stable(__hcx, __hasher); - } - Projection(p) => { - p.hash_stable(__hcx, __hasher); - } - Opaque(def_id, substs) => { - def_id.hash_stable(__hcx, __hasher); - substs.hash_stable(__hcx, __hasher); - } - Param(p) => { - p.hash_stable(__hcx, __hasher); - } - Bound(d, b) => { - d.hash_stable(__hcx, __hasher); - b.hash_stable(__hcx, __hasher); - } - Placeholder(p) => { - p.hash_stable(__hcx, __hasher); - } - Infer(i) => { - i.hash_stable(__hcx, __hasher); - } - Error(d) => { - d.hash_stable(__hcx, __hasher); - } - } - } -} diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index 766875dd82b..6380001b57c 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -8,7 +8,6 @@ extern crate rustc_macros; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::unify::{EqUnifyValue, UnifyKey}; -//use rustc_serialize::{Decodable, Encodable}; use smallvec::SmallVec; use std::fmt; use std::fmt::Debug; diff --git a/compiler/rustc_type_ir/src/sty.rs b/compiler/rustc_type_ir/src/sty.rs index 33b3a273f44..650c3519b56 100644 --- a/compiler/rustc_type_ir/src/sty.rs +++ b/compiler/rustc_type_ir/src/sty.rs @@ -13,6 +13,7 @@ use crate::UintTy; use self::TyKind::*; +use rustc_data_structures::stable_hasher::HashStable; use rustc_serialize::{Decodable, Encodable}; /// Defines the kinds of types used by the type system. @@ -198,7 +199,10 @@ impl TyKind { } } -fn discriminant(value: &TyKind) -> usize { +// This is manually implemented for `TyKind` because `std::mem::discriminant` +// returns an opaque value that is `PartialEq` but not `PartialOrd` +#[inline] +const fn discriminant(value: &TyKind) -> usize { match value { Bool => 0, Char => 1, @@ -230,6 +234,7 @@ fn discriminant(value: &TyKind) -> usize { } } +// This is manually implemented because a derive would require `I: Clone` impl Clone for TyKind { fn clone(&self) -> Self { match self { @@ -264,6 +269,7 @@ impl Clone for TyKind { } } +// This is manually implemented because a derive would require `I: PartialEq` impl PartialEq for TyKind { #[inline] fn eq(&self, other: &TyKind) -> bool { @@ -324,8 +330,10 @@ impl PartialEq for TyKind { } } +// This is manually implemented because a derive would require `I: Eq` impl Eq for TyKind {} +// This is manually implemented because a derive would require `I: PartialOrd` impl PartialOrd for TyKind { #[inline] fn partial_cmp(&self, other: &TyKind) -> Option { @@ -333,6 +341,7 @@ impl PartialOrd for TyKind { } } +// This is manually implemented because a derive would require `I: Ord` impl Ord for TyKind { #[inline] fn cmp(&self, other: &TyKind) -> Ordering { @@ -430,6 +439,7 @@ impl Ord for TyKind { } } +// This is manually implemented because a derive would require `I: Hash` impl hash::Hash for TyKind { fn hash<__H: hash::Hasher>(&self, state: &mut __H) -> () { match (&*self,) { @@ -541,6 +551,7 @@ impl hash::Hash for TyKind { } } +// This is manually implemented because a derive would require `I: Debug` impl fmt::Debug for TyKind { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match (&*self,) { @@ -678,6 +689,7 @@ impl fmt::Debug for TyKind { } } +// This is manually implemented because a derive would require `I: Encodable` impl Encodable for TyKind where I::DelaySpanBugEmitted: Encodable, @@ -819,6 +831,7 @@ where } } +// This is manually implemented because a derive would require `I: Decodable` impl> Decodable for TyKind where I::DelaySpanBugEmitted: Decodable, @@ -905,3 +918,123 @@ where } } } + +// This is not a derived impl because a derive would require `I: HashStable` +#[allow(rustc::usage_of_ty_tykind)] +impl HashStable for TyKind +where + I::AdtDef: HashStable, + I::DefId: HashStable, + I::SubstsRef: HashStable, + I::Ty: HashStable, + I::Const: HashStable, + I::TypeAndMut: HashStable, + I::PolyFnSig: HashStable, + I::ListBinderExistentialPredicate: HashStable, + I::Region: HashStable, + I::Movability: HashStable, + I::Mutability: HashStable, + I::BinderListTy: HashStable, + I::ListTy: HashStable, + I::ProjectionTy: HashStable, + I::BoundTy: HashStable, + I::ParamTy: HashStable, + I::PlaceholderType: HashStable, + I::InferTy: HashStable, + I::DelaySpanBugEmitted: HashStable, +{ + #[inline] + fn hash_stable( + &self, + __hcx: &mut CTX, + __hasher: &mut rustc_data_structures::stable_hasher::StableHasher, + ) { + std::mem::discriminant(self).hash_stable(__hcx, __hasher); + match self { + Bool => {} + Char => {} + Int(i) => { + i.hash_stable(__hcx, __hasher); + } + Uint(u) => { + u.hash_stable(__hcx, __hasher); + } + Float(f) => { + f.hash_stable(__hcx, __hasher); + } + Adt(adt, substs) => { + adt.hash_stable(__hcx, __hasher); + substs.hash_stable(__hcx, __hasher); + } + Foreign(def_id) => { + def_id.hash_stable(__hcx, __hasher); + } + Str => {} + Array(t, c) => { + t.hash_stable(__hcx, __hasher); + c.hash_stable(__hcx, __hasher); + } + Slice(t) => { + t.hash_stable(__hcx, __hasher); + } + RawPtr(tam) => { + tam.hash_stable(__hcx, __hasher); + } + Ref(r, t, m) => { + r.hash_stable(__hcx, __hasher); + t.hash_stable(__hcx, __hasher); + m.hash_stable(__hcx, __hasher); + } + FnDef(def_id, substs) => { + def_id.hash_stable(__hcx, __hasher); + substs.hash_stable(__hcx, __hasher); + } + FnPtr(polyfnsig) => { + polyfnsig.hash_stable(__hcx, __hasher); + } + Dynamic(l, r) => { + l.hash_stable(__hcx, __hasher); + r.hash_stable(__hcx, __hasher); + } + Closure(def_id, substs) => { + def_id.hash_stable(__hcx, __hasher); + substs.hash_stable(__hcx, __hasher); + } + Generator(def_id, substs, m) => { + def_id.hash_stable(__hcx, __hasher); + substs.hash_stable(__hcx, __hasher); + m.hash_stable(__hcx, __hasher); + } + GeneratorWitness(b) => { + b.hash_stable(__hcx, __hasher); + } + Never => {} + Tuple(substs) => { + substs.hash_stable(__hcx, __hasher); + } + Projection(p) => { + p.hash_stable(__hcx, __hasher); + } + Opaque(def_id, substs) => { + def_id.hash_stable(__hcx, __hasher); + substs.hash_stable(__hcx, __hasher); + } + Param(p) => { + p.hash_stable(__hcx, __hasher); + } + Bound(d, b) => { + d.hash_stable(__hcx, __hasher); + b.hash_stable(__hcx, __hasher); + } + Placeholder(p) => { + p.hash_stable(__hcx, __hasher); + } + Infer(i) => { + i.hash_stable(__hcx, __hasher); + } + Error(d) => { + d.hash_stable(__hcx, __hasher); + } + } + } +} diff --git a/compiler/rustc_typeck/src/check/op.rs b/compiler/rustc_typeck/src/check/op.rs index 6df9ac5f0a4..3ae04706e4b 100644 --- a/compiler/rustc_typeck/src/check/op.rs +++ b/compiler/rustc_typeck/src/check/op.rs @@ -677,8 +677,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { operand_ty: Ty<'tcx>, op: hir::UnOp, ) -> Ty<'tcx> { - use rustc_type_ir::sty::TyKind::*; - assert!(op.is_by_value()); match self.lookup_op_method(operand_ty, None, None, Op::Unary(op, ex.span)) { Ok(method) => {