Skip to content

Commit

Permalink
Add more Copy implementations
Browse files Browse the repository at this point in the history
This is mainly so that `QuantifiedWhereClause` can be put in a
`rustc_middle::ty::List` (rustc's interned slice type), the other
impls are added in case they're useful.
  • Loading branch information
matthewjasper committed Jun 18, 2020
1 parent c4c41b0 commit aec71e2
Showing 1 changed file with 121 additions and 2 deletions.
123 changes: 121 additions & 2 deletions chalk-ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ pub struct Environment<I: Interner> {
pub clauses: ProgramClauses<I>,
}

impl<I: Interner> Copy for Environment<I> where I::InternedProgramClauses: Copy {}

impl<I: Interner> Environment<I> {
pub fn new(interner: &I) -> Self {
Environment {
Expand All @@ -90,6 +92,11 @@ pub struct InEnvironment<G: HasInterner> {
pub goal: G,
}

impl<G: HasInterner<Interner = I> + Copy, I: Interner> Copy for InEnvironment<G> where
I::InternedProgramClauses: Copy
{
}

impl<G: HasInterner> InEnvironment<G> {
pub fn new(environment: &Environment<G::Interner>, goal: G) -> Self {
InEnvironment {
Expand Down Expand Up @@ -438,6 +445,15 @@ pub enum TyData<I: Interner> {
InferenceVar(InferenceVar, TyKind),
}

impl<I: Interner> Copy for TyData<I>
where
I::InternedLifetime: Copy,
I::InternedSubstitution: Copy,
I::InternedVariableKinds: Copy,
I::InternedQuantifiedWhereClauses: Copy,
{
}

impl<I: Interner> TyData<I> {
pub fn intern(self, interner: &I) -> Ty<I> {
Ty::new(interner, self)
Expand Down Expand Up @@ -712,6 +728,14 @@ pub struct DynTy<I: Interner> {
pub lifetime: Lifetime<I>,
}

impl<I: Interner> Copy for DynTy<I>
where
I::InternedLifetime: Copy,
I::InternedQuantifiedWhereClauses: Copy,
I::InternedVariableKinds: Copy,
{
}

#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct InferenceVar {
index: u32,
Expand Down Expand Up @@ -753,7 +777,9 @@ pub struct Fn<I: Interner> {
pub substitution: Substitution<I>,
}

#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)]
impl<I: Interner> Copy for Fn<I> where I::InternedSubstitution: Copy {}

#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)]
pub struct Const<I: Interner> {
interned: I::InternedConst,
}
Expand Down Expand Up @@ -817,13 +843,15 @@ pub enum ConstValue<I: Interner> {
Concrete(ConcreteConst<I>),
}

impl<I: Interner> Copy for ConstValue<I> where I::InternedConcreteConst: Copy {}

impl<I: Interner> ConstData<I> {
pub fn intern(self, interner: &I) -> Const<I> {
Const::new(interner, self)
}
}

#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)]
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)]
pub struct ConcreteConst<I: Interner> {
pub interned: I::InternedConcreteConst,
}
Expand Down Expand Up @@ -935,6 +963,8 @@ pub struct ApplicationTy<I: Interner> {
pub substitution: Substitution<I>,
}

impl<I: Interner> Copy for ApplicationTy<I> where I::InternedSubstitution: Copy {}

impl<I: Interner> ApplicationTy<I> {
pub fn intern(self, interner: &I) -> Ty<I> {
Ty::new(interner, self)
Expand Down Expand Up @@ -980,6 +1010,8 @@ pub enum VariableKind<I: Interner> {
Const(Ty<I>),
}

impl<I: Interner> Copy for VariableKind<I> where I::InternedType: Copy {}

impl<I: Interner> VariableKind<I> {
fn to_bound_variable(&self, interner: &I, bound_var: BoundVar) -> GenericArg<I> {
match self {
Expand Down Expand Up @@ -1070,6 +1102,14 @@ pub enum GenericArgData<I: Interner> {
Const(Const<I>),
}

impl<I: Interner> Copy for GenericArgData<I>
where
I::InternedType: Copy,
I::InternedLifetime: Copy,
I::InternedConst: Copy,
{
}

impl<I: Interner> GenericArgData<I> {
pub fn intern(self, interner: &I) -> GenericArg<I> {
GenericArg::new(interner, self)
Expand All @@ -1082,6 +1122,8 @@ pub struct WithKind<I: Interner, T> {
value: T,
}

impl<I: Interner, T: Copy> Copy for WithKind<I, T> where I::InternedType: Copy {}

impl<I: Interner, T> HasInterner for WithKind<I, T> {
type Interner = I;
}
Expand Down Expand Up @@ -1131,6 +1173,8 @@ pub enum AliasTy<I: Interner> {
Opaque(OpaqueTy<I>),
}

impl<I: Interner> Copy for AliasTy<I> where I::InternedSubstitution: Copy {}

impl<I: Interner> AliasTy<I> {
pub fn intern(self, interner: &I) -> Ty<I> {
Ty::new(interner, self)
Expand All @@ -1155,18 +1199,24 @@ pub struct ProjectionTy<I: Interner> {
pub substitution: Substitution<I>,
}

impl<I: Interner> Copy for ProjectionTy<I> where I::InternedSubstitution: Copy {}

#[derive(Clone, PartialEq, Eq, Hash, Fold, Visit, HasInterner, Zip)]
pub struct OpaqueTy<I: Interner> {
pub opaque_ty_id: OpaqueTyId<I>,
pub substitution: Substitution<I>,
}

impl<I: Interner> Copy for OpaqueTy<I> where I::InternedSubstitution: Copy {}

#[derive(Clone, PartialEq, Eq, Hash, Fold, Visit, HasInterner, Zip)]
pub struct TraitRef<I: Interner> {
pub trait_id: TraitId<I>,
pub substitution: Substitution<I>,
}

impl<I: Interner> Copy for TraitRef<I> where I::InternedSubstitution: Copy {}

impl<I: Interner> TraitRef<I> {
pub fn type_parameters<'a>(&'a self, interner: &'a I) -> impl Iterator<Item = Ty<I>> + 'a {
self.substitution
Expand All @@ -1193,6 +1243,9 @@ pub struct LifetimeOutlives<I: Interner> {
pub a: Lifetime<I>,
pub b: Lifetime<I>,
}

impl<I: Interner> Copy for LifetimeOutlives<I> where I::InternedLifetime: Copy {}

/// Where clauses that can be written by a Rust programmer.
#[derive(Clone, PartialEq, Eq, Hash, Fold, SuperVisit, HasInterner, Zip)]
pub enum WhereClause<I: Interner> {
Expand All @@ -1201,6 +1254,14 @@ pub enum WhereClause<I: Interner> {
LifetimeOutlives(LifetimeOutlives<I>),
}

impl<I: Interner> Copy for WhereClause<I>
where
I::InternedSubstitution: Copy,
I::InternedLifetime: Copy,
I::InternedType: Copy,
{
}

#[derive(Clone, PartialEq, Eq, Hash, Fold, Visit, HasInterner, Zip)]
pub enum WellFormed<I: Interner> {
/// A predicate which is true when some trait ref is well-formed.
Expand Down Expand Up @@ -1231,6 +1292,13 @@ pub enum WellFormed<I: Interner> {
Ty(Ty<I>),
}

impl<I: Interner> Copy for WellFormed<I>
where
I::InternedType: Copy,
I::InternedSubstitution: Copy,
{
}

#[derive(Clone, PartialEq, Eq, Hash, Fold, Visit, HasInterner, Zip)]
pub enum FromEnv<I: Interner> {
/// A predicate which enables deriving everything which should be true if we *know* that
Expand Down Expand Up @@ -1260,6 +1328,13 @@ pub enum FromEnv<I: Interner> {
Ty(Ty<I>),
}

impl<I: Interner> Copy for FromEnv<I>
where
I::InternedType: Copy,
I::InternedSubstitution: Copy,
{
}

/// A "domain goal" is a goal that is directly about Rust, rather than a pure
/// logical statement. As much as possible, the Chalk solver should avoid
/// decomposing this enum, and instead treat its values opaquely.
Expand Down Expand Up @@ -1333,6 +1408,14 @@ pub enum DomainGoal<I: Interner> {
ObjectSafe(TraitId<I>),
}

impl<I: Interner> Copy for DomainGoal<I>
where
I::InternedSubstitution: Copy,
I::InternedLifetime: Copy,
I::InternedType: Copy,
{
}

pub type QuantifiedWhereClause<I> = Binders<WhereClause<I>>;

impl<I: Interner> WhereClause<I> {
Expand Down Expand Up @@ -1471,6 +1554,8 @@ pub struct EqGoal<I: Interner> {
pub b: GenericArg<I>,
}

impl<I: Interner> Copy for EqGoal<I> where I::InternedGenericArg: Copy {}

/// Proves that the given type alias **normalizes** to the given
/// type. A projection `T::Foo` normalizes to the type `U` if we can
/// **match it to an impl** and that impl has a `type Foo = V` where
Expand All @@ -1481,13 +1566,27 @@ pub struct Normalize<I: Interner> {
pub ty: Ty<I>,
}

impl<I: Interner> Copy for Normalize<I>
where
I::InternedSubstitution: Copy,
I::InternedType: Copy,
{
}

/// Proves **equality** between an alias and a type.
#[derive(Clone, PartialEq, Eq, Hash, Fold, Visit, Zip)]
pub struct AliasEq<I: Interner> {
pub alias: AliasTy<I>,
pub ty: Ty<I>,
}

impl<I: Interner> Copy for AliasEq<I>
where
I::InternedSubstitution: Copy,
I::InternedType: Copy,
{
}

impl<I: Interner> HasInterner for AliasEq<I> {
type Interner = I;
}
Expand All @@ -1505,6 +1604,11 @@ pub struct Binders<T: HasInterner> {
value: T,
}

impl<T: HasInterner + Copy> Copy for Binders<T> where
<T::Interner as Interner>::InternedVariableKinds: Copy
{
}

impl<T: HasInterner> HasInterner for Binders<T> {
type Interner = T::Interner;
}
Expand Down Expand Up @@ -2197,6 +2301,19 @@ pub enum GoalData<I: Interner> {
CannotProve(()),
}

impl<I: Interner> Copy for GoalData<I>
where
I::InternedType: Copy,
I::InternedLifetime: Copy,
I::InternedGenericArg: Copy,
I::InternedSubstitution: Copy,
I::InternedGoal: Copy,
I::InternedGoals: Copy,
I::InternedProgramClauses: Copy,
I::InternedVariableKinds: Copy,
{
}

impl<I: Interner> GoalData<I> {
pub fn intern(self, interner: &I) -> Goal<I> {
Goal::new(interner, self)
Expand All @@ -2220,6 +2337,8 @@ pub enum Constraint<I: Interner> {
Outlives(Lifetime<I>, Lifetime<I>),
}

impl<I: Interner> Copy for Constraint<I> where I::InternedLifetime: Copy {}

/// A mapping of inference variables to instantiations thereof.
#[derive(Copy, Clone, PartialEq, Eq, Hash, HasInterner)]
pub struct Substitution<I: Interner> {
Expand Down

0 comments on commit aec71e2

Please sign in to comment.