1
- use rustc_ast:: { self as ast, Generics , ItemKind , MetaItem , VariantData } ;
1
+ use rustc_ast:: { self as ast, Generics , ItemKind , MetaItem , Safety , VariantData } ;
2
2
use rustc_data_structures:: fx:: FxHashSet ;
3
3
use rustc_expand:: base:: { Annotatable , ExtCtxt } ;
4
- use rustc_span:: { Ident , Span , kw, sym} ;
4
+ use rustc_span:: { DUMMY_SP , Ident , Span , kw, sym} ;
5
5
use thin_vec:: { ThinVec , thin_vec} ;
6
6
7
7
use crate :: deriving:: generic:: ty:: * ;
@@ -68,6 +68,25 @@ pub(crate) fn expand_deriving_clone(
68
68
_ => cx. dcx ( ) . span_bug ( span, "`#[derive(Clone)]` on trait item or impl item" ) ,
69
69
}
70
70
71
+ // If the clone method is just copying the value, also mark the type as
72
+ // `TrivialClone` to allow some library optimizations.
73
+ if is_simple {
74
+ let trivial_def = TraitDef {
75
+ span,
76
+ path : path_std ! ( clone:: TrivialClone ) ,
77
+ skip_path_as_bound : false ,
78
+ needs_copy_as_bound_if_packed : true ,
79
+ additional_bounds : bounds. clone ( ) ,
80
+ supports_unions : true ,
81
+ methods : Vec :: new ( ) ,
82
+ associated_types : Vec :: new ( ) ,
83
+ is_const,
84
+ safety : Safety :: Unsafe ( DUMMY_SP ) ,
85
+ } ;
86
+
87
+ trivial_def. expand_ext ( cx, mitem, item, push, true ) ;
88
+ }
89
+
71
90
let trait_def = TraitDef {
72
91
span,
73
92
path : path_std ! ( clone:: Clone ) ,
@@ -87,6 +106,7 @@ pub(crate) fn expand_deriving_clone(
87
106
} ] ,
88
107
associated_types : Vec :: new ( ) ,
89
108
is_const,
109
+ safety : Safety :: Default ,
90
110
} ;
91
111
92
112
trait_def. expand_ext ( cx, mitem, item, push, is_simple)
0 commit comments