Skip to content

Fix generated constants: f64::INFINITY & f64::NEG_ INFINITY #2854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bindgen-tests/tests/expectations/tests/infinite-macro.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 4 additions & 9 deletions bindgen/codegen/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,32 +301,27 @@ pub(crate) mod ast_ty {
}
}

pub(crate) fn float_expr(
ctx: &BindgenContext,
f: f64,
) -> Result<TokenStream, ()> {
pub(crate) fn float_expr(f: f64) -> Result<TokenStream, ()> {
if f.is_finite() {
let val = proc_macro2::Literal::f64_unsuffixed(f);

return Ok(quote!(#val));
}

let prefix = ctx.trait_prefix();

if f.is_nan() {
return Ok(quote! {
::#prefix::f64::NAN
f64::NAN
});
}

if f.is_infinite() {
return Ok(if f.is_sign_positive() {
quote! {
::#prefix::f64::INFINITY
f64::INFINITY
}
} else {
quote! {
::#prefix::f64::NEG_INFINITY
f64::NEG_INFINITY
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@
}
}
VarType::Float(f) => {
if let Ok(expr) = helpers::ast_ty::float_expr(ctx, f) {
if let Ok(expr) = helpers::ast_ty::float_expr(f) {
result.push(quote! {
#(#attrs)*
pub const #canonical_ident : #ty = #expr ;
Expand Down Expand Up @@ -1658,13 +1658,13 @@

/// Compute a fields or structs visibility based on multiple conditions.
/// 1. If the element was declared public, and we respect such CXX accesses specs
/// (context option) => By default Public, but this can be overruled by an `annotation`.

Check warning on line 1661 in bindgen/codegen/mod.rs

View workflow job for this annotation

GitHub Actions / rustfmt-clippy

doc list item missing indentation
///
/// 2. If the element was declared private, and we respect such CXX accesses specs
/// (context option) => By default Private, but this can be overruled by an `annotation`.

Check warning on line 1664 in bindgen/codegen/mod.rs

View workflow job for this annotation

GitHub Actions / rustfmt-clippy

doc list item missing indentation
///
/// 3. If we do not respect visibility modifiers, the result depends on the `annotation`,
/// if any, or the passed `default_kind`.

Check warning on line 1667 in bindgen/codegen/mod.rs

View workflow job for this annotation

GitHub Actions / rustfmt-clippy

doc list item missing indentation
///
fn compute_visibility(
ctx: &BindgenContext,
Expand Down
Loading