Skip to content

Commit 5875949

Browse files
emiliopvdrz
andauthored
codegen: Look through typedefs to detect void return type. (#2379)
* codegen: Look through typedefs to detect void return type. And reuse a bit more code. Should fix #2377, but needs a test (can't run tests atm). * Add tests * Run rustfmt * Update changelog Co-authored-by: Christian Poveda <[email protected]>
1 parent 1abaf7e commit 5875949

File tree

6 files changed

+71
-25
lines changed

6 files changed

+71
-25
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@
159159
* The `ParseCallbacks::generated_name_override` now receives `ItemInfo<'_>` as
160160
argument instead of a `&str`.
161161
* Updated the `clang-sys` crate version to 1.4.0 to support clang 15.
162-
162+
* The return type is now ommited in signatures of functions returning `void`.
163+
163164
## Removed
164165

165166
## Fixed

bindgen-tests/tests/expectations/tests/dynamic_loading_with_class.rs

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/void_typedef.rs

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
typedef void VOID;
2+
3+
typedef VOID ALSO_VOID;
4+
5+
void this_api_returns_nothing(void);
6+
7+
VOID this_api_also_returns_nothing(VOID);
8+
9+
ALSO_VOID this_other_api_also_returns_nothing(ALSO_VOID);

bindgen/codegen/dyngen.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl DynamicItems {
170170
if !is_variadic {
171171
self.struct_implementation.push(quote! {
172172
#(#attributes)*
173-
pub unsafe fn #ident ( &self, #( #args ),* ) -> #ret_ty {
173+
pub unsafe fn #ident ( &self, #( #args ),* ) #ret_ty {
174174
#call_body
175175
}
176176
});

bindgen/codegen/mod.rs

+40-22
Original file line numberDiff line numberDiff line change
@@ -4135,11 +4135,7 @@ impl CodeGenerator for Function {
41354135
if is_dynamic_function {
41364136
let args_identifiers =
41374137
utils::fnsig_argument_identifiers(ctx, signature);
4138-
let return_item = ctx.resolve_item(signature.return_type());
4139-
let ret_ty = match *return_item.kind().expect_type().kind() {
4140-
TypeKind::Void => quote! {()},
4141-
_ => return_item.to_rust_ty_or_opaque(ctx, &()),
4142-
};
4138+
let ret_ty = utils::fnsig_return_ty(ctx, signature);
41434139
result.dynamic_items().push(
41444140
ident,
41454141
abi,
@@ -4811,25 +4807,52 @@ pub mod utils {
48114807
})
48124808
}
48134809

4814-
pub fn fnsig_return_ty(
4810+
fn fnsig_return_ty_internal(
48154811
ctx: &BindgenContext,
48164812
sig: &FunctionSig,
4813+
include_arrow: bool,
48174814
) -> proc_macro2::TokenStream {
48184815
if sig.is_divergent() {
4819-
return quote! { -> ! };
4816+
return if include_arrow {
4817+
quote! { -> ! }
4818+
} else {
4819+
quote! { ! }
4820+
};
48204821
}
48214822

4822-
let return_item = ctx.resolve_item(sig.return_type());
4823-
if let TypeKind::Void = *return_item.kind().expect_type().kind() {
4824-
quote! {}
4823+
let canonical_type_kind = sig
4824+
.return_type()
4825+
.into_resolver()
4826+
.through_type_refs()
4827+
.through_type_aliases()
4828+
.resolve(ctx)
4829+
.kind()
4830+
.expect_type()
4831+
.kind();
4832+
4833+
if let TypeKind::Void = canonical_type_kind {
4834+
return if include_arrow {
4835+
quote! {}
4836+
} else {
4837+
quote! { () }
4838+
};
4839+
}
4840+
4841+
let ret_ty = sig.return_type().to_rust_ty_or_opaque(ctx, &());
4842+
if include_arrow {
4843+
quote! { -> #ret_ty }
48254844
} else {
4826-
let ret_ty = return_item.to_rust_ty_or_opaque(ctx, &());
4827-
quote! {
4828-
-> #ret_ty
4829-
}
4845+
ret_ty
48304846
}
48314847
}
48324848

4849+
pub fn fnsig_return_ty(
4850+
ctx: &BindgenContext,
4851+
sig: &FunctionSig,
4852+
) -> proc_macro2::TokenStream {
4853+
fnsig_return_ty_internal(ctx, sig, /* include_arrow = */ true)
4854+
}
4855+
48334856
pub fn fnsig_arguments(
48344857
ctx: &BindgenContext,
48354858
sig: &FunctionSig,
@@ -4942,14 +4965,9 @@ pub mod utils {
49424965
arg_item.to_rust_ty_or_opaque(ctx, &())
49434966
});
49444967

4945-
let return_item = ctx.resolve_item(sig.return_type());
4946-
let ret_ty =
4947-
if let TypeKind::Void = *return_item.kind().expect_type().kind() {
4948-
quote! { () }
4949-
} else {
4950-
return_item.to_rust_ty_or_opaque(ctx, &())
4951-
};
4952-
4968+
let ret_ty = fnsig_return_ty_internal(
4969+
ctx, sig, /* include_arrow = */ false,
4970+
);
49534971
quote! {
49544972
*const ::block::Block<(#(#args,)*), #ret_ty>
49554973
}

0 commit comments

Comments
 (0)