Skip to content

Commit 133342d

Browse files
committed
Disallow Copy/Clone/Eq*/Ord* for opaque types
1 parent ec85170 commit 133342d

File tree

2 files changed

+16
-36
lines changed

2 files changed

+16
-36
lines changed

src/codegen/impl_partialeq.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ pub fn gen_partialeq_impl(
1212
item: &Item,
1313
ty_for_impl: &proc_macro2::TokenStream,
1414
) -> Option<proc_macro2::TokenStream> {
15+
debug_assert!(
16+
!item.is_opaque(ctx, &()),
17+
"You're not supposed to call this on an opaque type"
18+
);
1519
let mut tokens = vec![];
1620

17-
if item.is_opaque(ctx, &()) {
18-
tokens.push(quote! {
19-
&self._bindgen_opaque_blob[..] == &other._bindgen_opaque_blob[..]
20-
});
21-
} else if comp_info.kind() == CompKind::Union {
21+
if comp_info.kind() == CompKind::Union {
2222
assert!(!ctx.options().rust_features().untagged_union);
2323
tokens.push(quote! {
2424
&self.bindgen_union_field[..] == &other.bindgen_union_field[..]

src/ir/analysis/derive.rs

+11-31
Original file line numberDiff line numberDiff line change
@@ -156,37 +156,7 @@ impl<'ctx> CannotDerive<'ctx> {
156156

157157
trace!("ty: {:?}", ty);
158158
if item.is_opaque(self.ctx, &()) {
159-
if !self.derive_trait.can_derive_union() &&
160-
ty.is_union() &&
161-
self.ctx.options().rust_features().untagged_union
162-
{
163-
trace!(
164-
" cannot derive {} for Rust unions",
165-
self.derive_trait
166-
);
167-
return CanDerive::No;
168-
}
169-
170-
let layout_can_derive =
171-
ty.layout(self.ctx).map_or(CanDerive::Yes, |l| {
172-
l.opaque().array_size_within_derive_limit(self.ctx)
173-
});
174-
175-
match layout_can_derive {
176-
CanDerive::Yes => {
177-
trace!(
178-
" we can trivially derive {} for the layout",
179-
self.derive_trait
180-
);
181-
}
182-
_ => {
183-
trace!(
184-
" we cannot derive {} for the layout",
185-
self.derive_trait
186-
);
187-
}
188-
};
189-
return layout_can_derive;
159+
return self.derive_trait.can_derive_opaque()
190160
}
191161

192162
match *ty.kind() {
@@ -513,6 +483,16 @@ impl DeriveTrait {
513483
}
514484
}
515485

486+
fn can_derive_opaque(&self) -> CanDerive {
487+
match self {
488+
DeriveTrait::Copy |
489+
DeriveTrait::Hash |
490+
DeriveTrait::PartialEqOrPartialOrd => CanDerive::No,
491+
DeriveTrait::Default => CanDerive::Yes,
492+
DeriveTrait::Debug => CanDerive::Manually,
493+
}
494+
}
495+
516496
fn can_derive_fnptr(&self, f: &FunctionSig) -> CanDerive {
517497
match (self, f.function_pointers_can_derive()) {
518498
(DeriveTrait::Copy, _) | (DeriveTrait::Default, _) | (_, true) => {

0 commit comments

Comments
 (0)