Skip to content

Commit f705ccf

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

File tree

2 files changed

+21
-34
lines changed

2 files changed

+21
-34
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

+16-29
Original file line numberDiff line numberDiff line change
@@ -156,37 +156,14 @@ 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| {
159+
let can_derive_opaque = self.derive_trait.can_derive_opaque();
160+
if can_derive_opaque == CanDerive::Yes {
161+
return ty.layout(self.ctx).map_or(can_derive_opaque, |l| {
172162
l.opaque().array_size_within_derive_limit(self.ctx)
173163
});
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;
164+
} else {
165+
return can_derive_opaque;
166+
}
190167
}
191168

192169
match *ty.kind() {
@@ -513,6 +490,16 @@ impl DeriveTrait {
513490
}
514491
}
515492

493+
fn can_derive_opaque(&self) -> CanDerive {
494+
match self {
495+
DeriveTrait::Copy |
496+
DeriveTrait::Hash |
497+
DeriveTrait::PartialEqOrPartialOrd => CanDerive::No,
498+
DeriveTrait::Default => CanDerive::Yes,
499+
DeriveTrait::Debug => CanDerive::Manually,
500+
}
501+
}
502+
516503
fn can_derive_fnptr(&self, f: &FunctionSig) -> CanDerive {
517504
match (self, f.function_pointers_can_derive()) {
518505
(DeriveTrait::Copy, _) | (DeriveTrait::Default, _) | (_, true) => {

0 commit comments

Comments
 (0)