Skip to content

Commit

Permalink
intern PredicateKind
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed May 20, 2020
1 parent f316479 commit 57746f9
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/librustc_infer/traits/mod.rs
Expand Up @@ -59,7 +59,7 @@ pub type TraitObligation<'tcx> = Obligation<'tcx, ty::PolyTraitPredicate<'tcx>>;

// `PredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger.
#[cfg(target_arch = "x86_64")]
static_assert_size!(PredicateObligation<'_>, 112);
static_assert_size!(PredicateObligation<'_>, 88);

pub type Obligations<'tcx, O> = Vec<Obligation<'tcx, O>>;
pub type PredicateObligations<'tcx> = Vec<PredicateObligation<'tcx>>;
Expand Down
23 changes: 18 additions & 5 deletions src/librustc_middle/ty/context.rs
Expand Up @@ -29,10 +29,9 @@ use crate::ty::{self, DefIdTree, Ty, TypeAndMut};
use crate::ty::{AdtDef, AdtKind, Const, Region};
use crate::ty::{BindingMode, BoundVar};
use crate::ty::{ConstVid, FloatVar, FloatVid, IntVar, IntVid, TyVar, TyVid};
use crate::ty::{
ExistentialPredicate, InferTy, ParamTy, PolyFnSig, Predicate, PredicateKind, ProjectionTy,
};
use crate::ty::{ExistentialPredicate, Predicate, PredicateKind};
use crate::ty::{InferConst, ParamConst};
use crate::ty::{InferTy, ParamTy, PolyFnSig, ProjectionTy};
use crate::ty::{List, TyKind, TyS};
use rustc_ast::ast;
use rustc_ast::expand::allocator::AllocatorKind;
Expand Down Expand Up @@ -91,6 +90,7 @@ pub struct CtxtInterners<'tcx> {
canonical_var_infos: InternedSet<'tcx, List<CanonicalVarInfo>>,
region: InternedSet<'tcx, RegionKind>,
existential_predicates: InternedSet<'tcx, List<ExistentialPredicate<'tcx>>>,
predicate_kind: InternedSet<'tcx, PredicateKind<'tcx>>,
predicates: InternedSet<'tcx, List<Predicate<'tcx>>>,
projs: InternedSet<'tcx, List<ProjectionKind>>,
place_elems: InternedSet<'tcx, List<PlaceElem<'tcx>>>,
Expand All @@ -109,6 +109,7 @@ impl<'tcx> CtxtInterners<'tcx> {
region: Default::default(),
existential_predicates: Default::default(),
canonical_var_infos: Default::default(),
predicate_kind: Default::default(),
predicates: Default::default(),
projs: Default::default(),
place_elems: Default::default(),
Expand Down Expand Up @@ -1579,6 +1580,7 @@ macro_rules! nop_list_lift {
nop_lift! {type_; Ty<'a> => Ty<'tcx>}
nop_lift! {region; Region<'a> => Region<'tcx>}
nop_lift! {const_; &'a Const<'a> => &'tcx Const<'tcx>}
nop_lift! {predicate_kind; &'a PredicateKind<'a> => &'tcx PredicateKind<'tcx>}

nop_list_lift! {type_list; Ty<'a> => Ty<'tcx>}
nop_list_lift! {existential_predicates; ExistentialPredicate<'a> => ExistentialPredicate<'tcx>}
Expand Down Expand Up @@ -2017,8 +2019,14 @@ impl<'tcx> Borrow<[traits::ChalkEnvironmentClause<'tcx>]>
}
}

impl<'tcx> Borrow<PredicateKind<'tcx>> for Interned<'tcx, PredicateKind<'tcx>> {
fn borrow<'a>(&'a self) -> &'a PredicateKind<'tcx> {
&self.0
}
}

macro_rules! direct_interners {
($($name:ident: $method:ident($ty:ty)),+) => {
($($name:ident: $method:ident($ty:ty),)+) => {
$(impl<'tcx> PartialEq for Interned<'tcx, $ty> {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
Expand All @@ -2043,7 +2051,11 @@ macro_rules! direct_interners {
}
}

direct_interners!(region: mk_region(RegionKind), const_: mk_const(Const<'tcx>));
direct_interners!(
region: mk_region(RegionKind),
const_: mk_const(Const<'tcx>),
predicate_kind: intern_predicate_kind(PredicateKind<'tcx>),
);

macro_rules! slice_interners {
($($field:ident: $method:ident($ty:ty)),+) => (
Expand Down Expand Up @@ -2107,6 +2119,7 @@ impl<'tcx> TyCtxt<'tcx> {

#[inline]
pub fn mk_predicate(&self, kind: PredicateKind<'tcx>) -> Predicate<'tcx> {
let kind = self.intern_predicate_kind(kind);
Predicate { kind }
}

Expand Down
6 changes: 3 additions & 3 deletions src/librustc_middle/ty/mod.rs
Expand Up @@ -1017,14 +1017,14 @@ impl<'tcx> GenericPredicates<'tcx> {
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Lift)]
#[derive(HashStable, TypeFoldable)]
#[derive(HashStable)]
pub struct Predicate<'tcx> {
kind: PredicateKind<'tcx>,
kind: &'tcx PredicateKind<'tcx>,
}

impl Predicate<'tcx> {
pub fn kind(&self) -> PredicateKind<'tcx> {
self.kind
*self.kind
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/librustc_middle/ty/structural_impls.rs
Expand Up @@ -987,6 +987,16 @@ impl<'tcx> TypeFoldable<'tcx> for ty::Region<'tcx> {
}
}

impl<'tcx> TypeFoldable<'tcx> for ty::Predicate<'tcx> {
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
folder.tcx().mk_predicate(ty::PredicateKind::super_fold_with(self.kind, folder))
}

fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
ty::PredicateKind::super_visit_with(self.kind, visitor)
}
}

impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ty::Predicate<'tcx>> {
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
fold_list(*self, folder, |tcx, v| tcx.intern_predicates(v))
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trait_selection/traits/fulfill.rs
Expand Up @@ -83,7 +83,7 @@ pub struct PendingPredicateObligation<'tcx> {

// `PendingPredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger.
#[cfg(target_arch = "x86_64")]
static_assert_size!(PendingPredicateObligation<'_>, 136);
static_assert_size!(PendingPredicateObligation<'_>, 112);

impl<'a, 'tcx> FulfillmentContext<'tcx> {
/// Creates a new fulfillment context.
Expand Down
6 changes: 2 additions & 4 deletions src/librustc_traits/type_op.rs
Expand Up @@ -6,10 +6,8 @@ use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
use rustc_infer::traits::TraitEngineExt as _;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::subst::{GenericArg, Subst, UserSelfTy, UserSubsts};
use rustc_middle::ty::{
self, FnSig, Lift, ParamEnv, ParamEnvAnd, PolyFnSig, Predicate, ToPredicate, Ty, TyCtxt,
TypeFoldable, Variance,
};
use rustc_middle::ty::{self, FnSig, Lift, PolyFnSig, Ty, TyCtxt, TypeFoldable, Variance};
use rustc_middle::ty::{ParamEnv, ParamEnvAnd, Predicate, ToPredicate};
use rustc_span::DUMMY_SP;
use rustc_trait_selection::infer::InferCtxtBuilderExt;
use rustc_trait_selection::infer::InferCtxtExt;
Expand Down

0 comments on commit 57746f9

Please sign in to comment.