Skip to content

Commit 3068dcc

Browse files
committed
Mark derived Clone impls as const
1 parent a00e130 commit 3068dcc

File tree

17 files changed

+163
-42
lines changed

17 files changed

+163
-42
lines changed

compiler/rustc_builtin_macros/src/deriving/bounds.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub fn expand_deriving_copy(
2020
additional_bounds: Vec::new(),
2121
generics: Bounds::empty(),
2222
is_unsafe: false,
23+
is_const: false,
2324
supports_unions: true,
2425
methods: Vec::new(),
2526
associated_types: Vec::new(),

compiler/rustc_builtin_macros/src/deriving/clone.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub fn expand_deriving_eq(
2828
additional_bounds: Vec::new(),
2929
generics: Bounds::empty(),
3030
is_unsafe: false,
31+
is_const: false,
3132
supports_unions: true,
3233
methods: vec![MethodDef {
3334
name: sym::assert_receiver_is_total_eq,

compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub fn expand_deriving_ord(
2424
additional_bounds: Vec::new(),
2525
generics: Bounds::empty(),
2626
is_unsafe: false,
27+
is_const: false,
2728
supports_unions: false,
2829
methods: vec![MethodDef {
2930
name: sym::cmp,

compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ pub fn expand_deriving_partial_eq(
104104
additional_bounds: Vec::new(),
105105
generics: Bounds::empty(),
106106
is_unsafe: false,
107+
is_const: false,
107108
supports_unions: false,
108109
methods,
109110
associated_types: Vec::new(),

compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub fn expand_deriving_partial_ord(
4747
additional_bounds: vec![],
4848
generics: Bounds::empty(),
4949
is_unsafe: false,
50+
is_const: false,
5051
supports_unions: false,
5152
methods: vec![partial_cmp_def],
5253
associated_types: Vec::new(),

compiler/rustc_builtin_macros/src/deriving/debug.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub fn expand_deriving_debug(
3030
additional_bounds: Vec::new(),
3131
generics: Bounds::empty(),
3232
is_unsafe: false,
33+
is_const: false,
3334
supports_unions: false,
3435
methods: vec![MethodDef {
3536
name: sym::fmt,

compiler/rustc_builtin_macros/src/deriving/decodable.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub fn expand_deriving_rustc_decodable(
2727
additional_bounds: Vec::new(),
2828
generics: Bounds::empty(),
2929
is_unsafe: false,
30+
is_const: false,
3031
supports_unions: false,
3132
methods: vec![MethodDef {
3233
name: sym::decode,

compiler/rustc_builtin_macros/src/deriving/default.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub fn expand_deriving_default(
3131
additional_bounds: Vec::new(),
3232
generics: Bounds::empty(),
3333
is_unsafe: false,
34+
is_const: false,
3435
supports_unions: false,
3536
methods: vec![MethodDef {
3637
name: kw::Default,

compiler/rustc_builtin_macros/src/deriving/encodable.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ pub fn expand_deriving_rustc_encodable(
112112
additional_bounds: Vec::new(),
113113
generics: Bounds::empty(),
114114
is_unsafe: false,
115+
is_const: false,
115116
supports_unions: false,
116117
methods: vec![MethodDef {
117118
name: sym::encode,

0 commit comments

Comments
 (0)