diff --git a/CHANGELOG.md b/CHANGELOG.md index 613292439a..28b548c51b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -217,6 +217,7 @@ enabling it by default for `bindgen-cli` (#2789) . - Fix `--allowlist-item` so anonymous enums are no longer ignored. - Use clang_getFileLocation instead of clang_getSpellingLocation to fix clang-trunk (#2824) +- Fix generated constants: `f64::INFINITY`, `f64::NEG_ INFINITY`, `f64::NAN` (#2854). ## Security diff --git a/bindgen-tests/tests/expectations/tests/infinite-macro.rs b/bindgen-tests/tests/expectations/tests/infinite-macro.rs index 455a7ae5ed..f19879fb17 100644 --- a/bindgen-tests/tests/expectations/tests/infinite-macro.rs +++ b/bindgen-tests/tests/expectations/tests/infinite-macro.rs @@ -1,3 +1,3 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] -pub const INFINITY: f64 = ::std::f64::INFINITY; -pub const NAN: f64 = ::std::f64::NAN; +pub const INFINITY: f64 = f64::INFINITY; +pub const NAN: f64 = f64::NAN; diff --git a/bindgen/codegen/helpers.rs b/bindgen/codegen/helpers.rs index 257ea5965a..4038340100 100644 --- a/bindgen/codegen/helpers.rs +++ b/bindgen/codegen/helpers.rs @@ -131,6 +131,7 @@ pub(crate) mod ast_ty { use crate::ir::function::FunctionSig; use crate::ir::layout::Layout; use crate::ir::ty::{FloatKind, IntKind}; + use crate::RustTarget; use proc_macro2::TokenStream; use std::str::FromStr; @@ -312,23 +313,45 @@ pub(crate) mod ast_ty { } let prefix = ctx.trait_prefix(); + let rust_target = ctx.options().rust_target; if f.is_nan() { - return Ok(quote! { - ::#prefix::f64::NAN - }); + let tokens = if rust_target >= RustTarget::Stable_1_43 { + quote! { + f64::NAN + } + } else { + quote! { + ::#prefix::f64::NAN + } + }; + return Ok(tokens); } if f.is_infinite() { - return Ok(if f.is_sign_positive() { - quote! { - ::#prefix::f64::INFINITY + let tokens = if f.is_sign_positive() { + if rust_target >= RustTarget::Stable_1_43 { + quote! { + f64::INFINITY + } + } else { + quote! { + ::#prefix::f64::INFINITY + } } } else { - quote! { - ::#prefix::f64::NEG_INFINITY + // Negative infinity + if rust_target >= RustTarget::Stable_1_43 { + quote! { + f64::NEG_INFINITY + } + } else { + quote! { + ::#prefix::f64::NEG_INFINITY + } } - }); + }; + return Ok(tokens); } warn!("Unknown non-finite float number: {:?}", f); diff --git a/bindgen/features.rs b/bindgen/features.rs index c07318c5e2..bd6a25cb84 100644 --- a/bindgen/features.rs +++ b/bindgen/features.rs @@ -106,6 +106,7 @@ define_rust_targets! { Stable_1_64(64) => { core_ffi_c: #94503 }, Stable_1_59(59) => { const_cstr: #54745 }, Stable_1_47(47) => { larger_arrays: #74060 }, + Stable_1_43(43) => { associated_constants: #68952 }, Stable_1_40(40) => { non_exhaustive: #44109 }, Stable_1_36(36) => { maybe_uninit: #60445 }, Stable_1_33(33) => { repr_packed_n: #57049 },