Skip to content

Commit e32b66c

Browse files
committed
Auto merge of #8906 - rust-lang:copy-large-enum-variants, r=Jarcho
remove `large_enum_variant` suggestion for `Copy` types Replaces the (erroneous) suggestion on `large_enum_variant` for `Copy` types by a note. This fixes #8894. --- changelog: none
2 parents 9428e2e + 756caf7 commit e32b66c

File tree

3 files changed

+156
-32
lines changed

3 files changed

+156
-32
lines changed

clippy_lints/src/large_enum_variant.rs

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
//! lint when there is a large size difference between variants on an enum
22
3-
use clippy_utils::diagnostics::span_lint_and_then;
43
use clippy_utils::source::snippet_with_applicability;
4+
use clippy_utils::{diagnostics::span_lint_and_then, ty::is_copy};
55
use rustc_errors::Applicability;
66
use rustc_hir::{Item, ItemKind};
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_middle::lint::in_external_macro;
99
use rustc_middle::ty::layout::LayoutOf;
10+
use rustc_middle::ty::{Adt, Ty};
1011
use rustc_session::{declare_tool_lint, impl_lint_pass};
1112
use rustc_span::source_map::Span;
1213

@@ -26,6 +27,15 @@ declare_clippy_lint! {
2627
/// the overhead is negligible and the boxing is counter-productive. Always
2728
/// measure the change this lint suggests.
2829
///
30+
/// For types that implement `Copy`, the suggestion to `Box` a variant's
31+
/// data would require removing the trait impl. The types can of course
32+
/// still be `Clone`, but that is worse ergonomically. Depending on the
33+
/// use case it may be possible to store the large data in an auxillary
34+
/// structure (e.g. Arena or ECS).
35+
///
36+
/// The lint will ignore generic types if the layout depends on the
37+
/// generics, even if the size difference will be large anyway.
38+
///
2939
/// ### Example
3040
/// ```rust
3141
/// // Bad
@@ -74,7 +84,7 @@ struct VariantInfo {
7484
impl_lint_pass!(LargeEnumVariant => [LARGE_ENUM_VARIANT]);
7585

7686
impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant {
77-
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
87+
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &Item<'tcx>) {
7888
if in_external_macro(cx.tcx.sess, item.span) {
7989
return;
8090
}
@@ -132,41 +142,57 @@ impl<'tcx> LateLintPass<'tcx> for LargeEnumVariant {
132142
let fields = def.variants[variants_size[0].ind].data.fields();
133143
variants_size[0].fields_size.sort_by(|a, b| (a.size.cmp(&b.size)));
134144
let mut applicability = Applicability::MaybeIncorrect;
135-
let sugg: Vec<(Span, String)> = variants_size[0]
136-
.fields_size
137-
.iter()
138-
.rev()
139-
.map_while(|val| {
140-
if difference > self.maximum_size_difference_allowed {
141-
difference = difference.saturating_sub(val.size);
142-
Some((
143-
fields[val.ind].ty.span,
144-
format!(
145-
"Box<{}>",
146-
snippet_with_applicability(
147-
cx,
148-
fields[val.ind].ty.span,
149-
"..",
150-
&mut applicability
151-
)
152-
.into_owned()
153-
),
154-
))
155-
} else {
156-
None
157-
}
158-
})
159-
.collect();
145+
if is_copy(cx, ty) || maybe_copy(cx, ty) {
146+
diag.span_note(
147+
item.ident.span,
148+
"boxing a variant would require the type no longer be `Copy`",
149+
);
150+
} else {
151+
let sugg: Vec<(Span, String)> = variants_size[0]
152+
.fields_size
153+
.iter()
154+
.rev()
155+
.map_while(|val| {
156+
if difference > self.maximum_size_difference_allowed {
157+
difference = difference.saturating_sub(val.size);
158+
Some((
159+
fields[val.ind].ty.span,
160+
format!(
161+
"Box<{}>",
162+
snippet_with_applicability(
163+
cx,
164+
fields[val.ind].ty.span,
165+
"..",
166+
&mut applicability
167+
)
168+
.into_owned()
169+
),
170+
))
171+
} else {
172+
None
173+
}
174+
})
175+
.collect();
160176

161-
if !sugg.is_empty() {
162-
diag.multipart_suggestion(help_text, sugg, Applicability::MaybeIncorrect);
163-
return;
177+
if !sugg.is_empty() {
178+
diag.multipart_suggestion(help_text, sugg, Applicability::MaybeIncorrect);
179+
return;
180+
}
164181
}
165-
166182
diag.span_help(def.variants[variants_size[0].ind].span, help_text);
167183
},
168184
);
169185
}
170186
}
171187
}
172188
}
189+
190+
fn maybe_copy<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
191+
if let Adt(_def, substs) = ty.kind()
192+
&& substs.types().next().is_some()
193+
&& let Some(copy_trait) = cx.tcx.lang_items().copy_trait()
194+
{
195+
return cx.tcx.non_blanket_impls_for_ty(copy_trait, ty).next().is_some();
196+
}
197+
false
198+
}

tests/ui/large_enum_variant.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,38 @@ struct Struct2 {
9898
a: [i32; 8000],
9999
}
100100

101+
#[derive(Copy, Clone)]
102+
enum CopyableLargeEnum {
103+
A(bool),
104+
B([u128; 4000]),
105+
}
106+
107+
enum ManuallyCopyLargeEnum {
108+
A(bool),
109+
B([u128; 4000]),
110+
}
111+
112+
impl Clone for ManuallyCopyLargeEnum {
113+
fn clone(&self) -> Self {
114+
*self
115+
}
116+
}
117+
118+
impl Copy for ManuallyCopyLargeEnum {}
119+
120+
enum SomeGenericPossiblyCopyEnum<T> {
121+
A(bool, std::marker::PhantomData<T>),
122+
B([u64; 4000]),
123+
}
124+
125+
impl<T: Copy> Clone for SomeGenericPossiblyCopyEnum<T> {
126+
fn clone(&self) -> Self {
127+
*self
128+
}
129+
}
130+
131+
impl<T: Copy> Copy for SomeGenericPossiblyCopyEnum<T> {}
132+
101133
fn main() {
102134
large_enum_variant!();
103135
}

tests/ui/large_enum_variant.stderr

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,71 @@ help: consider boxing the large fields to reduce the total size of the enum
127127
LL | B(Box<Struct2>),
128128
| ~~~~~~~~~~~~
129129

130-
error: aborting due to 8 previous errors
130+
error: large size difference between variants
131+
--> $DIR/large_enum_variant.rs:104:5
132+
|
133+
LL | B([u128; 4000]),
134+
| ^^^^^^^^^^^^^^^ this variant is 64000 bytes
135+
|
136+
note: and the second-largest variant is 1 bytes:
137+
--> $DIR/large_enum_variant.rs:103:5
138+
|
139+
LL | A(bool),
140+
| ^^^^^^^
141+
note: boxing a variant would require the type no longer be `Copy`
142+
--> $DIR/large_enum_variant.rs:102:6
143+
|
144+
LL | enum CopyableLargeEnum {
145+
| ^^^^^^^^^^^^^^^^^
146+
help: consider boxing the large fields to reduce the total size of the enum
147+
--> $DIR/large_enum_variant.rs:104:5
148+
|
149+
LL | B([u128; 4000]),
150+
| ^^^^^^^^^^^^^^^
151+
152+
error: large size difference between variants
153+
--> $DIR/large_enum_variant.rs:109:5
154+
|
155+
LL | B([u128; 4000]),
156+
| ^^^^^^^^^^^^^^^ this variant is 64000 bytes
157+
|
158+
note: and the second-largest variant is 1 bytes:
159+
--> $DIR/large_enum_variant.rs:108:5
160+
|
161+
LL | A(bool),
162+
| ^^^^^^^
163+
note: boxing a variant would require the type no longer be `Copy`
164+
--> $DIR/large_enum_variant.rs:107:6
165+
|
166+
LL | enum ManuallyCopyLargeEnum {
167+
| ^^^^^^^^^^^^^^^^^^^^^
168+
help: consider boxing the large fields to reduce the total size of the enum
169+
--> $DIR/large_enum_variant.rs:109:5
170+
|
171+
LL | B([u128; 4000]),
172+
| ^^^^^^^^^^^^^^^
173+
174+
error: large size difference between variants
175+
--> $DIR/large_enum_variant.rs:122:5
176+
|
177+
LL | B([u64; 4000]),
178+
| ^^^^^^^^^^^^^^ this variant is 32000 bytes
179+
|
180+
note: and the second-largest variant is 1 bytes:
181+
--> $DIR/large_enum_variant.rs:121:5
182+
|
183+
LL | A(bool, std::marker::PhantomData<T>),
184+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
185+
note: boxing a variant would require the type no longer be `Copy`
186+
--> $DIR/large_enum_variant.rs:120:6
187+
|
188+
LL | enum SomeGenericPossiblyCopyEnum<T> {
189+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
190+
help: consider boxing the large fields to reduce the total size of the enum
191+
--> $DIR/large_enum_variant.rs:122:5
192+
|
193+
LL | B([u64; 4000]),
194+
| ^^^^^^^^^^^^^^
195+
196+
error: aborting due to 11 previous errors
131197

0 commit comments

Comments
 (0)