Skip to content

Commit 5018181

Browse files
committed
Auto merge of rust-lang#98767 - Dylan-DPC:rollup-j1gq5sr, r=Dylan-DPC
Rollup of 6 pull requests Successful merges: - rust-lang#97488 (Suggest blanket impl to the local traits) - rust-lang#98585 (Make `ThinBox<T>` covariant in `T`) - rust-lang#98644 (fix ICE with -Wrust-2021-incompatible-closure-captures) - rust-lang#98739 (fix grammar in useless doc comment lint) - rust-lang#98741 (Many small deriving cleanups) - rust-lang#98756 (Use const instead of function and make it private) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 5b9775f + 6404620 commit 5018181

27 files changed

+545
-331
lines changed

compiler/rustc_builtin_macros/src/deriving/bounds.rs

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ pub fn expand_deriving_copy(
1919
path: path_std!(marker::Copy),
2020
additional_bounds: Vec::new(),
2121
generics: Bounds::empty(),
22-
is_unsafe: false,
2322
supports_unions: true,
2423
methods: Vec::new(),
2524
associated_types: Vec::new(),

compiler/rustc_builtin_macros/src/deriving/clone.rs

+24-32
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,22 @@ 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+
// The simple form is `fn clone(&self) -> Self { *self }`, possibly with
19+
// some additional `AssertParamIsClone` assertions.
1920
//
20-
// the short form is `fn clone(&self) -> Self { *self }`
21-
//
22-
// we can use the short form if:
23-
// - the item is Copy (unfortunately, all we can check is whether it's also deriving Copy)
24-
// - there are no generic parameters (after specialization this limitation can be removed)
25-
// if we used the short form with generics, we'd have to bound the generics with
26-
// Clone + Copy, and then there'd be no Clone impl at all if the user fills in something
27-
// that is Clone but not Copy. and until specialization we can't write both impls.
28-
// - the item is a union with Copy fields
29-
// Unions with generic parameters still can derive Clone because they require Copy
30-
// for deriving, Clone alone is not enough.
31-
// Wherever Clone is implemented for fields is irrelevant so we don't assert it.
21+
// We can use the simple form if either of the following are true.
22+
// - The type derives Copy and there are no generic parameters. (If we
23+
// used the simple form with generics, we'd have to bound the generics
24+
// with Clone + Copy, and then there'd be no Clone impl at all if the
25+
// user fills in something that is Clone but not Copy. After
26+
// specialization we can remove this no-generics limitation.)
27+
// - The item is a union. (Unions with generic parameters still can derive
28+
// Clone because they require Copy for deriving, Clone alone is not
29+
// enough. Whether Clone is implemented for fields is irrelevant so we
30+
// don't assert it.)
3231
let bounds;
3332
let substructure;
34-
let is_shallow;
33+
let is_simple;
3534
match *item {
3635
Annotatable::Item(ref annitem) => match annitem.kind {
3736
ItemKind::Struct(_, Generics { ref params, .. })
@@ -44,30 +43,25 @@ pub fn expand_deriving_clone(
4443
.any(|param| matches!(param.kind, ast::GenericParamKind::Type { .. }))
4544
{
4645
bounds = vec![];
47-
is_shallow = true;
46+
is_simple = true;
4847
substructure = combine_substructure(Box::new(|c, s, sub| {
49-
cs_clone_shallow("Clone", c, s, sub, false)
48+
cs_clone_simple("Clone", c, s, sub, false)
5049
}));
5150
} else {
5251
bounds = vec![];
53-
is_shallow = false;
52+
is_simple = false;
5453
substructure =
5554
combine_substructure(Box::new(|c, s, sub| cs_clone("Clone", c, s, sub)));
5655
}
5756
}
5857
ItemKind::Union(..) => {
59-
bounds = vec![Literal(path_std!(marker::Copy))];
60-
is_shallow = true;
58+
bounds = vec![Path(path_std!(marker::Copy))];
59+
is_simple = true;
6160
substructure = combine_substructure(Box::new(|c, s, sub| {
62-
cs_clone_shallow("Clone", c, s, sub, true)
61+
cs_clone_simple("Clone", c, s, sub, true)
6362
}));
6463
}
65-
_ => {
66-
bounds = vec![];
67-
is_shallow = false;
68-
substructure =
69-
combine_substructure(Box::new(|c, s, sub| cs_clone("Clone", c, s, sub)));
70-
}
64+
_ => cx.span_bug(span, "`#[derive(Clone)]` on wrong item kind"),
7165
},
7266

7367
_ => cx.span_bug(span, "`#[derive(Clone)]` on trait item or impl item"),
@@ -81,26 +75,24 @@ pub fn expand_deriving_clone(
8175
path: path_std!(clone::Clone),
8276
additional_bounds: bounds,
8377
generics: Bounds::empty(),
84-
is_unsafe: false,
8578
supports_unions: true,
8679
methods: vec![MethodDef {
8780
name: sym::clone,
8881
generics: Bounds::empty(),
89-
explicit_self: borrowed_explicit_self(),
82+
explicit_self: true,
9083
args: Vec::new(),
9184
ret_ty: Self_,
9285
attributes: attrs,
93-
is_unsafe: false,
9486
unify_fieldless_variants: false,
9587
combine_substructure: substructure,
9688
}],
9789
associated_types: Vec::new(),
9890
};
9991

100-
trait_def.expand_ext(cx, mitem, item, push, is_shallow)
92+
trait_def.expand_ext(cx, mitem, item, push, is_simple)
10193
}
10294

103-
fn cs_clone_shallow(
95+
fn cs_clone_simple(
10496
name: &str,
10597
cx: &mut ExtCtxt<'_>,
10698
trait_span: Span,
@@ -143,7 +135,7 @@ fn cs_clone_shallow(
143135
}
144136
_ => cx.span_bug(
145137
trait_span,
146-
&format!("unexpected substructure in shallow `derive({})`", name),
138+
&format!("unexpected substructure in simple `derive({})`", name),
147139
),
148140
}
149141
}

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,14 @@ pub fn expand_deriving_eq(
2727
path: path_std!(cmp::Eq),
2828
additional_bounds: Vec::new(),
2929
generics: Bounds::empty(),
30-
is_unsafe: false,
3130
supports_unions: true,
3231
methods: vec![MethodDef {
3332
name: sym::assert_receiver_is_total_eq,
3433
generics: Bounds::empty(),
35-
explicit_self: borrowed_explicit_self(),
34+
explicit_self: true,
3635
args: vec![],
37-
ret_ty: nil_ty(),
36+
ret_ty: Unit,
3837
attributes: attrs,
39-
is_unsafe: false,
4038
unify_fieldless_variants: true,
4139
combine_substructure: combine_substructure(Box::new(|a, b, c| {
4240
cs_total_eq_assert(a, b, c)

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

+5-7
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,14 @@ pub fn expand_deriving_ord(
2323
path: path_std!(cmp::Ord),
2424
additional_bounds: Vec::new(),
2525
generics: Bounds::empty(),
26-
is_unsafe: false,
2726
supports_unions: false,
2827
methods: vec![MethodDef {
2928
name: sym::cmp,
3029
generics: Bounds::empty(),
31-
explicit_self: borrowed_explicit_self(),
32-
args: vec![(borrowed_self(), sym::other)],
33-
ret_ty: Literal(path_std!(cmp::Ordering)),
30+
explicit_self: true,
31+
args: vec![(self_ref(), sym::other)],
32+
ret_ty: Path(path_std!(cmp::Ordering)),
3433
attributes: attrs,
35-
is_unsafe: false,
3634
unify_fieldless_variants: true,
3735
combine_substructure: combine_substructure(Box::new(|a, b, c| cs_cmp(a, b, c))),
3836
}],
@@ -99,8 +97,8 @@ pub fn cs_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> P<
9997
cx.expr_match(span, new, vec![eq_arm, neq_arm])
10098
},
10199
cx.expr_path(equals_path.clone()),
102-
Box::new(|cx, span, (self_args, tag_tuple), _non_self_args| {
103-
if self_args.len() != 2 {
100+
Box::new(|cx, span, tag_tuple| {
101+
if tag_tuple.len() != 2 {
104102
cx.span_bug(span, "not exactly 2 arguments in `derive(Ord)`")
105103
} else {
106104
ordering_collapsed(cx, span, tag_tuple)

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

+4-6
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub fn expand_deriving_partial_eq(
4848
None => cx.expr_bool(span, base),
4949
}
5050
},
51-
Box::new(|cx, span, _, _| cx.expr_bool(span, !base)),
51+
Box::new(|cx, span, _| cx.expr_bool(span, !base)),
5252
cx,
5353
span,
5454
substr,
@@ -69,11 +69,10 @@ pub fn expand_deriving_partial_eq(
6969
MethodDef {
7070
name: $name,
7171
generics: Bounds::empty(),
72-
explicit_self: borrowed_explicit_self(),
73-
args: vec![(borrowed_self(), sym::other)],
74-
ret_ty: Literal(path_local!(bool)),
72+
explicit_self: true,
73+
args: vec![(self_ref(), sym::other)],
74+
ret_ty: Path(path_local!(bool)),
7575
attributes: attrs,
76-
is_unsafe: false,
7776
unify_fieldless_variants: true,
7877
combine_substructure: combine_substructure(Box::new(|a, b, c| $f(a, b, c))),
7978
}
@@ -102,7 +101,6 @@ pub fn expand_deriving_partial_eq(
102101
path: path_std!(cmp::PartialEq),
103102
additional_bounds: Vec::new(),
104103
generics: Bounds::empty(),
105-
is_unsafe: false,
106104
supports_unions: false,
107105
methods,
108106
associated_types: Vec::new(),

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

+7-13
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,20 @@ pub fn expand_deriving_partial_ord(
1515
item: &Annotatable,
1616
push: &mut dyn FnMut(Annotatable),
1717
) {
18-
let ordering_ty = Literal(path_std!(cmp::Ordering));
19-
let ret_ty = Literal(Path::new_(
20-
pathvec_std!(option::Option),
21-
None,
22-
vec![Box::new(ordering_ty)],
23-
PathKind::Std,
24-
));
18+
let ordering_ty = Path(path_std!(cmp::Ordering));
19+
let ret_ty =
20+
Path(Path::new_(pathvec_std!(option::Option), vec![Box::new(ordering_ty)], PathKind::Std));
2521

2622
let inline = cx.meta_word(span, sym::inline);
2723
let attrs = vec![cx.attribute(inline)];
2824

2925
let partial_cmp_def = MethodDef {
3026
name: sym::partial_cmp,
3127
generics: Bounds::empty(),
32-
explicit_self: borrowed_explicit_self(),
33-
args: vec![(borrowed_self(), sym::other)],
28+
explicit_self: true,
29+
args: vec![(self_ref(), sym::other)],
3430
ret_ty,
3531
attributes: attrs,
36-
is_unsafe: false,
3732
unify_fieldless_variants: true,
3833
combine_substructure: combine_substructure(Box::new(|cx, span, substr| {
3934
cs_partial_cmp(cx, span, substr)
@@ -46,7 +41,6 @@ pub fn expand_deriving_partial_ord(
4641
path: path_std!(cmp::PartialOrd),
4742
additional_bounds: vec![],
4843
generics: Bounds::empty(),
49-
is_unsafe: false,
5044
supports_unions: false,
5145
methods: vec![partial_cmp_def],
5246
associated_types: Vec::new(),
@@ -102,8 +96,8 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_
10296
cx.expr_match(span, new, vec![eq_arm, neq_arm])
10397
},
10498
equals_expr,
105-
Box::new(|cx, span, (self_args, tag_tuple), _non_self_args| {
106-
if self_args.len() != 2 {
99+
Box::new(|cx, span, tag_tuple| {
100+
if tag_tuple.len() != 2 {
107101
cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`")
108102
} else {
109103
let lft = cx.expr_addr_of(span, cx.expr_ident(span, tag_tuple[0]));

compiler/rustc_builtin_macros/src/deriving/debug.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,22 @@ pub fn expand_deriving_debug(
1616
push: &mut dyn FnMut(Annotatable),
1717
) {
1818
// &mut ::std::fmt::Formatter
19-
let fmtr =
20-
Ptr(Box::new(Literal(path_std!(fmt::Formatter))), Borrowed(None, ast::Mutability::Mut));
19+
let fmtr = Ref(Box::new(Path(path_std!(fmt::Formatter))), ast::Mutability::Mut);
2120

2221
let trait_def = TraitDef {
2322
span,
2423
attributes: Vec::new(),
2524
path: path_std!(fmt::Debug),
2625
additional_bounds: Vec::new(),
2726
generics: Bounds::empty(),
28-
is_unsafe: false,
2927
supports_unions: false,
3028
methods: vec![MethodDef {
3129
name: sym::fmt,
3230
generics: Bounds::empty(),
33-
explicit_self: borrowed_explicit_self(),
31+
explicit_self: true,
3432
args: vec![(fmtr, sym::f)],
35-
ret_ty: Literal(path_std!(fmt::Result)),
33+
ret_ty: Path(path_std!(fmt::Result)),
3634
attributes: Vec::new(),
37-
is_unsafe: false,
3835
unify_fieldless_variants: false,
3936
combine_substructure: combine_substructure(Box::new(|a, b, c| {
4037
show_substructure(a, b, c)
@@ -64,8 +61,6 @@ fn show_substructure(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>
6461
let (is_struct, args_per_field) = match vdata {
6562
ast::VariantData::Unit(..) => {
6663
// Special fast path for unit variants.
67-
//let fn_path_write_str = cx.std_path(&[sym::fmt, sym::Formatter, sym::write_str]);
68-
//return cx.expr_call_global(span, fn_path_write_str, vec![fmt, name]);
6964
assert!(fields.is_empty());
7065
(false, 0)
7166
}

compiler/rustc_builtin_macros/src/deriving/decodable.rs

+6-17
Original file line numberDiff line numberDiff line change
@@ -23,40 +23,29 @@ pub fn expand_deriving_rustc_decodable(
2323
let trait_def = TraitDef {
2424
span,
2525
attributes: Vec::new(),
26-
path: Path::new_(vec![krate, sym::Decodable], None, vec![], PathKind::Global),
26+
path: Path::new_(vec![krate, sym::Decodable], vec![], PathKind::Global),
2727
additional_bounds: Vec::new(),
2828
generics: Bounds::empty(),
29-
is_unsafe: false,
3029
supports_unions: false,
3130
methods: vec![MethodDef {
3231
name: sym::decode,
3332
generics: Bounds {
3433
bounds: vec![(
3534
typaram,
36-
vec![Path::new_(vec![krate, sym::Decoder], None, vec![], PathKind::Global)],
35+
vec![Path::new_(vec![krate, sym::Decoder], vec![], PathKind::Global)],
3736
)],
3837
},
39-
explicit_self: None,
40-
args: vec![(
41-
Ptr(Box::new(Literal(Path::new_local(typaram))), Borrowed(None, Mutability::Mut)),
42-
sym::d,
43-
)],
44-
ret_ty: Literal(Path::new_(
38+
explicit_self: false,
39+
args: vec![(Ref(Box::new(Path(Path::new_local(typaram))), Mutability::Mut), sym::d)],
40+
ret_ty: Path(Path::new_(
4541
pathvec_std!(result::Result),
46-
None,
4742
vec![
4843
Box::new(Self_),
49-
Box::new(Literal(Path::new_(
50-
vec![typaram, sym::Error],
51-
None,
52-
vec![],
53-
PathKind::Local,
54-
))),
44+
Box::new(Path(Path::new_(vec![typaram, sym::Error], vec![], PathKind::Local))),
5545
],
5646
PathKind::Std,
5747
)),
5848
attributes: Vec::new(),
59-
is_unsafe: false,
6049
unify_fieldless_variants: false,
6150
combine_substructure: combine_substructure(Box::new(|a, b, c| {
6251
decodable_substructure(a, b, c, krate)

compiler/rustc_builtin_macros/src/deriving/default.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,14 @@ pub fn expand_deriving_default(
3030
path: Path::new(vec![kw::Default, sym::Default]),
3131
additional_bounds: Vec::new(),
3232
generics: Bounds::empty(),
33-
is_unsafe: false,
3433
supports_unions: false,
3534
methods: vec![MethodDef {
3635
name: kw::Default,
3736
generics: Bounds::empty(),
38-
explicit_self: None,
37+
explicit_self: false,
3938
args: Vec::new(),
4039
ret_ty: Self_,
4140
attributes: attrs,
42-
is_unsafe: false,
4341
unify_fieldless_variants: false,
4442
combine_substructure: combine_substructure(Box::new(|cx, trait_span, substr| {
4543
match substr.fields {

0 commit comments

Comments
 (0)