@@ -15,7 +15,7 @@ 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+ // check if we can use a short form, and if the impl can be const
1919 //
2020 // the short form is `fn clone(&self) -> Self { *self }`
2121 //
@@ -29,6 +29,9 @@ pub fn expand_deriving_clone(
2929 // Unions with generic parameters still can derive Clone because they require Copy
3030 // for deriving, Clone alone is not enough.
3131 // Whever Clone is implemented for fields is irrelevant so we don't assert it.
32+ //
33+ // for now, the impl is only able to be marked as const if we can use the short form;
34+ // in the future, this may be expanded
3235 let bounds;
3336 let substructure;
3437 let is_shallow;
@@ -73,15 +76,29 @@ pub fn expand_deriving_clone(
7376 _ => cx. span_bug ( span, "`#[derive(Clone)]` on trait item or impl item" ) ,
7477 }
7578
79+ let features = cx. sess . features_untracked ( ) ;
7680 let inline = cx. meta_word ( span, sym:: inline) ;
7781 let attrs = vec ! [ cx. attribute( inline) ] ;
82+ let is_const = is_shallow && features. const_trait_impl ;
7883 let trait_def = TraitDef {
7984 span,
80- attributes : Vec :: new ( ) ,
85+ attributes : if is_const && features. staged_api {
86+ vec ! [ cx. attribute( cx. meta_list(
87+ span,
88+ sym:: rustc_const_unstable,
89+ vec![
90+ cx. meta_name_value( span, sym:: feature, Symbol :: intern( "const_clone" ) ) ,
91+ cx. meta_name_value( span, sym:: issue, Symbol :: intern( "none" ) ) ,
92+ ] ,
93+ ) ) ]
94+ } else {
95+ Vec :: new ( )
96+ } ,
8197 path : path_std ! ( clone:: Clone ) ,
8298 additional_bounds : bounds,
8399 generics : Bounds :: empty ( ) ,
84100 is_unsafe : false ,
101+ is_const,
85102 supports_unions : true ,
86103 methods : vec ! [ MethodDef {
87104 name: sym:: clone,
0 commit comments