Skip to content

Commit de83057

Browse files
Use derivative for Clone
1 parent 22b2712 commit de83057

File tree

8 files changed

+27
-109
lines changed

8 files changed

+27
-109
lines changed

Cargo.lock

+12
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,17 @@ dependencies = [
10111011
"syn 2.0.29",
10121012
]
10131013

1014+
[[package]]
1015+
name = "derivative"
1016+
version = "2.2.0"
1017+
source = "registry+https://github.com/rust-lang/crates.io-index"
1018+
checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b"
1019+
dependencies = [
1020+
"proc-macro2",
1021+
"quote",
1022+
"syn 1.0.109",
1023+
]
1024+
10141025
[[package]]
10151026
name = "derive_builder"
10161027
version = "0.12.0"
@@ -4667,6 +4678,7 @@ name = "rustc_type_ir"
46674678
version = "0.0.0"
46684679
dependencies = [
46694680
"bitflags 1.3.2",
4681+
"derivative",
46704682
"rustc_data_structures",
46714683
"rustc_index",
46724684
"rustc_macros",

compiler/rustc_type_ir/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2021"
66
[dependencies]
77
# tidy-alphabetical-start
88
bitflags = "1.2.1"
9+
derivative = "2.2.0"
910
rustc_data_structures = { path = "../rustc_data_structures" }
1011
rustc_index = { path = "../rustc_index" }
1112
rustc_macros = { path = "../rustc_macros" }

compiler/rustc_type_ir/src/canonical.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use crate::{HashStableContext, Interner, TyEncoder, UniverseIndex};
1313
/// A "canonicalized" type `V` is one where all free inference
1414
/// variables have been rewritten to "canonical vars". These are
1515
/// numbered starting from 0 in order of first appearance.
16+
#[derive(derivative::Derivative)]
17+
#[derivative(Clone(bound = "V: Clone"))]
1618
pub struct Canonical<I: Interner, V> {
1719
pub value: V,
1820
pub max_universe: UniverseIndex,
@@ -108,16 +110,6 @@ impl<I: Interner, V: fmt::Debug> fmt::Debug for Canonical<I, V> {
108110
}
109111
}
110112

111-
impl<I: Interner, V: Clone> Clone for Canonical<I, V> {
112-
fn clone(&self) -> Self {
113-
Canonical {
114-
value: self.value.clone(),
115-
max_universe: self.max_universe.clone(),
116-
variables: self.variables.clone(),
117-
}
118-
}
119-
}
120-
121113
impl<I: Interner, V: Copy> Copy for Canonical<I, V> where I::CanonicalVars: Copy {}
122114

123115
impl<I: Interner, V: TypeFoldable<I>> TypeFoldable<I> for Canonical<I, V>

compiler/rustc_type_ir/src/const_kind.rs

+2-16
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use crate::{
1313
use self::ConstKind::*;
1414

1515
/// Represents a constant in Rust.
16-
// #[derive(derive_more::From)]
16+
#[derive(derivative::Derivative)]
17+
#[derivative(Clone(bound = ""))]
1718
pub enum ConstKind<I: Interner> {
1819
/// A const generic parameter.
1920
Param(I::ParamConst),
@@ -211,21 +212,6 @@ impl<I: Interner> PartialEq for ConstKind<I> {
211212

212213
impl<I: Interner> Eq for ConstKind<I> {}
213214

214-
impl<I: Interner> Clone for ConstKind<I> {
215-
fn clone(&self) -> Self {
216-
match self {
217-
Param(arg0) => Param(arg0.clone()),
218-
Infer(arg0) => Infer(arg0.clone()),
219-
Bound(arg0, arg1) => Bound(arg0.clone(), arg1.clone()),
220-
Placeholder(arg0) => Placeholder(arg0.clone()),
221-
Unevaluated(arg0) => Unevaluated(arg0.clone()),
222-
Value(arg0) => Value(arg0.clone()),
223-
Error(arg0) => Error(arg0.clone()),
224-
Expr(arg0) => Expr(arg0.clone()),
225-
}
226-
}
227-
}
228-
229215
impl<I: Interner> fmt::Debug for ConstKind<I> {
230216
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
231217
WithInfcx::with_no_infcx(self).fmt(f)

compiler/rustc_type_ir/src/predicate_kind.rs

+4-33
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use crate::{TyDecoder, TyEncoder};
1212

1313
/// A clause is something that can appear in where bounds or be inferred
1414
/// by implied bounds.
15+
#[derive(derivative::Derivative)]
16+
#[derivative(Clone(bound = ""))]
1517
pub enum ClauseKind<I: Interner> {
1618
/// Corresponds to `where Foo: Bar<A, B, C>`. `Foo` here would be
1719
/// the `Self` type of the trait reference and `A`, `B`, and `C`
@@ -39,20 +41,6 @@ pub enum ClauseKind<I: Interner> {
3941
ConstEvaluatable(I::Const),
4042
}
4143

42-
impl<I: Interner> Clone for ClauseKind<I> {
43-
fn clone(&self) -> Self {
44-
match self {
45-
Self::Trait(arg0) => Self::Trait(arg0.clone()),
46-
Self::RegionOutlives(arg0) => Self::RegionOutlives(arg0.clone()),
47-
Self::TypeOutlives(arg0) => Self::TypeOutlives(arg0.clone()),
48-
Self::Projection(arg0) => Self::Projection(arg0.clone()),
49-
Self::ConstArgHasType(arg0, arg1) => Self::ConstArgHasType(arg0.clone(), arg1.clone()),
50-
Self::WellFormed(arg0) => Self::WellFormed(arg0.clone()),
51-
Self::ConstEvaluatable(arg0) => Self::ConstEvaluatable(arg0.clone()),
52-
}
53-
}
54-
}
55-
5644
impl<I: Interner> Copy for ClauseKind<I>
5745
where
5846
I::Ty: Copy,
@@ -249,6 +237,8 @@ where
249237
}
250238
}
251239

240+
#[derive(derivative::Derivative)]
241+
#[derivative(Clone(bound = ""))]
252242
pub enum PredicateKind<I: Interner> {
253243
/// Prove a clause
254244
Clause(ClauseKind<I>),
@@ -305,25 +295,6 @@ where
305295
{
306296
}
307297

308-
impl<I: Interner> Clone for PredicateKind<I> {
309-
fn clone(&self) -> Self {
310-
match self {
311-
Self::Clause(arg0) => Self::Clause(arg0.clone()),
312-
Self::ObjectSafe(arg0) => Self::ObjectSafe(arg0.clone()),
313-
Self::ClosureKind(arg0, arg1, arg2) => {
314-
Self::ClosureKind(arg0.clone(), arg1.clone(), arg2.clone())
315-
}
316-
Self::Subtype(arg0) => Self::Subtype(arg0.clone()),
317-
Self::Coerce(arg0) => Self::Coerce(arg0.clone()),
318-
Self::ConstEquate(arg0, arg1) => Self::ConstEquate(arg0.clone(), arg1.clone()),
319-
Self::Ambiguous => Self::Ambiguous,
320-
Self::AliasRelate(arg0, arg1, arg2) => {
321-
Self::AliasRelate(arg0.clone(), arg1.clone(), arg2.clone())
322-
}
323-
}
324-
}
325-
}
326-
327298
impl<I: Interner> PartialEq for PredicateKind<I> {
328299
fn eq(&self, other: &Self) -> bool {
329300
match (self, other) {

compiler/rustc_type_ir/src/region_kind.rs

+2-16
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ use self::RegionKind::*;
118118
/// [1]: https://smallcultfollowing.com/babysteps/blog/2013/10/29/intermingled-parameter-lists/
119119
/// [2]: https://smallcultfollowing.com/babysteps/blog/2013/11/04/intermingled-parameter-lists/
120120
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/traits/hrtb.html
121+
#[derive(derivative::Derivative)]
122+
#[derivative(Clone(bound = ""))]
121123
pub enum RegionKind<I: Interner> {
122124
/// Region bound in a type or fn declaration which will be
123125
/// substituted 'early' -- that is, at the same time when type
@@ -178,22 +180,6 @@ where
178180
{
179181
}
180182

181-
// This is manually implemented because a derive would require `I: Clone`
182-
impl<I: Interner> Clone for RegionKind<I> {
183-
fn clone(&self) -> Self {
184-
match self {
185-
ReEarlyBound(r) => ReEarlyBound(r.clone()),
186-
ReLateBound(d, r) => ReLateBound(*d, r.clone()),
187-
ReFree(r) => ReFree(r.clone()),
188-
ReStatic => ReStatic,
189-
ReVar(r) => ReVar(r.clone()),
190-
RePlaceholder(r) => RePlaceholder(r.clone()),
191-
ReErased => ReErased,
192-
ReError(r) => ReError(r.clone()),
193-
}
194-
}
195-
}
196-
197183
// This is manually implemented because a derive would require `I: PartialEq`
198184
impl<I: Interner> PartialEq for RegionKind<I> {
199185
#[inline]

compiler/rustc_type_ir/src/ty_kind.rs

+3-34
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ pub enum AliasKind {
114114
/// Types written by the user start out as `hir::TyKind` and get
115115
/// converted to this representation using `AstConv::ast_ty_to_ty`.
116116
#[rustc_diagnostic_item = "IrTyKind"]
117+
#[derive(derivative::Derivative)]
118+
#[derivative(Clone(bound = ""))]
117119
pub enum TyKind<I: Interner> {
118120
/// The primitive boolean type. Written as `bool`.
119121
Bool,
@@ -324,40 +326,6 @@ const fn tykind_discriminant<I: Interner>(value: &TyKind<I>) -> usize {
324326
}
325327
}
326328

327-
// This is manually implemented because a derive would require `I: Clone`
328-
impl<I: Interner> Clone for TyKind<I> {
329-
fn clone(&self) -> Self {
330-
match self {
331-
Bool => Bool,
332-
Char => Char,
333-
Int(i) => Int(*i),
334-
Uint(u) => Uint(*u),
335-
Float(f) => Float(*f),
336-
Adt(d, s) => Adt(d.clone(), s.clone()),
337-
Foreign(d) => Foreign(d.clone()),
338-
Str => Str,
339-
Array(t, c) => Array(t.clone(), c.clone()),
340-
Slice(t) => Slice(t.clone()),
341-
RawPtr(p) => RawPtr(p.clone()),
342-
Ref(r, t, m) => Ref(r.clone(), t.clone(), m.clone()),
343-
FnDef(d, s) => FnDef(d.clone(), s.clone()),
344-
FnPtr(s) => FnPtr(s.clone()),
345-
Dynamic(p, r, repr) => Dynamic(p.clone(), r.clone(), *repr),
346-
Closure(d, s) => Closure(d.clone(), s.clone()),
347-
Coroutine(d, s, m) => Coroutine(d.clone(), s.clone(), m.clone()),
348-
CoroutineWitness(d, s) => CoroutineWitness(d.clone(), s.clone()),
349-
Never => Never,
350-
Tuple(t) => Tuple(t.clone()),
351-
Alias(k, p) => Alias(*k, p.clone()),
352-
Param(p) => Param(p.clone()),
353-
Bound(d, b) => Bound(*d, b.clone()),
354-
Placeholder(p) => Placeholder(p.clone()),
355-
Infer(t) => Infer(t.clone()),
356-
Error(e) => Error(e.clone()),
357-
}
358-
}
359-
}
360-
361329
// This is manually implemented because a derive would require `I: PartialEq`
362330
impl<I: Interner> PartialEq for TyKind<I> {
363331
#[inline]
@@ -614,6 +582,7 @@ impl<I: Interner> DebugWithInfcx<I> for TyKind<I> {
614582
}
615583
}
616584
}
585+
617586
// This is manually implemented because a derive would require `I: Debug`
618587
impl<I: Interner> fmt::Debug for TyKind<I> {
619588
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

src/tools/tidy/src/deps.rs

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
142142
"darling_core",
143143
"darling_macro",
144144
"datafrog",
145+
"derivative",
145146
"derive_more",
146147
"derive_setters",
147148
"digest",

0 commit comments

Comments
 (0)