@@ -15,23 +15,22 @@ pub fn expand_deriving_clone(
1515 item : & Annotatable ,
1616 push : & mut dyn FnMut ( Annotatable ) ,
1717) {
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.
1920 //
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.)
3231 let bounds;
3332 let substructure;
34- let is_shallow ;
33+ let is_simple ;
3534 match * item {
3635 Annotatable :: Item ( ref annitem) => match annitem. kind {
3736 ItemKind :: Struct ( _, Generics { ref params, .. } )
@@ -44,30 +43,25 @@ pub fn expand_deriving_clone(
4443 . any ( |param| matches ! ( param. kind, ast:: GenericParamKind :: Type { .. } ) )
4544 {
4645 bounds = vec ! [ ] ;
47- is_shallow = true ;
46+ is_simple = true ;
4847 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 )
5049 } ) ) ;
5150 } else {
5251 bounds = vec ! [ ] ;
53- is_shallow = false ;
52+ is_simple = false ;
5453 substructure =
5554 combine_substructure ( Box :: new ( |c, s, sub| cs_clone ( "Clone" , c, s, sub) ) ) ;
5655 }
5756 }
5857 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 ;
6160 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 )
6362 } ) ) ;
6463 }
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" ) ,
7165 } ,
7266
7367 _ => cx. span_bug ( span, "`#[derive(Clone)]` on trait item or impl item" ) ,
@@ -81,26 +75,24 @@ pub fn expand_deriving_clone(
8175 path : path_std ! ( clone:: Clone ) ,
8276 additional_bounds : bounds,
8377 generics : Bounds :: empty ( ) ,
84- is_unsafe : false ,
8578 supports_unions : true ,
8679 methods : vec ! [ MethodDef {
8780 name: sym:: clone,
8881 generics: Bounds :: empty( ) ,
89- explicit_self: borrowed_explicit_self ( ) ,
82+ explicit_self: true ,
9083 args: Vec :: new( ) ,
9184 ret_ty: Self_ ,
9285 attributes: attrs,
93- is_unsafe: false ,
9486 unify_fieldless_variants: false ,
9587 combine_substructure: substructure,
9688 } ] ,
9789 associated_types : Vec :: new ( ) ,
9890 } ;
9991
100- trait_def. expand_ext ( cx, mitem, item, push, is_shallow )
92+ trait_def. expand_ext ( cx, mitem, item, push, is_simple )
10193}
10294
103- fn cs_clone_shallow (
95+ fn cs_clone_simple (
10496 name : & str ,
10597 cx : & mut ExtCtxt < ' _ > ,
10698 trait_span : Span ,
@@ -143,7 +135,7 @@ fn cs_clone_shallow(
143135 }
144136 _ => cx. span_bug (
145137 trait_span,
146- & format ! ( "unexpected substructure in shallow `derive({})`" , name) ,
138+ & format ! ( "unexpected substructure in simple `derive({})`" , name) ,
147139 ) ,
148140 }
149141 }
0 commit comments