Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 78ec19f

Browse files
committed
expand_deriving_clone tweaks.
Improve a comment, and panic on an impossible code path.
1 parent 623ebbe commit 78ec19f

File tree

1 file changed

+22
-28
lines changed
  • compiler/rustc_builtin_macros/src/deriving

1 file changed

+22
-28
lines changed

compiler/rustc_builtin_macros/src/deriving/clone.rs

Lines changed: 22 additions & 28 deletions
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(..) => {
5958
bounds = vec![Literal(path_std!(marker::Copy))];
60-
is_shallow = true;
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"),
@@ -95,10 +89,10 @@ pub fn expand_deriving_clone(
9589
associated_types: Vec::new(),
9690
};
9791

98-
trait_def.expand_ext(cx, mitem, item, push, is_shallow)
92+
trait_def.expand_ext(cx, mitem, item, push, is_simple)
9993
}
10094

101-
fn cs_clone_shallow(
95+
fn cs_clone_simple(
10296
name: &str,
10397
cx: &mut ExtCtxt<'_>,
10498
trait_span: Span,
@@ -141,7 +135,7 @@ fn cs_clone_shallow(
141135
}
142136
_ => cx.span_bug(
143137
trait_span,
144-
&format!("unexpected substructure in shallow `derive({})`", name),
138+
&format!("unexpected substructure in simple `derive({})`", name),
145139
),
146140
}
147141
}

0 commit comments

Comments
 (0)