diff --git a/Cargo.lock b/Cargo.lock index 871ff8f9cdb..3f17f24875d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3676,6 +3676,7 @@ dependencies = [ "rustc_span", "rustc_target", "rustc_trait_selection", + "rustc_type_ir", "tracing", ] @@ -3969,6 +3970,7 @@ dependencies = [ "rustc_span", "rustc_target", "rustc_trait_selection", + "rustc_type_ir", "tracing", "unicode-security", ] @@ -4041,6 +4043,7 @@ dependencies = [ "rustc_session", "rustc_span", "rustc_target", + "rustc_type_ir", "smallvec", "snap", "tracing", @@ -4474,6 +4477,7 @@ dependencies = [ "rustc_span", "rustc_target", "rustc_trait_selection", + "rustc_type_ir", "tracing", ] @@ -4512,6 +4516,7 @@ dependencies = [ "rustc_target", "rustc_trait_selection", "rustc_ty_utils", + "rustc_type_ir", "smallvec", "tracing", ] diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs index d3aea1fd61c..93c7a832afd 100644 --- a/compiler/rustc_borrowck/src/lib.rs +++ b/compiler/rustc_borrowck/src/lib.rs @@ -98,7 +98,9 @@ struct Upvar<'tcx> { by_ref: bool, } -const DEREF_PROJECTION: &[PlaceElem<'_>; 1] = &[ProjectionElem::Deref]; +const fn deref_projection<'tcx>() -> &'tcx [PlaceElem<'tcx>; 1] { + &[ProjectionElem::Deref] +} pub fn provide(providers: &mut Providers) { *providers = Providers { @@ -1443,7 +1445,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { // Thread-locals might be dropped after the function exits // We have to dereference the outer reference because // borrows don't conflict behind shared references. - root_place.projection = DEREF_PROJECTION; + root_place.projection = deref_projection(); (true, true) } else { (false, self.locals_are_invalidated_at_exit) diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index 97d3acb34ce..bbbd1e94514 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -33,7 +33,7 @@ use rustc_middle::mir::{self, GeneratorLayout}; use rustc_middle::ty::layout::LayoutOf; use rustc_middle::ty::layout::TyAndLayout; use rustc_middle::ty::subst::GenericArgKind; -use rustc_middle::ty::{self, AdtKind, Instance, ParamEnv, Ty, TyCtxt, COMMON_VTABLE_ENTRIES}; +use rustc_middle::ty::{self, common_vtable_entries, AdtKind, Instance, ParamEnv, Ty, TyCtxt}; use rustc_session::config::{self, DebugInfo}; use rustc_span::symbol::Symbol; use rustc_span::FileName; @@ -1392,7 +1392,7 @@ fn build_vtable_type_di_node<'ll, 'tcx>( tcx.vtable_entries(trait_ref) } else { - COMMON_VTABLE_ENTRIES + common_vtable_entries() }; // All function pointers are described as opaque pointers. This could be improved in the future diff --git a/compiler/rustc_const_eval/Cargo.toml b/compiler/rustc_const_eval/Cargo.toml index 4ed908a3833..32e8233a041 100644 --- a/compiler/rustc_const_eval/Cargo.toml +++ b/compiler/rustc_const_eval/Cargo.toml @@ -24,3 +24,4 @@ rustc_session = { path = "../rustc_session" } rustc_target = { path = "../rustc_target" } rustc_trait_selection = { path = "../rustc_trait_selection" } rustc_span = { path = "../rustc_span" } +rustc_type_ir = { path = "../rustc_type_ir" } diff --git a/compiler/rustc_const_eval/src/interpret/traits.rs b/compiler/rustc_const_eval/src/interpret/traits.rs index 235938422a8..d5a448a8963 100644 --- a/compiler/rustc_const_eval/src/interpret/traits.rs +++ b/compiler/rustc_const_eval/src/interpret/traits.rs @@ -2,7 +2,7 @@ use std::convert::TryFrom; use rustc_middle::mir::interpret::{InterpResult, Pointer, PointerArithmetic}; use rustc_middle::ty::{ - self, Ty, COMMON_VTABLE_ENTRIES, COMMON_VTABLE_ENTRIES_ALIGN, + self, common_vtable_entries, Ty, COMMON_VTABLE_ENTRIES_ALIGN, COMMON_VTABLE_ENTRIES_DROPINPLACE, COMMON_VTABLE_ENTRIES_SIZE, }; use rustc_target::abi::{Align, Size}; @@ -38,7 +38,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { } /// Resolves the function at the specified slot in the provided - /// vtable. Currently an index of '3' (`COMMON_VTABLE_ENTRIES.len()`) + /// vtable. Currently an index of '3' (`common_vtable_entries().len()`) /// corresponds to the first method declared in the trait of the provided vtable. pub fn get_vtable_slot( &self, @@ -64,7 +64,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { let vtable = self .get_ptr_alloc( vtable, - pointer_size * u64::try_from(COMMON_VTABLE_ENTRIES.len()).unwrap(), + pointer_size * u64::try_from(common_vtable_entries().len()).unwrap(), self.tcx.data_layout.pointer_align.abi, )? .expect("cannot be a ZST"); @@ -99,7 +99,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { let vtable = self .get_ptr_alloc( vtable, - pointer_size * u64::try_from(COMMON_VTABLE_ENTRIES.len()).unwrap(), + pointer_size * u64::try_from(common_vtable_entries().len()).unwrap(), self.tcx.data_layout.pointer_align.abi, )? .expect("cannot be a ZST"); diff --git a/compiler/rustc_lint/Cargo.toml b/compiler/rustc_lint/Cargo.toml index 02f747eeccc..fab60b6f609 100644 --- a/compiler/rustc_lint/Cargo.toml +++ b/compiler/rustc_lint/Cargo.toml @@ -21,3 +21,4 @@ rustc_session = { path = "../rustc_session" } rustc_trait_selection = { path = "../rustc_trait_selection" } rustc_parse_format = { path = "../rustc_parse_format" } rustc_infer = { path = "../rustc_infer" } +rustc_type_ir = { path = "../rustc_type_ir" } diff --git a/compiler/rustc_metadata/Cargo.toml b/compiler/rustc_metadata/Cargo.toml index 59796dd6529..41224e33461 100644 --- a/compiler/rustc_metadata/Cargo.toml +++ b/compiler/rustc_metadata/Cargo.toml @@ -27,3 +27,4 @@ rustc_ast = { path = "../rustc_ast" } rustc_expand = { path = "../rustc_expand" } rustc_span = { path = "../rustc_span" } rustc_session = { path = "../rustc_session" } +rustc_type_ir = { path = "../rustc_type_ir" } diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 7f0b595347f..806db61471a 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, @@ -377,12 +377,13 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> { } } -impl<'a, 'tcx> TyDecoder<'tcx> for DecodeContext<'a, 'tcx> { +impl<'a, 'tcx> TyDecoder for DecodeContext<'a, 'tcx> { const CLEAR_CROSS_CRATE: bool = true; - #[inline] - fn tcx(&self) -> TyCtxt<'tcx> { - self.tcx.expect("missing TyCtxt in DecodeContext") + type I = TyInterner<'tcx>; + + fn interner(&self) -> Self::I { + TyInterner { tcx: self.tcx() } } #[inline] diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 339d2fc0867..2a862502f38 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}; +use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt, TyInterner}; use rustc_serialize::{opaque, Encodable, Encoder}; use rustc_session::config::CrateType; use rustc_session::cstore::{ForeignModule, LinkagePreference, NativeLib}; @@ -313,9 +313,11 @@ impl<'a, 'tcx> Encodable> for Span { } } -impl<'a, 'tcx> TyEncoder<'tcx> for EncodeContext<'a, 'tcx> { +impl<'a, 'tcx> TyEncoder for EncodeContext<'a, 'tcx> { const CLEAR_CROSS_CRATE: bool = true; + type I = TyInterner<'tcx>; + fn position(&self) -> usize { self.opaque.position() } diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index 234649f1056..2298cb4098b 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -17,8 +17,8 @@ use crate::traits; use crate::ty::subst::SubstsRef; use crate::ty::{self, AdtDef, Ty}; use rustc_data_structures::fx::FxHashMap; -use rustc_serialize::{Decodable, Encodable}; use rustc_middle::ty::TyInterner; +use rustc_serialize::{Decodable, Encodable}; use rustc_span::Span; pub use rustc_type_ir::{TyDecoder, TyEncoder}; use std::hash::Hash; @@ -165,25 +165,6 @@ impl<'tcx, E: TyEncoder>> Encodable for AllocId { } } -macro_rules! encodable_via_deref { - ($($t:ty),+) => { - $(impl<'tcx, E: TyEncoder>> Encodable for $t { - fn encode(&self, e: &mut E) -> Result<(), E::Error> { - (**self).encode(e) - } - })* - } -} - -encodable_via_deref! { - &'tcx ty::TypeckResults<'tcx>, - &'tcx traits::ImplSource<'tcx, ()>, - &'tcx mir::Body<'tcx>, - &'tcx mir::UnsafetyCheckResult, - &'tcx mir::BorrowCheckResult<'tcx>, - &'tcx mir::coverage::CodeRegion -} - #[inline] fn decode_arena_allocable< 'tcx, @@ -231,7 +212,9 @@ impl<'tcx, D: TyDecoder>> Decodable for Ty<'tcx> { } } -impl<'tcx, D: TyDecoder>> 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. @@ -318,7 +301,10 @@ macro_rules! impl_decodable_via_ref { 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() + .tcx + .mk_type_list((0..len).map::, _>(|_| Decodable::decode(decoder))) } } @@ -359,7 +345,9 @@ impl<'tcx, D: TyDecoder>> Decodable for AdtDef<'tcx> { } } -impl<'tcx, D: TyDecoder>> 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.interner().tcx.arena.alloc_from_iter( (0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::>(), @@ -367,7 +355,9 @@ impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for [(ty::P } } -impl<'tcx, D: TyDecoder>> 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.interner().tcx.arena.alloc_from_iter( (0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::>(), @@ -375,7 +365,9 @@ impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for [thir:: } } -impl<'tcx, D: TyDecoder>> 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.interner().tcx.arena.alloc_from_iter( (0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::>(), @@ -383,7 +375,9 @@ impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for [thir:: } } -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_bound_variable_kinds( @@ -449,17 +443,17 @@ 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 { + $(impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for $ty { #[inline] fn decode(decoder: &mut D) -> &'tcx Self { - decoder.tcx().arena.alloc(Decodable::decode(decoder)) + decoder.interner().tcx.arena.alloc(Decodable::decode(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 { - decoder.tcx().arena.alloc_from_iter( as Decodable>::decode(decoder)) + decoder.interner().tcx.arena.alloc_from_iter( as Decodable>::decode(decoder)) } })* }; diff --git a/compiler/rustc_middle/src/ty/consts.rs b/compiler/rustc_middle/src/ty/consts.rs index 58a237ee6f8..7af7eb4f5ec 100644 --- a/compiler/rustc_middle/src/ty/consts.rs +++ b/compiler/rustc_middle/src/ty/consts.rs @@ -2,7 +2,7 @@ use crate::mir::interpret::ConstValue; use crate::mir::interpret::{LitToConstInput, Scalar}; use crate::ty::{ self, InlineConstSubsts, InlineConstSubstsParts, InternalSubsts, ParamEnv, ParamEnvAnd, Ty, - TyCtxt, TyInterner, TypeFoldable, + TyCtxt, TypeFoldable, }; use rustc_data_structures::intern::Interned; use rustc_errors::ErrorGuaranteed; @@ -40,14 +40,6 @@ pub struct ConstS<'tcx> { pub val: ConstKind<'tcx>, } -impl<'tcx, S: rustc_type_ir::TyEncoder>> rustc_serialize::Encodable - for &'_ Const<'_> -{ - fn encode(&self, s: &mut S) -> Result<(), S::Error> { - (*self).encode(s) - } -} - #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] static_assert_size!(ConstS<'_>, 48); diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index efe7a54b5bf..a5e6a1b97dd 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -1744,7 +1744,7 @@ macro_rules! nop_lift { impl<'a, 'tcx> Lift<'tcx> for $ty { type Lifted = $lifted; fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option { - if tcx.interners.$set.contains_pointer_to(&InternedInSet(self.0.0)) { + if tcx.interners.$set.contains_pointer_to(&InternedInSet(&*self.0.0)) { // SAFETY: `self` is interned and therefore valid // for the entire lifetime of the `TyCtxt`. Some(unsafe { mem::transmute(self) }) diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 2804912c9ab..a7ecbdff5ae 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, TyInterner, TypeckResults, + UserType, UserTypeAnnotationIndex, }; pub use self::instance::{Instance, InstanceDef}; pub use self::list::List; @@ -78,13 +78,13 @@ pub use self::rvalue_scopes::RvalueScopes; pub use self::sty::BoundRegionKind::*; pub use self::sty::RegionKind::*; pub use self::sty::{ - Binder, BoundRegion, BoundRegionKind, BoundTy, BoundTyKind, BoundVar, BoundVariableKind, - CanonicalPolyFnSig, ClosureSubsts, ClosureSubstsParts, ConstVid, EarlyBinder, EarlyBoundRegion, - ExistentialPredicate, ExistentialProjection, ExistentialTraitRef, FnSig, FreeRegion, GenSig, - GeneratorSubsts, GeneratorSubstsParts, InlineConstSubsts, InlineConstSubstsParts, ParamConst, - ParamTy, PolyExistentialProjection, PolyExistentialTraitRef, PolyFnSig, PolyGenSig, - PolyTraitRef, ProjectionTy, Region, RegionKind, RegionVid, TraitRef, TyKind, TypeAndMut, - UpvarSubsts, VarianceDiagInfo, + Article, Binder, BoundRegion, BoundRegionKind, BoundTy, BoundTyKind, BoundVar, + BoundVariableKind, CanonicalPolyFnSig, ClosureSubsts, ClosureSubstsParts, ConstVid, + EarlyBinder, EarlyBoundRegion, ExistentialPredicate, ExistentialProjection, + ExistentialTraitRef, FnSig, FreeRegion, GenSig, GeneratorSubsts, GeneratorSubstsParts, + InlineConstSubsts, InlineConstSubstsParts, ParamConst, ParamTy, PolyExistentialProjection, + PolyExistentialTraitRef, PolyFnSig, PolyGenSig, PolyTraitRef, ProjectionTy, Region, RegionKind, + RegionVid, TraitRef, TyKind, TypeAndMut, UpvarSubsts, VarianceDiagInfo, }; pub use self::trait_def::TraitDef; @@ -463,16 +463,31 @@ pub(crate) struct TyS<'tcx> { #[rustc_pass_by_value] pub struct Ty<'tcx>(Interned<'tcx, WithStableHash>>); -// Statics only used for internal testing. -pub static BOOL_TY: Ty<'static> = Ty(Interned::new_unchecked(&WithStableHash { - internee: BOOL_TYS, - stable_hash: Fingerprint::ZERO, -})); -const BOOL_TYS: TyS<'static> = TyS { - kind: ty::Bool, - flags: TypeFlags::empty(), - outer_exclusive_binder: DebruijnIndex::from_usize(0), -}; +const 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 { + 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> { #[inline] diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 314d0ef2771..4edad27ee7a 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -954,9 +954,7 @@ impl<'tcx> List>> { } #[inline] - pub fn auto_traits<'a>( - &'a self, - ) -> impl Iterator + rustc_data_structures::captures::Captures<'tcx> + 'a { + pub fn auto_traits<'a>(&'a self) -> impl Iterator + Captures<'tcx> + 'a { self.iter().filter_map(|predicate| match predicate.skip_binder() { ExistentialPredicate::AutoTrait(did) => Some(did), _ => None, diff --git a/compiler/rustc_middle/src/ty/subst.rs b/compiler/rustc_middle/src/ty/subst.rs index e50179face0..8494d5c71fe 100644 --- a/compiler/rustc_middle/src/ty/subst.rs +++ b/compiler/rustc_middle/src/ty/subst.rs @@ -79,17 +79,17 @@ impl<'tcx> GenericArgKind<'tcx> { let (tag, ptr) = match self { GenericArgKind::Lifetime(lt) => { // Ensure we can use the tag bits. - assert_eq!(mem::align_of_val(lt.0.0) & TAG_MASK, 0); + assert_eq!(mem::align_of_val(&*lt.0.0) & TAG_MASK, 0); (REGION_TAG, lt.0.0 as *const ty::RegionKind as usize) } GenericArgKind::Type(ty) => { // Ensure we can use the tag bits. - assert_eq!(mem::align_of_val(ty.0.0) & TAG_MASK, 0); + assert_eq!(mem::align_of_val(&*ty.0.0) & TAG_MASK, 0); (TYPE_TAG, ty.0.0 as *const WithStableHash> as usize) } GenericArgKind::Const(ct) => { // Ensure we can use the tag bits. - assert_eq!(mem::align_of_val(ct.0.0) & TAG_MASK, 0); + assert_eq!(mem::align_of_val(&*ct.0.0) & TAG_MASK, 0); (CONST_TAG, ct.0.0 as *const ty::ConstS<'tcx> as usize) } }; diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index 084c47a1dc4..809e7ce2e74 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -5,9 +5,7 @@ use crate::ty::fold::{FallibleTypeFolder, TypeFolder}; use crate::ty::layout::IntegerExt; use crate::ty::query::TyCtxtAt; use crate::ty::subst::{GenericArgKind, Subst, SubstsRef}; -use crate::ty::{ - self, Const, DebruijnIndex, DefIdTree, List, ReEarlyBound, Ty, TyCtxt, TypeFoldable, -}; +use crate::ty::{self, DefIdTree, Ty, TyCtxt, TypeFoldable}; use rustc_apfloat::Float as _; use rustc_ast as ast; use rustc_attr::{self as attr, SignedInt, UnsignedInt}; @@ -22,7 +20,6 @@ use rustc_macros::HashStable; use rustc_span::{sym, DUMMY_SP}; use rustc_target::abi::{Integer, Size, TargetDataLayout}; use rustc_target::spec::abi::Abi; -use rustc_type_ir::TyKind::*; use smallvec::SmallVec; use std::{fmt, iter}; diff --git a/compiler/rustc_middle/src/ty/vtable.rs b/compiler/rustc_middle/src/ty/vtable.rs index f766cad2b3d..793b5768ad1 100644 --- a/compiler/rustc_middle/src/ty/vtable.rs +++ b/compiler/rustc_middle/src/ty/vtable.rs @@ -36,8 +36,9 @@ impl<'tcx> fmt::Debug for VtblEntry<'tcx> { } } -pub const COMMON_VTABLE_ENTRIES: &[VtblEntry<'_>] = - &[VtblEntry::MetadataDropInPlace, VtblEntry::MetadataSize, VtblEntry::MetadataAlign]; +pub const fn common_vtable_entries<'tcx>() -> &'tcx [VtblEntry<'tcx>] { + &[VtblEntry::MetadataDropInPlace, VtblEntry::MetadataSize, VtblEntry::MetadataAlign] +} pub const COMMON_VTABLE_ENTRIES_DROPINPLACE: usize = 0; pub const COMMON_VTABLE_ENTRIES_SIZE: usize = 1; @@ -57,7 +58,7 @@ pub(super) fn vtable_allocation_provider<'tcx>( tcx.vtable_entries(trait_ref) } else { - COMMON_VTABLE_ENTRIES + common_vtable_entries() }; let layout = tcx diff --git a/compiler/rustc_mir_transform/src/coverage/tests.rs b/compiler/rustc_mir_transform/src/coverage/tests.rs index 4615f9be33f..41f004b0c00 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, BOOL_TY}; +use rustc_middle::ty::{self, Ty}; use rustc_span::{self, BytePos, Pos, Span, DUMMY_SP}; // All `TEMP_BLOCK` targets should be replaced before calling `to_body() -> mir::Body`. @@ -47,6 +47,7 @@ struct MockBlocks<'tcx> { blocks: IndexVec>, dummy_place: Place<'tcx>, next_local: usize, + bool_ty: Ty<'tcx>, } impl<'tcx> MockBlocks<'tcx> { @@ -55,6 +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(), } } @@ -155,7 +157,7 @@ impl<'tcx> MockBlocks<'tcx> { fn switchint(&mut self, some_from_block: Option) -> BasicBlock { let switchint_kind = TerminatorKind::SwitchInt { discr: Operand::Move(Place::from(self.new_temp())), - switch_ty: BOOL_TY, // just a dummy value + switch_ty: self.bool_ty, // just a dummy value targets: SwitchTargets::static_if(0, TEMP_BLOCK, TEMP_BLOCK), }; self.add_block_from(some_from_block, switchint_kind) diff --git a/compiler/rustc_query_impl/src/on_disk_cache.rs b/compiler/rustc_query_impl/src/on_disk_cache.rs index 5282c317fc4..7dc6921a569 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}; +use rustc_middle::ty::{self, Ty, TyCtxt, TyInterner}; use rustc_query_system::dep_graph::DepContext; use rustc_query_system::query::{QueryCache, QueryContext, QuerySideEffects}; use rustc_serialize::{ diff --git a/compiler/rustc_trait_selection/src/traits/mod.rs b/compiler/rustc_trait_selection/src/traits/mod.rs index dcfdff68640..0a0a1296aae 100644 --- a/compiler/rustc_trait_selection/src/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/mod.rs @@ -33,7 +33,7 @@ use rustc_hir::lang_items::LangItem; use rustc_middle::ty::fold::TypeFoldable; use rustc_middle::ty::subst::{InternalSubsts, SubstsRef}; use rustc_middle::ty::{ - self, GenericParamDefKind, ToPredicate, Ty, TyCtxt, VtblEntry, COMMON_VTABLE_ENTRIES, + self, common_vtable_entries, GenericParamDefKind, ToPredicate, Ty, TyCtxt, VtblEntry, }; use rustc_span::{sym, Span}; use smallvec::SmallVec; @@ -695,7 +695,7 @@ fn vtable_entries<'tcx>( let vtable_segment_callback = |segment| -> ControlFlow<()> { match segment { VtblSegment::MetadataDSA => { - entries.extend(COMMON_VTABLE_ENTRIES); + entries.extend(common_vtable_entries()); } VtblSegment::TraitOwnEntries { trait_ref, emit_vptr } => { let existential_trait_ref = trait_ref @@ -785,7 +785,7 @@ fn vtable_trait_first_method_offset<'tcx>( move |segment| { match segment { VtblSegment::MetadataDSA => { - vtable_base += COMMON_VTABLE_ENTRIES.len(); + vtable_base += common_vtable_entries().len(); } VtblSegment::TraitOwnEntries { trait_ref, emit_vptr } => { if tcx.erase_regions(trait_ref) == trait_to_be_found_erased { diff --git a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs index d607f4e7642..548b60d74ba 100644 --- a/compiler/rustc_trait_selection/src/traits/select/confirmation.rs +++ b/compiler/rustc_trait_selection/src/traits/select/confirmation.rs @@ -834,7 +834,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { move |segment| { match segment { VtblSegment::MetadataDSA => { - vptr_offset += ty::COMMON_VTABLE_ENTRIES.len(); + vptr_offset += ty::common_vtable_entries().len(); } VtblSegment::TraitOwnEntries { trait_ref, emit_vptr } => { vptr_offset += util::count_own_vtable_entries(tcx, trait_ref); diff --git a/compiler/rustc_ty_utils/Cargo.toml b/compiler/rustc_ty_utils/Cargo.toml index 78df95e680e..d03d675bfd2 100644 --- a/compiler/rustc_ty_utils/Cargo.toml +++ b/compiler/rustc_ty_utils/Cargo.toml @@ -14,3 +14,4 @@ rustc_span = { path = "../rustc_span" } rustc_session = { path = "../rustc_session" } rustc_target = { path = "../rustc_target" } rustc_trait_selection = { path = "../rustc_trait_selection" } +rustc_type_ir = { path = "../rustc_type_ir" } diff --git a/compiler/rustc_type_ir/src/codec.rs b/compiler/rustc_type_ir/src/codec.rs index e0138f8aad9..09f781fae75 100644 --- a/compiler/rustc_type_ir/src/codec.rs +++ b/compiler/rustc_type_ir/src/codec.rs @@ -47,7 +47,11 @@ pub trait TyDecoder: Decoder { fn position(&self) -> usize; - fn cached_ty_for_shorthand(&mut self, shorthand: usize, or_insert_with: F) -> ::Ty + fn cached_ty_for_shorthand( + &mut self, + shorthand: usize, + or_insert_with: F, + ) -> ::Ty where F: FnOnce(&mut Self) -> ::Ty; @@ -60,4 +64,4 @@ pub trait TyDecoder: Decoder { } fn decode_alloc_id(&mut self) -> ::AllocId; -} \ No newline at end of file +} diff --git a/compiler/rustc_type_ir/src/sty.rs b/compiler/rustc_type_ir/src/sty.rs index 8f43904d96a..0f614967def 100644 --- a/compiler/rustc_type_ir/src/sty.rs +++ b/compiler/rustc_type_ir/src/sty.rs @@ -13,7 +13,7 @@ use rustc_serialize::{Decodable, Encodable}; /// Types written by the user start out as [hir::TyKind](rustc_hir::TyKind) and get /// converted to this representation using `AstConv::ast_ty_to_ty`. #[allow(rustc::usage_of_ty_tykind)] -#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] //#[derive(TyEncodable, TyDecodable)] //#[derive(HashStable)] #[rustc_diagnostic_item = "TyKind"] @@ -186,6 +186,42 @@ pub enum TyKind { Error(I::DelaySpanBugEmitted), } +#[allow(rustc::usage_of_ty_tykind)] +impl Clone for TyKind { + fn clone(&self) -> Self { + use crate::TyKind::*; + match self { + Bool => Bool, + Char => Char, + Int(i) => Int(i.clone()), + Uint(u) => Uint(u.clone()), + Float(f) => Float(f.clone()), + Adt(d, s) => Adt(d.clone(), s.clone()), + Foreign(d) => Foreign(d.clone()), + Str => Str, + Array(t, c) => Array(t.clone(), c.clone()), + Slice(t) => Slice(t.clone()), + RawPtr(t) => RawPtr(t.clone()), + Ref(r, t, m) => Ref(r.clone(), t.clone(), m.clone()), + FnDef(d, s) => FnDef(d.clone(), s.clone()), + FnPtr(s) => FnPtr(s.clone()), + Dynamic(p, r) => Dynamic(p.clone(), r.clone()), + Closure(d, s) => Closure(d.clone(), s.clone()), + Generator(d, s, m) => Generator(d.clone(), s.clone(), m.clone()), + GeneratorWitness(g) => GeneratorWitness(g.clone()), + Never => Never, + Tuple(t) => Tuple(t.clone()), + Projection(p) => Projection(p.clone()), + Opaque(d, s) => Opaque(d.clone(), s.clone()), + Param(p) => Param(p.clone()), + Bound(d, b) => Bound(d.clone(), b.clone()), + Placeholder(p) => Placeholder(p.clone()), + Infer(t) => Infer(t.clone()), + Error(e) => Error(e.clone()), + } + } +} + #[allow(rustc::usage_of_ty_tykind)] impl TyKind { #[inline] @@ -383,9 +419,7 @@ where rustc_serialize::Decodable::decode(__decoder), ), 9 => Slice(rustc_serialize::Decodable::decode(__decoder)), - 10 => RawPtr( - rustc_serialize::Decodable::decode(__decoder), - ), + 10 => RawPtr(rustc_serialize::Decodable::decode(__decoder)), 11 => Ref( rustc_serialize::Decodable::decode(__decoder), rustc_serialize::Decodable::decode(__decoder), @@ -395,9 +429,7 @@ where rustc_serialize::Decodable::decode(__decoder), rustc_serialize::Decodable::decode(__decoder), ), - 13 => FnPtr( - rustc_serialize::Decodable::decode(__decoder), - ), + 13 => FnPtr(rustc_serialize::Decodable::decode(__decoder)), 14 => Dynamic( rustc_serialize::Decodable::decode(__decoder), rustc_serialize::Decodable::decode(__decoder), @@ -411,44 +443,29 @@ where rustc_serialize::Decodable::decode(__decoder), rustc_serialize::Decodable::decode(__decoder), ), - 17 => GeneratorWitness( - rustc_serialize::Decodable::decode(__decoder), - ), + 17 => GeneratorWitness(rustc_serialize::Decodable::decode(__decoder)), 18 => Never, - 19 => Tuple( - rustc_serialize::Decodable::decode(__decoder), - ), - 20 => Projection( - rustc_serialize::Decodable::decode(__decoder), - ), + 19 => Tuple(rustc_serialize::Decodable::decode(__decoder)), + 20 => Projection(rustc_serialize::Decodable::decode(__decoder)), 21 => Opaque( rustc_serialize::Decodable::decode(__decoder), rustc_serialize::Decodable::decode(__decoder), ), - 22 => Param( - rustc_serialize::Decodable::decode(__decoder), - ), + 22 => Param(rustc_serialize::Decodable::decode(__decoder)), 23 => Bound( rustc_serialize::Decodable::decode(__decoder), rustc_serialize::Decodable::decode(__decoder), ), - 24 => Placeholder( - rustc_serialize::Decodable::decode(__decoder), + 24 => Placeholder(rustc_serialize::Decodable::decode(__decoder)), + 25 => Infer(rustc_serialize::Decodable::decode(__decoder)), + 26 => Error(rustc_serialize::Decodable::decode(__decoder)), + _ => panic!( + "{}", + format!( + "invalid enum variant tag while decoding `{}`, expected 0..{}", + "TyKind", 27, + ) ), - 25 => Infer( - rustc_serialize::Decodable::decode(__decoder), - ), - 26 => Error( - rustc_serialize::Decodable::decode(__decoder), - ), - _ => - panic!( - "{}", - format!( - "invalid enum variant tag while decoding `{}`, expected 0..{}", - "TyKind", 27, - ) - ), } } } diff --git a/compiler/rustc_typeck/Cargo.toml b/compiler/rustc_typeck/Cargo.toml index 57930a28a35..c08023ee6a7 100644 --- a/compiler/rustc_typeck/Cargo.toml +++ b/compiler/rustc_typeck/Cargo.toml @@ -29,3 +29,4 @@ rustc_trait_selection = { path = "../rustc_trait_selection" } rustc_ty_utils = { path = "../rustc_ty_utils" } rustc_lint = { path = "../rustc_lint" } rustc_serialize = { path = "../rustc_serialize" } +rustc_type_ir = { path = "../rustc_type_ir" } diff --git a/compiler/rustc_typeck/src/check/demand.rs b/compiler/rustc_typeck/src/check/demand.rs index d0d2841209a..419ccb5a73a 100644 --- a/compiler/rustc_typeck/src/check/demand.rs +++ b/compiler/rustc_typeck/src/check/demand.rs @@ -12,7 +12,7 @@ use rustc_middle::lint::in_external_macro; use rustc_middle::ty::adjustment::AllowTwoPhase; use rustc_middle::ty::error::{ExpectedFound, TypeError}; use rustc_middle::ty::print::with_no_trimmed_paths; -use rustc_middle::ty::{self, AssocItem, Ty, TypeAndMut}; +use rustc_middle::ty::{self, Article, AssocItem, Ty, TypeAndMut}; use rustc_span::symbol::{sym, Symbol}; use rustc_span::{BytePos, Span}; diff --git a/compiler/rustc_typeck/src/check/intrinsicck.rs b/compiler/rustc_typeck/src/check/intrinsicck.rs index 027868be8bb..75508009ad8 100644 --- a/compiler/rustc_typeck/src/check/intrinsicck.rs +++ b/compiler/rustc_typeck/src/check/intrinsicck.rs @@ -4,7 +4,7 @@ use rustc_errors::struct_span_err; use rustc_hir as hir; use rustc_index::vec::Idx; use rustc_middle::ty::layout::{LayoutError, SizeSkeleton}; -use rustc_middle::ty::{self, FloatTy, InferTy, IntTy, Ty, TyCtxt, TypeFoldable, UintTy}; +use rustc_middle::ty::{self, FloatTy, InferTy, IntTy, Ty, TyCtxt, TypeFoldable, UintTy, Article}; use rustc_session::lint; use rustc_span::{Span, Symbol, DUMMY_SP}; use rustc_target::abi::{Pointer, VariantIdx}; diff --git a/compiler/rustc_typeck/src/check/op.rs b/compiler/rustc_typeck/src/check/op.rs index 637f6459525..6df9ac5f0a4 100644 --- a/compiler/rustc_typeck/src/check/op.rs +++ b/compiler/rustc_typeck/src/check/op.rs @@ -10,7 +10,6 @@ use rustc_middle::ty::adjustment::{ Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, }; use rustc_middle::ty::fold::TypeFolder; -use rustc_middle::ty::TyKind::{Adt, Array, Char, FnDef, Never, Ref, Str, Tuple, Uint}; use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable, TypeVisitor}; use rustc_span::source_map::Spanned; use rustc_span::symbol::{sym, Ident}; @@ -18,6 +17,7 @@ use rustc_span::Span; use rustc_trait_selection::infer::InferCtxtExt; use rustc_trait_selection::traits::error_reporting::suggestions::InferCtxtExt as _; use rustc_trait_selection::traits::{FulfillmentError, TraitEngine, TraitEngineExt}; +use rustc_type_ir::sty::TyKind::*; use std::ops::ControlFlow; @@ -677,6 +677,8 @@ 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) => {