Skip to content

Commit 469fe22

Browse files
committed
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).
1 parent 1abaf7e commit 469fe22

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

bindgen/codegen/dyngen.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 39 additions & 22 deletions
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,53 @@ 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+
4850+
pub fn fnsig_return_ty(
4851+
ctx: &BindgenContext,
4852+
sig: &FunctionSig,
4853+
) -> proc_macro2::TokenStream {
4854+
fnsig_return_ty_internal(ctx, sig, /* include_arrow = */ true)
4855+
}
4856+
48334857
pub fn fnsig_arguments(
48344858
ctx: &BindgenContext,
48354859
sig: &FunctionSig,
@@ -4942,14 +4966,7 @@ pub mod utils {
49424966
arg_item.to_rust_ty_or_opaque(ctx, &())
49434967
});
49444968

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-
4969+
let ret_ty = fnsig_return_ty_internal(ctx, sig, /* include_arrow = */ false);
49534970
quote! {
49544971
*const ::block::Block<(#(#args,)*), #ret_ty>
49554972
}

0 commit comments

Comments
 (0)