1- use rustc_ast:: { self as ast, Generics , ItemKind , MetaItem , VariantData } ;
1+ use rustc_ast:: { self as ast, Generics , ItemKind , MetaItem , Safety , VariantData } ;
22use rustc_data_structures:: fx:: FxHashSet ;
33use rustc_expand:: base:: { Annotatable , ExtCtxt } ;
4- use rustc_span:: { Ident , Span , kw, sym} ;
4+ use rustc_span:: { DUMMY_SP , Ident , Span , kw, sym} ;
55use thin_vec:: { ThinVec , thin_vec} ;
66
77use crate :: deriving:: generic:: ty:: * ;
@@ -68,6 +68,26 @@ pub(crate) fn expand_deriving_clone(
6868 _ => cx. dcx ( ) . span_bug ( span, "`#[derive(Clone)]` on trait item or impl item" ) ,
6969 }
7070
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+ is_staged_api_crate : cx. ecfg . features . staged_api ( ) ,
85+ safety : Safety :: Unsafe ( DUMMY_SP ) ,
86+ } ;
87+
88+ trivial_def. expand_ext ( cx, mitem, item, push, true ) ;
89+ }
90+
7191 let trait_def = TraitDef {
7292 span,
7393 path : path_std ! ( clone:: Clone ) ,
@@ -88,6 +108,7 @@ pub(crate) fn expand_deriving_clone(
88108 associated_types : Vec :: new ( ) ,
89109 is_const,
90110 is_staged_api_crate : cx. ecfg . features . staged_api ( ) ,
111+ safety : Safety :: Default ,
91112 } ;
92113
93114 trait_def. expand_ext ( cx, mitem, item, push, is_simple)
0 commit comments