Skip to content

Commit 38c9f24

Browse files
author
Gabi Ganam
committed
Fix generated constants: f64::INFINITY, f64::NEG_ INFINITY, f64::NAN
#2853
1 parent 72c2c5d commit 38c9f24

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

bindgen/codegen/helpers.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,27 +301,44 @@ pub(crate) mod ast_ty {
301301
}
302302
}
303303

304-
pub(crate) fn float_expr(f: f64) -> Result<TokenStream, ()> {
304+
pub(crate) fn float_expr(
305+
ctx: &BindgenContext,
306+
f: f64,
307+
) -> Result<TokenStream, ()> {
305308
if f.is_finite() {
306309
let val = proc_macro2::Literal::f64_unsuffixed(f);
307310

308311
return Ok(quote!(#val));
309312
}
310313

314+
let prefix = ctx.trait_prefix();
315+
311316
if f.is_nan() {
312317
return Ok(quote! {
313-
f64::NAN
318+
if rust_target >= RustTarget::Stable_1_43 {
319+
f64::NAN
320+
} else {
321+
::#prefix::f64::NAN
322+
}
314323
});
315324
}
316325

317326
if f.is_infinite() {
318327
return Ok(if f.is_sign_positive() {
319328
quote! {
320-
f64::INFINITY
329+
if rust_target >= RustTarget::Stable_1_43 {
330+
f64::INFINITY
331+
} else {
332+
::#prefix::f64::INFINITY
333+
}
321334
}
322335
} else {
323336
quote! {
324-
f64::NEG_INFINITY
337+
if rust_target >= RustTarget::Stable_1_43 {
338+
f64::NEG_INFINITY
339+
} else {
340+
::#prefix::f64::NEG_INFINITY
341+
}
325342
}
326343
});
327344
}

bindgen/codegen/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ impl CodeGenerator for Var {
756756
}
757757
}
758758
VarType::Float(f) => {
759-
if let Ok(expr) = helpers::ast_ty::float_expr(f) {
759+
if let Ok(expr) = helpers::ast_ty::float_expr(ctx, f) {
760760
result.push(quote! {
761761
#(#attrs)*
762762
pub const #canonical_ident : #ty = #expr ;

0 commit comments

Comments
 (0)