@@ -15,23 +15,22 @@ pub fn expand_deriving_clone(
15
15
item : & Annotatable ,
16
16
push : & mut dyn FnMut ( Annotatable ) ,
17
17
) {
18
- // check if we can use a short form
18
+ // The simple form is `fn clone(&self) -> Self { *self }`, possibly with
19
+ // some additional `AssertParamIsClone` assertions.
19
20
//
20
- // the short form is `fn clone(&self) -> Self { *self }`
21
- //
22
- // we can use the short form if:
23
- // - the item is Copy (unfortunately, all we can check is whether it's also deriving Copy)
24
- // - there are no generic parameters (after specialization this limitation can be removed)
25
- // if we used the short form with generics, we'd have to bound the generics with
26
- // Clone + Copy, and then there'd be no Clone impl at all if the user fills in something
27
- // that is Clone but not Copy. and until specialization we can't write both impls.
28
- // - the item is a union with Copy fields
29
- // Unions with generic parameters still can derive Clone because they require Copy
30
- // for deriving, Clone alone is not enough.
31
- // Wherever Clone is implemented for fields is irrelevant so we don't assert it.
21
+ // We can use the simple form if either of the following are true.
22
+ // - The type derives Copy and there are no generic parameters. (If we
23
+ // used the simple form with generics, we'd have to bound the generics
24
+ // with Clone + Copy, and then there'd be no Clone impl at all if the
25
+ // user fills in something that is Clone but not Copy. After
26
+ // specialization we can remove this no-generics limitation.)
27
+ // - The item is a union. (Unions with generic parameters still can derive
28
+ // Clone because they require Copy for deriving, Clone alone is not
29
+ // enough. Whether Clone is implemented for fields is irrelevant so we
30
+ // don't assert it.)
32
31
let bounds;
33
32
let substructure;
34
- let is_shallow ;
33
+ let is_simple ;
35
34
match * item {
36
35
Annotatable :: Item ( ref annitem) => match annitem. kind {
37
36
ItemKind :: Struct ( _, Generics { ref params, .. } )
@@ -44,30 +43,25 @@ pub fn expand_deriving_clone(
44
43
. any ( |param| matches ! ( param. kind, ast:: GenericParamKind :: Type { .. } ) )
45
44
{
46
45
bounds = vec ! [ ] ;
47
- is_shallow = true ;
46
+ is_simple = true ;
48
47
substructure = combine_substructure ( Box :: new ( |c, s, sub| {
49
- cs_clone_shallow ( "Clone" , c, s, sub, false )
48
+ cs_clone_simple ( "Clone" , c, s, sub, false )
50
49
} ) ) ;
51
50
} else {
52
51
bounds = vec ! [ ] ;
53
- is_shallow = false ;
52
+ is_simple = false ;
54
53
substructure =
55
54
combine_substructure ( Box :: new ( |c, s, sub| cs_clone ( "Clone" , c, s, sub) ) ) ;
56
55
}
57
56
}
58
57
ItemKind :: Union ( ..) => {
59
58
bounds = vec ! [ Literal ( path_std!( marker:: Copy ) ) ] ;
60
- is_shallow = true ;
59
+ is_simple = true ;
61
60
substructure = combine_substructure ( Box :: new ( |c, s, sub| {
62
- cs_clone_shallow ( "Clone" , c, s, sub, true )
61
+ cs_clone_simple ( "Clone" , c, s, sub, true )
63
62
} ) ) ;
64
63
}
65
- _ => {
66
- bounds = vec ! [ ] ;
67
- is_shallow = false ;
68
- substructure =
69
- combine_substructure ( Box :: new ( |c, s, sub| cs_clone ( "Clone" , c, s, sub) ) ) ;
70
- }
64
+ _ => cx. span_bug ( span, "`#[derive(Clone)]` on wrong item kind" ) ,
71
65
} ,
72
66
73
67
_ => cx. span_bug ( span, "`#[derive(Clone)]` on trait item or impl item" ) ,
@@ -95,10 +89,10 @@ pub fn expand_deriving_clone(
95
89
associated_types : Vec :: new ( ) ,
96
90
} ;
97
91
98
- trait_def. expand_ext ( cx, mitem, item, push, is_shallow )
92
+ trait_def. expand_ext ( cx, mitem, item, push, is_simple )
99
93
}
100
94
101
- fn cs_clone_shallow (
95
+ fn cs_clone_simple (
102
96
name : & str ,
103
97
cx : & mut ExtCtxt < ' _ > ,
104
98
trait_span : Span ,
@@ -141,7 +135,7 @@ fn cs_clone_shallow(
141
135
}
142
136
_ => cx. span_bug (
143
137
trait_span,
144
- & format ! ( "unexpected substructure in shallow `derive({})`" , name) ,
138
+ & format ! ( "unexpected substructure in simple `derive({})`" , name) ,
145
139
) ,
146
140
}
147
141
}
0 commit comments