@@ -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
- bounds = vec ! [ Literal ( path_std!( marker:: Copy ) ) ] ;
60
- is_shallow = true ;
58
+ bounds = vec ! [ Path ( path_std!( marker:: Copy ) ) ] ;
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" ) ,
@@ -81,26 +75,24 @@ pub fn expand_deriving_clone(
81
75
path : path_std ! ( clone:: Clone ) ,
82
76
additional_bounds : bounds,
83
77
generics : Bounds :: empty ( ) ,
84
- is_unsafe : false ,
85
78
supports_unions : true ,
86
79
methods : vec ! [ MethodDef {
87
80
name: sym:: clone,
88
81
generics: Bounds :: empty( ) ,
89
- explicit_self: borrowed_explicit_self ( ) ,
82
+ explicit_self: true ,
90
83
args: Vec :: new( ) ,
91
84
ret_ty: Self_ ,
92
85
attributes: attrs,
93
- is_unsafe: false ,
94
86
unify_fieldless_variants: false ,
95
87
combine_substructure: substructure,
96
88
} ] ,
97
89
associated_types : Vec :: new ( ) ,
98
90
} ;
99
91
100
- trait_def. expand_ext ( cx, mitem, item, push, is_shallow )
92
+ trait_def. expand_ext ( cx, mitem, item, push, is_simple )
101
93
}
102
94
103
- fn cs_clone_shallow (
95
+ fn cs_clone_simple (
104
96
name : & str ,
105
97
cx : & mut ExtCtxt < ' _ > ,
106
98
trait_span : Span ,
@@ -143,7 +135,7 @@ fn cs_clone_shallow(
143
135
}
144
136
_ => cx. span_bug (
145
137
trait_span,
146
- & format ! ( "unexpected substructure in shallow `derive({})`" , name) ,
138
+ & format ! ( "unexpected substructure in simple `derive({})`" , name) ,
147
139
) ,
148
140
}
149
141
}
0 commit comments