From e2eb8e56a1e5f465e8753b2ae10c72b854e526c1 Mon Sep 17 00:00:00 2001 From: Wilco Kusee Date: Sun, 31 Jan 2021 10:32:34 +0100 Subject: [PATCH 1/7] Put mk_predicate in interner trait --- compiler/rustc_metadata/src/rmeta/decoder.rs | 8 +++-- compiler/rustc_middle/src/ty/codec.rs | 7 ++-- compiler/rustc_middle/src/ty/context.rs | 33 +++++++++++++++---- .../src/ty/query/on_disk_cache.rs | 7 +++- compiler/rustc_type_ir/src/lib.rs | 12 +++++++ 5 files changed, 56 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index e9b8388c1c915..4156452547369 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -27,8 +27,7 @@ use rustc_middle::middle::cstore::{ForeignModule, LinkagePreference, NativeLib}; 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}; +use rustc_middle::ty::{self, codec::TyDecoder, Ty, TyCtxt, TyInterner}; use rustc_serialize::{opaque, Decodable, Decoder}; use rustc_session::Session; use rustc_span::hygiene::ExpnDataDecodeMode; @@ -281,6 +280,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 73ad87a9ef219..5bad09519ab85 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::{CrateNum, 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,6 +162,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; @@ -244,7 +246,8 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Binder> 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 e1d79248171a8..07b5dce419491 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -18,11 +18,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, AdtDef, AdtKind, Binder, BindingMode, BoundVar, CanonicalPolyFnSig, Const, ConstVid, - DefIdTree, ExistentialPredicate, FloatTy, FloatVar, FloatVid, GenericParamDefKind, InferConst, - InferTy, IntTy, IntVar, IntVid, List, 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, 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; @@ -47,7 +47,10 @@ use rustc_hir::{ }; use rustc_index::vec::{Idx, IndexVec}; use rustc_macros::HashStable; -use rustc_serialize::opaque::{FileEncodeResult, FileEncoder}; +use rustc_serialize::{ + opaque::{FileEncodeResult, FileEncoder}, + Decoder, +}; use rustc_session::config::{BorrowckMode, CrateType, OutputFilenames}; use rustc_session::lint::{Level, Lint}; use rustc_session::Session; @@ -56,6 +59,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; @@ -69,6 +73,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>; + + fn mk_predicate(self, binder: Binder>) -> 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)] @@ -1025,6 +1042,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/query/on_disk_cache.rs b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs index d0cd8a48f99b3..0346dada2c6a8 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::fingerprint::{Fingerprint, FingerprintDecoder, FingerprintEncoder}; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet}; use rustc_data_structures::sync::{HashMapExt, Lock, Lrc, OnceCell}; @@ -745,6 +745,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 fccd8b795ef56..27ca4838921ec 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -9,9 +9,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 ed1274f26c68aa0df8b97c5381db993ff512f95e Mon Sep 17 00:00:00 2001 From: Wilco Kusee Date: Mon, 1 Feb 2021 17:43:48 +0100 Subject: [PATCH 2/7] 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 3c278a6a491a9..b3b21c131ce4a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4546,6 +4546,7 @@ dependencies = [ "rustc_index", "rustc_macros", "rustc_serialize", + "smallvec 1.4.2", ] [[package]] diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index 5bad09519ab85..4fa0f18c42b77 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -222,8 +222,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)?, + )) } } } @@ -255,8 +257,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 07b5dce419491..5ec5355c06e52 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -59,9 +59,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; @@ -77,13 +76,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>; + type Ty = Ty<'tcx>; + type TyKind = TyKind<'tcx>; + fn mk_predicate(self, binder: Binder>) -> 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 @@ -2668,81 +2685,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 27ca4838921ec..e3791b47cfa21 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -10,18 +10,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 816bb8db0ef09ccaef26f73917bd03caa823e8a6 Mon Sep 17 00:00:00 2001 From: Zahari Dichev Date: Fri, 5 Feb 2021 15:37:27 +0000 Subject: [PATCH 3/7] 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 | 64 +++++++++++++++++-- .../src/ty/query/on_disk_cache.rs | 13 ++-- compiler/rustc_type_ir/src/lib.rs | 28 ++++++++ 5 files changed, 130 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_middle/src/mir/interpret/mod.rs b/compiler/rustc_middle/src/mir/interpret/mod.rs index 55fe5f971e718..92d38c4842b71 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, @@ -301,7 +302,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) @@ -345,7 +348,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 => { @@ -353,7 +360,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 => { @@ -361,7 +371,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) } } @@ -545,7 +558,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 4fa0f18c42b77..1f76792adae01 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -276,7 +276,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)?, + )) } } @@ -285,7 +288,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(), + )) } } @@ -336,13 +342,19 @@ 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)?, + )) } } 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 5ec5355c06e52..3aed0a07fd520 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, 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::traits; use crate::ty::query::{self, OnDiskCache, TyCtxtAt}; @@ -20,8 +20,8 @@ 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, ParamConst, ParamTy, - PolyFnSig, Predicate, PredicateInner, PredicateKind, ProjectionTy, Region, RegionKind, + GenericParamDefKind, InferConst, InferTy, Instance, IntTy, IntVar, IntVid, List, ParamConst, + ParamTy, PolyFnSig, Predicate, PredicateInner, PredicateKind, ProjectionTy, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyS, TyVar, TyVid, TypeAndMut, UintTy, Visibility, }; use rustc_ast as ast; @@ -38,7 +38,7 @@ use rustc_data_structures::sync::{self, Lock, Lrc, WorkerLocal}; 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; @@ -86,6 +86,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>) -> Predicate<'tcx> { self.tcx.mk_predicate(binder) @@ -101,6 +119,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.allocation_interner.intern(alloc, |alloc| self.tcx.arena.alloc(alloc)) + } + + 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.queries.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 0346dada2c6a8..4341f5b9e16eb 100644 --- a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs @@ -28,6 +28,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::iter::FromIterator; use std::mem; @@ -909,12 +910,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 e3791b47cfa21..decc11e77a3fb 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -23,11 +23,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 f2cbdcc54a3bb46125ab5b419e09b66f2762d690 Mon Sep 17 00:00:00 2001 From: Zahari Dichev Date: Mon, 8 Feb 2021 16:07:39 +0000 Subject: [PATCH 4/7] 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 | 29 ++++++++++----- .../src/ty/query/on_disk_cache.rs | 5 +-- compiler/rustc_type_ir/src/lib.rs | 14 +++++-- 5 files changed, 46 insertions(+), 61 deletions(-) diff --git a/compiler/rustc_middle/src/mir/interpret/mod.rs b/compiler/rustc_middle/src/mir/interpret/mod.rs index 92d38c4842b71..5bcaece8881f6 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, @@ -302,9 +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 = 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) @@ -348,11 +346,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 => { @@ -360,10 +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 = as Interner>::create_fn_alloc( - decoder.interner(), - instance, - ); + let alloc_id = decoder.interner().create_fn_alloc(instance); Ok(alloc_id) } AllocDiscriminant::Static => { @@ -371,10 +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 = 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 1f76792adae01..44e0c52d76d5e 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -222,10 +222,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)?)) } } } @@ -248,8 +245,7 @@ impl<'tcx, D: TyDecoder<'tcx>> Decodable for ty::Binder> 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) } } @@ -257,10 +253,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)))?) } } @@ -276,10 +269,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)?)) } } @@ -288,10 +278,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())) } } @@ -336,25 +323,21 @@ 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)?)) } } 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 3aed0a07fd520..9c9a234c3e0b2 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -18,11 +18,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, 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, 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; @@ -47,10 +47,7 @@ use rustc_hir::{ }; use rustc_index::vec::{Idx, IndexVec}; use rustc_macros::HashStable; -use rustc_serialize::{ - opaque::{FileEncodeResult, FileEncoder}, - Decoder, -}; +use rustc_serialize::opaque::{FileEncodeResult, FileEncoder}; use rustc_session::config::{BorrowckMode, CrateType, OutputFilenames}; use rustc_session::lint::{Level, Lint}; use rustc_session::Session; @@ -77,10 +74,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>; + type ListExistentialPredicate = &'tcx List>>; + type Predicate = Predicate<'tcx>; type BinderPredicateKind = Binder>; @@ -120,6 +120,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.allocation_interner.intern(alloc, |alloc| self.tcx.arena.alloc(alloc)) } 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 4341f5b9e16eb..457bb2302559c 100644 --- a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs @@ -911,10 +911,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 decc11e77a3fb..a9cff878a9c44 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -9,16 +9,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; @@ -53,6 +55,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 6a8378f9298e73ad161dc7a35f16938e509c2193 Mon Sep 17 00:00:00 2001 From: Zahari Dichev Date: Sat, 20 Feb 2021 14:42:16 +0000 Subject: [PATCH 5/7] remove `tcx()` --- compiler/rustc_metadata/src/rmeta/decoder.rs | 19 +- compiler/rustc_middle/src/arena.rs | 18 +- compiler/rustc_middle/src/ty/codec.rs | 105 ++++--- compiler/rustc_middle/src/ty/context.rs | 264 ++++++++++++++++-- .../src/ty/query/on_disk_cache.rs | 16 +- compiler/rustc_type_ir/src/lib.rs | 171 +++++++++++- 6 files changed, 476 insertions(+), 117 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 4156452547369..d287ba9824675 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::cstore::{ForeignModule, LinkagePreference, NativeLib}; 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::{self, codec::TyDecoder, Ty, TyCtxt, TyInterner}; +use rustc_middle::ty::{self, codec::TyDecoder, Interner, Ty, TyCtxt, TyInterner}; 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") } @@ -275,14 +271,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] @@ -303,16 +294,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: 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 9a42bbe7bacdd..4b39912284b52 100644 --- a/compiler/rustc_middle/src/arena.rs +++ b/compiler/rustc_middle/src/arena.rs @@ -15,7 +15,7 @@ macro_rules! arena_types { // AdtDef are interned and compared by address [] adt_def: rustc_middle::ty::AdtDef, [] 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< @@ -23,16 +23,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, @@ -99,11 +99,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 44e0c52d76d5e..445159eeb5027 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::{CrateNum, DefId}; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; @@ -161,7 +160,7 @@ encodable_via_deref! { pub trait TyDecoder<'tcx>: Decoder { const CLEAR_CROSS_CRATE: bool; - fn tcx(&self) -> TyCtxt<'tcx>; + //fn tcx(&self) -> TyCtxt<'tcx>; fn interner(&self) -> TyInterner<'tcx>; fn peek_byte(&self) -> u8; @@ -189,26 +188,6 @@ pub trait TyDecoder<'tcx>: Decoder { fn decode_alloc_id(&mut self) -> Result; } -#[inline] -pub 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] -pub 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> { @@ -262,7 +241,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 }) } } @@ -290,7 +269,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()?)) } } @@ -307,14 +286,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)))?) } } @@ -343,7 +322,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::, _>>()?, @@ -353,7 +332,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::, _>>()?, @@ -363,7 +342,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::, _>>()?, @@ -394,39 +373,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 9c9a234c3e0b2..9427f0745ee80 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -11,8 +11,13 @@ use crate::middle; use crate::middle::cstore::{CrateStoreDyn, EncodedMetadata}; use crate::middle::resolve_lifetime::{self, 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::traits; use crate::ty::query::{self, OnDiskCache, TyCtxtAt}; use crate::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, Subst, SubstsRef, UserSubsts}; @@ -21,7 +26,7 @@ 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, ParamConst, ParamTy, PolyFnSig, Predicate, - PredicateInner, PredicateKind, ProjectionTy, Region, RegionKind, ReprOptions, + PredicateInner, PredicateKind, ProjectionTy, Region, RegionKind, ReprOptions, SymbolName, TraitObjectVisitor, Ty, TyKind, TyS, TyVar, TyVid, TypeAndMut, UintTy, Visibility, }; use rustc_ast as ast; @@ -34,7 +39,7 @@ use rustc_data_structures::stable_hasher::{ hash_stable_hashmap, HashStable, StableHasher, StableVec, }; 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_errors::ErrorReported; use rustc_hir as hir; use rustc_hir::def::{DefKind, Res}; @@ -47,6 +52,7 @@ use rustc_hir::{ }; use rustc_index::vec::{Idx, IndexVec}; use rustc_macros::HashStable; +use rustc_middle::arena::ArenaAllocatable; use rustc_serialize::opaque::{FileEncodeResult, FileEncoder}; use rustc_session::config::{BorrowckMode, CrateType, OutputFilenames}; use rustc_session::lint::{Level, Lint}; @@ -85,14 +91,16 @@ impl<'tcx> Interner for TyInterner<'tcx> { type BinderPredicateKind = Binder>; 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>; @@ -103,9 +111,211 @@ impl<'tcx> Interner for TyInterner<'tcx> { type Const = Const<'tcx>; type InternedConst = &'tcx Const<'tcx>; + 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 mk_predicate(self, binder: Binder>) -> Predicate<'tcx> { + 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 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) } @@ -129,25 +339,29 @@ 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.allocation_interner.intern(alloc, |alloc| self.tcx.arena.alloc(alloc)) + 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 intern_const_alloc(self, alloc: Self::Allocation) -> Self::InternedAllocation { + self.tcx.allocation_interner.intern(alloc, |alloc| self.tcx.arena.alloc(alloc)) } + fn intern_canonical_var_infos( self, ts: &[Self::CanonicalVarInfo], @@ -155,12 +369,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 457bb2302559c..d4a6b53fc60a1 100644 --- a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs @@ -6,7 +6,7 @@ use crate::ty::context::TyCtxt; use crate::ty::{self, Ty, TyInterner}; use rustc_data_structures::fingerprint::{Fingerprint, FingerprintDecoder, FingerprintEncoder}; 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; @@ -741,10 +741,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> { @@ -769,18 +769,18 @@ 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: CrateNum::ReservedForIncrCompCache, 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 a9cff878a9c44..3de555cef2303 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -11,6 +11,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 { @@ -24,13 +25,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; @@ -40,30 +44,173 @@ pub trait Interner { type Const; type InternedConst; + + 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 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 540c2e45af6680ead1f34905040d95d7c66bc656 Mon Sep 17 00:00:00 2001 From: Zahari Dichev Date: Fri, 12 Mar 2021 14:19:15 +0000 Subject: [PATCH 6/7] 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 | 3 +-- compiler/rustc_middle/src/ty/query/on_disk_cache.rs | 5 ----- 5 files changed, 9 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_arena/src/lib.rs b/compiler/rustc_arena/src/lib.rs index 721cfdd4459e5..cc894ecf8c737 100644 --- a/compiler/rustc_arena/src/lib.rs +++ b/compiler/rustc_arena/src/lib.rs @@ -733,10 +733,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 1e224dbf83390..fa1662fd1ce43 100644 --- a/compiler/rustc_ast/src/attr/mod.rs +++ b/compiler/rustc_ast/src/attr/mod.rs @@ -495,12 +495,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 445159eeb5027..461e4462b2e4e 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -160,7 +160,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 9427f0745ee80..1b5973a2f3139 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -52,7 +52,6 @@ use rustc_hir::{ }; use rustc_index::vec::{Idx, IndexVec}; use rustc_macros::HashStable; -use rustc_middle::arena::ArenaAllocatable; use rustc_serialize::opaque::{FileEncodeResult, FileEncoder}; use rustc_session::config::{BorrowckMode, CrateType, OutputFilenames}; use rustc_session::lint::{Level, Lint}; @@ -193,7 +192,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 d4a6b53fc60a1..38a1476f49115 100644 --- a/compiler/rustc_middle/src/ty/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/ty/query/on_disk_cache.rs @@ -741,11 +741,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 45c151bfd357ff0ff8b7426c93fe83658402fe87 Mon Sep 17 00:00:00 2001 From: Zahari Dichev Date: Mon, 15 Mar 2021 11:58:19 +0000 Subject: [PATCH 7/7] fix Signed-off-by: Zahari Dichev --- Cargo.lock | 2 +- compiler/rustc_middle/src/ty/context.rs | 2 +- compiler/rustc_middle/src/ty/mod.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b3b21c131ce4a..fc31b8506cd87 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4546,7 +4546,7 @@ dependencies = [ "rustc_index", "rustc_macros", "rustc_serialize", - "smallvec 1.4.2", + "smallvec 1.6.1", ] [[package]] diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 1b5973a2f3139..08c2af639b5f1 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -385,7 +385,7 @@ impl<'tcx> Interner for TyInterner<'tcx> { } fn def_path_hash_to_def_id(self, hash: Self::DefPathHash) -> Option { - self.tcx.queries.on_disk_cache.as_ref().unwrap().def_path_hash_to_def_id(self.tcx, hash) + self.tcx.on_disk_cache.as_ref().unwrap().def_path_hash_to_def_id(self.tcx, hash) } } diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index f5aef108927db..7bec890706436 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -59,7 +59,7 @@ pub use self::consts::{Const, ConstInt, ConstKind, InferConst, ScalarInt}; pub use self::context::{ tls, CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, CtxtInterners, DelaySpanBugEmitted, FreeRegionInfo, GeneratorInteriorTypeCause, GlobalCtxt, - Lift, ResolvedOpaqueTy, TyCtxt, TypeckResults, UserType, UserTypeAnnotationIndex, + Lift, ResolvedOpaqueTy, TyCtxt, TyInterner, TypeckResults, UserType, UserTypeAnnotationIndex, }; pub use self::instance::{Instance, InstanceDef}; pub use self::list::List;