Skip to content

Commit e7af76c

Browse files
committed
add a TrivialClone implementation when deriving both Clone and Copy
1 parent a2b4ef7 commit e7af76c

File tree

15 files changed

+77
-15
lines changed

15 files changed

+77
-15
lines changed

compiler/rustc_builtin_macros/src/deriving/bounds.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_ast::MetaItem;
1+
use rustc_ast::{MetaItem, Safety};
22
use rustc_expand::base::{Annotatable, ExtCtxt};
33
use rustc_span::Span;
44

@@ -23,6 +23,7 @@ pub(crate) fn expand_deriving_copy(
2323
methods: Vec::new(),
2424
associated_types: Vec::new(),
2525
is_const,
26+
safety: Safety::Default,
2627
};
2728

2829
trait_def.expand(cx, mitem, item, push);
@@ -46,6 +47,7 @@ pub(crate) fn expand_deriving_const_param_ty(
4647
methods: Vec::new(),
4748
associated_types: Vec::new(),
4849
is_const,
50+
safety: Safety::Default,
4951
};
5052

5153
trait_def.expand(cx, mitem, item, push);
@@ -60,6 +62,7 @@ pub(crate) fn expand_deriving_const_param_ty(
6062
methods: Vec::new(),
6163
associated_types: Vec::new(),
6264
is_const,
65+
safety: Safety::Default,
6366
};
6467

6568
trait_def.expand(cx, mitem, item, push);
@@ -83,6 +86,7 @@ pub(crate) fn expand_deriving_unsized_const_param_ty(
8386
methods: Vec::new(),
8487
associated_types: Vec::new(),
8588
is_const,
89+
safety: Safety::Default,
8690
};
8791

8892
trait_def.expand(cx, mitem, item, push);

compiler/rustc_builtin_macros/src/deriving/clone.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use rustc_ast::{self as ast, Generics, ItemKind, MetaItem, VariantData};
1+
use rustc_ast::{self as ast, Generics, ItemKind, MetaItem, Safety, VariantData};
22
use rustc_data_structures::fx::FxHashSet;
33
use rustc_expand::base::{Annotatable, ExtCtxt};
4-
use rustc_span::{Ident, Span, kw, sym};
4+
use rustc_span::{DUMMY_SP, Ident, Span, kw, sym};
55
use thin_vec::{ThinVec, thin_vec};
66

77
use 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)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_ast::{self as ast, MetaItem};
1+
use rustc_ast::{self as ast, MetaItem, Safety};
22
use rustc_data_structures::fx::FxHashSet;
33
use rustc_expand::base::{Annotatable, ExtCtxt};
44
use rustc_span::{Span, sym};
@@ -43,6 +43,7 @@ pub(crate) fn expand_deriving_eq(
4343
}],
4444
associated_types: Vec::new(),
4545
is_const,
46+
safety: Safety::Default,
4647
};
4748
trait_def.expand_ext(cx, mitem, item, push, true)
4849
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_ast::MetaItem;
1+
use rustc_ast::{MetaItem, Safety};
22
use rustc_expand::base::{Annotatable, ExtCtxt};
33
use rustc_span::{Ident, Span, sym};
44
use thin_vec::thin_vec;
@@ -34,6 +34,7 @@ pub(crate) fn expand_deriving_ord(
3434
}],
3535
associated_types: Vec::new(),
3636
is_const,
37+
safety: Safety::Default,
3738
};
3839

3940
trait_def.expand(cx, mitem, item, push)

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_ast::ptr::P;
2-
use rustc_ast::{BinOpKind, BorrowKind, Expr, ExprKind, MetaItem, Mutability};
2+
use rustc_ast::{BinOpKind, BorrowKind, Expr, ExprKind, MetaItem, Mutability, Safety};
33
use rustc_expand::base::{Annotatable, ExtCtxt};
44
use rustc_span::{Span, sym};
55
use thin_vec::thin_vec;
@@ -84,6 +84,7 @@ pub(crate) fn expand_deriving_partial_eq(
8484
methods: Vec::new(),
8585
associated_types: Vec::new(),
8686
is_const: false,
87+
safety: Safety::Default,
8788
};
8889
structural_trait_def.expand(cx, mitem, item, push);
8990

@@ -110,6 +111,7 @@ pub(crate) fn expand_deriving_partial_eq(
110111
methods,
111112
associated_types: Vec::new(),
112113
is_const,
114+
safety: Safety::Default,
113115
};
114116
trait_def.expand(cx, mitem, item, push)
115117
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_ast::{ExprKind, ItemKind, MetaItem, PatKind};
1+
use rustc_ast::{ExprKind, ItemKind, MetaItem, PatKind, Safety};
22
use rustc_expand::base::{Annotatable, ExtCtxt};
33
use rustc_span::{Ident, Span, sym};
44
use thin_vec::thin_vec;
@@ -64,6 +64,7 @@ pub(crate) fn expand_deriving_partial_ord(
6464
methods: vec![partial_cmp_def],
6565
associated_types: Vec::new(),
6666
is_const,
67+
safety: Safety::Default,
6768
};
6869
trait_def.expand(cx, mitem, item, push)
6970
}

compiler/rustc_builtin_macros/src/deriving/debug.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_ast::{self as ast, EnumDef, MetaItem};
1+
use rustc_ast::{self as ast, EnumDef, MetaItem, Safety};
22
use rustc_expand::base::{Annotatable, ExtCtxt};
33
use rustc_session::config::FmtDebug;
44
use rustc_span::{Ident, Span, Symbol, sym};
@@ -41,6 +41,7 @@ pub(crate) fn expand_deriving_debug(
4141
}],
4242
associated_types: Vec::new(),
4343
is_const,
44+
safety: Safety::Default,
4445
};
4546
trait_def.expand(cx, mitem, item, push)
4647
}

compiler/rustc_builtin_macros/src/deriving/default.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use core::ops::ControlFlow;
22

3-
use rustc_ast as ast;
43
use rustc_ast::visit::visit_opt;
5-
use rustc_ast::{EnumDef, VariantData, attr};
4+
use rustc_ast::{self as ast, EnumDef, Safety, VariantData, attr};
65
use rustc_expand::base::{Annotatable, DummyResult, ExtCtxt};
76
use rustc_span::{ErrorGuaranteed, Ident, Span, kw, sym};
87
use smallvec::SmallVec;
@@ -51,6 +50,7 @@ pub(crate) fn expand_deriving_default(
5150
}],
5251
associated_types: Vec::new(),
5352
is_const,
53+
safety: Safety::Default,
5454
};
5555
trait_def.expand(cx, mitem, item, push)
5656
}

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ pub(crate) use SubstructureFields::*;
183183
use rustc_ast::ptr::P;
184184
use rustc_ast::{
185185
self as ast, AnonConst, BindingMode, ByRef, EnumDef, Expr, GenericArg, GenericParamKind,
186-
Generics, Mutability, PatKind, VariantData,
186+
Generics, Mutability, PatKind, Safety, VariantData,
187187
};
188188
use rustc_attr_parsing::{AttributeKind, AttributeParser, ReprPacked};
189189
use rustc_expand::base::{Annotatable, ExtCtxt};
@@ -221,6 +221,9 @@ pub(crate) struct TraitDef<'a> {
221221
pub associated_types: Vec<(Ident, Ty)>,
222222

223223
pub is_const: bool,
224+
225+
/// The safety of the `impl`.
226+
pub safety: Safety,
224227
}
225228

226229
pub(crate) struct MethodDef<'a> {
@@ -792,7 +795,7 @@ impl<'a> TraitDef<'a> {
792795
Ident::empty(),
793796
attrs,
794797
ast::ItemKind::Impl(Box::new(ast::Impl {
795-
safety: ast::Safety::Default,
798+
safety: self.safety,
796799
polarity: ast::ImplPolarity::Positive,
797800
defaultness: ast::Defaultness::Final,
798801
constness: if self.is_const { ast::Const::Yes(DUMMY_SP) } else { ast::Const::No },

compiler/rustc_builtin_macros/src/deriving/hash.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_ast::{MetaItem, Mutability};
1+
use rustc_ast::{MetaItem, Mutability, Safety};
22
use rustc_expand::base::{Annotatable, ExtCtxt};
33
use rustc_span::{Span, sym};
44
use thin_vec::thin_vec;
@@ -41,6 +41,7 @@ pub(crate) fn expand_deriving_hash(
4141
}],
4242
associated_types: Vec::new(),
4343
is_const,
44+
safety: Safety::Default,
4445
};
4546

4647
hash_trait_def.expand(cx, mitem, item, push);

0 commit comments

Comments
 (0)