Skip to content

Commit

Permalink
Merge pull request #539 from matthewjasper/copy-everything
Browse files Browse the repository at this point in the history
Add more Copy implementations
  • Loading branch information
nikomatsakis authored Jun 23, 2020
2 parents 363f7ad + 5aa7c1e commit d7d69a6
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 @@ -71,6 +71,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> {
/// Creates a new environment.
pub fn new(interner: &I) -> Self {
Expand Down Expand Up @@ -99,6 +101,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> {
/// Creates a new environment/goal pair.
pub fn new(environment: &Environment<G::Interner>, goal: G) -> Self {
Expand Down Expand Up @@ -482,6 +489,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> {
/// Casts the type data to a type.
pub fn intern(self, interner: &I) -> Ty<I> {
Expand Down Expand Up @@ -770,6 +786,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,
{
}

/// A type, lifetime or constant whose value is being inferred.
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct InferenceVar {
Expand Down Expand Up @@ -817,8 +841,10 @@ pub struct Fn<I: Interner> {
pub substitution: Substitution<I>,
}

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

/// Constants.
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)]
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)]
pub struct Const<I: Interner> {
interned: I::InternedConst,
}
Expand Down Expand Up @@ -893,6 +919,8 @@ pub enum ConstValue<I: Interner> {
Concrete(ConcreteConst<I>),
}

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

impl<I: Interner> ConstData<I> {
/// Wraps the constant data in a `Const`.
pub fn intern(self, interner: &I) -> Const<I> {
Expand All @@ -902,7 +930,7 @@ impl<I: Interner> ConstData<I> {

/// Concrete constant, whose value is known (as opposed to
/// inferred constants and placeholders).
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)]
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, HasInterner)]
pub struct ConcreteConst<I: Interner> {
/// The interned constant.
pub interned: I::InternedConcreteConst,
Expand Down Expand Up @@ -1032,6 +1060,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> {
/// Create an interned type from this application type.
pub fn intern(self, interner: &I) -> Ty<I> {
Expand Down Expand Up @@ -1084,6 +1114,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 @@ -1189,6 +1221,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> {
/// Create an interned type.
pub fn intern(self, interner: &I) -> GenericArg<I> {
Expand All @@ -1205,6 +1245,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 @@ -1262,6 +1304,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> {
/// Create an interned type for this alias.
pub fn intern(self, interner: &I) -> Ty<I> {
Expand Down Expand Up @@ -1291,6 +1335,8 @@ pub struct ProjectionTy<I: Interner> {
pub substitution: Substitution<I>,
}

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

/// An opaque type `opaque type T<..>: Trait = HiddenTy`.
#[derive(Clone, PartialEq, Eq, Hash, Fold, Visit, HasInterner, Zip)]
pub struct OpaqueTy<I: Interner> {
Expand All @@ -1300,6 +1346,8 @@ pub struct OpaqueTy<I: Interner> {
pub substitution: Substitution<I>,
}

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

/// A trait reference describes the relationship between a type and a trait.
/// This can be used in two forms:
/// - `P0: Trait<P1..Pn>` (e.g. `i32: Copy`), which mentions that the type
Expand All @@ -1314,6 +1362,8 @@ pub struct TraitRef<I: Interner> {
pub substitution: Substitution<I>,
}

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

impl<I: Interner> TraitRef<I> {
/// Gets all type parameters in this trait ref, including `Self`.
pub fn type_parameters<'a>(&'a self, interner: &'a I) -> impl Iterator<Item = Ty<I>> + 'a {
Expand Down Expand Up @@ -1347,6 +1397,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 @@ -1358,6 +1411,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,
{
}

/// Checks whether a type or trait ref is well-formed.
#[derive(Clone, PartialEq, Eq, Hash, Fold, Visit, HasInterner, Zip)]
pub enum WellFormed<I: Interner> {
Expand Down Expand Up @@ -1389,6 +1450,13 @@ pub enum WellFormed<I: Interner> {
Ty(Ty<I>),
}

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

/// Checks whether a type or trait ref can be derived from the contents of the environment.
#[derive(Clone, PartialEq, Eq, Hash, Fold, Visit, HasInterner, Zip)]
pub enum FromEnv<I: Interner> {
Expand Down Expand Up @@ -1419,6 +1487,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 @@ -1496,6 +1571,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,
{
}

/// A where clause that can contain `forall<>` or `exists<>` quantifiers.
pub type QuantifiedWhereClause<I> = Binders<WhereClause<I>>;

Expand Down Expand Up @@ -1647,6 +1730,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 @@ -1658,6 +1743,13 @@ 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)]
#[allow(missing_docs)]
Expand All @@ -1666,6 +1758,13 @@ pub struct AliasEq<I: Interner> {
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 @@ -1686,6 +1785,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 @@ -2460,6 +2564,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> {
/// Create an interned goal.
pub fn intern(self, interner: &I) -> Goal<I> {
Expand Down Expand Up @@ -2501,6 +2618,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 d7d69a6

Please sign in to comment.