From 58dcf53fa12803536483aa4a8241fcdef3f77293 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 30 Oct 2023 17:09:17 +0000 Subject: [PATCH] Use derivative for clone --- Cargo.lock | 12 +++++++ compiler/rustc_type_ir/Cargo.toml | 1 + compiler/rustc_type_ir/src/canonical.rs | 12 ++----- compiler/rustc_type_ir/src/const_kind.rs | 18 ++-------- compiler/rustc_type_ir/src/predicate_kind.rs | 37 +++----------------- compiler/rustc_type_ir/src/region_kind.rs | 18 ++-------- compiler/rustc_type_ir/src/ty_kind.rs | 37 ++------------------ src/tools/tidy/src/deps.rs | 1 + 8 files changed, 27 insertions(+), 109 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ff21d5f8c0861..a46ab1c952020 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1011,6 +1011,17 @@ dependencies = [ "syn 2.0.29", ] +[[package]] +name = "derivative" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "derive_builder" version = "0.12.0" @@ -4671,6 +4682,7 @@ name = "rustc_type_ir" version = "0.0.0" dependencies = [ "bitflags 1.3.2", + "derivative", "rustc_data_structures", "rustc_index", "rustc_macros", diff --git a/compiler/rustc_type_ir/Cargo.toml b/compiler/rustc_type_ir/Cargo.toml index c4008e9b61296..055f406942868 100644 --- a/compiler/rustc_type_ir/Cargo.toml +++ b/compiler/rustc_type_ir/Cargo.toml @@ -12,3 +12,4 @@ rustc_serialize = { path = "../rustc_serialize" } rustc_data_structures = { path = "../rustc_data_structures" } rustc_macros = { path = "../rustc_macros" } smallvec = { version = "1.8.1", features = ["union", "may_dangle"] } +derivative = "2.2.0" diff --git a/compiler/rustc_type_ir/src/canonical.rs b/compiler/rustc_type_ir/src/canonical.rs index c0b6aed98ef2e..1333a29a141cd 100644 --- a/compiler/rustc_type_ir/src/canonical.rs +++ b/compiler/rustc_type_ir/src/canonical.rs @@ -13,6 +13,8 @@ use crate::{HashStableContext, Interner, TyEncoder, UniverseIndex}; /// A "canonicalized" type `V` is one where all free inference /// variables have been rewritten to "canonical vars". These are /// numbered starting from 0 in order of first appearance. +#[derive(derivative::Derivative)] +#[derivative(Clone(bound = "V: Clone"))] pub struct Canonical { pub value: V, pub max_universe: UniverseIndex, @@ -108,16 +110,6 @@ impl fmt::Debug for Canonical { } } -impl Clone for Canonical { - fn clone(&self) -> Self { - Canonical { - value: self.value.clone(), - max_universe: self.max_universe.clone(), - variables: self.variables.clone(), - } - } -} - impl Copy for Canonical where I::CanonicalVars: Copy {} impl> TypeFoldable for Canonical diff --git a/compiler/rustc_type_ir/src/const_kind.rs b/compiler/rustc_type_ir/src/const_kind.rs index a40c41583af5c..3e0162328eefb 100644 --- a/compiler/rustc_type_ir/src/const_kind.rs +++ b/compiler/rustc_type_ir/src/const_kind.rs @@ -13,7 +13,8 @@ use crate::{ use self::ConstKind::*; /// Represents a constant in Rust. -// #[derive(derive_more::From)] +#[derive(derivative::Derivative)] +#[derivative(Clone(bound = ""))] pub enum ConstKind { /// A const generic parameter. Param(I::ParamConst), @@ -211,21 +212,6 @@ impl PartialEq for ConstKind { impl Eq for ConstKind {} -impl Clone for ConstKind { - fn clone(&self) -> Self { - match self { - Param(arg0) => Param(arg0.clone()), - Infer(arg0) => Infer(arg0.clone()), - Bound(arg0, arg1) => Bound(arg0.clone(), arg1.clone()), - Placeholder(arg0) => Placeholder(arg0.clone()), - Unevaluated(arg0) => Unevaluated(arg0.clone()), - Value(arg0) => Value(arg0.clone()), - Error(arg0) => Error(arg0.clone()), - Expr(arg0) => Expr(arg0.clone()), - } - } -} - impl fmt::Debug for ConstKind { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { WithInfcx::with_no_infcx(self).fmt(f) diff --git a/compiler/rustc_type_ir/src/predicate_kind.rs b/compiler/rustc_type_ir/src/predicate_kind.rs index f6fabe691c6a7..124d9d0eb9598 100644 --- a/compiler/rustc_type_ir/src/predicate_kind.rs +++ b/compiler/rustc_type_ir/src/predicate_kind.rs @@ -12,6 +12,8 @@ use crate::{TyDecoder, TyEncoder}; /// A clause is something that can appear in where bounds or be inferred /// by implied bounds. +#[derive(derivative::Derivative)] +#[derivative(Clone(bound = ""))] pub enum ClauseKind { /// Corresponds to `where Foo: Bar`. `Foo` here would be /// the `Self` type of the trait reference and `A`, `B`, and `C` @@ -39,20 +41,6 @@ pub enum ClauseKind { ConstEvaluatable(I::Const), } -impl Clone for ClauseKind { - fn clone(&self) -> Self { - match self { - Self::Trait(arg0) => Self::Trait(arg0.clone()), - Self::RegionOutlives(arg0) => Self::RegionOutlives(arg0.clone()), - Self::TypeOutlives(arg0) => Self::TypeOutlives(arg0.clone()), - Self::Projection(arg0) => Self::Projection(arg0.clone()), - Self::ConstArgHasType(arg0, arg1) => Self::ConstArgHasType(arg0.clone(), arg1.clone()), - Self::WellFormed(arg0) => Self::WellFormed(arg0.clone()), - Self::ConstEvaluatable(arg0) => Self::ConstEvaluatable(arg0.clone()), - } - } -} - impl Copy for ClauseKind where I::Ty: Copy, @@ -249,6 +237,8 @@ where } } +#[derive(derivative::Derivative)] +#[derivative(Clone(bound = ""))] pub enum PredicateKind { /// Prove a clause Clause(ClauseKind), @@ -305,25 +295,6 @@ where { } -impl Clone for PredicateKind { - fn clone(&self) -> Self { - match self { - Self::Clause(arg0) => Self::Clause(arg0.clone()), - Self::ObjectSafe(arg0) => Self::ObjectSafe(arg0.clone()), - Self::ClosureKind(arg0, arg1, arg2) => { - Self::ClosureKind(arg0.clone(), arg1.clone(), arg2.clone()) - } - Self::Subtype(arg0) => Self::Subtype(arg0.clone()), - Self::Coerce(arg0) => Self::Coerce(arg0.clone()), - Self::ConstEquate(arg0, arg1) => Self::ConstEquate(arg0.clone(), arg1.clone()), - Self::Ambiguous => Self::Ambiguous, - Self::AliasRelate(arg0, arg1, arg2) => { - Self::AliasRelate(arg0.clone(), arg1.clone(), arg2.clone()) - } - } - } -} - impl PartialEq for PredicateKind { fn eq(&self, other: &Self) -> bool { match (self, other) { diff --git a/compiler/rustc_type_ir/src/region_kind.rs b/compiler/rustc_type_ir/src/region_kind.rs index 19576ea58f1f0..66115adefeaf1 100644 --- a/compiler/rustc_type_ir/src/region_kind.rs +++ b/compiler/rustc_type_ir/src/region_kind.rs @@ -118,6 +118,8 @@ use self::RegionKind::*; /// [1]: https://smallcultfollowing.com/babysteps/blog/2013/10/29/intermingled-parameter-lists/ /// [2]: https://smallcultfollowing.com/babysteps/blog/2013/11/04/intermingled-parameter-lists/ /// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/hrtb.html +#[derive(derivative::Derivative)] +#[derivative(Clone(bound = ""))] pub enum RegionKind { /// Region bound in a type or fn declaration which will be /// substituted 'early' -- that is, at the same time when type @@ -178,22 +180,6 @@ where { } -// This is manually implemented because a derive would require `I: Clone` -impl Clone for RegionKind { - fn clone(&self) -> Self { - match self { - ReEarlyBound(r) => ReEarlyBound(r.clone()), - ReLateBound(d, r) => ReLateBound(*d, r.clone()), - ReFree(r) => ReFree(r.clone()), - ReStatic => ReStatic, - ReVar(r) => ReVar(r.clone()), - RePlaceholder(r) => RePlaceholder(r.clone()), - ReErased => ReErased, - ReError(r) => ReError(r.clone()), - } - } -} - // This is manually implemented because a derive would require `I: PartialEq` impl PartialEq for RegionKind { #[inline] diff --git a/compiler/rustc_type_ir/src/ty_kind.rs b/compiler/rustc_type_ir/src/ty_kind.rs index b542547589ac4..39116d33c3634 100644 --- a/compiler/rustc_type_ir/src/ty_kind.rs +++ b/compiler/rustc_type_ir/src/ty_kind.rs @@ -114,6 +114,8 @@ pub enum AliasKind { /// Types written by the user start out as `hir::TyKind` and get /// converted to this representation using `AstConv::ast_ty_to_ty`. #[rustc_diagnostic_item = "IrTyKind"] +#[derive(derivative::Derivative)] +#[derivative(Clone(bound = ""))] pub enum TyKind { /// The primitive boolean type. Written as `bool`. Bool, @@ -324,40 +326,6 @@ const fn tykind_discriminant(value: &TyKind) -> usize { } } -// This is manually implemented because a derive would require `I: Clone` -impl Clone for TyKind { - fn clone(&self) -> Self { - match self { - Bool => Bool, - Char => Char, - Int(i) => Int(*i), - Uint(u) => Uint(*u), - Float(f) => Float(*f), - Adt(d, s) => Adt(d.clone(), s.clone()), - Foreign(d) => Foreign(d.clone()), - Str => Str, - Array(t, c) => Array(t.clone(), c.clone()), - Slice(t) => Slice(t.clone()), - RawPtr(p) => RawPtr(p.clone()), - Ref(r, t, m) => Ref(r.clone(), t.clone(), m.clone()), - FnDef(d, s) => FnDef(d.clone(), s.clone()), - FnPtr(s) => FnPtr(s.clone()), - Dynamic(p, r, repr) => Dynamic(p.clone(), r.clone(), *repr), - Closure(d, s) => Closure(d.clone(), s.clone()), - Coroutine(d, s, m) => Coroutine(d.clone(), s.clone(), m.clone()), - CoroutineWitness(d, s) => CoroutineWitness(d.clone(), s.clone()), - Never => Never, - Tuple(t) => Tuple(t.clone()), - Alias(k, p) => Alias(*k, p.clone()), - Param(p) => Param(p.clone()), - Bound(d, b) => Bound(*d, b.clone()), - Placeholder(p) => Placeholder(p.clone()), - Infer(t) => Infer(t.clone()), - Error(e) => Error(e.clone()), - } - } -} - // This is manually implemented because a derive would require `I: PartialEq` impl PartialEq for TyKind { #[inline] @@ -614,6 +582,7 @@ impl DebugWithInfcx for TyKind { } } } + // This is manually implemented because a derive would require `I: Debug` impl fmt::Debug for TyKind { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/src/tools/tidy/src/deps.rs b/src/tools/tidy/src/deps.rs index f89faacc2d17b..4b12e9172af7f 100644 --- a/src/tools/tidy/src/deps.rs +++ b/src/tools/tidy/src/deps.rs @@ -142,6 +142,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[ "darling_core", "darling_macro", "datafrog", + "derivative", "derive_more", "derive_setters", "digest",