From b1fbed3b1c00107342c0aa2831e511fd46332d29 Mon Sep 17 00:00:00 2001 From: Wilco Kusee Date: Sun, 31 Jan 2021 10:32:34 +0100 Subject: [PATCH 1/9] Put mk_predicate in interner trait --- compiler/rustc_metadata/src/rmeta/decoder.rs | 7 ++++- compiler/rustc_middle/src/ty/codec.rs | 7 +++-- compiler/rustc_middle/src/ty/context.rs | 30 +++++++++++++++---- compiler/rustc_middle/src/ty/mod.rs | 2 +- .../src/ty/query/on_disk_cache.rs | 7 ++++- compiler/rustc_type_ir/src/lib.rs | 12 ++++++++ 6 files changed, 55 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 48900fecd3e1b..72b82a8b05efb 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -27,7 +27,7 @@ use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel}; use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState}; use rustc_middle::mir::{self, Body, Promoted}; use rustc_middle::ty::codec::TyDecoder; -use rustc_middle::ty::{self, Ty, TyCtxt, Visibility}; +use rustc_middle::ty::{self, Ty, TyCtxt, TyInterner, Visibility}; use rustc_serialize::{opaque, Decodable, Decoder}; use rustc_session::Session; use rustc_span::hygiene::ExpnDataDecodeMode; @@ -284,6 +284,11 @@ impl<'a, 'tcx> TyDecoder<'tcx> for DecodeContext<'a, 'tcx> { self.tcx.expect("missing TyCtxt in DecodeContext") } + #[inline] + fn interner(&self) -> TyInterner<'tcx> { + self.tcx().interner() + } + #[inline] fn peek_byte(&self) -> u8 { self.opaque.data[self.opaque.position()] diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index 5ec665e913cc5..92345db2da283 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -13,11 +13,12 @@ use crate::mir::{ interpret::{AllocId, Allocation}, }; use crate::ty::subst::SubstsRef; -use crate::ty::{self, List, Ty, TyCtxt}; +use crate::ty::{self, List, Ty, TyCtxt, TyInterner}; use rustc_data_structures::fx::FxHashMap; use rustc_hir::def_id::DefId; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use rustc_span::Span; +use rustc_type_ir::Interner; use std::hash::Hash; use std::intrinsics; use std::marker::DiscriminantKind; @@ -162,6 +163,7 @@ pub trait TyDecoder<'tcx>: Decoder { const CLEAR_CROSS_CRATE: bool; fn tcx(&self) -> TyCtxt<'tcx>; + fn interner(&self) -> TyInterner<'tcx>; fn peek_byte(&self) -> u8; @@ -247,7 +249,8 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Binder<'tcx, ty::PredicateKi impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Predicate<'tcx> { fn decode(decoder: &mut D) -> Result, D::Error> { let predicate_kind = Decodable::decode(decoder)?; - let predicate = decoder.tcx().mk_predicate(predicate_kind); + let predicate = + as Interner>::mk_predicate(decoder.interner(), predicate_kind); Ok(predicate) } } diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 73991436b7b6b..9d1eac72ae150 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -19,11 +19,12 @@ use crate::ty::query::{self, OnDiskCache, TyCtxtAt}; use crate::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, Subst, SubstsRef, UserSubsts}; use crate::ty::TyKind::*; use crate::ty::{ - self, AdtDef, AdtKind, Binder, BindingMode, BoundVar, CanonicalPolyFnSig, Const, ConstVid, - DefIdTree, ExistentialPredicate, FloatTy, FloatVar, FloatVid, GenericParamDefKind, InferConst, - InferTy, IntTy, IntVar, IntVid, List, MainDefinition, ParamConst, ParamTy, PolyFnSig, - Predicate, PredicateInner, PredicateKind, ProjectionTy, Region, RegionKind, ReprOptions, - TraitObjectVisitor, Ty, TyKind, TyS, TyVar, TyVid, TypeAndMut, UintTy, Visibility, + self, codec::TyDecoder, AdtDef, AdtKind, Binder, BindingMode, BoundVar, CanonicalPolyFnSig, + Const, ConstVid, DefIdTree, ExistentialPredicate, FloatTy, FloatVar, FloatVid, + GenericParamDefKind, InferConst, InferTy, IntTy, IntVar, IntVid, List, MainDefinition, + ParamConst, ParamTy, PolyFnSig, Predicate, PredicateInner, PredicateKind, ProjectionTy, Region, + RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyS, TyVar, TyVid, TypeAndMut, UintTy, + Visibility, }; use rustc_ast as ast; use rustc_ast::expand::allocator::AllocatorKind; @@ -50,6 +51,7 @@ use rustc_macros::HashStable; use rustc_middle::mir::FakeReadCause; use rustc_middle::ty::OpaqueTypeKey; use rustc_serialize::opaque::{FileEncodeResult, FileEncoder}; +use rustc_serialize::Decoder; use rustc_session::config::{BorrowckMode, CrateType, OutputFilenames}; use rustc_session::lint::{Level, Lint}; use rustc_session::Session; @@ -59,6 +61,7 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::{Span, DUMMY_SP}; use rustc_target::abi::{Layout, TargetDataLayout, VariantIdx}; use rustc_target::spec::abi; +use rustc_type_ir::Interner; use smallvec::SmallVec; use std::any::Any; @@ -72,6 +75,19 @@ use std::mem; use std::ops::{Bound, Deref}; use std::sync::Arc; +pub struct TyInterner<'tcx> { + tcx: TyCtxt<'tcx>, +} + +impl<'tcx, D: Decoder + TyDecoder<'tcx>> Interner for TyInterner<'tcx> { + type Predicate = Predicate<'tcx>; + type BinderPredicateKind = Binder<'tcx, PredicateKind<'tcx>>; + + fn mk_predicate(self, binder: Binder<'tcx, PredicateKind<'tcx>>) -> Predicate<'tcx> { + self.tcx.mk_predicate(binder) + } +} + /// A type that is not publicly constructable. This prevents people from making [`TyKind::Error`]s /// except through the error-reporting functions on a [`tcx`][TyCtxt]. #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)] @@ -1019,6 +1035,10 @@ pub struct GlobalCtxt<'tcx> { } impl<'tcx> TyCtxt<'tcx> { + pub fn interner(self) -> TyInterner<'tcx> { + TyInterner { tcx: self } + } + pub fn typeck_opt_const_arg( self, def: ty::WithOptConstParam, diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index a2abbec74927c..674fd51ce302a 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -58,7 +58,7 @@ pub use self::consts::{Const, ConstInt, ConstKind, InferConst, ScalarInt, Uneval pub use self::context::{ tls, CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, CtxtInterners, DelaySpanBugEmitted, FreeRegionInfo, GeneratorInteriorTypeCause, GlobalCtxt, - Lift, TyCtxt, TypeckResults, UserType, UserTypeAnnotationIndex, + Lift, TyCtxt, TyInterner, TypeckResults, UserType, UserTypeAnnotationIndex, }; pub use self::instance::{Instance, InstanceDef}; pub use self::list::List; diff --git a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs index ebaef347f4293..c8f5f774ec1b3 100644 --- a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs @@ -3,7 +3,7 @@ use crate::mir::interpret::{AllocDecodingSession, AllocDecodingState}; use crate::mir::{self, interpret}; use crate::ty::codec::{RefDecodable, TyDecoder, TyEncoder}; use crate::ty::context::TyCtxt; -use crate::ty::{self, Ty}; +use crate::ty::{self, Ty, TyInterner}; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet}; use rustc_data_structures::sync::{HashMapExt, Lock, Lrc, OnceCell}; use rustc_data_structures::thin_vec::ThinVec; @@ -679,6 +679,11 @@ impl<'a, 'tcx> TyDecoder<'tcx> for CacheDecoder<'a, 'tcx> { self.tcx } + #[inline] + fn interner(&self) -> TyInterner<'tcx> { + self.tcx.interner() + } + #[inline] fn position(&self) -> usize { self.opaque.position() diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index 2d102127dd9d6..5970abf29fe3b 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -7,9 +7,21 @@ extern crate rustc_macros; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::unify::{EqUnifyValue, UnifyKey}; +use rustc_serialize::{Decodable, Decoder}; use std::fmt; use std::mem::discriminant; +pub trait Interner { + type Predicate: Decodable; + type BinderPredicateKind; + + fn mk_predicate(self, binder: Self::BinderPredicateKind) -> Self::Predicate; +} + +pub trait HasInterner { + type Interner: Interner; +} + bitflags! { /// Flags that we track on types. These flags are propagated upwards /// through the type during type construction, so that we can quickly check From d80cb1e270897b83079209310170592aaca9ad8f Mon Sep 17 00:00:00 2001 From: Wilco Kusee Date: Mon, 1 Feb 2021 17:43:48 +0100 Subject: [PATCH 2/9] Add mk_ty and mk_substs, move InternAs --- Cargo.lock | 1 + compiler/rustc_middle/src/ty/codec.rs | 12 ++-- compiler/rustc_middle/src/ty/context.rs | 96 +++++-------------------- compiler/rustc_type_ir/Cargo.toml | 1 + compiler/rustc_type_ir/src/lib.rs | 89 +++++++++++++++++++++-- 5 files changed, 114 insertions(+), 85 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2f06506eaa458..f91d388d786a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4467,6 +4467,7 @@ dependencies = [ "rustc_index", "rustc_macros", "rustc_serialize", + "smallvec", ] [[package]] diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index 92345db2da283..fdeec217936c5 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -221,8 +221,10 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable for Ty<'tcx> { decoder.with_position(shorthand, Ty::decode) }) } else { - let tcx = decoder.tcx(); - Ok(tcx.mk_ty(ty::TyKind::decode(decoder)?)) + Ok( as Interner>::mk_ty( + decoder.interner(), + ty::TyKind::decode(decoder)?, + )) } } } @@ -258,8 +260,10 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Predicate<'tcx> { impl<'tcx, D: TyDecoder<'tcx>> Decodable for SubstsRef<'tcx> { fn decode(decoder: &mut D) -> Result { let len = decoder.read_usize()?; - let tcx = decoder.tcx(); - tcx.mk_substs((0..len).map(|_| Decodable::decode(decoder))) + Ok( as Interner>::mk_substs( + decoder.interner(), + (0..len).map(|_| Decodable::decode(decoder)), + )?) } } diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 9d1eac72ae150..4b9d0e47f3b4a 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -61,9 +61,8 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::{Span, DUMMY_SP}; use rustc_target::abi::{Layout, TargetDataLayout, VariantIdx}; use rustc_target::spec::abi; -use rustc_type_ir::Interner; +use rustc_type_ir::{InternAs, InternIteratorElement, Interner}; -use smallvec::SmallVec; use std::any::Any; use std::borrow::Borrow; use std::cmp::Ordering; @@ -79,13 +78,31 @@ pub struct TyInterner<'tcx> { tcx: TyCtxt<'tcx>, } +#[allow(rustc::usage_of_ty_tykind)] impl<'tcx, D: Decoder + TyDecoder<'tcx>> Interner for TyInterner<'tcx> { + type GenericArg = GenericArg<'tcx>; + type ListGenericArg = &'tcx List>; + type Predicate = Predicate<'tcx>; type BinderPredicateKind = Binder<'tcx, PredicateKind<'tcx>>; + type Ty = Ty<'tcx>; + type TyKind = TyKind<'tcx>; + fn mk_predicate(self, binder: Binder<'tcx, PredicateKind<'tcx>>) -> Predicate<'tcx> { self.tcx.mk_predicate(binder) } + + fn mk_ty(self, st: TyKind<'tcx>) -> Ty<'tcx> { + self.tcx.mk_ty(st) + } + + fn mk_substs>( + self, + iter: I, + ) -> I::Output { + self.tcx.mk_substs(iter) + } } /// A type that is not publicly constructable. This prevents people from making [`TyKind::Error`]s @@ -2709,81 +2726,6 @@ impl TyCtxtAt<'tcx> { } } -pub trait InternAs { - type Output; - fn intern_with(self, f: F) -> Self::Output - where - F: FnOnce(&T) -> R; -} - -impl InternAs<[T], R> for I -where - E: InternIteratorElement, - I: Iterator, -{ - type Output = E::Output; - fn intern_with(self, f: F) -> Self::Output - where - F: FnOnce(&[T]) -> R, - { - E::intern_with(self, f) - } -} - -pub trait InternIteratorElement: Sized { - type Output; - fn intern_with, F: FnOnce(&[T]) -> R>(iter: I, f: F) -> Self::Output; -} - -impl InternIteratorElement for T { - type Output = R; - fn intern_with, F: FnOnce(&[T]) -> R>(iter: I, f: F) -> Self::Output { - f(&iter.collect::>()) - } -} - -impl<'a, T, R> InternIteratorElement for &'a T -where - T: Clone + 'a, -{ - type Output = R; - fn intern_with, F: FnOnce(&[T]) -> R>(iter: I, f: F) -> Self::Output { - f(&iter.cloned().collect::>()) - } -} - -impl InternIteratorElement for Result { - type Output = Result; - fn intern_with, F: FnOnce(&[T]) -> R>( - mut iter: I, - f: F, - ) -> Self::Output { - // This code is hot enough that it's worth specializing for the most - // common length lists, to avoid the overhead of `SmallVec` creation. - // The match arms are in order of frequency. The 1, 2, and 0 cases are - // typically hit in ~95% of cases. We assume that if the upper and - // lower bounds from `size_hint` agree they are correct. - Ok(match iter.size_hint() { - (1, Some(1)) => { - let t0 = iter.next().unwrap()?; - assert!(iter.next().is_none()); - f(&[t0]) - } - (2, Some(2)) => { - let t0 = iter.next().unwrap()?; - let t1 = iter.next().unwrap()?; - assert!(iter.next().is_none()); - f(&[t0, t1]) - } - (0, Some(0)) => { - assert!(iter.next().is_none()); - f(&[]) - } - _ => f(&iter.collect::, _>>()?), - }) - } -} - // We are comparing types with different invariant lifetimes, so `ptr::eq` // won't work for us. fn ptr_eq(t: *const T, u: *const U) -> bool { diff --git a/compiler/rustc_type_ir/Cargo.toml b/compiler/rustc_type_ir/Cargo.toml index 3f64bd899979f..b35bac47fdd3e 100644 --- a/compiler/rustc_type_ir/Cargo.toml +++ b/compiler/rustc_type_ir/Cargo.toml @@ -13,3 +13,4 @@ rustc_index = { path = "../rustc_index" } rustc_serialize = { path = "../rustc_serialize" } rustc_data_structures = { path = "../rustc_data_structures" } rustc_macros = { path = "../rustc_macros" } +smallvec = { version = "1.0", features = ["union", "may_dangle"] } diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index 5970abf29fe3b..469ad45caa6f7 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -8,18 +8,99 @@ extern crate rustc_macros; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::unify::{EqUnifyValue, UnifyKey}; use rustc_serialize::{Decodable, Decoder}; +use smallvec::SmallVec; use std::fmt; use std::mem::discriminant; pub trait Interner { + type GenericArg; + type ListGenericArg; + type Predicate: Decodable; type BinderPredicateKind; - fn mk_predicate(self, binder: Self::BinderPredicateKind) -> Self::Predicate; -} + type Ty; + type TyKind; -pub trait HasInterner { - type Interner: Interner; + fn mk_predicate(self, binder: Self::BinderPredicateKind) -> Self::Predicate; + fn mk_ty(self, st: Self::TyKind) -> Self::Ty; + fn mk_substs>(self, iter: I) + -> I::Output; +} + +pub trait InternAs { + type Output; + fn intern_with(self, f: F) -> Self::Output + where + F: FnOnce(&T) -> R; +} + +impl InternAs<[T], R> for I +where + E: InternIteratorElement, + I: Iterator, +{ + type Output = E::Output; + fn intern_with(self, f: F) -> Self::Output + where + F: FnOnce(&[T]) -> R, + { + E::intern_with(self, f) + } +} + +pub trait InternIteratorElement: Sized { + type Output; + fn intern_with, F: FnOnce(&[T]) -> R>(iter: I, f: F) -> Self::Output; +} + +impl InternIteratorElement for T { + type Output = R; + fn intern_with, F: FnOnce(&[T]) -> R>(iter: I, f: F) -> Self::Output { + f(&iter.collect::>()) + } +} + +impl<'a, T, R> InternIteratorElement for &'a T +where + T: Clone + 'a, +{ + type Output = R; + fn intern_with, F: FnOnce(&[T]) -> R>(iter: I, f: F) -> Self::Output { + f(&iter.cloned().collect::>()) + } +} + +impl InternIteratorElement for Result { + type Output = Result; + fn intern_with, F: FnOnce(&[T]) -> R>( + mut iter: I, + f: F, + ) -> Self::Output { + // This code is hot enough that it's worth specializing for the most + // common length lists, to avoid the overhead of `SmallVec` creation. + // The match arms are in order of frequency. The 1, 2, and 0 cases are + // typically hit in ~95% of cases. We assume that if the upper and + // lower bounds from `size_hint` agree they are correct. + Ok(match iter.size_hint() { + (1, Some(1)) => { + let t0 = iter.next().unwrap()?; + assert!(iter.next().is_none()); + f(&[t0]) + } + (2, Some(2)) => { + let t0 = iter.next().unwrap()?; + let t1 = iter.next().unwrap()?; + assert!(iter.next().is_none()); + f(&[t0, t1]) + } + (0, Some(0)) => { + assert!(iter.next().is_none()); + f(&[]) + } + _ => f(&iter.collect::, _>>()?), + }) + } } bitflags! { From 196203400994488f0802aa77c10b23532068ef33 Mon Sep 17 00:00:00 2001 From: Zahari Dichev Date: Fri, 5 Feb 2021 15:37:27 +0000 Subject: [PATCH 3/9] move more to the interner Signed-off-by: Zahari Dichev --- .../rustc_middle/src/mir/interpret/mod.rs | 25 +++++-- compiler/rustc_middle/src/ty/codec.rs | 20 ++++-- compiler/rustc_middle/src/ty/context.rs | 68 +++++++++++++++++-- .../src/ty/query/on_disk_cache.rs | 13 ++-- compiler/rustc_type_ir/src/lib.rs | 28 ++++++++ 5 files changed, 132 insertions(+), 22 deletions(-) diff --git a/compiler/rustc_middle/src/mir/interpret/mod.rs b/compiler/rustc_middle/src/mir/interpret/mod.rs index 14bdb0a5a2d50..9d06692b31939 100644 --- a/compiler/rustc_middle/src/mir/interpret/mod.rs +++ b/compiler/rustc_middle/src/mir/interpret/mod.rs @@ -111,11 +111,12 @@ use rustc_macros::HashStable; use rustc_middle::ty::print::with_no_trimmed_paths; use rustc_serialize::{Decodable, Encodable}; use rustc_target::abi::Endian; +use rustc_type_ir::Interner; use crate::mir; use crate::ty::codec::{TyDecoder, TyEncoder}; use crate::ty::subst::GenericArgKind; -use crate::ty::{self, Instance, Ty, TyCtxt}; +use crate::ty::{self, Instance, Ty, TyCtxt, TyInterner}; pub use self::error::{ struct_error, CheckInAllocMsg, ErrorHandled, EvalToAllocationRawResult, EvalToConstValueResult, @@ -302,7 +303,9 @@ 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.tcx().reserve_alloc_id(); + let alloc_id = as Interner>::reserve_alloc_id( + decoder.interner(), + ); *entry = State::InProgress(TinyList::new_single(self.session_id), alloc_id); Some(alloc_id) @@ -346,7 +349,11 @@ impl<'s> AllocDecodingSession<'s> { // We already have a reserved `AllocId`. let alloc_id = alloc_id.unwrap(); trace!("decoded alloc {:?}: {:#?}", alloc_id, alloc); - decoder.tcx().set_alloc_id_same_memory(alloc_id, alloc); + as Interner>::set_alloc_id_same_memory( + decoder.interner(), + alloc_id, + alloc, + ); Ok(alloc_id) } AllocDiscriminant::Fn => { @@ -354,7 +361,10 @@ 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.tcx().create_fn_alloc(instance); + let alloc_id = as Interner>::create_fn_alloc( + decoder.interner(), + instance, + ); Ok(alloc_id) } AllocDiscriminant::Static => { @@ -362,7 +372,10 @@ impl<'s> AllocDecodingSession<'s> { trace!("creating extern static alloc ID"); let did = >::decode(decoder)?; trace!("decoded static def-ID: {:?}", did); - let alloc_id = decoder.tcx().create_static_alloc(did); + let alloc_id = as Interner>::create_static_alloc( + decoder.interner(), + did, + ); Ok(alloc_id) } } @@ -546,7 +559,7 @@ impl<'tcx> TyCtxt<'tcx> { /// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. May be called /// twice for the same `(AllocId, Allocation)` pair. - fn set_alloc_id_same_memory(self, id: AllocId, mem: &'tcx Allocation) { + pub fn set_alloc_id_same_memory(self, id: AllocId, mem: &'tcx Allocation) { self.alloc_map.lock().alloc_map.insert_same(id, GlobalAlloc::Memory(mem)); } } diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index fdeec217936c5..e2147c9828b8d 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -279,7 +279,10 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable for mir::Place<'tcx> { impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Region<'tcx> { fn decode(decoder: &mut D) -> Result { - Ok(decoder.tcx().mk_region(Decodable::decode(decoder)?)) + Ok( as Interner>::mk_region( + decoder.interner(), + Decodable::decode(decoder)?, + )) } } @@ -288,7 +291,10 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable for CanonicalVarInfos<'tcx> { let len = decoder.read_usize()?; let interned: Result>, _> = (0..len).map(|_| Decodable::decode(decoder)).collect(); - Ok(decoder.tcx().intern_canonical_var_infos(interned?.as_slice())) + Ok( as Interner>::intern_canonical_var_infos( + decoder.interner(), + interned?.as_slice(), + )) } } @@ -339,7 +345,10 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::Const<'tcx> { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { - Ok(decoder.tcx().mk_const(Decodable::decode(decoder)?)) + Ok( as Interner>::mk_const( + decoder.interner(), + Decodable::decode(decoder)?, + )) } } @@ -355,7 +364,10 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [ty::ValTree<'tcx>] { impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for Allocation { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { - Ok(decoder.tcx().intern_const_alloc(Decodable::decode(decoder)?)) + Ok( as Interner>::intern_const_alloc( + decoder.interner(), + Decodable::decode(decoder)?, + )) } } diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 4b9d0e47f3b4a..1c733aa4dd9e7 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -11,7 +11,7 @@ use crate::middle; use crate::middle::cstore::{CrateStoreDyn, EncodedMetadata}; use crate::middle::resolve_lifetime::{self, LifetimeScopeForPath, ObjectLifetimeDefault}; use crate::middle::stability; -use crate::mir::interpret::{self, Allocation, ConstValue, Scalar}; +use crate::mir::interpret::{self, AllocId, Allocation, ConstValue, Scalar}; use crate::mir::{Body, Field, Local, Place, PlaceElem, ProjectionKind, Promoted}; use crate::thir::Thir; use crate::traits; @@ -21,10 +21,10 @@ use crate::ty::TyKind::*; use crate::ty::{ self, codec::TyDecoder, AdtDef, AdtKind, Binder, BindingMode, BoundVar, CanonicalPolyFnSig, Const, ConstVid, DefIdTree, ExistentialPredicate, FloatTy, FloatVar, FloatVid, - GenericParamDefKind, InferConst, InferTy, IntTy, IntVar, IntVid, List, MainDefinition, - ParamConst, ParamTy, PolyFnSig, Predicate, PredicateInner, PredicateKind, ProjectionTy, Region, - RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyS, TyVar, TyVid, TypeAndMut, UintTy, - Visibility, + GenericParamDefKind, InferConst, InferTy, Instance, IntTy, IntVar, IntVid, List, + MainDefinition, ParamConst, ParamTy, PolyFnSig, Predicate, PredicateInner, PredicateKind, + ProjectionTy, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyS, TyVar, + TyVid, TypeAndMut, UintTy, Visibility, }; use rustc_ast as ast; use rustc_ast::expand::allocator::AllocatorKind; @@ -39,7 +39,7 @@ use rustc_data_structures::vec_map::VecMap; use rustc_errors::ErrorReported; use rustc_hir as hir; use rustc_hir::def::{DefKind, Res}; -use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, LOCAL_CRATE}; +use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefPathHash, LocalDefId, LOCAL_CRATE}; use rustc_hir::definitions::Definitions; use rustc_hir::intravisit::Visitor; use rustc_hir::lang_items::LangItem; @@ -88,6 +88,24 @@ impl<'tcx, D: Decoder + TyDecoder<'tcx>> Interner for TyInterner<'tcx> { type Ty = Ty<'tcx>; type TyKind = TyKind<'tcx>; + type Allocation = Allocation; + type AllocationId = AllocId; + + type InternedAllocation = &'tcx Allocation; + type Instance = Instance<'tcx>; + + type DefId = DefId; + + type CanonicalVarInfo = CanonicalVarInfo<'tcx>; + type ListCanonicalVarInfo = CanonicalVarInfos<'tcx>; + + type RegionKind = RegionKind; + type Region = Region<'tcx>; + + type Const = Const<'tcx>; + type InternedConst = &'tcx Const<'tcx>; + + type DefPathHash = DefPathHash; fn mk_predicate(self, binder: Binder<'tcx, PredicateKind<'tcx>>) -> Predicate<'tcx> { self.tcx.mk_predicate(binder) @@ -103,6 +121,44 @@ impl<'tcx, D: Decoder + TyDecoder<'tcx>> Interner for TyInterner<'tcx> { ) -> I::Output { self.tcx.mk_substs(iter) } + + fn intern_const_alloc(self, alloc: Self::Allocation) -> Self::InternedAllocation { + self.tcx.interners.allocation.intern(alloc, |alloc| Interned(self.tcx.arena.alloc(alloc))).0 + } + + fn reserve_alloc_id(self) -> Self::AllocationId { + self.tcx.reserve_alloc_id() + } + + fn set_alloc_id_same_memory(self, id: Self::AllocationId, mem: Self::InternedAllocation) { + self.tcx.set_alloc_id_same_memory(id, mem) + } + + fn create_fn_alloc(self, instance: Self::Instance) -> Self::AllocationId { + self.tcx.create_fn_alloc(instance) + } + + fn create_static_alloc(self, static_id: Self::DefId) -> Self::AllocationId { + self.tcx.create_static_alloc(static_id) + } + fn intern_canonical_var_infos( + self, + ts: &[Self::CanonicalVarInfo], + ) -> Self::ListCanonicalVarInfo { + self.tcx.intern_canonical_var_infos(ts) + } + + fn mk_region(self, kind: Self::RegionKind) -> Self::Region { + self.tcx.mk_region(kind) + } + + fn mk_const(self, c: Self::Const) -> Self::InternedConst { + self.tcx.mk_const(c) + } + + fn def_path_hash_to_def_id(self, hash: Self::DefPathHash) -> Option { + self.tcx.on_disk_cache.as_ref().unwrap().def_path_hash_to_def_id(self.tcx, hash) + } } /// A type that is not publicly constructable. This prevents people from making [`TyKind::Error`]s diff --git a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs index c8f5f774ec1b3..70ee28b1a005b 100644 --- a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs @@ -26,6 +26,7 @@ use rustc_span::hygiene::{ use rustc_span::source_map::{SourceMap, StableSourceFileId}; use rustc_span::CachingSourceMapView; use rustc_span::{BytePos, ExpnData, SourceFile, Span, DUMMY_SP}; +use rustc_type_ir::Interner; use std::collections::hash_map::Entry; use std::mem; @@ -839,12 +840,12 @@ impl<'a, 'tcx> Decodable> for DefId { // If we get to this point, then all of the query inputs were green, // which means that the definition with this hash is guaranteed to // still exist in the current compilation session. - Ok(d.tcx() - .on_disk_cache - .as_ref() - .unwrap() - .def_path_hash_to_def_id(d.tcx(), def_path_hash) - .unwrap()) + + let def_if = as Interner>>::def_path_hash_to_def_id( + d.interner(), + def_path_hash, + ); + Ok(def_if.unwrap()) } } diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index 469ad45caa6f7..ac6126335a2dc 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -21,11 +21,39 @@ pub trait Interner { type Ty; type TyKind; + type Allocation; + type AllocationId; + + type InternedAllocation; + type Instance; + type DefId; + + type CanonicalVarInfo; + type ListCanonicalVarInfo; + + type RegionKind; + type Region; + + type Const; + type InternedConst; + type DefPathHash; fn mk_predicate(self, binder: Self::BinderPredicateKind) -> Self::Predicate; fn mk_ty(self, st: Self::TyKind) -> Self::Ty; fn mk_substs>(self, iter: I) -> I::Output; + fn intern_const_alloc(self, alloc: Self::Allocation) -> Self::InternedAllocation; + fn reserve_alloc_id(self) -> Self::AllocationId; + fn set_alloc_id_same_memory(self, id: Self::AllocationId, mem: Self::InternedAllocation); + fn create_fn_alloc(self, instance: Self::Instance) -> Self::AllocationId; + fn create_static_alloc(self, static_id: Self::DefId) -> Self::AllocationId; + fn intern_canonical_var_infos( + self, + ts: &[Self::CanonicalVarInfo], + ) -> Self::ListCanonicalVarInfo; + fn mk_region(self, kind: Self::RegionKind) -> Self::Region; + fn mk_const(self, c: Self::Const) -> Self::InternedConst; + fn def_path_hash_to_def_id(self, hash: Self::DefPathHash) -> Option; } pub trait InternAs { From f96f3d91f1e0c6eba5306e6d1ff03970d88bf09f Mon Sep 17 00:00:00 2001 From: Zahari Dichev Date: Mon, 8 Feb 2021 16:07:39 +0000 Subject: [PATCH 4/9] Remove type param from Interner Signed-off-by: Zahari Dichev --- .../rustc_middle/src/mir/interpret/mod.rs | 22 +++-------- compiler/rustc_middle/src/ty/codec.rs | 37 +++++-------------- compiler/rustc_middle/src/ty/context.rs | 26 +++++++++---- .../src/ty/query/on_disk_cache.rs | 5 +-- compiler/rustc_type_ir/src/lib.rs | 14 +++++-- 5 files changed, 45 insertions(+), 59 deletions(-) diff --git a/compiler/rustc_middle/src/mir/interpret/mod.rs b/compiler/rustc_middle/src/mir/interpret/mod.rs index 9d06692b31939..fe298ac65a8ad 100644 --- a/compiler/rustc_middle/src/mir/interpret/mod.rs +++ b/compiler/rustc_middle/src/mir/interpret/mod.rs @@ -116,7 +116,7 @@ use rustc_type_ir::Interner; 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, @@ -303,9 +303,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 = as Interner>::reserve_alloc_id( - decoder.interner(), - ); + let alloc_id = decoder.interner().reserve_alloc_id(); *entry = State::InProgress(TinyList::new_single(self.session_id), alloc_id); Some(alloc_id) @@ -349,11 +347,7 @@ impl<'s> AllocDecodingSession<'s> { // We already have a reserved `AllocId`. let alloc_id = alloc_id.unwrap(); trace!("decoded alloc {:?}: {:#?}", alloc_id, alloc); - as Interner>::set_alloc_id_same_memory( - decoder.interner(), - alloc_id, - alloc, - ); + decoder.interner().set_alloc_id_same_memory(alloc_id, alloc); Ok(alloc_id) } AllocDiscriminant::Fn => { @@ -361,10 +355,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 = as Interner>::create_fn_alloc( - decoder.interner(), - instance, - ); + let alloc_id = decoder.interner().create_fn_alloc(instance); Ok(alloc_id) } AllocDiscriminant::Static => { @@ -372,10 +363,7 @@ impl<'s> AllocDecodingSession<'s> { trace!("creating extern static alloc ID"); let did = >::decode(decoder)?; trace!("decoded static def-ID: {:?}", did); - let alloc_id = as Interner>::create_static_alloc( - decoder.interner(), - did, - ); + let alloc_id = decoder.interner().create_static_alloc(did); Ok(alloc_id) } } diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index e2147c9828b8d..1f805dec049be 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -221,10 +221,7 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable for Ty<'tcx> { decoder.with_position(shorthand, Ty::decode) }) } else { - Ok( as Interner>::mk_ty( - decoder.interner(), - ty::TyKind::decode(decoder)?, - )) + Ok(decoder.interner().mk_ty(ty::TyKind::decode(decoder)?)) } } } @@ -251,8 +248,7 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Binder<'tcx, ty::PredicateKi impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Predicate<'tcx> { fn decode(decoder: &mut D) -> Result, D::Error> { let predicate_kind = Decodable::decode(decoder)?; - let predicate = - as Interner>::mk_predicate(decoder.interner(), predicate_kind); + let predicate = decoder.interner().mk_predicate(predicate_kind); Ok(predicate) } } @@ -260,10 +256,7 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Predicate<'tcx> { impl<'tcx, D: TyDecoder<'tcx>> Decodable for SubstsRef<'tcx> { fn decode(decoder: &mut D) -> Result { let len = decoder.read_usize()?; - Ok( as Interner>::mk_substs( - decoder.interner(), - (0..len).map(|_| Decodable::decode(decoder)), - )?) + Ok(decoder.interner().mk_substs((0..len).map(|_| Decodable::decode(decoder)))?) } } @@ -279,10 +272,7 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable for mir::Place<'tcx> { impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Region<'tcx> { fn decode(decoder: &mut D) -> Result { - Ok( as Interner>::mk_region( - decoder.interner(), - Decodable::decode(decoder)?, - )) + Ok(decoder.interner().mk_region(Decodable::decode(decoder)?)) } } @@ -291,10 +281,7 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable for CanonicalVarInfos<'tcx> { let len = decoder.read_usize()?; let interned: Result>, _> = (0..len).map(|_| Decodable::decode(decoder)).collect(); - Ok( as Interner>::intern_canonical_var_infos( - decoder.interner(), - interned?.as_slice(), - )) + Ok(decoder.interner().intern_canonical_var_infos(interned?.as_slice())) } } @@ -339,16 +326,15 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { let len = decoder.read_usize()?; - decoder.tcx().mk_poly_existential_predicates((0..len).map(|_| Decodable::decode(decoder))) + Ok(decoder + .interner() + .mk_poly_existential_predicates((0..len).map(|_| Decodable::decode(decoder)))?) } } impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::Const<'tcx> { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { - Ok( as Interner>::mk_const( - decoder.interner(), - Decodable::decode(decoder)?, - )) + Ok(decoder.interner().mk_const(Decodable::decode(decoder)?)) } } @@ -364,10 +350,7 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [ty::ValTree<'tcx>] { impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for Allocation { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { - Ok( as Interner>::intern_const_alloc( - decoder.interner(), - Decodable::decode(decoder)?, - )) + Ok(decoder.interner().intern_const_alloc(Decodable::decode(decoder)?)) } } diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 1c733aa4dd9e7..00bfa23106bbd 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -19,12 +19,11 @@ use crate::ty::query::{self, OnDiskCache, TyCtxtAt}; use crate::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, Subst, SubstsRef, UserSubsts}; use crate::ty::TyKind::*; use crate::ty::{ - self, codec::TyDecoder, AdtDef, AdtKind, Binder, BindingMode, BoundVar, CanonicalPolyFnSig, - Const, ConstVid, DefIdTree, ExistentialPredicate, FloatTy, FloatVar, FloatVid, - GenericParamDefKind, InferConst, InferTy, Instance, IntTy, IntVar, IntVid, List, - MainDefinition, ParamConst, ParamTy, PolyFnSig, Predicate, PredicateInner, PredicateKind, - ProjectionTy, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyS, TyVar, - TyVid, TypeAndMut, UintTy, Visibility, + self, AdtDef, AdtKind, Binder, BindingMode, BoundVar, CanonicalPolyFnSig, Const, ConstVid, + DefIdTree, ExistentialPredicate, FloatTy, FloatVar, FloatVid, GenericParamDefKind, InferConst, + InferTy, Instance, IntTy, IntVar, IntVid, List, MainDefinition, ParamConst, ParamTy, PolyFnSig, + Predicate, PredicateInner, PredicateKind, ProjectionTy, Region, RegionKind, ReprOptions, + TraitObjectVisitor, Ty, TyKind, TyS, TyVar, TyVid, TypeAndMut, UintTy, Visibility, }; use rustc_ast as ast; use rustc_ast::expand::allocator::AllocatorKind; @@ -51,7 +50,6 @@ use rustc_macros::HashStable; use rustc_middle::mir::FakeReadCause; use rustc_middle::ty::OpaqueTypeKey; use rustc_serialize::opaque::{FileEncodeResult, FileEncoder}; -use rustc_serialize::Decoder; use rustc_session::config::{BorrowckMode, CrateType, OutputFilenames}; use rustc_session::lint::{Level, Lint}; use rustc_session::Session; @@ -79,10 +77,13 @@ pub struct TyInterner<'tcx> { } #[allow(rustc::usage_of_ty_tykind)] -impl<'tcx, D: Decoder + TyDecoder<'tcx>> Interner for TyInterner<'tcx> { +impl<'tcx> Interner for TyInterner<'tcx> { type GenericArg = GenericArg<'tcx>; type ListGenericArg = &'tcx List>; + type ExistentialPredicate = ty::Binder<'tcx, ExistentialPredicate<'tcx>>; + type ListExistentialPredicate = &'tcx List>>; + type Predicate = Predicate<'tcx>; type BinderPredicateKind = Binder<'tcx, PredicateKind<'tcx>>; @@ -122,6 +123,15 @@ impl<'tcx, D: Decoder + TyDecoder<'tcx>> Interner for TyInterner<'tcx> { self.tcx.mk_substs(iter) } + fn mk_poly_existential_predicates< + I: InternAs<[Self::ExistentialPredicate], Self::ListExistentialPredicate>, + >( + self, + iter: I, + ) -> I::Output { + self.tcx.mk_poly_existential_predicates(iter) + } + fn intern_const_alloc(self, alloc: Self::Allocation) -> Self::InternedAllocation { self.tcx.interners.allocation.intern(alloc, |alloc| Interned(self.tcx.arena.alloc(alloc))).0 } diff --git a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs index 70ee28b1a005b..b54b1505352b3 100644 --- a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs @@ -841,10 +841,7 @@ impl<'a, 'tcx> Decodable> for DefId { // which means that the definition with this hash is guaranteed to // still exist in the current compilation session. - let def_if = as Interner>>::def_path_hash_to_def_id( - d.interner(), - def_path_hash, - ); + let def_if = d.interner().def_path_hash_to_def_id(def_path_hash); Ok(def_if.unwrap()) } } diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index ac6126335a2dc..bc7e3e5cf8c1f 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -7,16 +7,18 @@ extern crate rustc_macros; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::unify::{EqUnifyValue, UnifyKey}; -use rustc_serialize::{Decodable, Decoder}; use smallvec::SmallVec; use std::fmt; use std::mem::discriminant; -pub trait Interner { +pub trait Interner { type GenericArg; type ListGenericArg; - type Predicate: Decodable; + type ExistentialPredicate; + type ListExistentialPredicate; + + type Predicate; type BinderPredicateKind; type Ty; @@ -51,6 +53,12 @@ pub trait Interner { self, ts: &[Self::CanonicalVarInfo], ) -> Self::ListCanonicalVarInfo; + fn mk_poly_existential_predicates< + I: InternAs<[Self::ExistentialPredicate], Self::ListExistentialPredicate>, + >( + self, + iter: I, + ) -> I::Output; fn mk_region(self, kind: Self::RegionKind) -> Self::Region; fn mk_const(self, c: Self::Const) -> Self::InternedConst; fn def_path_hash_to_def_id(self, hash: Self::DefPathHash) -> Option; From 9b05459825fa9eaf0bfc64d4f04a2c8382030b18 Mon Sep 17 00:00:00 2001 From: Zahari Dichev Date: Sat, 20 Feb 2021 14:42:16 +0000 Subject: [PATCH 5/9] remove `tcx()` --- compiler/rustc_metadata/src/rmeta/decoder.rs | 19 +- compiler/rustc_middle/src/arena.rs | 18 +- compiler/rustc_middle/src/ty/codec.rs | 107 +++---- compiler/rustc_middle/src/ty/context.rs | 284 ++++++++++++++++-- .../src/ty/query/on_disk_cache.rs | 16 +- compiler/rustc_type_ir/src/lib.rs | 180 ++++++++++- 6 files changed, 503 insertions(+), 121 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 72b82a8b05efb..bcd031d1562b8 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -27,7 +27,7 @@ use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel}; use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState}; use rustc_middle::mir::{self, Body, Promoted}; use rustc_middle::ty::codec::TyDecoder; -use rustc_middle::ty::{self, Ty, TyCtxt, TyInterner, Visibility}; +use rustc_middle::ty::{self, Interner, Ty, TyCtxt, TyInterner, Visibility}; use rustc_serialize::{opaque, Decodable, Decoder}; use rustc_session::Session; use rustc_span::hygiene::ExpnDataDecodeMode; @@ -244,10 +244,6 @@ impl<'a: 'x, 'tcx: 'x, 'x, T: Decodable>> Lazy<[T]> { } impl<'a, 'tcx> DecodeContext<'a, 'tcx> { - fn tcx(&self) -> TyCtxt<'tcx> { - self.tcx.expect("missing TyCtxt in DecodeContext") - } - fn cdata(&self) -> CrateMetadataRef<'a> { self.cdata.expect("missing CrateMetadata in DecodeContext") } @@ -279,14 +275,9 @@ impl<'a, 'tcx> DecodeContext<'a, 'tcx> { impl<'a, 'tcx> TyDecoder<'tcx> for DecodeContext<'a, 'tcx> { const CLEAR_CROSS_CRATE: bool = true; - #[inline] - fn tcx(&self) -> TyCtxt<'tcx> { - self.tcx.expect("missing TyCtxt in DecodeContext") - } - #[inline] fn interner(&self) -> TyInterner<'tcx> { - self.tcx().interner() + self.tcx.expect("missing TyCtxt in DecodeContext").interner() } #[inline] @@ -307,16 +298,16 @@ impl<'a, 'tcx> TyDecoder<'tcx> for DecodeContext<'a, 'tcx> { where F: FnOnce(&mut Self) -> Result, Self::Error>, { - let tcx = self.tcx(); + let mut interner = self.interner(); let key = ty::CReaderCacheKey { cnum: Some(self.cdata().cnum), pos: shorthand }; - if let Some(&ty) = tcx.ty_rcache.borrow().get(&key) { + if let Some(ty) = interner.get_cached_ty(key) { return Ok(ty); } let ty = or_insert_with(self)?; - tcx.ty_rcache.borrow_mut().insert(key, ty); + interner.insert_cached_ty(key, ty); Ok(ty) } diff --git a/compiler/rustc_middle/src/arena.rs b/compiler/rustc_middle/src/arena.rs index a89d00e26ac19..513b8274ec955 100644 --- a/compiler/rustc_middle/src/arena.rs +++ b/compiler/rustc_middle/src/arena.rs @@ -16,7 +16,7 @@ macro_rules! arena_types { [] adt_def: rustc_middle::ty::AdtDef, [] steal_thir: rustc_data_structures::steal::Steal>, [] steal_mir: rustc_data_structures::steal::Steal>, - [decode] mir: rustc_middle::mir::Body<$tcx>, + [] mir: rustc_middle::mir::Body<$tcx>, [] steal_promoted: rustc_data_structures::steal::Steal< rustc_index::vec::IndexVec< @@ -24,16 +24,16 @@ macro_rules! arena_types { rustc_middle::mir::Body<$tcx> > >, - [decode] promoted: + [] promoted: rustc_index::vec::IndexVec< rustc_middle::mir::Promoted, rustc_middle::mir::Body<$tcx> >, - [decode] typeck_results: rustc_middle::ty::TypeckResults<$tcx>, - [decode] borrowck_result: + [] typeck_results: rustc_middle::ty::TypeckResults<$tcx>, + [] borrowck_result: rustc_middle::mir::BorrowCheckResult<$tcx>, - [decode] unsafety_check_result: rustc_middle::mir::UnsafetyCheckResult, - [decode] code_region: rustc_middle::mir::coverage::CodeRegion, + [] unsafety_check_result: rustc_middle::mir::UnsafetyCheckResult, + [] code_region: rustc_middle::mir::coverage::CodeRegion, [] const_allocs: rustc_middle::mir::interpret::Allocation, // Required for the incremental on-disk cache [few] mir_keys: rustc_hir::def_id::DefIdSet, @@ -100,11 +100,11 @@ macro_rules! arena_types { // Note that this deliberately duplicates items in the `rustc_hir::arena`, // since we need to allocate this type on both the `rustc_hir` arena // (during lowering) and the `librustc_middle` arena (for decoding MIR) - [decode] asm_template: rustc_ast::InlineAsmTemplatePiece, + [] asm_template: rustc_ast::InlineAsmTemplatePiece, // This is used to decode the &'tcx [Span] for InlineAsm's line_spans. - [decode] span: rustc_span::Span, - [decode] used_trait_imports: rustc_data_structures::fx::FxHashSet, + [] span: rustc_span::Span, + [] used_trait_imports: rustc_data_structures::fx::FxHashSet, ], $tcx); ) } diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index 1f805dec049be..05f145e0bf5f2 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -6,14 +6,13 @@ // The functionality in here is shared between persisting to crate metadata and // persisting to incr. comp. caches. -use crate::arena::ArenaAllocatable; use crate::infer::canonical::{CanonicalVarInfo, CanonicalVarInfos}; use crate::mir::{ self, interpret::{AllocId, Allocation}, }; use crate::ty::subst::SubstsRef; -use crate::ty::{self, List, Ty, TyCtxt, TyInterner}; +use crate::ty::{self, List, Ty, TyInterner}; use rustc_data_structures::fx::FxHashMap; use rustc_hir::def_id::DefId; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; @@ -188,26 +187,6 @@ pub trait TyDecoder<'tcx>: Decoder { fn decode_alloc_id(&mut self) -> Result; } -#[inline] -fn decode_arena_allocable<'tcx, D, T: ArenaAllocatable<'tcx> + Decodable>( - decoder: &mut D, -) -> Result<&'tcx T, D::Error> -where - D: TyDecoder<'tcx>, -{ - Ok(decoder.tcx().arena.alloc(Decodable::decode(decoder)?)) -} - -#[inline] -fn decode_arena_allocable_slice<'tcx, D, T: ArenaAllocatable<'tcx> + Decodable>( - decoder: &mut D, -) -> Result<&'tcx [T], D::Error> -where - D: TyDecoder<'tcx>, -{ - Ok(decoder.tcx().arena.alloc_from_iter( as Decodable>::decode(decoder)?)) -} - impl<'tcx, D: TyDecoder<'tcx>> Decodable for Ty<'tcx> { #[allow(rustc::usage_of_ty_tykind)] fn decode(decoder: &mut D) -> Result, D::Error> { @@ -265,7 +244,7 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable for mir::Place<'tcx> { let local: mir::Local = Decodable::decode(decoder)?; let len = decoder.read_usize()?; let projection: &'tcx List> = - decoder.tcx().mk_place_elems((0..len).map(|_| Decodable::decode(decoder)))?; + decoder.interner().mk_place_elems((0..len).map(|_| Decodable::decode(decoder)))?; Ok(mir::Place { local, projection }) } } @@ -293,7 +272,7 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable for AllocId { impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::SymbolName<'tcx> { fn decode(decoder: &mut D) -> Result { - Ok(ty::SymbolName::new(decoder.tcx(), &decoder.read_str()?)) + Ok(decoder.interner().mk_symbol_name(&decoder.read_str()?)) } } @@ -310,14 +289,14 @@ macro_rules! impl_decodable_via_ref { impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::AdtDef { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { let def_id = >::decode(decoder)?; - Ok(decoder.tcx().adt_def(def_id)) + Ok(decoder.interner().adt_def(def_id)) } } impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List> { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { let len = decoder.read_usize()?; - decoder.tcx().mk_type_list((0..len).map(|_| Decodable::decode(decoder))) + Ok(decoder.interner().mk_type_list((0..len).map(|_| Decodable::decode(decoder)))?) } } @@ -356,7 +335,7 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for Allocation { impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [(ty::Predicate<'tcx>, Span)] { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { - Ok(decoder.tcx().arena.alloc_from_iter( + Ok(decoder.interner().alloc_predicate_span_from_iter( (0..decoder.read_usize()?) .map(|_| Decodable::decode(decoder)) .collect::, _>>()?, @@ -366,7 +345,7 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [(ty::Predicate<'tcx>, impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [mir::abstract_const::Node<'tcx>] { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { - Ok(decoder.tcx().arena.alloc_from_iter( + Ok(decoder.interner().alloc_node_from_iter( (0..decoder.read_usize()?) .map(|_| Decodable::decode(decoder)) .collect::, _>>()?, @@ -376,7 +355,7 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [mir::abstract_const::N impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [mir::abstract_const::NodeId] { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { - Ok(decoder.tcx().arena.alloc_from_iter( + Ok(decoder.interner().alloc_node_id_from_iter( (0..decoder.read_usize()?) .map(|_| Decodable::decode(decoder)) .collect::, _>>()?, @@ -387,7 +366,9 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [mir::abstract_const::N impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { let len = decoder.read_usize()?; - Ok(decoder.tcx().mk_bound_variable_kinds((0..len).map(|_| Decodable::decode(decoder)))?) + Ok(decoder + .interner() + .mk_bound_variable_kinds((0..len).map(|_| Decodable::decode(decoder)))?) } } @@ -415,39 +396,59 @@ macro_rules! __impl_decoder_methods { } } -macro_rules! impl_arena_allocatable_decoder { - ([]$args:tt) => {}; - ([decode $(, $attrs:ident)*] - [[$name:ident: $ty:ty], $tcx:lifetime]) => { - impl<$tcx, D: TyDecoder<$tcx>> RefDecodable<$tcx, D> for $ty { +macro_rules! impl_ty_decoder_arena_type { + ($ty:ty, $alloc_method:ident, $alloc_slice_method:ident) => { + impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for $ty { #[inline] - fn decode(decoder: &mut D) -> Result<&$tcx Self, D::Error> { - decode_arena_allocable(decoder) + fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { + Ok(decoder.interner().$alloc_method(Decodable::decode(decoder)?)) } } - impl<$tcx, D: TyDecoder<$tcx>> RefDecodable<$tcx, D> for [$ty] { + impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [$ty] { #[inline] - fn decode(decoder: &mut D) -> Result<&$tcx Self, D::Error> { - decode_arena_allocable_slice(decoder) + fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { + Ok(decoder + .interner() + .$alloc_slice_method( as Decodable>::decode(decoder)?)) } } }; - ([$ignore:ident $(, $attrs:ident)*]$args:tt) => { - impl_arena_allocatable_decoder!([$($attrs),*]$args); - }; -} - -macro_rules! impl_arena_allocatable_decoders { - ([], [$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) => { - $( - impl_arena_allocatable_decoder!($a [[$name: $ty], $tcx]); - )* - } } -rustc_hir::arena_types!(impl_arena_allocatable_decoders, [], 'tcx); -arena_types!(impl_arena_allocatable_decoders, [], 'tcx); +impl_ty_decoder_arena_type!(rustc_middle::mir::Body<'tcx>, alloc_mir, alloc_mir_from_iter); +impl_ty_decoder_arena_type!( + rustc_middle::ty::TypeckResults<'tcx>, + alloc_type_check_results, + alloc_type_check_results_from_iter +); +impl_ty_decoder_arena_type!( + rustc_middle::mir::BorrowCheckResult<'tcx>, + alloc_borrowck_result, + alloc_borrowck_result_from_iter +); +impl_ty_decoder_arena_type!( + rustc_middle::mir::UnsafetyCheckResult, + alloc_unsafety_check_result, + alloc_unsafety_check_result_from_iter +); +impl_ty_decoder_arena_type!( + rustc_middle::mir::coverage::CodeRegion, + alloc_code_region, + alloc_code_region_from_iter +); +impl_ty_decoder_arena_type!( + rustc_ast::InlineAsmTemplatePiece, + alloc_asm_template, + alloc_asm_template_from_iter +); +impl_ty_decoder_arena_type!(rustc_span::Span, alloc_span, alloc_span_from_iter); +impl_ty_decoder_arena_type!( + rustc_data_structures::fx::FxHashSet, + alloc_used_trait_imports, + alloc_used_trait_imports_from_iter +); +impl_ty_decoder_arena_type!(rustc_index::vec::IndexVec>, alloc_promoted, alloc_promoted_from_iter); #[macro_export] macro_rules! implement_ty_decoder { diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 00bfa23106bbd..07cf0633709d8 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -11,19 +11,25 @@ use crate::middle; use crate::middle::cstore::{CrateStoreDyn, EncodedMetadata}; use crate::middle::resolve_lifetime::{self, LifetimeScopeForPath, ObjectLifetimeDefault}; use crate::middle::stability; +use crate::mir::abstract_const::{Node as ConstNode, NodeId}; +use crate::mir::coverage::CodeRegion; use crate::mir::interpret::{self, AllocId, Allocation, ConstValue, Scalar}; -use crate::mir::{Body, Field, Local, Place, PlaceElem, ProjectionKind, Promoted}; +use crate::mir::{ + Body, BorrowCheckResult, Field, Local, Place, PlaceElem, ProjectionKind, Promoted, + UnsafetyCheckResult, +}; use crate::thir::Thir; use crate::traits; use crate::ty::query::{self, OnDiskCache, TyCtxtAt}; use crate::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, Subst, SubstsRef, UserSubsts}; use crate::ty::TyKind::*; use crate::ty::{ - self, AdtDef, AdtKind, Binder, BindingMode, BoundVar, CanonicalPolyFnSig, Const, ConstVid, - DefIdTree, ExistentialPredicate, FloatTy, FloatVar, FloatVid, GenericParamDefKind, InferConst, - InferTy, Instance, IntTy, IntVar, IntVid, List, MainDefinition, ParamConst, ParamTy, PolyFnSig, - Predicate, PredicateInner, PredicateKind, ProjectionTy, Region, RegionKind, ReprOptions, - TraitObjectVisitor, Ty, TyKind, TyS, TyVar, TyVid, TypeAndMut, UintTy, Visibility, + self, AdtDef, AdtKind, Binder, BindingMode, BoundVar, BoundVariableKind, CanonicalPolyFnSig, + Const, ConstVid, DefIdTree, ExistentialPredicate, FloatTy, FloatVar, FloatVid, + GenericParamDefKind, InferConst, InferTy, Instance, IntTy, IntVar, IntVid, List, + MainDefinition, ParamConst, ParamTy, PolyFnSig, Predicate, PredicateInner, PredicateKind, + ProjectionTy, Region, RegionKind, ReprOptions, SymbolName, TraitObjectVisitor, Ty, TyKind, TyS, + TyVar, TyVid, TypeAndMut, UintTy, Visibility, }; use rustc_ast as ast; use rustc_ast::expand::allocator::AllocatorKind; @@ -33,7 +39,7 @@ use rustc_data_structures::profiling::SelfProfilerRef; use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::steal::Steal; -use rustc_data_structures::sync::{self, Lock, Lrc, WorkerLocal}; +use rustc_data_structures::sync::{self, HashMapExt, Lock, Lrc, WorkerLocal}; use rustc_data_structures::vec_map::VecMap; use rustc_errors::ErrorReported; use rustc_hir as hir; @@ -88,14 +94,16 @@ impl<'tcx> Interner for TyInterner<'tcx> { type BinderPredicateKind = Binder<'tcx, PredicateKind<'tcx>>; type Ty = Ty<'tcx>; + type ListType = &'tcx List>; type TyKind = TyKind<'tcx>; - type Allocation = Allocation; - type AllocationId = AllocId; + type TypeKey = ty::CReaderCacheKey; + type AllocationId = AllocId; + type Allocation = Allocation; type InternedAllocation = &'tcx Allocation; - type Instance = Instance<'tcx>; type DefId = DefId; + type Instance = Instance<'tcx>; type CanonicalVarInfo = CanonicalVarInfo<'tcx>; type ListCanonicalVarInfo = CanonicalVarInfos<'tcx>; @@ -106,9 +114,214 @@ impl<'tcx> Interner for TyInterner<'tcx> { type Const = Const<'tcx>; type InternedConst = &'tcx Const<'tcx>; + type BoundVariableKind = BoundVariableKind; + type ListBoundVariableKind = &'tcx List; + + type PlaceElem = PlaceElem<'tcx>; + type ListPlaceElem = &'tcx List>; + type DefPathHash = DefPathHash; + type AdtDef = &'tcx ty::AdtDef; + + type SymbolName = SymbolName<'tcx>; + + type Mir = Body<'tcx>; + type AllocatedMir = &'tcx mut Body<'tcx>; + type AllocatedMirSlice = &'tcx mut [Body<'tcx>]; + + type Promoted = IndexVec>; + type AllocatedPromoted = &'tcx mut IndexVec>; + type AllocatedPromotedSlice = &'tcx mut [IndexVec>]; + + type TypeCheckResults = TypeckResults<'tcx>; + type AllocatedTypeCheckResults = &'tcx mut TypeckResults<'tcx>; + type AllocatedTypeCheckResultsSlice = &'tcx mut [TypeckResults<'tcx>]; + + type BorrowCheckResult = BorrowCheckResult<'tcx>; + type AllocatedBorrowCheckResult = &'tcx mut BorrowCheckResult<'tcx>; + type AllocatedBorrowCheckResultSlice = &'tcx mut [BorrowCheckResult<'tcx>]; + + type CodeRegion = CodeRegion; + type AllocatedCodeRegion = &'tcx mut CodeRegion; + type AllocatedCodeRegionSlice = &'tcx mut [CodeRegion]; + + type UnsafetyCheckResult = UnsafetyCheckResult; + type AllocatedUnsafetyCheckResult = &'tcx mut UnsafetyCheckResult; + type AllocatedUnsafetyCheckResultSlice = &'tcx mut [UnsafetyCheckResult]; + + type Span = Span; + type AllocatedSpan = &'tcx mut Span; + type AllocatedSpanSlice = &'tcx mut [Span]; + + type UsedTraitsImports = FxHashSet; + type AllocatedUsedTraitsImports = &'tcx mut FxHashSet; + type AllocatedUsedTraitsImportsSlice = &'tcx mut [FxHashSet]; + + type AsmTemplate = ast::InlineAsmTemplatePiece; + type AllocatedAsmTemplate = &'tcx mut ast::InlineAsmTemplatePiece; + type AllocatedAsmTemplateSlice = &'tcx mut [ast::InlineAsmTemplatePiece]; + + type PredicateSpan = (ty::Predicate<'tcx>, Span); + type AllocatedPredicateSpanSlice = &'tcx mut [(ty::Predicate<'tcx>, Span)]; + + type Node = ConstNode<'tcx>; + type AllocatedNodeSlice = &'tcx mut [ConstNode<'tcx>]; + + type NodeId = NodeId; + type AllocatedNodeIdSlice = &'tcx mut [NodeId]; + + fn alloc_predicate_span_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedPredicateSpanSlice { + self.tcx.arena.alloc_from_iter(iter) + } + + fn alloc_node_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedNodeSlice { + self.tcx.arena.alloc_from_iter(iter) + } + + fn alloc_node_id_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedNodeIdSlice { + self.tcx.arena.alloc_from_iter(iter) + } + + fn alloc_span(self, value: Self::Span) -> Self::AllocatedSpan { + self.tcx.arena.alloc::<_, Self::Span>(value) + } + + fn alloc_span_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedSpanSlice { + self.tcx.arena.alloc_from_iter(iter) + } + + fn alloc_promoted(self, value: Self::Promoted) -> Self::AllocatedPromoted { + self.tcx.arena.alloc(value) + } + + fn alloc_promoted_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedPromotedSlice { + self.tcx.arena.alloc_from_iter(iter) + } + + fn alloc_borrowck_result( + self, + value: Self::BorrowCheckResult, + ) -> Self::AllocatedBorrowCheckResult { + self.tcx.arena.alloc(value) + } + + fn alloc_borrowck_result_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedBorrowCheckResultSlice { + self.tcx.arena.alloc_from_iter(iter) + } + + fn alloc_type_check_results( + self, + value: Self::TypeCheckResults, + ) -> Self::AllocatedTypeCheckResults { + self.tcx.arena.alloc(value) + } + + fn alloc_type_check_results_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedTypeCheckResultsSlice { + self.tcx.arena.alloc_from_iter(iter) + } + + fn alloc_mir(self, value: Self::Mir) -> Self::AllocatedMir { + self.tcx.arena.alloc(value) + } + + fn alloc_mir_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedMirSlice { + self.tcx.arena.alloc_from_iter(iter) + } + + fn alloc_code_region(self, value: Self::CodeRegion) -> Self::AllocatedCodeRegion { + self.tcx.arena.alloc(value) + } + + fn alloc_code_region_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedCodeRegionSlice { + self.tcx.arena.alloc_from_iter(iter) + } + + fn alloc_unsafety_check_result( + self, + value: Self::UnsafetyCheckResult, + ) -> Self::AllocatedUnsafetyCheckResult { + self.tcx.arena.alloc(value) + } - fn mk_predicate(self, binder: Binder<'tcx, PredicateKind<'tcx>>) -> Predicate<'tcx> { + fn alloc_unsafety_check_result_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedUnsafetyCheckResultSlice { + self.tcx.arena.alloc_from_iter(iter) + } + + fn alloc_used_trait_imports( + self, + value: Self::UsedTraitsImports, + ) -> Self::AllocatedUsedTraitsImports { + self.tcx.arena.alloc(value) + } + + fn alloc_used_trait_imports_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedUsedTraitsImportsSlice { + self.tcx.arena.alloc_from_iter(iter) + } + + fn alloc_asm_template(self, value: Self::AsmTemplate) -> Self::AllocatedAsmTemplate { + self.tcx.arena.alloc(value) + } + + fn alloc_asm_template_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedAsmTemplateSlice { + self.tcx.arena.alloc_from_iter(iter) + } + + fn mk_symbol_name(self, name: &str) -> Self::SymbolName { + SymbolName::new(self.tcx, name) + } + + fn get_cached_ty(&self, k: Self::TypeKey) -> Option { + self.tcx.ty_rcache.borrow().get(&k).map(|x| x.to_owned()) + } + fn insert_same_cached_ty(&mut self, key: Self::TypeKey, value: Self::Ty) { + self.tcx.ty_rcache.borrow_mut().insert_same(key, value); + } + + fn insert_cached_ty(&mut self, key: Self::TypeKey, value: Self::Ty) -> Option { + self.tcx.ty_rcache.borrow_mut().insert(key, value) + } + + fn adt_def(self, def_id: Self::DefId) -> Self::AdtDef { + self.tcx.adt_def(def_id) + } + + fn mk_predicate(self, binder: Self::BinderPredicateKind) -> Self::Predicate { self.tcx.mk_predicate(binder) } @@ -132,25 +345,38 @@ impl<'tcx> Interner for TyInterner<'tcx> { self.tcx.mk_poly_existential_predicates(iter) } - fn intern_const_alloc(self, alloc: Self::Allocation) -> Self::InternedAllocation { - self.tcx.interners.allocation.intern(alloc, |alloc| Interned(self.tcx.arena.alloc(alloc))).0 + fn mk_type_list>(self, iter: I) -> I::Output { + iter.intern_with(|xs| self.tcx.intern_type_list(xs)) } - fn reserve_alloc_id(self) -> Self::AllocationId { - self.tcx.reserve_alloc_id() + fn mk_place_elems>( + self, + iter: I, + ) -> I::Output { + iter.intern_with(|xs| self.tcx.intern_place_elems(xs)) } - fn set_alloc_id_same_memory(self, id: Self::AllocationId, mem: Self::InternedAllocation) { - self.tcx.set_alloc_id_same_memory(id, mem) + fn mk_region(self, kind: Self::RegionKind) -> Self::Region { + self.tcx.mk_region(kind) } - fn create_fn_alloc(self, instance: Self::Instance) -> Self::AllocationId { - self.tcx.create_fn_alloc(instance) + fn mk_const(self, c: Self::Const) -> Self::InternedConst { + self.tcx.mk_const(c) } - fn create_static_alloc(self, static_id: Self::DefId) -> Self::AllocationId { - self.tcx.create_static_alloc(static_id) + fn mk_bound_variable_kinds< + I: InternAs<[Self::BoundVariableKind], Self::ListBoundVariableKind>, + >( + self, + iter: I, + ) -> I::Output { + self.tcx.mk_bound_variable_kinds(iter) + } + + fn intern_const_alloc(self, alloc: Self::Allocation) -> Self::InternedAllocation { + self.tcx.interners.allocation.intern(alloc, |alloc| Interned(self.tcx.arena.alloc(alloc))).0 } + fn intern_canonical_var_infos( self, ts: &[Self::CanonicalVarInfo], @@ -158,12 +384,20 @@ impl<'tcx> Interner for TyInterner<'tcx> { self.tcx.intern_canonical_var_infos(ts) } - fn mk_region(self, kind: Self::RegionKind) -> Self::Region { - self.tcx.mk_region(kind) + fn reserve_alloc_id(self) -> Self::AllocationId { + self.tcx.reserve_alloc_id() } - fn mk_const(self, c: Self::Const) -> Self::InternedConst { - self.tcx.mk_const(c) + fn set_alloc_id_same_memory(self, id: Self::AllocationId, mem: Self::InternedAllocation) { + self.tcx.set_alloc_id_same_memory(id, mem) + } + + fn create_fn_alloc(self, instance: Self::Instance) -> Self::AllocationId { + self.tcx.create_fn_alloc(instance) + } + + fn create_static_alloc(self, static_id: Self::DefId) -> Self::AllocationId { + self.tcx.create_static_alloc(static_id) } fn def_path_hash_to_def_id(self, hash: Self::DefPathHash) -> Option { diff --git a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs index b54b1505352b3..02c2fded444d4 100644 --- a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs @@ -5,7 +5,7 @@ use crate::ty::codec::{RefDecodable, TyDecoder, TyEncoder}; use crate::ty::context::TyCtxt; use crate::ty::{self, Ty, TyInterner}; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet}; -use rustc_data_structures::sync::{HashMapExt, Lock, Lrc, OnceCell}; +use rustc_data_structures::sync::{Lock, Lrc, OnceCell}; use rustc_data_structures::thin_vec::ThinVec; use rustc_data_structures::unhash::UnhashMap; use rustc_errors::Diagnostic; @@ -675,10 +675,10 @@ where impl<'a, 'tcx> TyDecoder<'tcx> for CacheDecoder<'a, 'tcx> { const CLEAR_CROSS_CRATE: bool = false; - #[inline] - fn tcx(&self) -> TyCtxt<'tcx> { - self.tcx - } + // #[inline] + // fn tcx(&self) -> TyCtxt<'tcx> { + // self.tcx + // } #[inline] fn interner(&self) -> TyInterner<'tcx> { @@ -703,17 +703,17 @@ impl<'a, 'tcx> TyDecoder<'tcx> for CacheDecoder<'a, 'tcx> { where F: FnOnce(&mut Self) -> Result, Self::Error>, { - let tcx = self.tcx(); + let mut interner = self.interner(); let cache_key = ty::CReaderCacheKey { cnum: None, pos: shorthand }; - if let Some(&ty) = tcx.ty_rcache.borrow().get(&cache_key) { + if let Some(ty) = interner.get_cached_ty(cache_key) { return Ok(ty); } let ty = or_insert_with(self)?; // This may overwrite the entry, but it should overwrite with the same value. - tcx.ty_rcache.borrow_mut().insert_same(cache_key, ty); + interner.insert_same_cached_ty(cache_key, ty); Ok(ty) } diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index bc7e3e5cf8c1f..e7455e0cbad3e 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -9,6 +9,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::unify::{EqUnifyValue, UnifyKey}; use smallvec::SmallVec; use std::fmt; +use std::iter::IntoIterator; use std::mem::discriminant; pub trait Interner { @@ -22,13 +23,16 @@ pub trait Interner { type BinderPredicateKind; type Ty; + type ListType; type TyKind; - type Allocation; - type AllocationId; + type TypeKey; + type AllocationId; + type Allocation; type InternedAllocation; - type Instance; + type DefId; + type Instance; type CanonicalVarInfo; type ListCanonicalVarInfo; @@ -38,30 +42,182 @@ pub trait Interner { type Const; type InternedConst; + + type BoundVariableKind; + type ListBoundVariableKind; + + type PlaceElem; + type ListPlaceElem; + type DefPathHash; + type AdtDef; + + type SymbolName; + + type Mir; + type AllocatedMir; + type AllocatedMirSlice; + + type Promoted; + type AllocatedPromoted; + type AllocatedPromotedSlice; + + type TypeCheckResults; + type AllocatedTypeCheckResults; + type AllocatedTypeCheckResultsSlice; + + type BorrowCheckResult; + type AllocatedBorrowCheckResult; + type AllocatedBorrowCheckResultSlice; + + type CodeRegion; + type AllocatedCodeRegion; + type AllocatedCodeRegionSlice; + + type UnsafetyCheckResult; + type AllocatedUnsafetyCheckResult; + type AllocatedUnsafetyCheckResultSlice; + + type Span; + type AllocatedSpan; + type AllocatedSpanSlice; + + type UsedTraitsImports; + type AllocatedUsedTraitsImports; + type AllocatedUsedTraitsImportsSlice; + + type AsmTemplate; + type AllocatedAsmTemplate; + type AllocatedAsmTemplateSlice; + + type PredicateSpan; + type AllocatedPredicateSpanSlice; + + type Node; + type AllocatedNodeSlice; + + type NodeId; + type AllocatedNodeIdSlice; + + fn alloc_mir(self, value: Self::Mir) -> Self::AllocatedMir; + fn alloc_mir_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedMirSlice; + + fn alloc_promoted(self, value: Self::Promoted) -> Self::AllocatedPromoted; + fn alloc_promoted_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedPromotedSlice; + + fn alloc_type_check_results( + self, + value: Self::TypeCheckResults, + ) -> Self::AllocatedTypeCheckResults; + fn alloc_type_check_results_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedTypeCheckResultsSlice; + + fn alloc_borrowck_result( + self, + value: Self::BorrowCheckResult, + ) -> Self::AllocatedBorrowCheckResult; + fn alloc_borrowck_result_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedBorrowCheckResultSlice; + + fn alloc_unsafety_check_result( + self, + value: Self::UnsafetyCheckResult, + ) -> Self::AllocatedUnsafetyCheckResult; + fn alloc_unsafety_check_result_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedUnsafetyCheckResultSlice; + + fn alloc_code_region(self, value: Self::CodeRegion) -> Self::AllocatedCodeRegion; + fn alloc_code_region_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedCodeRegionSlice; + + fn alloc_span(self, value: Self::Span) -> Self::AllocatedSpan; + fn alloc_span_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedSpanSlice; + + fn alloc_used_trait_imports( + self, + value: Self::UsedTraitsImports, + ) -> Self::AllocatedUsedTraitsImports; + fn alloc_used_trait_imports_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedUsedTraitsImportsSlice; + + fn alloc_asm_template(self, value: Self::AsmTemplate) -> Self::AllocatedAsmTemplate; + fn alloc_asm_template_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedAsmTemplateSlice; + fn alloc_predicate_span_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedPredicateSpanSlice; + fn alloc_node_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedNodeSlice; + fn alloc_node_id_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedNodeIdSlice; + + fn get_cached_ty(&self, k: Self::TypeKey) -> Option; + fn insert_same_cached_ty(&mut self, key: Self::TypeKey, value: Self::Ty); + fn insert_cached_ty(&mut self, key: Self::TypeKey, value: Self::Ty) -> Option; + fn adt_def(self, def_id: Self::DefId) -> Self::AdtDef; + fn mk_symbol_name(self, name: &str) -> Self::SymbolName; fn mk_predicate(self, binder: Self::BinderPredicateKind) -> Self::Predicate; fn mk_ty(self, st: Self::TyKind) -> Self::Ty; fn mk_substs>(self, iter: I) -> I::Output; - fn intern_const_alloc(self, alloc: Self::Allocation) -> Self::InternedAllocation; - fn reserve_alloc_id(self) -> Self::AllocationId; - fn set_alloc_id_same_memory(self, id: Self::AllocationId, mem: Self::InternedAllocation); - fn create_fn_alloc(self, instance: Self::Instance) -> Self::AllocationId; - fn create_static_alloc(self, static_id: Self::DefId) -> Self::AllocationId; - fn intern_canonical_var_infos( - self, - ts: &[Self::CanonicalVarInfo], - ) -> Self::ListCanonicalVarInfo; fn mk_poly_existential_predicates< I: InternAs<[Self::ExistentialPredicate], Self::ListExistentialPredicate>, >( self, iter: I, ) -> I::Output; + fn mk_type_list>(self, iter: I) -> I::Output; + fn mk_place_elems>( + self, + iter: I, + ) -> I::Output; fn mk_region(self, kind: Self::RegionKind) -> Self::Region; fn mk_const(self, c: Self::Const) -> Self::InternedConst; + fn mk_bound_variable_kinds< + I: InternAs<[Self::BoundVariableKind], Self::ListBoundVariableKind>, + >( + self, + iter: I, + ) -> I::Output; + fn intern_const_alloc(self, alloc: Self::Allocation) -> Self::InternedAllocation; + fn intern_canonical_var_infos( + self, + ts: &[Self::CanonicalVarInfo], + ) -> Self::ListCanonicalVarInfo; + fn reserve_alloc_id(self) -> Self::AllocationId; + fn set_alloc_id_same_memory(self, id: Self::AllocationId, mem: Self::InternedAllocation); + fn create_fn_alloc(self, instance: Self::Instance) -> Self::AllocationId; + fn create_static_alloc(self, static_id: Self::DefId) -> Self::AllocationId; fn def_path_hash_to_def_id(self, hash: Self::DefPathHash) -> Option; + + // arena methods } pub trait InternAs { From 7a35638533f9d6cc2aee919c58d46492c63674a9 Mon Sep 17 00:00:00 2001 From: Zahari Dichev Date: Fri, 12 Mar 2021 14:19:15 +0000 Subject: [PATCH 6/9] compiles Signed-off-by: Zahari Dichev --- compiler/rustc_arena/src/lib.rs | 8 ++++---- compiler/rustc_ast/src/attr/mod.rs | 10 ++++------ compiler/rustc_middle/src/ty/codec.rs | 1 - compiler/rustc_middle/src/ty/context.rs | 2 +- compiler/rustc_middle/src/ty/query/on_disk_cache.rs | 5 ----- 5 files changed, 9 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_arena/src/lib.rs b/compiler/rustc_arena/src/lib.rs index c3e4945c4464c..940023eed8a71 100644 --- a/compiler/rustc_arena/src/lib.rs +++ b/compiler/rustc_arena/src/lib.rs @@ -713,10 +713,10 @@ macro_rules! declare_arena { self.dropless.alloc_slice(value) } - pub fn alloc_from_iter<'a, T: ArenaAllocatable<'tcx, U>, U>( - &'a self, - iter: impl ::std::iter::IntoIterator, - ) -> &'a mut [T] { + pub fn alloc_from_iter, U, V: IntoIterator>( + &self, + iter: V, + ) -> &mut [T] { T::allocate_from_iter(self, iter) } } diff --git a/compiler/rustc_ast/src/attr/mod.rs b/compiler/rustc_ast/src/attr/mod.rs index 41121d095f3ed..a1654807c8f04 100644 --- a/compiler/rustc_ast/src/attr/mod.rs +++ b/compiler/rustc_ast/src/attr/mod.rs @@ -460,12 +460,10 @@ impl MetaItemKind { fn token_trees_and_spacings(&self, span: Span) -> Vec { match *self { MetaItemKind::Word => vec![], - MetaItemKind::NameValue(ref lit) => { - vec![ - TokenTree::token(token::Eq, span).into(), - TokenTree::Token(lit.to_token()).into(), - ] - } + MetaItemKind::NameValue(ref lit) => vec![ + TokenTree::token(token::Eq, span).into(), + TokenTree::Token(lit.to_token()).into(), + ], MetaItemKind::List(ref list) => { let mut tokens = Vec::new(); for (i, item) in list.iter().enumerate() { diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index 05f145e0bf5f2..d7e56280538dd 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -161,7 +161,6 @@ encodable_via_deref! { pub trait TyDecoder<'tcx>: Decoder { const CLEAR_CROSS_CRATE: bool; - fn tcx(&self) -> TyCtxt<'tcx>; fn interner(&self) -> TyInterner<'tcx>; fn peek_byte(&self) -> u8; diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 07cf0633709d8..aaa40f5c219ad 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -199,7 +199,7 @@ impl<'tcx> Interner for TyInterner<'tcx> { self, iter: impl IntoIterator, ) -> Self::AllocatedSpanSlice { - self.tcx.arena.alloc_from_iter(iter) + self.tcx.arena.alloc_from_iter::(iter) } fn alloc_promoted(self, value: Self::Promoted) -> Self::AllocatedPromoted { diff --git a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs index 02c2fded444d4..aa71d7480f78a 100644 --- a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs @@ -675,11 +675,6 @@ where impl<'a, 'tcx> TyDecoder<'tcx> for CacheDecoder<'a, 'tcx> { const CLEAR_CROSS_CRATE: bool = false; - // #[inline] - // fn tcx(&self) -> TyCtxt<'tcx> { - // self.tcx - // } - #[inline] fn interner(&self) -> TyInterner<'tcx> { self.tcx.interner() From 9966dc95d841f1061127471d9822a8f4a75a9a15 Mon Sep 17 00:00:00 2001 From: LeSeulArtichaut Date: Fri, 18 Jun 2021 16:23:40 +0200 Subject: [PATCH 7/9] Add allocation for `ValTree` --- compiler/rustc_middle/src/ty/codec.rs | 2 +- compiler/rustc_middle/src/ty/context.rs | 14 +++++++++++++- compiler/rustc_type_ir/src/lib.rs | 7 +++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index d7e56280538dd..abb81532bbfc8 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -318,7 +318,7 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::Const<'tcx> { impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [ty::ValTree<'tcx>] { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { - Ok(decoder.tcx().arena.alloc_from_iter( + Ok(decoder.interner().alloc_valtree_from_iter( (0..decoder.read_usize()?) .map(|_| Decodable::decode(decoder)) .collect::, _>>()?, diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index aaa40f5c219ad..a018755e0e7db 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -1,5 +1,7 @@ //! Type context book-keeping. +// ignore-tidy-filelength + use crate::arena::Arena; use crate::dep_graph::DepGraph; use crate::hir::exports::ExportMap; @@ -161,6 +163,9 @@ impl<'tcx> Interner for TyInterner<'tcx> { type AllocatedAsmTemplate = &'tcx mut ast::InlineAsmTemplatePiece; type AllocatedAsmTemplateSlice = &'tcx mut [ast::InlineAsmTemplatePiece]; + type ValTree = ty::ValTree<'tcx>; + type AllocatedValTreeSlice = &'tcx mut [ty::ValTree<'tcx>]; + type PredicateSpan = (ty::Predicate<'tcx>, Span); type AllocatedPredicateSpanSlice = &'tcx mut [(ty::Predicate<'tcx>, Span)]; @@ -177,6 +182,13 @@ impl<'tcx> Interner for TyInterner<'tcx> { self.tcx.arena.alloc_from_iter(iter) } + fn alloc_valtree_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedValTreeSlice { + self.tcx.arena.alloc_from_iter(iter) + } + fn alloc_node_from_iter( self, iter: impl IntoIterator, @@ -199,7 +211,7 @@ impl<'tcx> Interner for TyInterner<'tcx> { self, iter: impl IntoIterator, ) -> Self::AllocatedSpanSlice { - self.tcx.arena.alloc_from_iter::(iter) + self.tcx.arena.alloc_from_iter::<_, Self::Span, _>(iter) } fn alloc_promoted(self, value: Self::Promoted) -> Self::AllocatedPromoted { diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index e7455e0cbad3e..b8cf8984c4fd1 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -90,6 +90,9 @@ pub trait Interner { type AllocatedAsmTemplate; type AllocatedAsmTemplateSlice; + type ValTree; + type AllocatedValTreeSlice; + type PredicateSpan; type AllocatedPredicateSpanSlice; @@ -165,6 +168,10 @@ pub trait Interner { iter: impl IntoIterator, ) -> Self::AllocatedAsmTemplateSlice; + fn alloc_valtree_from_iter( + self, + iter: impl IntoIterator, + ) -> Self::AllocatedValTreeSlice; fn alloc_predicate_span_from_iter( self, iter: impl IntoIterator, From aab6a4da5943bdbed83d42bb3ad29038d56fcd61 Mon Sep 17 00:00:00 2001 From: jackh726 Date: Fri, 18 Jun 2021 15:14:39 -0400 Subject: [PATCH 8/9] Keep tcx in TyDecoder --- compiler/rustc_metadata/src/rmeta/decoder.rs | 12 +- compiler/rustc_middle/src/arena.rs | 18 +-- .../rustc_middle/src/mir/interpret/mod.rs | 11 +- compiler/rustc_middle/src/ty/codec.rs | 132 +++++++++--------- compiler/rustc_middle/src/ty/context.rs | 33 +---- .../src/ty/query/on_disk_cache.rs | 24 ++-- compiler/rustc_type_ir/src/lib.rs | 8 -- 7 files changed, 99 insertions(+), 139 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index bcd031d1562b8..1d49ec498a907 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -27,7 +27,7 @@ use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel}; use rustc_middle::mir::interpret::{AllocDecodingSession, AllocDecodingState}; use rustc_middle::mir::{self, Body, Promoted}; use rustc_middle::ty::codec::TyDecoder; -use rustc_middle::ty::{self, Interner, Ty, TyCtxt, TyInterner, Visibility}; +use rustc_middle::ty::{self, Ty, TyCtxt, Visibility}; use rustc_serialize::{opaque, Decodable, Decoder}; use rustc_session::Session; use rustc_span::hygiene::ExpnDataDecodeMode; @@ -276,8 +276,8 @@ impl<'a, 'tcx> TyDecoder<'tcx> for DecodeContext<'a, 'tcx> { const CLEAR_CROSS_CRATE: bool = true; #[inline] - fn interner(&self) -> TyInterner<'tcx> { - self.tcx.expect("missing TyCtxt in DecodeContext").interner() + fn tcx(&self) -> TyCtxt<'tcx> { + self.tcx.expect("missing TyCtxt in DecodeContext") } #[inline] @@ -298,16 +298,16 @@ impl<'a, 'tcx> TyDecoder<'tcx> for DecodeContext<'a, 'tcx> { where F: FnOnce(&mut Self) -> Result, Self::Error>, { - let mut interner = self.interner(); + let tcx = self.tcx(); let key = ty::CReaderCacheKey { cnum: Some(self.cdata().cnum), pos: shorthand }; - if let Some(ty) = interner.get_cached_ty(key) { + if let Some(&ty) = tcx.ty_rcache.borrow().get(&key) { return Ok(ty); } let ty = or_insert_with(self)?; - interner.insert_cached_ty(key, ty); + tcx.ty_rcache.borrow_mut().insert(key, ty); Ok(ty) } diff --git a/compiler/rustc_middle/src/arena.rs b/compiler/rustc_middle/src/arena.rs index 513b8274ec955..a89d00e26ac19 100644 --- a/compiler/rustc_middle/src/arena.rs +++ b/compiler/rustc_middle/src/arena.rs @@ -16,7 +16,7 @@ macro_rules! arena_types { [] adt_def: rustc_middle::ty::AdtDef, [] steal_thir: rustc_data_structures::steal::Steal>, [] steal_mir: rustc_data_structures::steal::Steal>, - [] mir: rustc_middle::mir::Body<$tcx>, + [decode] mir: rustc_middle::mir::Body<$tcx>, [] steal_promoted: rustc_data_structures::steal::Steal< rustc_index::vec::IndexVec< @@ -24,16 +24,16 @@ macro_rules! arena_types { rustc_middle::mir::Body<$tcx> > >, - [] promoted: + [decode] promoted: rustc_index::vec::IndexVec< rustc_middle::mir::Promoted, rustc_middle::mir::Body<$tcx> >, - [] typeck_results: rustc_middle::ty::TypeckResults<$tcx>, - [] borrowck_result: + [decode] typeck_results: rustc_middle::ty::TypeckResults<$tcx>, + [decode] borrowck_result: rustc_middle::mir::BorrowCheckResult<$tcx>, - [] unsafety_check_result: rustc_middle::mir::UnsafetyCheckResult, - [] code_region: rustc_middle::mir::coverage::CodeRegion, + [decode] unsafety_check_result: rustc_middle::mir::UnsafetyCheckResult, + [decode] code_region: rustc_middle::mir::coverage::CodeRegion, [] const_allocs: rustc_middle::mir::interpret::Allocation, // Required for the incremental on-disk cache [few] mir_keys: rustc_hir::def_id::DefIdSet, @@ -100,11 +100,11 @@ macro_rules! arena_types { // Note that this deliberately duplicates items in the `rustc_hir::arena`, // since we need to allocate this type on both the `rustc_hir` arena // (during lowering) and the `librustc_middle` arena (for decoding MIR) - [] asm_template: rustc_ast::InlineAsmTemplatePiece, + [decode] asm_template: rustc_ast::InlineAsmTemplatePiece, // This is used to decode the &'tcx [Span] for InlineAsm's line_spans. - [] span: rustc_span::Span, - [] used_trait_imports: rustc_data_structures::fx::FxHashSet, + [decode] span: rustc_span::Span, + [decode] used_trait_imports: rustc_data_structures::fx::FxHashSet, ], $tcx); ) } diff --git a/compiler/rustc_middle/src/mir/interpret/mod.rs b/compiler/rustc_middle/src/mir/interpret/mod.rs index fe298ac65a8ad..14bdb0a5a2d50 100644 --- a/compiler/rustc_middle/src/mir/interpret/mod.rs +++ b/compiler/rustc_middle/src/mir/interpret/mod.rs @@ -111,7 +111,6 @@ use rustc_macros::HashStable; use rustc_middle::ty::print::with_no_trimmed_paths; use rustc_serialize::{Decodable, Encodable}; use rustc_target::abi::Endian; -use rustc_type_ir::Interner; use crate::mir; use crate::ty::codec::{TyDecoder, TyEncoder}; @@ -303,7 +302,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().reserve_alloc_id(); + let alloc_id = decoder.tcx().reserve_alloc_id(); *entry = State::InProgress(TinyList::new_single(self.session_id), alloc_id); Some(alloc_id) @@ -347,7 +346,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().set_alloc_id_same_memory(alloc_id, alloc); + decoder.tcx().set_alloc_id_same_memory(alloc_id, alloc); Ok(alloc_id) } AllocDiscriminant::Fn => { @@ -355,7 +354,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().create_fn_alloc(instance); + let alloc_id = decoder.tcx().create_fn_alloc(instance); Ok(alloc_id) } AllocDiscriminant::Static => { @@ -363,7 +362,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().create_static_alloc(did); + let alloc_id = decoder.tcx().create_static_alloc(did); Ok(alloc_id) } } @@ -547,7 +546,7 @@ impl<'tcx> TyCtxt<'tcx> { /// Freezes an `AllocId` created with `reserve` by pointing it at an `Allocation`. May be called /// twice for the same `(AllocId, Allocation)` pair. - pub fn set_alloc_id_same_memory(self, id: AllocId, mem: &'tcx Allocation) { + fn set_alloc_id_same_memory(self, id: AllocId, mem: &'tcx Allocation) { self.alloc_map.lock().alloc_map.insert_same(id, GlobalAlloc::Memory(mem)); } } diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index abb81532bbfc8..5ec665e913cc5 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -6,18 +6,18 @@ // The functionality in here is shared between persisting to crate metadata and // persisting to incr. comp. caches. +use crate::arena::ArenaAllocatable; use crate::infer::canonical::{CanonicalVarInfo, CanonicalVarInfos}; use crate::mir::{ self, interpret::{AllocId, Allocation}, }; use crate::ty::subst::SubstsRef; -use crate::ty::{self, List, Ty, TyInterner}; +use crate::ty::{self, List, Ty, TyCtxt}; use rustc_data_structures::fx::FxHashMap; use rustc_hir::def_id::DefId; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use rustc_span::Span; -use rustc_type_ir::Interner; use std::hash::Hash; use std::intrinsics; use std::marker::DiscriminantKind; @@ -161,7 +161,7 @@ encodable_via_deref! { pub trait TyDecoder<'tcx>: Decoder { const CLEAR_CROSS_CRATE: bool; - fn interner(&self) -> TyInterner<'tcx>; + fn tcx(&self) -> TyCtxt<'tcx>; fn peek_byte(&self) -> u8; @@ -186,6 +186,26 @@ pub trait TyDecoder<'tcx>: Decoder { fn decode_alloc_id(&mut self) -> Result; } +#[inline] +fn decode_arena_allocable<'tcx, D, T: ArenaAllocatable<'tcx> + Decodable>( + decoder: &mut D, +) -> Result<&'tcx T, D::Error> +where + D: TyDecoder<'tcx>, +{ + Ok(decoder.tcx().arena.alloc(Decodable::decode(decoder)?)) +} + +#[inline] +fn decode_arena_allocable_slice<'tcx, D, T: ArenaAllocatable<'tcx> + Decodable>( + decoder: &mut D, +) -> Result<&'tcx [T], D::Error> +where + D: TyDecoder<'tcx>, +{ + Ok(decoder.tcx().arena.alloc_from_iter( as Decodable>::decode(decoder)?)) +} + impl<'tcx, D: TyDecoder<'tcx>> Decodable for Ty<'tcx> { #[allow(rustc::usage_of_ty_tykind)] fn decode(decoder: &mut D) -> Result, D::Error> { @@ -199,7 +219,8 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable for Ty<'tcx> { decoder.with_position(shorthand, Ty::decode) }) } else { - Ok(decoder.interner().mk_ty(ty::TyKind::decode(decoder)?)) + let tcx = decoder.tcx(); + Ok(tcx.mk_ty(ty::TyKind::decode(decoder)?)) } } } @@ -226,7 +247,7 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Binder<'tcx, ty::PredicateKi impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Predicate<'tcx> { fn decode(decoder: &mut D) -> Result, D::Error> { let predicate_kind = Decodable::decode(decoder)?; - let predicate = decoder.interner().mk_predicate(predicate_kind); + let predicate = decoder.tcx().mk_predicate(predicate_kind); Ok(predicate) } } @@ -234,7 +255,8 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Predicate<'tcx> { impl<'tcx, D: TyDecoder<'tcx>> Decodable for SubstsRef<'tcx> { fn decode(decoder: &mut D) -> Result { let len = decoder.read_usize()?; - Ok(decoder.interner().mk_substs((0..len).map(|_| Decodable::decode(decoder)))?) + let tcx = decoder.tcx(); + tcx.mk_substs((0..len).map(|_| Decodable::decode(decoder))) } } @@ -243,14 +265,14 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable for mir::Place<'tcx> { let local: mir::Local = Decodable::decode(decoder)?; let len = decoder.read_usize()?; let projection: &'tcx List> = - decoder.interner().mk_place_elems((0..len).map(|_| Decodable::decode(decoder)))?; + decoder.tcx().mk_place_elems((0..len).map(|_| Decodable::decode(decoder)))?; Ok(mir::Place { local, projection }) } } impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Region<'tcx> { fn decode(decoder: &mut D) -> Result { - Ok(decoder.interner().mk_region(Decodable::decode(decoder)?)) + Ok(decoder.tcx().mk_region(Decodable::decode(decoder)?)) } } @@ -259,7 +281,7 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable for CanonicalVarInfos<'tcx> { let len = decoder.read_usize()?; let interned: Result>, _> = (0..len).map(|_| Decodable::decode(decoder)).collect(); - Ok(decoder.interner().intern_canonical_var_infos(interned?.as_slice())) + Ok(decoder.tcx().intern_canonical_var_infos(interned?.as_slice())) } } @@ -271,7 +293,7 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable for AllocId { impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::SymbolName<'tcx> { fn decode(decoder: &mut D) -> Result { - Ok(decoder.interner().mk_symbol_name(&decoder.read_str()?)) + Ok(ty::SymbolName::new(decoder.tcx(), &decoder.read_str()?)) } } @@ -288,14 +310,14 @@ macro_rules! impl_decodable_via_ref { impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::AdtDef { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { let def_id = >::decode(decoder)?; - Ok(decoder.interner().adt_def(def_id)) + Ok(decoder.tcx().adt_def(def_id)) } } impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List> { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { let len = decoder.read_usize()?; - Ok(decoder.interner().mk_type_list((0..len).map(|_| Decodable::decode(decoder)))?) + decoder.tcx().mk_type_list((0..len).map(|_| Decodable::decode(decoder))) } } @@ -304,21 +326,19 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { let len = decoder.read_usize()?; - Ok(decoder - .interner() - .mk_poly_existential_predicates((0..len).map(|_| Decodable::decode(decoder)))?) + decoder.tcx().mk_poly_existential_predicates((0..len).map(|_| Decodable::decode(decoder))) } } impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::Const<'tcx> { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { - Ok(decoder.interner().mk_const(Decodable::decode(decoder)?)) + Ok(decoder.tcx().mk_const(Decodable::decode(decoder)?)) } } impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [ty::ValTree<'tcx>] { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { - Ok(decoder.interner().alloc_valtree_from_iter( + Ok(decoder.tcx().arena.alloc_from_iter( (0..decoder.read_usize()?) .map(|_| Decodable::decode(decoder)) .collect::, _>>()?, @@ -328,13 +348,13 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [ty::ValTree<'tcx>] { impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for Allocation { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { - Ok(decoder.interner().intern_const_alloc(Decodable::decode(decoder)?)) + Ok(decoder.tcx().intern_const_alloc(Decodable::decode(decoder)?)) } } impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [(ty::Predicate<'tcx>, Span)] { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { - Ok(decoder.interner().alloc_predicate_span_from_iter( + Ok(decoder.tcx().arena.alloc_from_iter( (0..decoder.read_usize()?) .map(|_| Decodable::decode(decoder)) .collect::, _>>()?, @@ -344,7 +364,7 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [(ty::Predicate<'tcx>, impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [mir::abstract_const::Node<'tcx>] { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { - Ok(decoder.interner().alloc_node_from_iter( + Ok(decoder.tcx().arena.alloc_from_iter( (0..decoder.read_usize()?) .map(|_| Decodable::decode(decoder)) .collect::, _>>()?, @@ -354,7 +374,7 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [mir::abstract_const::N impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [mir::abstract_const::NodeId] { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { - Ok(decoder.interner().alloc_node_id_from_iter( + Ok(decoder.tcx().arena.alloc_from_iter( (0..decoder.read_usize()?) .map(|_| Decodable::decode(decoder)) .collect::, _>>()?, @@ -365,9 +385,7 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [mir::abstract_const::N impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { let len = decoder.read_usize()?; - Ok(decoder - .interner() - .mk_bound_variable_kinds((0..len).map(|_| Decodable::decode(decoder)))?) + Ok(decoder.tcx().mk_bound_variable_kinds((0..len).map(|_| Decodable::decode(decoder)))?) } } @@ -395,59 +413,39 @@ macro_rules! __impl_decoder_methods { } } -macro_rules! impl_ty_decoder_arena_type { - ($ty:ty, $alloc_method:ident, $alloc_slice_method:ident) => { - impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for $ty { +macro_rules! impl_arena_allocatable_decoder { + ([]$args:tt) => {}; + ([decode $(, $attrs:ident)*] + [[$name:ident: $ty:ty], $tcx:lifetime]) => { + impl<$tcx, D: TyDecoder<$tcx>> RefDecodable<$tcx, D> for $ty { #[inline] - fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { - Ok(decoder.interner().$alloc_method(Decodable::decode(decoder)?)) + fn decode(decoder: &mut D) -> Result<&$tcx Self, D::Error> { + decode_arena_allocable(decoder) } } - impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [$ty] { + impl<$tcx, D: TyDecoder<$tcx>> RefDecodable<$tcx, D> for [$ty] { #[inline] - fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { - Ok(decoder - .interner() - .$alloc_slice_method( as Decodable>::decode(decoder)?)) + fn decode(decoder: &mut D) -> Result<&$tcx Self, D::Error> { + decode_arena_allocable_slice(decoder) } } }; + ([$ignore:ident $(, $attrs:ident)*]$args:tt) => { + impl_arena_allocatable_decoder!([$($attrs),*]$args); + }; +} + +macro_rules! impl_arena_allocatable_decoders { + ([], [$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) => { + $( + impl_arena_allocatable_decoder!($a [[$name: $ty], $tcx]); + )* + } } -impl_ty_decoder_arena_type!(rustc_middle::mir::Body<'tcx>, alloc_mir, alloc_mir_from_iter); -impl_ty_decoder_arena_type!( - rustc_middle::ty::TypeckResults<'tcx>, - alloc_type_check_results, - alloc_type_check_results_from_iter -); -impl_ty_decoder_arena_type!( - rustc_middle::mir::BorrowCheckResult<'tcx>, - alloc_borrowck_result, - alloc_borrowck_result_from_iter -); -impl_ty_decoder_arena_type!( - rustc_middle::mir::UnsafetyCheckResult, - alloc_unsafety_check_result, - alloc_unsafety_check_result_from_iter -); -impl_ty_decoder_arena_type!( - rustc_middle::mir::coverage::CodeRegion, - alloc_code_region, - alloc_code_region_from_iter -); -impl_ty_decoder_arena_type!( - rustc_ast::InlineAsmTemplatePiece, - alloc_asm_template, - alloc_asm_template_from_iter -); -impl_ty_decoder_arena_type!(rustc_span::Span, alloc_span, alloc_span_from_iter); -impl_ty_decoder_arena_type!( - rustc_data_structures::fx::FxHashSet, - alloc_used_trait_imports, - alloc_used_trait_imports_from_iter -); -impl_ty_decoder_arena_type!(rustc_index::vec::IndexVec>, alloc_promoted, alloc_promoted_from_iter); +rustc_hir::arena_types!(impl_arena_allocatable_decoders, [], 'tcx); +arena_types!(impl_arena_allocatable_decoders, [], 'tcx); #[macro_export] macro_rules! implement_ty_decoder { diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index a018755e0e7db..2a72e44af20ea 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -41,7 +41,7 @@ use rustc_data_structures::profiling::SelfProfilerRef; use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::steal::Steal; -use rustc_data_structures::sync::{self, HashMapExt, Lock, Lrc, WorkerLocal}; +use rustc_data_structures::sync::{self, Lock, Lrc, WorkerLocal}; use rustc_data_structures::vec_map::VecMap; use rustc_errors::ErrorReported; use rustc_hir as hir; @@ -318,17 +318,6 @@ impl<'tcx> Interner for TyInterner<'tcx> { SymbolName::new(self.tcx, name) } - fn get_cached_ty(&self, k: Self::TypeKey) -> Option { - self.tcx.ty_rcache.borrow().get(&k).map(|x| x.to_owned()) - } - fn insert_same_cached_ty(&mut self, key: Self::TypeKey, value: Self::Ty) { - self.tcx.ty_rcache.borrow_mut().insert_same(key, value); - } - - fn insert_cached_ty(&mut self, key: Self::TypeKey, value: Self::Ty) -> Option { - self.tcx.ty_rcache.borrow_mut().insert(key, value) - } - fn adt_def(self, def_id: Self::DefId) -> Self::AdtDef { self.tcx.adt_def(def_id) } @@ -395,26 +384,6 @@ impl<'tcx> Interner for TyInterner<'tcx> { ) -> Self::ListCanonicalVarInfo { self.tcx.intern_canonical_var_infos(ts) } - - fn reserve_alloc_id(self) -> Self::AllocationId { - self.tcx.reserve_alloc_id() - } - - fn set_alloc_id_same_memory(self, id: Self::AllocationId, mem: Self::InternedAllocation) { - self.tcx.set_alloc_id_same_memory(id, mem) - } - - fn create_fn_alloc(self, instance: Self::Instance) -> Self::AllocationId { - self.tcx.create_fn_alloc(instance) - } - - fn create_static_alloc(self, static_id: Self::DefId) -> Self::AllocationId { - self.tcx.create_static_alloc(static_id) - } - - fn def_path_hash_to_def_id(self, hash: Self::DefPathHash) -> Option { - self.tcx.on_disk_cache.as_ref().unwrap().def_path_hash_to_def_id(self.tcx, hash) - } } /// A type that is not publicly constructable. This prevents people from making [`TyKind::Error`]s diff --git a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs index aa71d7480f78a..ebaef347f4293 100644 --- a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs @@ -3,9 +3,9 @@ use crate::mir::interpret::{AllocDecodingSession, AllocDecodingState}; use crate::mir::{self, interpret}; use crate::ty::codec::{RefDecodable, TyDecoder, TyEncoder}; use crate::ty::context::TyCtxt; -use crate::ty::{self, Ty, TyInterner}; +use crate::ty::{self, Ty}; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet}; -use rustc_data_structures::sync::{Lock, Lrc, OnceCell}; +use rustc_data_structures::sync::{HashMapExt, Lock, Lrc, OnceCell}; use rustc_data_structures::thin_vec::ThinVec; use rustc_data_structures::unhash::UnhashMap; use rustc_errors::Diagnostic; @@ -26,7 +26,6 @@ use rustc_span::hygiene::{ use rustc_span::source_map::{SourceMap, StableSourceFileId}; use rustc_span::CachingSourceMapView; use rustc_span::{BytePos, ExpnData, SourceFile, Span, DUMMY_SP}; -use rustc_type_ir::Interner; use std::collections::hash_map::Entry; use std::mem; @@ -676,8 +675,8 @@ impl<'a, 'tcx> TyDecoder<'tcx> for CacheDecoder<'a, 'tcx> { const CLEAR_CROSS_CRATE: bool = false; #[inline] - fn interner(&self) -> TyInterner<'tcx> { - self.tcx.interner() + fn tcx(&self) -> TyCtxt<'tcx> { + self.tcx } #[inline] @@ -698,17 +697,17 @@ impl<'a, 'tcx> TyDecoder<'tcx> for CacheDecoder<'a, 'tcx> { where F: FnOnce(&mut Self) -> Result, Self::Error>, { - let mut interner = self.interner(); + let tcx = self.tcx(); let cache_key = ty::CReaderCacheKey { cnum: None, pos: shorthand }; - if let Some(ty) = interner.get_cached_ty(cache_key) { + if let Some(&ty) = tcx.ty_rcache.borrow().get(&cache_key) { return Ok(ty); } let ty = or_insert_with(self)?; // This may overwrite the entry, but it should overwrite with the same value. - interner.insert_same_cached_ty(cache_key, ty); + tcx.ty_rcache.borrow_mut().insert_same(cache_key, ty); Ok(ty) } @@ -835,9 +834,12 @@ impl<'a, 'tcx> Decodable> for DefId { // If we get to this point, then all of the query inputs were green, // which means that the definition with this hash is guaranteed to // still exist in the current compilation session. - - let def_if = d.interner().def_path_hash_to_def_id(def_path_hash); - Ok(def_if.unwrap()) + Ok(d.tcx() + .on_disk_cache + .as_ref() + .unwrap() + .def_path_hash_to_def_id(d.tcx(), def_path_hash) + .unwrap()) } } diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index b8cf8984c4fd1..f1247daa7b0ed 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -185,9 +185,6 @@ pub trait Interner { iter: impl IntoIterator, ) -> Self::AllocatedNodeIdSlice; - fn get_cached_ty(&self, k: Self::TypeKey) -> Option; - fn insert_same_cached_ty(&mut self, key: Self::TypeKey, value: Self::Ty); - fn insert_cached_ty(&mut self, key: Self::TypeKey, value: Self::Ty) -> Option; fn adt_def(self, def_id: Self::DefId) -> Self::AdtDef; fn mk_symbol_name(self, name: &str) -> Self::SymbolName; fn mk_predicate(self, binder: Self::BinderPredicateKind) -> Self::Predicate; @@ -218,11 +215,6 @@ pub trait Interner { self, ts: &[Self::CanonicalVarInfo], ) -> Self::ListCanonicalVarInfo; - fn reserve_alloc_id(self) -> Self::AllocationId; - fn set_alloc_id_same_memory(self, id: Self::AllocationId, mem: Self::InternedAllocation); - fn create_fn_alloc(self, instance: Self::Instance) -> Self::AllocationId; - fn create_static_alloc(self, static_id: Self::DefId) -> Self::AllocationId; - fn def_path_hash_to_def_id(self, hash: Self::DefPathHash) -> Option; // arena methods } From 3d943017c9bcc3102d9cb5c0329893c0f73a20bc Mon Sep 17 00:00:00 2001 From: jackh726 Date: Mon, 21 Jun 2021 16:05:46 -0400 Subject: [PATCH 9/9] WIP move TyKind --- compiler/rustc_ast/src/attr/mod.rs | 10 +- compiler/rustc_lint/src/builtin.rs | 4 +- compiler/rustc_macros/src/serialize.rs | 4 +- .../rustc_middle/src/mir/interpret/mod.rs | 16 +- compiler/rustc_middle/src/mir/mod.rs | 12 +- compiler/rustc_middle/src/ty/adt.rs | 12 +- compiler/rustc_middle/src/ty/codec.rs | 201 ++++---- compiler/rustc_middle/src/ty/consts.rs | 10 +- compiler/rustc_middle/src/ty/context.rs | 365 +++------------ compiler/rustc_middle/src/ty/diagnostics.rs | 2 +- .../rustc_middle/src/ty/inhabitedness/mod.rs | 3 +- compiler/rustc_middle/src/ty/mod.rs | 2 +- .../src/ty/query/on_disk_cache.rs | 18 +- compiler/rustc_middle/src/ty/sty.rs | 139 +++++- compiler/rustc_middle/src/ty/subst.rs | 6 +- compiler/rustc_middle/src/ty/util.rs | 2 +- compiler/rustc_mir/src/interpret/cast.rs | 6 +- compiler/rustc_ty_utils/src/ty.rs | 2 +- compiler/rustc_type_ir/src/codec.rs | 63 +++ compiler/rustc_type_ir/src/lib.rs | 233 ++-------- compiler/rustc_type_ir/src/sty.rs | 437 ++++++++++++++++++ .../rustc_typeck/src/coherence/builtin.rs | 2 +- 22 files changed, 892 insertions(+), 657 deletions(-) create mode 100644 compiler/rustc_type_ir/src/codec.rs create mode 100644 compiler/rustc_type_ir/src/sty.rs diff --git a/compiler/rustc_ast/src/attr/mod.rs b/compiler/rustc_ast/src/attr/mod.rs index a1654807c8f04..41121d095f3ed 100644 --- a/compiler/rustc_ast/src/attr/mod.rs +++ b/compiler/rustc_ast/src/attr/mod.rs @@ -460,10 +460,12 @@ impl MetaItemKind { fn token_trees_and_spacings(&self, span: Span) -> Vec { match *self { MetaItemKind::Word => vec![], - MetaItemKind::NameValue(ref lit) => vec![ - TokenTree::token(token::Eq, span).into(), - TokenTree::Token(lit.to_token()).into(), - ], + MetaItemKind::NameValue(ref lit) => { + vec![ + TokenTree::token(token::Eq, span).into(), + TokenTree::Token(lit.to_token()).into(), + ] + } MetaItemKind::List(ref list) => { let mut tokens = Vec::new(); for (i, item) in list.iter().enumerate() { diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index f6a84966f7a91..23e9c80db01aa 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -2476,7 +2476,7 @@ impl<'tcx> LateLintPass<'tcx> for InvalidValue { ty: Ty<'tcx>, init: InitKind, ) -> Option { - use rustc_middle::ty::TyKind::*; + use rustc_type_ir::sty::TyKind::*; match ty.kind() { // Primitive types that don't like 0 as a value. Ref(..) => Some(("references must be non-null".to_string(), None)), @@ -2783,7 +2783,7 @@ impl ClashingExternDeclarations { true } else { // Do a full, depth-first comparison between the two. - use rustc_middle::ty::TyKind::*; + use rustc_type_ir::sty::TyKind::*; let a_kind = a.kind(); let b_kind = b.kind(); diff --git a/compiler/rustc_macros/src/serialize.rs b/compiler/rustc_macros/src/serialize.rs index 7bc669f2b005c..dea0d8cf24f0c 100644 --- a/compiler/rustc_macros/src/serialize.rs +++ b/compiler/rustc_macros/src/serialize.rs @@ -7,7 +7,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_middle::ty::codec::TyDecoder<'tcx>}); + s.add_impl_generic(parse_quote! {#decoder_ty: ::rustc_type_ir::codec::TyDecoder>}); s.add_bounds(synstructure::AddBounds::Generics); decodable_body(s, decoder_ty) @@ -136,7 +136,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_middle::ty::codec::TyEncoder<'tcx>}); + 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_middle/src/mir/interpret/mod.rs b/compiler/rustc_middle/src/mir/interpret/mod.rs index 14bdb0a5a2d50..b8613ff5069a1 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}; +use crate::ty::{self, Instance, Ty, TyCtxt, TyInterner}; pub use self::error::{ struct_error, CheckInAllocMsg, ErrorHandled, EvalToAllocationRawResult, EvalToConstValueResult, @@ -200,7 +200,7 @@ enum AllocDiscriminant { Static, } -pub fn specialized_encode_alloc_id<'tcx, E: TyEncoder<'tcx>>( +pub fn specialized_encode_alloc_id<'tcx, E: TyEncoder>>( encoder: &mut E, tcx: TyCtxt<'tcx>, alloc_id: AllocId, @@ -270,11 +270,11 @@ pub struct AllocDecodingSession<'s> { session_id: DecodingSessionId, } -impl<'s> AllocDecodingSession<'s> { +impl<'s, 'tcx> AllocDecodingSession<'s> { /// Decodes an `AllocId` in a thread-safe way. pub fn decode_alloc_id(&self, decoder: &mut D) -> Result where - D: TyDecoder<'tcx>, + D: TyDecoder>, { // Read the index of the allocation. let idx = usize::try_from(decoder.read_u32()?).unwrap(); @@ -302,7 +302,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.tcx().reserve_alloc_id(); + let alloc_id = decoder.interner().tcx.reserve_alloc_id(); *entry = State::InProgress(TinyList::new_single(self.session_id), alloc_id); Some(alloc_id) @@ -346,7 +346,7 @@ impl<'s> AllocDecodingSession<'s> { // We already have a reserved `AllocId`. let alloc_id = alloc_id.unwrap(); trace!("decoded alloc {:?}: {:#?}", alloc_id, alloc); - decoder.tcx().set_alloc_id_same_memory(alloc_id, alloc); + decoder.interner().tcx.set_alloc_id_same_memory(alloc_id, alloc); Ok(alloc_id) } AllocDiscriminant::Fn => { @@ -354,7 +354,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.tcx().create_fn_alloc(instance); + let alloc_id = decoder.interner().tcx.create_fn_alloc(instance); Ok(alloc_id) } AllocDiscriminant::Static => { @@ -362,7 +362,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.tcx().create_static_alloc(did); + let alloc_id = decoder.interner().tcx.create_static_alloc(did); Ok(alloc_id) } } diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index 7ae7eab6e5a31..526aac8208144 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -381,7 +381,9 @@ impl<'tcx> Body<'tcx> { /// Returns an iterator over all user-declared mutable locals. #[inline] - pub fn mut_vars_iter<'a>(&'a self) -> impl Iterator + 'a { + pub fn mut_vars_iter<'a>( + &'a self, + ) -> impl Iterator + rustc_data_structures::captures::Captures<'tcx> + 'a { (self.arg_count + 1..self.local_decls.len()).filter_map(move |index| { let local = Local::new(index); let decl = &self.local_decls[local]; @@ -395,7 +397,9 @@ impl<'tcx> Body<'tcx> { /// Returns an iterator over all user-declared mutable arguments and locals. #[inline] - pub fn mut_vars_and_args_iter<'a>(&'a self) -> impl Iterator + 'a { + pub fn mut_vars_and_args_iter<'a>( + &'a self, + ) -> impl Iterator + rustc_data_structures::captures::Captures<'tcx> + 'a { (1..self.local_decls.len()).filter_map(move |index| { let local = Local::new(index); let decl = &self.local_decls[local]; @@ -543,7 +547,7 @@ impl ClearCrossCrate { const TAG_CLEAR_CROSS_CRATE_CLEAR: u8 = 0; const TAG_CLEAR_CROSS_CRATE_SET: u8 = 1; -impl<'tcx, E: TyEncoder<'tcx>, T: Encodable> Encodable for ClearCrossCrate { +impl> Encodable for ClearCrossCrate { #[inline] fn encode(&self, e: &mut E) -> Result<(), E::Error> { if E::CLEAR_CROSS_CRATE { @@ -559,7 +563,7 @@ impl<'tcx, E: TyEncoder<'tcx>, T: Encodable> Encodable for ClearCrossCrate } } } -impl<'tcx, D: TyDecoder<'tcx>, T: Decodable> Decodable for ClearCrossCrate { +impl> Decodable for ClearCrossCrate { #[inline] fn decode(d: &mut D) -> Result, D::Error> { if D::CLEAR_CROSS_CRATE { diff --git a/compiler/rustc_middle/src/ty/adt.rs b/compiler/rustc_middle/src/ty/adt.rs index 95159ea46aec3..c4780aef05793 100644 --- a/compiler/rustc_middle/src/ty/adt.rs +++ b/compiler/rustc_middle/src/ty/adt.rs @@ -2,6 +2,7 @@ use crate::ich::StableHashingContext; use crate::mir::interpret::ErrorHandled; use crate::ty; use crate::ty::util::{Discr, IntTypeExt}; +use crate::ty::TyInterner; use rustc_data_structures::captures::Captures; use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fx::FxHashMap; @@ -10,10 +11,11 @@ use rustc_errors::ErrorReported; use rustc_hir::def::{DefKind, Res}; use rustc_hir::def_id::DefId; use rustc_index::vec::{Idx, IndexVec}; -use rustc_serialize::{self, Encodable, Encoder}; +use rustc_serialize::{self, Encodable}; use rustc_session::DataTypeKind; use rustc_span::symbol::sym; use rustc_target::abi::VariantIdx; +use rustc_type_ir::codec::TyEncoder; use std::cell::RefCell; use std::cmp::Ordering; @@ -105,7 +107,13 @@ impl Hash for AdtDef { } } -impl Encodable for AdtDef { +impl>> Encodable for AdtDef { + fn encode(&self, s: &mut S) -> Result<(), S::Error> { + self.did.encode(s) + } +} + +impl>> Encodable for &AdtDef { fn encode(&self, s: &mut S) -> Result<(), S::Error> { self.did.encode(s) } diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index 5ec665e913cc5..0f8faa5a5aa33 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -13,11 +13,13 @@ use crate::mir::{ interpret::{AllocId, Allocation}, }; use crate::ty::subst::SubstsRef; -use crate::ty::{self, List, Ty, TyCtxt}; +use crate::ty::{self, List, Ty}; use rustc_data_structures::fx::FxHashMap; use rustc_hir::def_id::DefId; -use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; +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; use std::intrinsics; use std::marker::DiscriminantKind; @@ -27,13 +29,13 @@ use std::marker::DiscriminantKind; /// This offset is also chosen so that the first byte is never < 0x80. pub const SHORTHAND_OFFSET: usize = 0x80; -pub trait EncodableWithShorthand<'tcx, E: TyEncoder<'tcx>>: Copy + Eq + Hash { +pub trait EncodableWithShorthand: Copy + Eq + Hash { type Variant: Encodable; fn variant(&self) -> &Self::Variant; } #[allow(rustc::usage_of_ty_tykind)] -impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for Ty<'tcx> { +impl<'tcx, E: TyEncoder>> EncodableWithShorthand for Ty<'tcx> { type Variant = ty::TyKind<'tcx>; #[inline] @@ -42,7 +44,9 @@ impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for Ty<'tcx> { } } -impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for ty::PredicateKind<'tcx> { +impl<'tcx, E: TyEncoder>> EncodableWithShorthand + for ty::PredicateKind<'tcx> +{ type Variant = ty::PredicateKind<'tcx>; #[inline] @@ -51,15 +55,6 @@ impl<'tcx, E: TyEncoder<'tcx>> EncodableWithShorthand<'tcx, E> for ty::Predicate } } -pub trait TyEncoder<'tcx>: Encoder { - const CLEAR_CROSS_CRATE: bool; - - fn position(&self) -> usize; - fn type_shorthands(&mut self) -> &mut FxHashMap, usize>; - fn predicate_shorthands(&mut self) -> &mut FxHashMap, usize>; - fn encode_alloc_id(&mut self, alloc_id: &AllocId) -> Result<(), Self::Error>; -} - /// Trait for decoding to a reference. /// /// This is a separate trait from `Decodable` so that we can implement it for @@ -70,16 +65,16 @@ pub trait TyEncoder<'tcx>: Encoder { /// /// `Decodable` can still be implemented in cases where `Decodable` is required /// by a trait bound. -pub trait RefDecodable<'tcx, D: TyDecoder<'tcx>> { +pub trait RefDecodable<'tcx, D: TyDecoder>> { fn decode(d: &mut D) -> Result<&'tcx Self, D::Error>; } /// Encode the given value or a previously cached shorthand. pub fn encode_with_shorthand(encoder: &mut E, value: &T, cache: M) -> Result<(), E::Error> where - E: TyEncoder<'tcx>, + E: TyEncoder>, M: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap, - T: EncodableWithShorthand<'tcx, E>, + T: EncodableWithShorthand, // The discriminant and shorthand must have the same size. T::Variant: DiscriminantKind, { @@ -114,26 +109,28 @@ where Ok(()) } -impl<'tcx, E: TyEncoder<'tcx>> Encodable for Ty<'tcx> { +impl<'tcx, E: TyEncoder>> Encodable for Ty<'tcx> { fn encode(&self, e: &mut E) -> Result<(), E::Error> { encode_with_shorthand(e, self, TyEncoder::type_shorthands) } } -impl<'tcx, E: TyEncoder<'tcx>> Encodable for ty::Binder<'tcx, ty::PredicateKind<'tcx>> { +impl<'tcx, E: TyEncoder>> Encodable + for ty::Binder<'tcx, ty::PredicateKind<'tcx>> +{ fn encode(&self, e: &mut E) -> Result<(), E::Error> { self.bound_vars().encode(e)?; encode_with_shorthand(e, &self.skip_binder(), TyEncoder::predicate_shorthands) } } -impl<'tcx, E: TyEncoder<'tcx>> Encodable for ty::Predicate<'tcx> { +impl<'tcx, E: TyEncoder>> Encodable for ty::Predicate<'tcx> { fn encode(&self, e: &mut E) -> Result<(), E::Error> { self.kind().encode(e) } } -impl<'tcx, E: TyEncoder<'tcx>> Encodable for AllocId { +impl<'tcx, E: TyEncoder>> Encodable for AllocId { fn encode(&self, e: &mut E) -> Result<(), E::Error> { e.encode_alloc_id(self) } @@ -141,7 +138,7 @@ impl<'tcx, E: TyEncoder<'tcx>> Encodable for AllocId { macro_rules! encodable_via_deref { ($($t:ty),+) => { - $(impl<'tcx, E: TyEncoder<'tcx>> Encodable for $t { + $(impl<'tcx, E: TyEncoder>> Encodable for $t { fn encode(&self, e: &mut E) -> Result<(), E::Error> { (**self).encode(e) } @@ -158,55 +155,35 @@ encodable_via_deref! { &'tcx mir::coverage::CodeRegion } -pub trait TyDecoder<'tcx>: Decoder { - const CLEAR_CROSS_CRATE: bool; - - fn tcx(&self) -> TyCtxt<'tcx>; - - fn peek_byte(&self) -> u8; - - fn position(&self) -> usize; - - fn cached_ty_for_shorthand( - &mut self, - shorthand: usize, - or_insert_with: F, - ) -> Result, Self::Error> - where - F: FnOnce(&mut Self) -> Result, Self::Error>; - - 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) -> Result; -} - #[inline] -fn decode_arena_allocable<'tcx, D, T: ArenaAllocatable<'tcx> + Decodable>( +fn decode_arena_allocable< + 'tcx, + D: TyDecoder>, + T: ArenaAllocatable<'tcx> + Decodable, +>( decoder: &mut D, ) -> Result<&'tcx T, D::Error> where - D: TyDecoder<'tcx>, + D: TyDecoder, { - Ok(decoder.tcx().arena.alloc(Decodable::decode(decoder)?)) + Ok(decoder.interner().tcx.arena.alloc(Decodable::decode(decoder)?)) } #[inline] -fn decode_arena_allocable_slice<'tcx, D, T: ArenaAllocatable<'tcx> + Decodable>( +fn decode_arena_allocable_slice< + 'tcx, + D: TyDecoder>, + T: ArenaAllocatable<'tcx> + Decodable, +>( decoder: &mut D, ) -> Result<&'tcx [T], D::Error> where - D: TyDecoder<'tcx>, + D: TyDecoder, { - Ok(decoder.tcx().arena.alloc_from_iter( as Decodable>::decode(decoder)?)) + Ok(decoder.interner().tcx.arena.alloc_from_iter( as Decodable>::decode(decoder)?)) } -impl<'tcx, D: TyDecoder<'tcx>> Decodable for Ty<'tcx> { +impl<'tcx, D: TyDecoder>> Decodable for Ty<'tcx> { #[allow(rustc::usage_of_ty_tykind)] fn decode(decoder: &mut D) -> Result, D::Error> { // Handle shorthands first, if we have an usize > 0x80. @@ -219,13 +196,15 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable for Ty<'tcx> { decoder.with_position(shorthand, Ty::decode) }) } else { - let tcx = decoder.tcx(); - Ok(tcx.mk_ty(ty::TyKind::decode(decoder)?)) + let tcx = decoder.interner().tcx; + Ok(tcx.mk_ty(rustc_type_ir::TyKind::decode(decoder)?)) } } } -impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Binder<'tcx, ty::PredicateKind<'tcx>> { +impl<'tcx, D: TyDecoder>> Decodable + for ty::Binder<'tcx, ty::PredicateKind<'tcx>> +{ fn decode(decoder: &mut D) -> Result>, D::Error> { let bound_vars = Decodable::decode(decoder)?; // Handle shorthands first, if we have an usize > 0x80. @@ -244,62 +223,62 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Binder<'tcx, ty::PredicateKi } } -impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Predicate<'tcx> { +impl<'tcx, D: TyDecoder>> Decodable for ty::Predicate<'tcx> { fn decode(decoder: &mut D) -> Result, D::Error> { let predicate_kind = Decodable::decode(decoder)?; - let predicate = decoder.tcx().mk_predicate(predicate_kind); + let predicate = decoder.interner().tcx.mk_predicate(predicate_kind); Ok(predicate) } } -impl<'tcx, D: TyDecoder<'tcx>> Decodable for SubstsRef<'tcx> { +impl<'tcx, D: TyDecoder>> Decodable for SubstsRef<'tcx> { fn decode(decoder: &mut D) -> Result { let len = decoder.read_usize()?; - let tcx = decoder.tcx(); + let tcx = decoder.interner().tcx; tcx.mk_substs((0..len).map(|_| Decodable::decode(decoder))) } } -impl<'tcx, D: TyDecoder<'tcx>> Decodable for mir::Place<'tcx> { +impl<'tcx, D: TyDecoder>> Decodable for mir::Place<'tcx> { fn decode(decoder: &mut D) -> Result { let local: mir::Local = Decodable::decode(decoder)?; let len = decoder.read_usize()?; let projection: &'tcx List> = - decoder.tcx().mk_place_elems((0..len).map(|_| Decodable::decode(decoder)))?; + decoder.interner().tcx.mk_place_elems((0..len).map(|_| Decodable::decode(decoder)))?; Ok(mir::Place { local, projection }) } } -impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Region<'tcx> { +impl<'tcx, D: TyDecoder>> Decodable for ty::Region<'tcx> { fn decode(decoder: &mut D) -> Result { - Ok(decoder.tcx().mk_region(Decodable::decode(decoder)?)) + Ok(decoder.interner().tcx.mk_region(Decodable::decode(decoder)?)) } } -impl<'tcx, D: TyDecoder<'tcx>> Decodable for CanonicalVarInfos<'tcx> { +impl<'tcx, D: TyDecoder>> Decodable for CanonicalVarInfos<'tcx> { fn decode(decoder: &mut D) -> Result { let len = decoder.read_usize()?; let interned: Result>, _> = (0..len).map(|_| Decodable::decode(decoder)).collect(); - Ok(decoder.tcx().intern_canonical_var_infos(interned?.as_slice())) + Ok(decoder.interner().tcx.intern_canonical_var_infos(interned?.as_slice())) } } -impl<'tcx, D: TyDecoder<'tcx>> Decodable for AllocId { +impl>> Decodable for AllocId { fn decode(decoder: &mut D) -> Result { decoder.decode_alloc_id() } } -impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::SymbolName<'tcx> { +impl<'tcx, D: TyDecoder>> Decodable for ty::SymbolName<'tcx> { fn decode(decoder: &mut D) -> Result { - Ok(ty::SymbolName::new(decoder.tcx(), &decoder.read_str()?)) + Ok(ty::SymbolName::new(decoder.interner().tcx, &decoder.read_str()?)) } } macro_rules! impl_decodable_via_ref { ($($t:ty),+) => { - $(impl<'tcx, D: TyDecoder<'tcx>> Decodable for $t { + $(impl<'tcx, D: TyDecoder>> Decodable for $t { fn decode(decoder: &mut D) -> Result { RefDecodable::decode(decoder) } @@ -307,38 +286,53 @@ macro_rules! impl_decodable_via_ref { } } -impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::AdtDef { +impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for ty::AdtDef { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { let def_id = >::decode(decoder)?; - Ok(decoder.tcx().adt_def(def_id)) + Ok(decoder.interner().tcx.adt_def(def_id)) + } +} + +impl<'tcx, D: TyDecoder>> Decodable for &ty::AdtDef { + fn decode(decoder: &mut D) -> Result { + RefDecodable::decode(decoder) + } +} + +impl<'tcx, D: TyDecoder>> Decodable for &ty::Const<'_> { + fn decode(decoder: &mut D) -> Result { + RefDecodable::decode(decoder) } } -impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List> { +impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for ty::List> { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { let len = decoder.read_usize()?; - decoder.tcx().mk_type_list((0..len).map(|_| Decodable::decode(decoder))) + decoder.interner().tcx.mk_type_list((0..len).map(|_| Decodable::decode(decoder))) } } -impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> +impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for ty::List>> { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { let len = decoder.read_usize()?; - decoder.tcx().mk_poly_existential_predicates((0..len).map(|_| Decodable::decode(decoder))) + decoder + .interner() + .tcx + .mk_poly_existential_predicates((0..len).map(|_| Decodable::decode(decoder))) } } -impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::Const<'tcx> { +impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for ty::Const<'tcx> { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { - Ok(decoder.tcx().mk_const(Decodable::decode(decoder)?)) + Ok(decoder.interner().tcx.mk_const(Decodable::decode(decoder)?)) } } -impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [ty::ValTree<'tcx>] { +impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for [ty::ValTree<'tcx>] { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { - Ok(decoder.tcx().arena.alloc_from_iter( + Ok(decoder.interner().tcx.arena.alloc_from_iter( (0..decoder.read_usize()?) .map(|_| Decodable::decode(decoder)) .collect::, _>>()?, @@ -346,15 +340,17 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [ty::ValTree<'tcx>] { } } -impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for Allocation { +impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for Allocation { fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { - Ok(decoder.tcx().intern_const_alloc(Decodable::decode(decoder)?)) + Ok(decoder.interner().tcx.intern_const_alloc(Decodable::decode(decoder)?)) } } -impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [(ty::Predicate<'tcx>, Span)] { +impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> + for [(ty::Predicate<'tcx>, Span)] +{ fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { - Ok(decoder.tcx().arena.alloc_from_iter( + Ok(decoder.interner().tcx.arena.alloc_from_iter( (0..decoder.read_usize()?) .map(|_| Decodable::decode(decoder)) .collect::, _>>()?, @@ -362,9 +358,11 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [(ty::Predicate<'tcx>, } } -impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [mir::abstract_const::Node<'tcx>] { +impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> + for [mir::abstract_const::Node<'tcx>] +{ fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { - Ok(decoder.tcx().arena.alloc_from_iter( + Ok(decoder.interner().tcx.arena.alloc_from_iter( (0..decoder.read_usize()?) .map(|_| Decodable::decode(decoder)) .collect::, _>>()?, @@ -372,9 +370,11 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [mir::abstract_const::N } } -impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [mir::abstract_const::NodeId] { +impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> + for [mir::abstract_const::NodeId] +{ fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { - Ok(decoder.tcx().arena.alloc_from_iter( + Ok(decoder.interner().tcx.arena.alloc_from_iter( (0..decoder.read_usize()?) .map(|_| Decodable::decode(decoder)) .collect::, _>>()?, @@ -382,10 +382,15 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [mir::abstract_const::N } } -impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::List { +impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> + for ty::List +{ fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> { let len = decoder.read_usize()?; - Ok(decoder.tcx().mk_bound_variable_kinds((0..len).map(|_| Decodable::decode(decoder)))?) + Ok(decoder + .interner() + .tcx + .mk_bound_variable_kinds((0..len).map(|_| Decodable::decode(decoder)))?) } } @@ -417,14 +422,14 @@ macro_rules! impl_arena_allocatable_decoder { ([]$args:tt) => {}; ([decode $(, $attrs:ident)*] [[$name:ident: $ty:ty], $tcx:lifetime]) => { - 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) -> Result<&$tcx Self, D::Error> { decode_arena_allocable(decoder) } } - impl<$tcx, D: TyDecoder<$tcx>> RefDecodable<$tcx, D> for [$ty] { + impl<$tcx, D: TyDecoder>> RefDecodable<$tcx, D> for [$ty] { #[inline] fn decode(decoder: &mut D) -> Result<&$tcx Self, D::Error> { decode_arena_allocable_slice(decoder) @@ -499,13 +504,13 @@ macro_rules! implement_ty_decoder { macro_rules! impl_binder_encode_decode { ($($t:ty),+ $(,)?) => { $( - impl<'tcx, E: TyEncoder<'tcx>> Encodable for ty::Binder<'tcx, $t> { + impl<'tcx, E: TyEncoder>> Encodable for ty::Binder<'tcx, $t> { fn encode(&self, e: &mut E) -> Result<(), E::Error> { self.bound_vars().encode(e)?; self.as_ref().skip_binder().encode(e) } } - impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Binder<'tcx, $t> { + impl<'tcx, D: TyDecoder>> Decodable for ty::Binder<'tcx, $t> { fn decode(decoder: &mut D) -> Result { let bound_vars = Decodable::decode(decoder)?; Ok(ty::Binder::bind_with_vars(Decodable::decode(decoder)?, bound_vars)) diff --git a/compiler/rustc_middle/src/ty/consts.rs b/compiler/rustc_middle/src/ty/consts.rs index c78151271c171..3165b65935849 100644 --- a/compiler/rustc_middle/src/ty/consts.rs +++ b/compiler/rustc_middle/src/ty/consts.rs @@ -1,7 +1,7 @@ use crate::mir::interpret::ConstValue; use crate::mir::interpret::{LitToConstInput, Scalar}; use crate::ty::subst::InternalSubsts; -use crate::ty::{self, Ty, TyCtxt}; +use crate::ty::{self, Ty, TyCtxt, TyInterner}; use crate::ty::{ParamEnv, ParamEnvAnd}; use rustc_errors::ErrorReported; use rustc_hir as hir; @@ -25,6 +25,14 @@ pub struct Const<'tcx> { pub val: ConstKind<'tcx>, } +impl>> 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!(Const<'_>, 48); diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 2a72e44af20ea..6e3656946475c 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -1,7 +1,5 @@ //! Type context book-keeping. -// ignore-tidy-filelength - use crate::arena::Arena; use crate::dep_graph::DepGraph; use crate::hir::exports::ExportMap; @@ -13,25 +11,18 @@ use crate::middle; use crate::middle::cstore::{CrateStoreDyn, EncodedMetadata}; use crate::middle::resolve_lifetime::{self, LifetimeScopeForPath, ObjectLifetimeDefault}; use crate::middle::stability; -use crate::mir::abstract_const::{Node as ConstNode, NodeId}; -use crate::mir::coverage::CodeRegion; -use crate::mir::interpret::{self, AllocId, Allocation, ConstValue, Scalar}; -use crate::mir::{ - Body, BorrowCheckResult, Field, Local, Place, PlaceElem, ProjectionKind, Promoted, - UnsafetyCheckResult, -}; +use crate::mir::interpret::{self, Allocation, ConstValue, Scalar}; +use crate::mir::{Body, Field, Local, Place, PlaceElem, ProjectionKind, Promoted}; use crate::thir::Thir; use crate::traits; use crate::ty::query::{self, OnDiskCache, TyCtxtAt}; use crate::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, Subst, SubstsRef, UserSubsts}; -use crate::ty::TyKind::*; use crate::ty::{ - self, AdtDef, AdtKind, Binder, BindingMode, BoundVar, BoundVariableKind, CanonicalPolyFnSig, - Const, ConstVid, DefIdTree, ExistentialPredicate, FloatTy, FloatVar, FloatVid, - GenericParamDefKind, InferConst, InferTy, Instance, IntTy, IntVar, IntVid, List, - MainDefinition, ParamConst, ParamTy, PolyFnSig, Predicate, PredicateInner, PredicateKind, - ProjectionTy, Region, RegionKind, ReprOptions, SymbolName, TraitObjectVisitor, Ty, TyKind, TyS, - TyVar, TyVid, TypeAndMut, UintTy, Visibility, + self, AdtDef, AdtKind, Binder, BindingMode, BoundVar, CanonicalPolyFnSig, Const, ConstVid, + DefIdTree, ExistentialPredicate, FloatTy, FloatVar, FloatVid, GenericParamDefKind, InferConst, + InferTy, IntTy, IntVar, IntVid, List, MainDefinition, ParamConst, ParamTy, PolyFnSig, + Predicate, PredicateInner, PredicateKind, ProjectionTy, Region, RegionKind, ReprOptions, + TraitObjectVisitor, Ty, TyKind, TyS, TyVar, TyVid, TypeAndMut, UintTy, Visibility, }; use rustc_ast as ast; use rustc_ast::expand::allocator::AllocatorKind; @@ -46,7 +37,7 @@ use rustc_data_structures::vec_map::VecMap; use rustc_errors::ErrorReported; use rustc_hir as hir; use rustc_hir::def::{DefKind, Res}; -use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefPathHash, LocalDefId, LOCAL_CRATE}; +use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, LOCAL_CRATE}; use rustc_hir::definitions::Definitions; use rustc_hir::intravisit::Visitor; use rustc_hir::lang_items::LangItem; @@ -67,6 +58,7 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::{Span, DUMMY_SP}; use rustc_target::abi::{Layout, TargetDataLayout, VariantIdx}; use rustc_target::spec::abi; +use rustc_type_ir::sty::TyKind::*; use rustc_type_ir::{InternAs, InternIteratorElement, Interner}; use std::any::Any; @@ -81,309 +73,66 @@ use std::ops::{Bound, Deref}; use std::sync::Arc; pub struct TyInterner<'tcx> { - tcx: TyCtxt<'tcx>, + pub tcx: TyCtxt<'tcx>, } -#[allow(rustc::usage_of_ty_tykind)] -impl<'tcx> Interner for TyInterner<'tcx> { - type GenericArg = GenericArg<'tcx>; - type ListGenericArg = &'tcx List>; - - type ExistentialPredicate = ty::Binder<'tcx, ExistentialPredicate<'tcx>>; - type ListExistentialPredicate = &'tcx List>>; - - type Predicate = Predicate<'tcx>; - type BinderPredicateKind = Binder<'tcx, PredicateKind<'tcx>>; - - type Ty = Ty<'tcx>; - type ListType = &'tcx List>; - type TyKind = TyKind<'tcx>; - type TypeKey = ty::CReaderCacheKey; - - type AllocationId = AllocId; - type Allocation = Allocation; - type InternedAllocation = &'tcx Allocation; - - type DefId = DefId; - type Instance = Instance<'tcx>; - - type CanonicalVarInfo = CanonicalVarInfo<'tcx>; - type ListCanonicalVarInfo = CanonicalVarInfos<'tcx>; - - type RegionKind = RegionKind; - type Region = Region<'tcx>; - - type Const = Const<'tcx>; - type InternedConst = &'tcx Const<'tcx>; - - type BoundVariableKind = BoundVariableKind; - type ListBoundVariableKind = &'tcx List; - - type PlaceElem = PlaceElem<'tcx>; - type ListPlaceElem = &'tcx List>; - - type DefPathHash = DefPathHash; - type AdtDef = &'tcx ty::AdtDef; - - type SymbolName = SymbolName<'tcx>; - - type Mir = Body<'tcx>; - type AllocatedMir = &'tcx mut Body<'tcx>; - type AllocatedMirSlice = &'tcx mut [Body<'tcx>]; - - type Promoted = IndexVec>; - type AllocatedPromoted = &'tcx mut IndexVec>; - type AllocatedPromotedSlice = &'tcx mut [IndexVec>]; - - type TypeCheckResults = TypeckResults<'tcx>; - type AllocatedTypeCheckResults = &'tcx mut TypeckResults<'tcx>; - type AllocatedTypeCheckResultsSlice = &'tcx mut [TypeckResults<'tcx>]; - - type BorrowCheckResult = BorrowCheckResult<'tcx>; - type AllocatedBorrowCheckResult = &'tcx mut BorrowCheckResult<'tcx>; - type AllocatedBorrowCheckResultSlice = &'tcx mut [BorrowCheckResult<'tcx>]; - - type CodeRegion = CodeRegion; - type AllocatedCodeRegion = &'tcx mut CodeRegion; - type AllocatedCodeRegionSlice = &'tcx mut [CodeRegion]; - - type UnsafetyCheckResult = UnsafetyCheckResult; - type AllocatedUnsafetyCheckResult = &'tcx mut UnsafetyCheckResult; - type AllocatedUnsafetyCheckResultSlice = &'tcx mut [UnsafetyCheckResult]; - - type Span = Span; - type AllocatedSpan = &'tcx mut Span; - type AllocatedSpanSlice = &'tcx mut [Span]; - - type UsedTraitsImports = FxHashSet; - type AllocatedUsedTraitsImports = &'tcx mut FxHashSet; - type AllocatedUsedTraitsImportsSlice = &'tcx mut [FxHashSet]; - - type AsmTemplate = ast::InlineAsmTemplatePiece; - type AllocatedAsmTemplate = &'tcx mut ast::InlineAsmTemplatePiece; - type AllocatedAsmTemplateSlice = &'tcx mut [ast::InlineAsmTemplatePiece]; - - type ValTree = ty::ValTree<'tcx>; - type AllocatedValTreeSlice = &'tcx mut [ty::ValTree<'tcx>]; - - type PredicateSpan = (ty::Predicate<'tcx>, Span); - type AllocatedPredicateSpanSlice = &'tcx mut [(ty::Predicate<'tcx>, Span)]; - - type Node = ConstNode<'tcx>; - type AllocatedNodeSlice = &'tcx mut [ConstNode<'tcx>]; - - type NodeId = NodeId; - type AllocatedNodeIdSlice = &'tcx mut [NodeId]; - - fn alloc_predicate_span_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedPredicateSpanSlice { - self.tcx.arena.alloc_from_iter(iter) - } - - fn alloc_valtree_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedValTreeSlice { - self.tcx.arena.alloc_from_iter(iter) - } - - fn alloc_node_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedNodeSlice { - self.tcx.arena.alloc_from_iter(iter) - } - - fn alloc_node_id_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedNodeIdSlice { - self.tcx.arena.alloc_from_iter(iter) - } - - fn alloc_span(self, value: Self::Span) -> Self::AllocatedSpan { - self.tcx.arena.alloc::<_, Self::Span>(value) - } - - fn alloc_span_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedSpanSlice { - self.tcx.arena.alloc_from_iter::<_, Self::Span, _>(iter) - } - - fn alloc_promoted(self, value: Self::Promoted) -> Self::AllocatedPromoted { - self.tcx.arena.alloc(value) - } - - fn alloc_promoted_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedPromotedSlice { - self.tcx.arena.alloc_from_iter(iter) - } - - fn alloc_borrowck_result( - self, - value: Self::BorrowCheckResult, - ) -> Self::AllocatedBorrowCheckResult { - self.tcx.arena.alloc(value) - } - - fn alloc_borrowck_result_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedBorrowCheckResultSlice { - self.tcx.arena.alloc_from_iter(iter) - } - - fn alloc_type_check_results( - self, - value: Self::TypeCheckResults, - ) -> Self::AllocatedTypeCheckResults { - self.tcx.arena.alloc(value) - } - - fn alloc_type_check_results_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedTypeCheckResultsSlice { - self.tcx.arena.alloc_from_iter(iter) - } - - fn alloc_mir(self, value: Self::Mir) -> Self::AllocatedMir { - self.tcx.arena.alloc(value) - } - - fn alloc_mir_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedMirSlice { - self.tcx.arena.alloc_from_iter(iter) - } - - fn alloc_code_region(self, value: Self::CodeRegion) -> Self::AllocatedCodeRegion { - self.tcx.arena.alloc(value) - } - - fn alloc_code_region_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedCodeRegionSlice { - self.tcx.arena.alloc_from_iter(iter) - } - - fn alloc_unsafety_check_result( - self, - value: Self::UnsafetyCheckResult, - ) -> Self::AllocatedUnsafetyCheckResult { - self.tcx.arena.alloc(value) - } - - fn alloc_unsafety_check_result_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedUnsafetyCheckResultSlice { - self.tcx.arena.alloc_from_iter(iter) - } - - fn alloc_used_trait_imports( - self, - value: Self::UsedTraitsImports, - ) -> Self::AllocatedUsedTraitsImports { - self.tcx.arena.alloc(value) - } - - fn alloc_used_trait_imports_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedUsedTraitsImportsSlice { - self.tcx.arena.alloc_from_iter(iter) - } - - fn alloc_asm_template(self, value: Self::AsmTemplate) -> Self::AllocatedAsmTemplate { - self.tcx.arena.alloc(value) - } - - fn alloc_asm_template_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedAsmTemplateSlice { - self.tcx.arena.alloc_from_iter(iter) - } - - fn mk_symbol_name(self, name: &str) -> Self::SymbolName { - SymbolName::new(self.tcx, name) - } - - fn adt_def(self, def_id: Self::DefId) -> Self::AdtDef { - self.tcx.adt_def(def_id) - } - - fn mk_predicate(self, binder: Self::BinderPredicateKind) -> Self::Predicate { - self.tcx.mk_predicate(binder) - } - - fn mk_ty(self, st: TyKind<'tcx>) -> Ty<'tcx> { - self.tcx.mk_ty(st) - } - - fn mk_substs>( - self, - iter: I, - ) -> I::Output { - self.tcx.mk_substs(iter) - } - - fn mk_poly_existential_predicates< - I: InternAs<[Self::ExistentialPredicate], Self::ListExistentialPredicate>, - >( - self, - iter: I, - ) -> I::Output { - self.tcx.mk_poly_existential_predicates(iter) - } - - fn mk_type_list>(self, iter: I) -> I::Output { - iter.intern_with(|xs| self.tcx.intern_type_list(xs)) - } +/// We don't ever actually need this. It's only required for derives. +impl<'tcx> Hash for TyInterner<'tcx> { + fn hash(&self, _state: &mut H) {} +} - fn mk_place_elems>( - self, - iter: I, - ) -> I::Output { - iter.intern_with(|xs| self.tcx.intern_place_elems(xs)) +/// We don't ever actually need this. It's only required for derives. +impl<'tcx> Ord for TyInterner<'tcx> { + fn cmp(&self, _other: &Self) -> Ordering { + Ordering::Equal } +} - fn mk_region(self, kind: Self::RegionKind) -> Self::Region { - self.tcx.mk_region(kind) +/// We don't ever actually need this. It's only required for derives. +impl<'tcx> PartialOrd for TyInterner<'tcx> { + fn partial_cmp(&self, _other: &Self) -> Option { + None } +} - fn mk_const(self, c: Self::Const) -> Self::InternedConst { - self.tcx.mk_const(c) +/// We don't ever actually need this. It's only required for derives. +impl<'tcx> PartialEq for TyInterner<'tcx> { + fn eq(&self, _other: &Self) -> bool { + false } +} - fn mk_bound_variable_kinds< - I: InternAs<[Self::BoundVariableKind], Self::ListBoundVariableKind>, - >( - self, - iter: I, - ) -> I::Output { - self.tcx.mk_bound_variable_kinds(iter) - } +/// We don't ever actually need this. It's only required for derives. +impl<'tcx> Eq for TyInterner<'tcx> {} - fn intern_const_alloc(self, alloc: Self::Allocation) -> Self::InternedAllocation { - self.tcx.interners.allocation.intern(alloc, |alloc| Interned(self.tcx.arena.alloc(alloc))).0 +impl fmt::Debug for TyInterner<'_> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "TyInterner") } +} - fn intern_canonical_var_infos( - self, - ts: &[Self::CanonicalVarInfo], - ) -> Self::ListCanonicalVarInfo { - self.tcx.intern_canonical_var_infos(ts) - } +#[allow(rustc::usage_of_ty_tykind)] +impl<'tcx> Interner for TyInterner<'tcx> { + type AdtDef = &'tcx ty::AdtDef; + type SubstsRef = ty::SubstsRef<'tcx>; + type DefId = DefId; + type Ty = Ty<'tcx>; + type Const = &'tcx ty::Const<'tcx>; + type Region = Region<'tcx>; + type TypeAndMut = TypeAndMut<'tcx>; + type Mutability = hir::Mutability; + type Movability = hir::Movability; + type PolyFnSig = PolyFnSig<'tcx>; + type ListBinderExistentialPredicate = &'tcx List>>; + type BinderListTy = Binder<'tcx, &'tcx List>>; + type ProjectionTy = ty::ProjectionTy<'tcx>; + type ParamTy = ParamTy; + type BoundTy = ty::BoundTy; + type PlaceholderType = ty::PlaceholderType; + type InferTy = InferTy; + type DelaySpanBugEmitted = DelaySpanBugEmitted; + type PredicateKind = ty::PredicateKind<'tcx>; + type AllocId = crate::mir::interpret::AllocId; } /// A type that is not publicly constructable. This prevents people from making [`TyKind::Error`]s diff --git a/compiler/rustc_middle/src/ty/diagnostics.rs b/compiler/rustc_middle/src/ty/diagnostics.rs index bfb4c0cb538de..2fe9024358057 100644 --- a/compiler/rustc_middle/src/ty/diagnostics.rs +++ b/compiler/rustc_middle/src/ty/diagnostics.rs @@ -1,11 +1,11 @@ //! Diagnostics related methods for `TyS`. -use crate::ty::TyKind::*; use crate::ty::{InferTy, TyCtxt, TyS}; use rustc_errors::{Applicability, DiagnosticBuilder}; use rustc_hir as hir; use rustc_hir::def_id::DefId; use rustc_hir::{QPath, TyKind, WhereBoundPredicate, WherePredicate}; +use rustc_type_ir::sty::TyKind::*; impl<'tcx> TyS<'tcx> { /// Similar to `TyS::is_primitive`, but also considers inferred numeric values to be primitive. diff --git a/compiler/rustc_middle/src/ty/inhabitedness/mod.rs b/compiler/rustc_middle/src/ty/inhabitedness/mod.rs index 119cb135046db..6bbfe590d00c8 100644 --- a/compiler/rustc_middle/src/ty/inhabitedness/mod.rs +++ b/compiler/rustc_middle/src/ty/inhabitedness/mod.rs @@ -2,11 +2,12 @@ pub use self::def_id_forest::DefIdForest; use crate::ty; use crate::ty::context::TyCtxt; -use crate::ty::TyKind::*; use crate::ty::{AdtDef, FieldDef, Ty, TyS, VariantDef}; use crate::ty::{AdtKind, Visibility}; use crate::ty::{DefId, SubstsRef}; +use rustc_type_ir::sty::TyKind::*; + mod def_id_forest; // The methods in this module calculate `DefIdForest`s of modules in which a diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 674fd51ce302a..f8a22c96d56f3 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -50,6 +50,7 @@ use std::{fmt, ptr, str}; pub use crate::ty::diagnostics::*; pub use rustc_type_ir::InferTy::*; +pub use rustc_type_ir::TyKind::*; pub use rustc_type_ir::*; pub use self::binding::BindingMode; @@ -64,7 +65,6 @@ pub use self::instance::{Instance, InstanceDef}; pub use self::list::List; pub use self::sty::BoundRegionKind::*; pub use self::sty::RegionKind::*; -pub use self::sty::TyKind::*; pub use self::sty::{ Binder, BoundRegion, BoundRegionKind, BoundTy, BoundTyKind, BoundVar, BoundVariableKind, CanonicalPolyFnSig, ClosureSubsts, ClosureSubstsParts, ConstVid, EarlyBoundRegion, diff --git a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs index ebaef347f4293..f3364de8d8217 100644 --- a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs @@ -3,7 +3,7 @@ use crate::mir::interpret::{AllocDecodingSession, AllocDecodingState}; use crate::mir::{self, interpret}; use crate::ty::codec::{RefDecodable, TyDecoder, TyEncoder}; use crate::ty::context::TyCtxt; -use crate::ty::{self, Ty}; +use crate::ty::{self, Ty, TyInterner}; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet}; use rustc_data_structures::sync::{HashMapExt, Lock, Lrc, OnceCell}; use rustc_data_structures::thin_vec::ThinVec; @@ -671,12 +671,13 @@ where Ok(value) } -impl<'a, 'tcx> TyDecoder<'tcx> for CacheDecoder<'a, 'tcx> { +impl<'a, 'tcx> TyDecoder for CacheDecoder<'a, 'tcx> { + type I = TyInterner<'tcx>; const CLEAR_CROSS_CRATE: bool = false; #[inline] - fn tcx(&self) -> TyCtxt<'tcx> { - self.tcx + fn interner(&self) -> TyInterner<'tcx> { + TyInterner { tcx: self.tcx } } #[inline] @@ -697,7 +698,7 @@ impl<'a, 'tcx> TyDecoder<'tcx> for CacheDecoder<'a, 'tcx> { where F: FnOnce(&mut Self) -> Result, Self::Error>, { - let tcx = self.tcx(); + let tcx = self.tcx; let cache_key = ty::CReaderCacheKey { cnum: None, pos: shorthand }; @@ -834,11 +835,11 @@ impl<'a, 'tcx> Decodable> for DefId { // If we get to this point, then all of the query inputs were green, // which means that the definition with this hash is guaranteed to // still exist in the current compilation session. - Ok(d.tcx() + Ok(d.tcx .on_disk_cache .as_ref() .unwrap() - .def_path_hash_to_def_id(d.tcx(), def_path_hash) + .def_path_hash_to_def_id(d.tcx, def_path_hash) .unwrap()) } } @@ -995,10 +996,11 @@ where } } -impl<'a, 'tcx, E> TyEncoder<'tcx> for CacheEncoder<'a, 'tcx, E> +impl<'a, 'tcx, E> TyEncoder for CacheEncoder<'a, 'tcx, E> where E: 'a + OpaqueEncoder, { + type I = TyInterner<'tcx>; const CLEAR_CROSS_CRATE: bool = false; fn position(&self) -> usize { diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 1d9ff512288db..35902f120380c 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -2,23 +2,23 @@ #![allow(rustc::usage_of_ty_tykind)] -use self::TyKind::*; - use crate::infer::canonical::Canonical; use crate::ty::fold::BoundVarsCollector; use crate::ty::fold::ValidateBoundVars; use crate::ty::subst::{GenericArg, InternalSubsts, Subst, SubstsRef}; -use crate::ty::InferTy::{self, *}; +use crate::ty::InferTy::*; use crate::ty::{ self, AdtDef, DefIdTree, Discr, Ty, TyCtxt, TypeFlags, TypeFoldable, WithConstness, }; -use crate::ty::{DelaySpanBugEmitted, List, ParamEnv, TyS}; +use crate::ty::{List, ParamEnv, TyS}; use polonius_engine::Atom; use rustc_data_structures::captures::Captures; +use rustc_data_structures::stable_hasher::HashStable; use rustc_hir as hir; use rustc_hir::def_id::DefId; use rustc_index::vec::Idx; use rustc_macros::HashStable; +use rustc_middle::ich::StableHashingContext; use rustc_span::symbol::{kw, Symbol}; use rustc_target::abi::VariantIdx; use rustc_target::spec::abi; @@ -28,6 +28,10 @@ use std::marker::PhantomData; use std::ops::Range; use ty::util::IntTypeExt; +use rustc_type_ir::TyKind as IrTyKind; +pub type TyKind<'tcx> = IrTyKind>; +use rustc_type_ir::sty::TyKind::*; + #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, TyEncodable, TyDecodable)] #[derive(HashStable, TypeFoldable, Lift)] pub struct TypeAndMut<'tcx> { @@ -77,6 +81,7 @@ impl BoundRegionKind { } } +/* /// Defines the kinds of types. /// /// N.B., if you change this, you'll probably want to change the corresponding @@ -200,7 +205,9 @@ pub enum TyKind<'tcx> { /// propagated to avoid useless error messages. Error(DelaySpanBugEmitted), } +*/ +/* impl TyKind<'tcx> { #[inline] pub fn is_primitive(&self) -> bool { @@ -219,6 +226,7 @@ impl TyKind<'tcx> { } } } +*/ // `TyKind` is used a lot. Make sure it doesn't unintentionally get bigger. #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))] @@ -812,7 +820,9 @@ impl<'tcx> List>> { } #[inline] - pub fn auto_traits<'a>(&'a self) -> impl Iterator + 'a { + pub fn auto_traits<'a>( + &'a self, + ) -> impl Iterator + rustc_data_structures::captures::Captures<'tcx> + 'a { self.iter().filter_map(|predicate| match predicate.skip_binder() { ExistentialPredicate::AutoTrait(did) => Some(did), _ => None, @@ -2233,3 +2243,122 @@ impl<'tcx> Default for VarianceDiagInfo<'tcx> { Self::None } } + +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::ProjectionTy: HashStable>, + I::BoundTy: HashStable>, + I::ParamTy: HashStable>, + I::PlaceholderType: HashStable>, + I::InferTy: HashStable>, + I::DelaySpanBugEmitted: HashStable>, +{ + #[inline] + fn hash_stable( + &self, + __hcx: &mut rustc_middle::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_middle/src/ty/subst.rs b/compiler/rustc_middle/src/ty/subst.rs index 9b8d22d8eafce..88a45feeda9e4 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::{TypeFoldable, TypeFolder, TypeVisitor}; use crate::ty::sty::{ClosureSubsts, GeneratorSubsts}; -use crate::ty::{self, Lift, List, ParamConst, Ty, TyCtxt}; +use crate::ty::{self, Lift, List, ParamConst, Ty, TyCtxt, TyInterner}; use rustc_hir::def_id::DefId; use rustc_macros::HashStable; @@ -170,13 +170,13 @@ impl<'tcx> TypeFoldable<'tcx> for GenericArg<'tcx> { } } -impl<'tcx, E: TyEncoder<'tcx>> 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<'tcx>> Decodable for GenericArg<'tcx> { +impl<'tcx, D: TyDecoder>> Decodable for GenericArg<'tcx> { fn decode(d: &mut D) -> Result, D::Error> { Ok(GenericArgKind::decode(d)?.pack()) } diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index 7bf69b9e637e9..5570e224de0a2 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -6,7 +6,6 @@ use crate::ty::fold::TypeFolder; use crate::ty::layout::IntegerExt; use crate::ty::query::TyCtxtAt; use crate::ty::subst::{GenericArgKind, Subst, SubstsRef}; -use crate::ty::TyKind::*; use crate::ty::{self, DefIdTree, List, Ty, TyCtxt, TypeFoldable}; use rustc_apfloat::Float as _; use rustc_ast as ast; @@ -20,6 +19,7 @@ use rustc_hir::def_id::DefId; use rustc_macros::HashStable; use rustc_span::DUMMY_SP; use rustc_target::abi::{Integer, Size, TargetDataLayout}; +use rustc_type_ir::TyKind::*; use smallvec::SmallVec; use std::{fmt, iter}; diff --git a/compiler/rustc_mir/src/interpret/cast.rs b/compiler/rustc_mir/src/interpret/cast.rs index 848b44d13aadf..919acc8f70126 100644 --- a/compiler/rustc_mir/src/interpret/cast.rs +++ b/compiler/rustc_mir/src/interpret/cast.rs @@ -102,7 +102,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { src: &ImmTy<'tcx, M::PointerTag>, cast_ty: Ty<'tcx>, ) -> InterpResult<'tcx, Immediate> { - use rustc_middle::ty::TyKind::*; + use rustc_type_ir::sty::TyKind::*; trace!("Casting {:?}: {:?} to {:?}", *src, src.layout.ty, cast_ty); match src.layout.ty.kind() { @@ -189,7 +189,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { let signed = src_layout.abi.is_signed(); // Also asserts that abi is `Scalar`. let v = if signed { self.sign_extend(v, src_layout) } else { v }; trace!("cast_from_scalar: {}, {} -> {}", v, src_layout.ty, cast_ty); - use rustc_middle::ty::TyKind::*; + use rustc_type_ir::sty::TyKind::*; match *cast_ty.kind() { Int(_) | Uint(_) | RawPtr(_) => { let size = match *cast_ty.kind() { @@ -221,7 +221,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { where F: Float + Into> + FloatConvert + FloatConvert, { - use rustc_middle::ty::TyKind::*; + use rustc_type_ir::sty::TyKind::*; match *dest_ty.kind() { // float -> uint Uint(t) => { diff --git a/compiler/rustc_ty_utils/src/ty.rs b/compiler/rustc_ty_utils/src/ty.rs index ebc7b0d0d99cf..5576ea5fa4917 100644 --- a/compiler/rustc_ty_utils/src/ty.rs +++ b/compiler/rustc_ty_utils/src/ty.rs @@ -15,7 +15,7 @@ fn sized_constraint_for_ty<'tcx>( adtdef: &ty::AdtDef, ty: Ty<'tcx>, ) -> Vec> { - use ty::TyKind::*; + use rustc_type_ir::sty::TyKind::*; let result = match ty.kind() { Bool | Char | Int(..) | Uint(..) | Float(..) | RawPtr(..) | Ref(..) | FnDef(..) diff --git a/compiler/rustc_type_ir/src/codec.rs b/compiler/rustc_type_ir/src/codec.rs new file mode 100644 index 0000000000000..92114c3db1ede --- /dev/null +++ b/compiler/rustc_type_ir/src/codec.rs @@ -0,0 +1,63 @@ +use crate::Interner; + +use rustc_data_structures::stable_map::FxHashMap; +use rustc_serialize::{Decoder, Encoder}; + +/// 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 `TyDecodable` 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) -> Result<&'tcx Self, D::Error>; +} + +pub trait TyEncoder: Encoder { + 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<::PredicateKind, usize>; + fn encode_alloc_id(&mut self, alloc_id: &::AllocId) -> Result<(), Self::Error>; +} + + +pub trait TyDecoder: Decoder { + type I: Interner; + const CLEAR_CROSS_CRATE: bool; + + fn interner(&self) -> Self::I; + + fn peek_byte(&self) -> u8; + + fn position(&self) -> usize; + + fn cached_ty_for_shorthand( + &mut self, + shorthand: usize, + or_insert_with: F, + ) -> Result<::Ty, Self::Error> + where + F: FnOnce(&mut Self) -> Result<::Ty, Self::Error>; + + 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) -> Result<::AllocId, Self::Error>; +} diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index f1247daa7b0ed..cc7dcd9dfe655 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -1,4 +1,5 @@ #![feature(min_specialization)] +#![feature(rustc_attrs)] #[macro_use] extern crate bitflags; @@ -7,216 +8,42 @@ 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::iter::IntoIterator; +use std::fmt::Debug; +use std::hash::Hash; use std::mem::discriminant; -pub trait Interner { - type GenericArg; - type ListGenericArg; - - type ExistentialPredicate; - type ListExistentialPredicate; - - type Predicate; - type BinderPredicateKind; +pub mod codec; +pub mod sty; - type Ty; - type ListType; - type TyKind; - type TypeKey; +pub use codec::*; +pub use sty::*; - type AllocationId; - type Allocation; - type InternedAllocation; +extern crate self as rustc_type_ir; - type DefId; - type Instance; - - type CanonicalVarInfo; - type ListCanonicalVarInfo; - - type RegionKind; - type Region; - - type Const; - type InternedConst; - - type BoundVariableKind; - type ListBoundVariableKind; - - type PlaceElem; - type ListPlaceElem; - - type DefPathHash; - type AdtDef; - - type SymbolName; - - type Mir; - type AllocatedMir; - type AllocatedMirSlice; - - type Promoted; - type AllocatedPromoted; - type AllocatedPromotedSlice; - - type TypeCheckResults; - type AllocatedTypeCheckResults; - type AllocatedTypeCheckResultsSlice; - - type BorrowCheckResult; - type AllocatedBorrowCheckResult; - type AllocatedBorrowCheckResultSlice; - - type CodeRegion; - type AllocatedCodeRegion; - type AllocatedCodeRegionSlice; - - type UnsafetyCheckResult; - type AllocatedUnsafetyCheckResult; - type AllocatedUnsafetyCheckResultSlice; - - type Span; - type AllocatedSpan; - type AllocatedSpanSlice; - - type UsedTraitsImports; - type AllocatedUsedTraitsImports; - type AllocatedUsedTraitsImportsSlice; - - type AsmTemplate; - type AllocatedAsmTemplate; - type AllocatedAsmTemplateSlice; - - type ValTree; - type AllocatedValTreeSlice; - - type PredicateSpan; - type AllocatedPredicateSpanSlice; - - type Node; - type AllocatedNodeSlice; - - type NodeId; - type AllocatedNodeIdSlice; - - fn alloc_mir(self, value: Self::Mir) -> Self::AllocatedMir; - fn alloc_mir_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedMirSlice; - - fn alloc_promoted(self, value: Self::Promoted) -> Self::AllocatedPromoted; - fn alloc_promoted_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedPromotedSlice; - - fn alloc_type_check_results( - self, - value: Self::TypeCheckResults, - ) -> Self::AllocatedTypeCheckResults; - fn alloc_type_check_results_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedTypeCheckResultsSlice; - - fn alloc_borrowck_result( - self, - value: Self::BorrowCheckResult, - ) -> Self::AllocatedBorrowCheckResult; - fn alloc_borrowck_result_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedBorrowCheckResultSlice; - - fn alloc_unsafety_check_result( - self, - value: Self::UnsafetyCheckResult, - ) -> Self::AllocatedUnsafetyCheckResult; - fn alloc_unsafety_check_result_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedUnsafetyCheckResultSlice; - - fn alloc_code_region(self, value: Self::CodeRegion) -> Self::AllocatedCodeRegion; - fn alloc_code_region_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedCodeRegionSlice; - - fn alloc_span(self, value: Self::Span) -> Self::AllocatedSpan; - fn alloc_span_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedSpanSlice; - - fn alloc_used_trait_imports( - self, - value: Self::UsedTraitsImports, - ) -> Self::AllocatedUsedTraitsImports; - fn alloc_used_trait_imports_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedUsedTraitsImportsSlice; - - fn alloc_asm_template(self, value: Self::AsmTemplate) -> Self::AllocatedAsmTemplate; - fn alloc_asm_template_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedAsmTemplateSlice; - - fn alloc_valtree_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedValTreeSlice; - fn alloc_predicate_span_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedPredicateSpanSlice; - fn alloc_node_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedNodeSlice; - fn alloc_node_id_from_iter( - self, - iter: impl IntoIterator, - ) -> Self::AllocatedNodeIdSlice; - - fn adt_def(self, def_id: Self::DefId) -> Self::AdtDef; - fn mk_symbol_name(self, name: &str) -> Self::SymbolName; - fn mk_predicate(self, binder: Self::BinderPredicateKind) -> Self::Predicate; - fn mk_ty(self, st: Self::TyKind) -> Self::Ty; - fn mk_substs>(self, iter: I) - -> I::Output; - fn mk_poly_existential_predicates< - I: InternAs<[Self::ExistentialPredicate], Self::ListExistentialPredicate>, - >( - self, - iter: I, - ) -> I::Output; - fn mk_type_list>(self, iter: I) -> I::Output; - fn mk_place_elems>( - self, - iter: I, - ) -> I::Output; - fn mk_region(self, kind: Self::RegionKind) -> Self::Region; - fn mk_const(self, c: Self::Const) -> Self::InternedConst; - fn mk_bound_variable_kinds< - I: InternAs<[Self::BoundVariableKind], Self::ListBoundVariableKind>, - >( - self, - iter: I, - ) -> I::Output; - fn intern_const_alloc(self, alloc: Self::Allocation) -> Self::InternedAllocation; - fn intern_canonical_var_infos( - self, - ts: &[Self::CanonicalVarInfo], - ) -> Self::ListCanonicalVarInfo; - - // arena methods +pub trait Interner { + type AdtDef: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; + type SubstsRef: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; + type DefId: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; + type Ty: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; + type Const: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; + type Region: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; + type TypeAndMut: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; + type Mutability: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; + type Movability: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; + type PolyFnSig: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; + type ListBinderExistentialPredicate: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; + type BinderListTy: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; + type ProjectionTy: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; + type ParamTy: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; + type BoundTy: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; + type PlaceholderType: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; + type InferTy: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; + type DelaySpanBugEmitted: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; + type PredicateKind: Clone + Debug + Hash + PartialEq + Eq; + type AllocId: Clone + Debug + Hash + PartialEq + Eq + PartialOrd + Ord; } pub trait InternAs { diff --git a/compiler/rustc_type_ir/src/sty.rs b/compiler/rustc_type_ir/src/sty.rs new file mode 100644 index 0000000000000..d31fa5936a9e6 --- /dev/null +++ b/compiler/rustc_type_ir/src/sty.rs @@ -0,0 +1,437 @@ +use crate::DebruijnIndex; +use crate::FloatTy; +use crate::IntTy; +use crate::UintTy; +use crate::Interner; +use crate::TyDecoder; +use crate::TyEncoder; + +use rustc_serialize::{Decodable, Encodable}; + +/// Defines the kinds of types. +/// +/// N.B., if you change this, you'll probably want to change the corresponding +/// AST structure in `rustc_ast/src/ast.rs` as well. +#[allow(rustc::usage_of_ty_tykind)] +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +//#[derive(TyEncodable, TyDecodable)] +#[rustc_diagnostic_item = "TyKind"] +pub enum TyKind { + /// The primitive boolean type. Written as `bool`. + Bool, + + /// The primitive character type; holds a Unicode scalar value + /// (a non-surrogate code point). Written as `char`. + Char, + + /// A primitive signed integer type. For example, `i32`. + Int(IntTy), + + /// A primitive unsigned integer type. For example, `u32`. + Uint(UintTy), + + /// A primitive floating-point type. For example, `f64`. + Float(FloatTy), + + /// Algebraic data types (ADT). For example: structures, enumerations and unions. + /// + /// InternalSubsts here, possibly against intuition, *may* contain `Param`s. + /// That is, even after substitution it is possible that there are type + /// variables. This happens when the `Adt` corresponds to an ADT + /// definition and not a concrete use of it. + Adt(I::AdtDef, I::SubstsRef), + + /// An unsized FFI type that is opaque to Rust. Written as `extern type T`. + Foreign(I::DefId), + + /// The pointee of a string slice. Written as `str`. + Str, + + /// An array with the given length. Written as `[T; n]`. + Array(I::Ty, I::Const), + + /// The pointee of an array slice. Written as `[T]`. + Slice(I::Ty), + + /// A raw pointer. Written as `*mut T` or `*const T` + RawPtr(I::TypeAndMut), + + /// A reference; a pointer with an associated lifetime. Written as + /// `&'a mut T` or `&'a T`. + Ref(I::Region, I::Ty, I::Mutability), + + /// The anonymous type of a function declaration/definition. Each + /// function has a unique type, which is output (for a function + /// named `foo` returning an `i32`) as `fn() -> i32 {foo}`. + /// + /// For example the type of `bar` here: + /// + /// ```rust + /// fn foo() -> i32 { 1 } + /// let bar = foo; // bar: fn() -> i32 {foo} + /// ``` + FnDef(I::DefId, I::SubstsRef), + + /// A pointer to a function. Written as `fn() -> i32`. + /// + /// For example the type of `bar` here: + /// + /// ```rust + /// fn foo() -> i32 { 1 } + /// let bar: fn() -> i32 = foo; + /// ``` + FnPtr(I::PolyFnSig), + + /// A trait object. Written as `dyn for<'b> Trait<'b, Assoc = u32> + Send + 'a`. + Dynamic(I::ListBinderExistentialPredicate, I::Region), + + /// The anonymous type of a closure. Used to represent the type of + /// `|a| a`. + Closure(I::DefId, I::SubstsRef), + + /// The anonymous type of a generator. Used to represent the type of + /// `|a| yield a`. + Generator(I::DefId, I::SubstsRef, I::Movability), + + /// A type representing the types stored inside a generator. + /// This should only appear in GeneratorInteriors. + GeneratorWitness(I::BinderListTy), + + /// The never type `!`. + Never, + + /// A tuple type. For example, `(i32, bool)`. + /// Use `TyS::tuple_fields` to iterate over the field types. + Tuple(I::SubstsRef), + + /// The projection of an associated type. For example, + /// `>::N`. + Projection(I::ProjectionTy), + + /// Opaque (`impl Trait`) type found in a return type. + /// The `DefId` comes either from + /// * the `impl Trait` ast::Ty node, + /// * or the `type Foo = impl Trait` declaration + /// The substitutions are for the generics of the function in question. + /// After typeck, the concrete type can be found in the `types` map. + Opaque(I::DefId, I::SubstsRef), + + /// A type parameter; for example, `T` in `fn f(x: T) {}`. + Param(I::ParamTy), + + /// Bound type variable, used only when preparing a trait query. + Bound(DebruijnIndex, I::BoundTy), + + /// A placeholder type - universally quantified higher-ranked type. + Placeholder(I::PlaceholderType), + + /// A type variable used during type checking. + Infer(I::InferTy), + + /// A placeholder for a type which could not be computed; this is + /// propagated to avoid useless error messages. + Error(I::DelaySpanBugEmitted), +} + +#[allow(rustc::usage_of_ty_tykind)] +impl TyKind { + #[inline] + pub fn is_primitive(&self) -> bool { + use crate::TyKind::*; + matches!(self, Bool | Char | Int(_) | Uint(_) | Float(_)) + } +} + +#[allow(rustc::usage_of_ty_tykind)] +impl<__I: Interner, __E: TyEncoder> Encodable<__E> for TyKind<__I> + where + __I::DelaySpanBugEmitted: Encodable<__E>, + __I::AdtDef: Encodable<__E>, + __I::SubstsRef: Encodable<__E>, + __I::DefId: Encodable<__E>, + __I::Ty: Encodable<__E>, + __I::Const: Encodable<__E>, + __I::Region: Encodable<__E>, + __I::TypeAndMut: Encodable<__E>, + __I::Mutability: Encodable<__E>, + __I::Movability: Encodable<__E>, + __I::PolyFnSig: Encodable<__E>, + __I::ListBinderExistentialPredicate: Encodable<__E>, + __I::BinderListTy: Encodable<__E>, + __I::ProjectionTy: Encodable<__E>, + __I::ParamTy: Encodable<__E>, + __I::BoundTy: Encodable<__E>, + __I::PlaceholderType: Encodable<__E>, + __I::InferTy: Encodable<__E>, + __I::DelaySpanBugEmitted: Encodable<__E>, + __I::PredicateKind: Encodable<__E>, + __I::AllocId: Encodable<__E>, +{ + fn encode(&self, e: &mut __E) -> Result<(), <__E as rustc_serialize::Encoder>::Error> { + rustc_serialize::Encoder::emit_enum(e, |e| { + use rustc_type_ir::TyKind::*; + match self { + Bool => e.emit_enum_variant("Bool", 0, 0, |_| Ok(())), + Char => e.emit_enum_variant("Char", 1, 0, |_| Ok(())), + Int(i) => e.emit_enum_variant("Int", 2, 1, |e| { + e.emit_enum_variant_arg(true, |e| i.encode(e))?; + Ok(()) + }), + Uint(u) => e.emit_enum_variant("Uint", 3, 1, |e| { + e.emit_enum_variant_arg(true, |e| u.encode(e))?; + Ok(()) + }), + Float(f) => e.emit_enum_variant("Float", 4, 1, |e| { + e.emit_enum_variant_arg(true, |e| f.encode(e))?; + Ok(()) + }), + Adt(adt, substs) => e.emit_enum_variant("Adt", 5, 2, |e| { + e.emit_enum_variant_arg(true, |e| adt.encode(e))?; + e.emit_enum_variant_arg(false, |e| substs.encode(e))?; + Ok(()) + }), + Foreign(def_id) => e.emit_enum_variant("Foreign", 6, 1, |e| { + e.emit_enum_variant_arg(true, |e| def_id.encode(e))?; + Ok(()) + }), + Str => e.emit_enum_variant("Str", 7, 0, |_| Ok(())), + Array(t, c) => e.emit_enum_variant("Array", 8, 2, |e| { + e.emit_enum_variant_arg(true, |e| t.encode(e))?; + e.emit_enum_variant_arg(false, |e| c.encode(e))?; + Ok(()) + }), + Slice(t) => e.emit_enum_variant("Slice", 9, 1, |e| { + e.emit_enum_variant_arg(true, |e| t.encode(e))?; + Ok(()) + }), + RawPtr(tam) => e.emit_enum_variant("RawPtr", 10, 1, |e| { + e.emit_enum_variant_arg(true, |e| tam.encode(e))?; + Ok(()) + }), + Ref(r, t, m) => e.emit_enum_variant("Ref", 11, 3, |e| { + e.emit_enum_variant_arg(true, |e| r.encode(e))?; + e.emit_enum_variant_arg(false, |e| t.encode(e))?; + e.emit_enum_variant_arg(false, |e| m.encode(e))?; + Ok(()) + }), + FnDef(def_id, substs) => e.emit_enum_variant("FnDef", 12, 2, |e| { + e.emit_enum_variant_arg(true, |e| def_id.encode(e))?; + e.emit_enum_variant_arg(false, |e| substs.encode(e))?; + Ok(()) + }), + FnPtr(polyfnsig) => e.emit_enum_variant("FnPtr", 13, 1, |e| { + e.emit_enum_variant_arg(true, |e| polyfnsig.encode(e))?; + Ok(()) + }), + Dynamic(l, r) => e.emit_enum_variant("Dynamic", 14, 2, |e| { + e.emit_enum_variant_arg(true, |e| l.encode(e))?; + e.emit_enum_variant_arg(false, |e| r.encode(e))?; + Ok(()) + }), + Closure(def_id, substs) => e.emit_enum_variant("Closure", 15, 2, |e| { + e.emit_enum_variant_arg(true, |e| def_id.encode(e))?; + e.emit_enum_variant_arg(false, |e| substs.encode(e))?; + Ok(()) + }), + Generator(def_id, substs, m) => e.emit_enum_variant("Generator", 16, 3, |e| { + e.emit_enum_variant_arg(true, |e| def_id.encode(e))?; + e.emit_enum_variant_arg(false, |e| substs.encode(e))?; + e.emit_enum_variant_arg(false, |e| m.encode(e))?; + Ok(()) + }), + GeneratorWitness(b) => e.emit_enum_variant("GeneratorWitness", 17, 1, |e| { + e.emit_enum_variant_arg(true, |e| b.encode(e))?; + Ok(()) + }), + Never => e.emit_enum_variant("Never", 18, 0, |_| Ok(())), + Tuple(substs) => e.emit_enum_variant("Tuple", 19, 1, |e| { + e.emit_enum_variant_arg(true, |e| substs.encode(e))?; + Ok(()) + }), + Projection(p) => e.emit_enum_variant("Projection", 20, 1, |e| { + e.emit_enum_variant_arg(true, |e| p.encode(e))?; + Ok(()) + }), + Opaque(def_id, substs) => e.emit_enum_variant("Opaque", 21, 2, |e| { + e.emit_enum_variant_arg(true, |e| def_id.encode(e))?; + e.emit_enum_variant_arg(false, |e| substs.encode(e))?; + Ok(()) + }), + Param(p) => e.emit_enum_variant("Param", 22, 1, |e| { + e.emit_enum_variant_arg(true, |e| p.encode(e))?; + Ok(()) + }), + Bound(d, b) => e.emit_enum_variant("Bound", 23, 2, |e| { + e.emit_enum_variant_arg(true, |e| d.encode(e))?; + e.emit_enum_variant_arg(false, |e| b.encode(e))?; + Ok(()) + }), + Placeholder(p) => e.emit_enum_variant("Placeholder", 24, 1, |e| { + e.emit_enum_variant_arg(true, |e| p.encode(e))?; + Ok(()) + }), + Infer(i) => e.emit_enum_variant("Infer", 25, 1, |e| { + e.emit_enum_variant_arg(true, |e| i.encode(e))?; + Ok(()) + }), + Error(d) => e.emit_enum_variant("Error", 26, 1, |e| { + e.emit_enum_variant_arg(true, |e| d.encode(e))?; + Ok(()) + }), + } + }) + } +} + +#[allow(rustc::usage_of_ty_tykind)] +impl<__I: Interner, __D: TyDecoder> Decodable<__D> for TyKind<__I> + where + __I::DelaySpanBugEmitted: Decodable<__D>, + __I::AdtDef: Decodable<__D>, + __I::SubstsRef: Decodable<__D>, + __I::DefId: Decodable<__D>, + __I::Ty: Decodable<__D>, + __I::Const: Decodable<__D>, + __I::Region: Decodable<__D>, + __I::TypeAndMut: Decodable<__D>, + __I::Mutability: Decodable<__D>, + __I::Movability: Decodable<__D>, + __I::PolyFnSig: Decodable<__D>, + __I::ListBinderExistentialPredicate: Decodable<__D>, + __I::BinderListTy: Decodable<__D>, + __I::ProjectionTy: Decodable<__D>, + __I::ParamTy: Decodable<__D>, + __I::BoundTy: Decodable<__D>, + __I::PlaceholderType: Decodable<__D>, + __I::InferTy: Decodable<__D>, + __I::DelaySpanBugEmitted: Decodable<__D>, + __I::PredicateKind: Decodable<__D>, + __I::AllocId: Decodable<__D>, +{ + fn decode( + __decoder: &mut __D, + ) -> Result::Error> { + __decoder.read_enum( + |__decoder| { + __decoder.read_enum_variant( + &[ + "Bool", + "Char", + "Int", + "Uint", + "Float", + "Adt", + "Foreign", + "Str", + "Array", + "Slice", + "RawPtr", + "Ref", + "FnDef", + "FnPtr", + "Dynamic", + "Closure", + "Generator", + "GeneratorWitness", + "Never", + "Tuple", + "Projection", + "Opaque", + "Param", + "Bound", + "Placeholder", + "Infer", + "Error", + ], + |__decoder, __variant_idx| { + use TyKind::*; + Ok(match __variant_idx { + 0 => Bool, + 1 => Char, + 2 => Int(__decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?), + 3 => Uint(__decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?), + 4 => Float(__decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?), + 5 => Adt( + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + ), + 6 => Foreign( + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + ), + 7 => Str, + 8 => Array( + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + ), + 9 => Slice( + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + ), + 10 => RawPtr( + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + ), + 11 => Ref( + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + ), + 12 => FnDef( + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + ), + 13 => FnPtr( + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + ), + 14 => Dynamic( + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + ), + 15 => Closure( + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + ), + 16 => Generator( + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + ), + 17 => GeneratorWitness( + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + ), + 18 => Never, + 19 => Tuple( + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + ), + 20 => Projection( + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + ), + 21 => Opaque( + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + ), + 22 => Param( + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + ), + 23 => Bound( + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + ), + 24 => Placeholder( + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + ), + 25 => Infer( + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + ), + 26 => Error( + __decoder.read_enum_variant_arg(rustc_serialize::Decodable::decode)?, + ), + _ => return Err(rustc_serialize::Decoder::error(__decoder, &format!( + "invalid enum variant tag while decoding `{}`, expected 0..{}", + "TyKind", + 27, + ))), + }) + }) + } + ) + } +} diff --git a/compiler/rustc_typeck/src/coherence/builtin.rs b/compiler/rustc_typeck/src/coherence/builtin.rs index 8cae61e8c22f6..95a32a026bdca 100644 --- a/compiler/rustc_typeck/src/coherence/builtin.rs +++ b/compiler/rustc_typeck/src/coherence/builtin.rs @@ -145,7 +145,7 @@ fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDef tcx.infer_ctxt().enter(|infcx| { let cause = ObligationCause::misc(span, impl_hir_id); - use ty::TyKind::*; + use rustc_type_ir::sty::TyKind::*; match (source.kind(), target.kind()) { (&Ref(r_a, _, mutbl_a), Ref(r_b, _, mutbl_b)) if infcx.at(&cause, param_env).eq(r_a, r_b).is_ok() && mutbl_a == *mutbl_b => {}