From 5fbbd262d5e7f75589701df975ab4c1301baa287 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sun, 5 Jun 2022 19:37:24 +0200 Subject: [PATCH] ir: Centralize must_use checks and simplify codegen. --- src/codegen/mod.rs | 26 ++++--------------- src/ir/context.rs | 3 +-- src/ir/item.rs | 5 ++++ .../tests/func_return_must_use.rs | 2 ++ 4 files changed, 13 insertions(+), 23 deletions(-) diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index fdc9b5ed16..4dd520368b 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -2082,8 +2082,7 @@ impl CodeGenerator for CompInfo { attributes.push(attributes::derives(&derives)) } - if item.annotations().must_use_type() || ctx.must_use_type_by_name(item) - { + if item.must_use(ctx) { attributes.push(attributes::must_use()); } @@ -3054,8 +3053,7 @@ impl CodeGenerator for Enum { attrs.push(attributes::doc(comment)); } - if item.annotations().must_use_type() || ctx.must_use_type_by_name(item) - { + if item.must_use(ctx) { attrs.push(attributes::must_use()); } @@ -3967,26 +3965,12 @@ impl CodeGenerator for Function { if ctx.options().rust_features().must_use_function { let must_use = signature.must_use() || { - let ret_ty = signature.return_type(); - - let resolved_ret = ret_ty + let ret_ty = signature + .return_type() .into_resolver() .through_type_refs() - .through_type_aliases() .resolve(ctx); - - let must_use_resolved_ty = - resolved_ret.annotations().must_use_type() || - ctx.must_use_type_by_name(resolved_ret); - - let ret = ctx.resolve_item(ret_ty); - let must_use_ty = ret.annotations().must_use_type() || - ctx.must_use_type_by_name(ret); - - // If the return type already has #[must_use], the function does not - // need the annotation. This preserves the codegen behavior before - // type aliases with #[must_use] were supported. - !must_use_resolved_ty && must_use_ty + ret_ty.must_use(ctx) }; if must_use { diff --git a/src/ir/context.rs b/src/ir/context.rs index 2f71debd00..9cf43ec3c6 100644 --- a/src/ir/context.rs +++ b/src/ir/context.rs @@ -1921,8 +1921,7 @@ If you encounter an error missing from this list, please file an issue or a PR!" let item = Item::new( with_id, None, - self.resolve_item_fallible(wrapped_id) - .map(|item| item.annotations().clone()), + None, parent_id.unwrap_or_else(|| self.current_module.into()), ItemKind::Type(ty), Some(location), diff --git a/src/ir/item.rs b/src/ir/item.rs index aed575ca11..6ce420171f 100644 --- a/src/ir/item.rs +++ b/src/ir/item.rs @@ -1096,6 +1096,11 @@ impl Item { _ => return None, }) } + + /// Whether this is a #[must_use] type. + pub fn must_use(&self, ctx: &BindgenContext) -> bool { + self.annotations().must_use_type() || ctx.must_use_type_by_name(self) + } } impl IsOpaque for T diff --git a/tests/expectations/tests/func_return_must_use.rs b/tests/expectations/tests/func_return_must_use.rs index 9f40aaa61a..6ea6c704f0 100644 --- a/tests/expectations/tests/func_return_must_use.rs +++ b/tests/expectations/tests/func_return_must_use.rs @@ -17,6 +17,7 @@ pub struct MustUseStruct { _unused: [u8; 0], } extern "C" { + #[must_use] pub fn return_struct() -> MustUseStruct; } ///
@@ -47,6 +48,7 @@ fn bindgen_test_layout_AnnotatedStruct() { ); } extern "C" { + #[must_use] pub fn return_annotated_struct() -> AnnotatedStruct; } #[repr(C)]