Skip to content

Commit aa103a6

Browse files
committed
ir: Centralize must_use checks and simplify codegen.
1 parent 3a8a60c commit aa103a6

File tree

4 files changed

+13
-23
lines changed

4 files changed

+13
-23
lines changed

src/codegen/mod.rs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,8 +2082,7 @@ impl CodeGenerator for CompInfo {
20822082
attributes.push(attributes::derives(&derives))
20832083
}
20842084

2085-
if item.annotations().must_use_type() || ctx.must_use_type_by_name(item)
2086-
{
2085+
if item.must_use(ctx) {
20872086
attributes.push(attributes::must_use());
20882087
}
20892088

@@ -3054,8 +3053,7 @@ impl CodeGenerator for Enum {
30543053
attrs.push(attributes::doc(comment));
30553054
}
30563055

3057-
if item.annotations().must_use_type() || ctx.must_use_type_by_name(item)
3058-
{
3056+
if item.must_use(ctx) {
30593057
attrs.push(attributes::must_use());
30603058
}
30613059

@@ -3967,26 +3965,12 @@ impl CodeGenerator for Function {
39673965

39683966
if ctx.options().rust_features().must_use_function {
39693967
let must_use = signature.must_use() || {
3970-
let ret_ty = signature.return_type();
3971-
3972-
let resolved_ret = ret_ty
3968+
let ret_ty = signature
3969+
.return_type()
39733970
.into_resolver()
39743971
.through_type_refs()
3975-
.through_type_aliases()
39763972
.resolve(ctx);
3977-
3978-
let must_use_resolved_ty =
3979-
resolved_ret.annotations().must_use_type() ||
3980-
ctx.must_use_type_by_name(resolved_ret);
3981-
3982-
let ret = ctx.resolve_item(ret_ty);
3983-
let must_use_ty = ret.annotations().must_use_type() ||
3984-
ctx.must_use_type_by_name(ret);
3985-
3986-
// If the return type already has #[must_use], the function does not
3987-
// need the annotation. This preserves the codegen behavior before
3988-
// type aliases with #[must_use] were supported.
3989-
!must_use_resolved_ty && must_use_ty
3973+
ret_ty.must_use(ctx)
39903974
};
39913975

39923976
if must_use {

src/ir/context.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,8 +1921,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
19211921
let item = Item::new(
19221922
with_id,
19231923
None,
1924-
self.resolve_item_fallible(wrapped_id)
1925-
.map(|item| item.annotations().clone()),
1924+
None,
19261925
parent_id.unwrap_or_else(|| self.current_module.into()),
19271926
ItemKind::Type(ty),
19281927
Some(location),

src/ir/item.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,11 @@ impl Item {
10961096
_ => return None,
10971097
})
10981098
}
1099+
1100+
/// Whether this is a #[must_use] type.
1101+
pub fn must_use(&self, ctx: &BindgenContext) -> bool {
1102+
self.annotations().must_use_type() || ctx.must_use_type_by_name(self)
1103+
}
10991104
}
11001105

11011106
impl<T> IsOpaque for T

tests/expectations/tests/func_return_must_use.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct MustUseStruct {
1717
_unused: [u8; 0],
1818
}
1919
extern "C" {
20+
#[must_use]
2021
pub fn return_struct() -> MustUseStruct;
2122
}
2223
/// <div rustbindgen mustusetype></div>
@@ -47,6 +48,7 @@ fn bindgen_test_layout_AnnotatedStruct() {
4748
);
4849
}
4950
extern "C" {
51+
#[must_use]
5052
pub fn return_annotated_struct() -> AnnotatedStruct;
5153
}
5254
#[repr(C)]

0 commit comments

Comments
 (0)