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,25 @@ 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+ safety : Safety :: Unsafe ( DUMMY_SP ) ,
85+ } ;
86+
87+ trivial_def. expand_ext ( cx, mitem, item, push, true ) ;
88+ }
89+
7190 let trait_def = TraitDef {
7291 span,
7392 path : path_std ! ( clone:: Clone ) ,
@@ -87,6 +106,7 @@ pub(crate) fn expand_deriving_clone(
87106 } ] ,
88107 associated_types : Vec :: new ( ) ,
89108 is_const,
109+ safety : Safety :: Default ,
90110 } ;
91111
92112 trait_def. expand_ext ( cx, mitem, item, push, is_simple)
0 commit comments