Repository: rust-lang/rust-bindgen
Pull request #2854 (2024-06-25 07:09:20)

Fix generated constants: f64::INFINITY & f64::NEG_ INFINITY
https://github.com/rust-lang/rust-bindgen/issues/2853

pvdrz: > I'm not sure of Bindgen's MSRV policy for generated crates, but these constants require at least Rust 1.43.

If that's the case, there should be a new variant in `RustTarget` representing 1.43 and this change should be gated behind a feature that enables if the target version is larger or equal than 1.43
gabiganam: > > I'm not sure of Bindgen's MSRV policy for generated crates, but these constants require at least Rust 1.43.
> 
> If that's the case, there should be a new variant in `RustTarget` representing 1.43 and this change should be gated behind a feature that enables if the target version is larger or equal than 1.43

Updated the PR, hope this is what you meant.
madsmtm: > @gabiganam requested a review from @madsmtm

Sorry, I'm the wrong person to ask for this, I'm not a maintainer here.

Patch:
diff --git a/.github/workflows/bindgen.yml b/.github/workflows/bindgen.yml
index 502f9146..899d7713 100644
--- a/.github/workflows/bindgen.yml
+++ b/.github/workflows/bindgen.yml
@@ -16,7 +16,7 @@ jobs:
     runs-on: ubuntu-latest
 
     steps:
-      - uses: actions/checkout@v3
+      - uses: actions/checkout@v4
 
       - name: Install stable
         uses: actions-rs/toolchain@v1
@@ -43,7 +43,7 @@ jobs:
   msrv:
     runs-on: ubuntu-latest
     steps:
-      - uses: actions/checkout@v3
+      - uses: actions/checkout@v4
 
       - name: Install msrv for lib
         uses: dtolnay/rust-toolchain@master
@@ -70,7 +70,7 @@ jobs:
     env:
       RUSTFLAGS: -D warnings
     steps:
-      - uses: actions/checkout@v3
+      - uses: actions/checkout@v4
 
       - name: Install stable
         uses: actions-rs/toolchain@v1
@@ -87,7 +87,7 @@ jobs:
     env:
       RUSTDOCFLAGS: -D warnings
     steps:
-      - uses: actions/checkout@v3
+      - uses: actions/checkout@v4
 
       - name: Install stable
         uses: actions-rs/toolchain@v1
@@ -105,7 +105,7 @@ jobs:
   quickchecking:
     runs-on: ubuntu-latest
     steps:
-      - uses: actions/checkout@v3
+      - uses: actions/checkout@v4
 
       - name: Install stable
         uses: actions-rs/toolchain@v1
@@ -124,7 +124,7 @@ jobs:
       matrix:
         os: [ubuntu-latest, macos-12]
     steps:
-      - uses: actions/checkout@v3
+      - uses: actions/checkout@v4
 
       - name: Install stable
         uses: actions-rs/toolchain@v1
@@ -187,7 +187,7 @@ jobs:
             no_default_features: 0
             feature_extra_asserts: 0
     steps:
-      - uses: actions/checkout@v3
+      - uses: actions/checkout@v4
 
       - name: Install multiarch packages
         if: matrix.target.debian
@@ -234,7 +234,7 @@ jobs:
     env:
       RUSTFLAGS: -D warnings
     steps:
-      - uses: actions/checkout@v3
+      - uses: actions/checkout@v4
       
       - name: Install nightly
         uses: actions-rs/toolchain@v1
@@ -249,7 +249,7 @@ jobs:
   test-book:
     runs-on: ubuntu-latest
     steps:
-      - uses: actions/checkout@v3
+      - uses: actions/checkout@v4
 
       # NOTE(emilio): Change deploy-book as well if you change this.
       - name: Test book
diff --git a/.github/workflows/deploy-book.yml b/.github/workflows/deploy-book.yml
index 9bb1e1b8..5eaeed58 100644
--- a/.github/workflows/deploy-book.yml
+++ b/.github/workflows/deploy-book.yml
@@ -9,7 +9,7 @@ jobs:
   deploy-book:
     runs-on: ubuntu-latest
     steps:
-      - uses: actions/checkout@v3
+      - uses: actions/checkout@v4
         with:
           persist-credentials: false
 
diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml
index 1fbc1504..352199d7 100644
--- a/.github/workflows/publish.yml
+++ b/.github/workflows/publish.yml
@@ -13,7 +13,7 @@ jobs:
     if: ${{ github.event.workflow_run.conclusion == 'success' }}
     steps:
       - name: Checkout sources
-        uses: actions/checkout@v3
+        uses: actions/checkout@v4
       - name: Install stable toolchain
         uses: dtolnay/rust-toolchain@master
         with:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 61329243..266db1dc 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
 
@@ -448,7 +449,7 @@ This version was skipped due to some problems on the release workflow.
  * The `ParseCallbacks::generated_name_override` method now receives `ItemInfo<'_>` as
    argument instead of a `&str`.
  * Updated the `clang-sys` crate version to 1.4.0 to support clang 15.
- * The return type is now ommited in signatures of functions returning `void`.
+ * The return type is now omitted in signatures of functions returning `void`.
  * Updated the `clap` dependency for `bindgen-cli` to 4.
  * Rewrote the `bindgen-cli` argument parser which could introduce unexpected
    behavior changes.
@@ -1485,7 +1486,7 @@ Released 2017/10/27
   We <3 folks who [help us find and fix issues via fuzzing][fuzzing]! *hint
   hint*
 
-* Added experimental support for the `thiscall` ABI when targetting Rust
+* Added experimental support for the `thiscall` ABI when targeting Rust
   nightly. [#1065][]
 
 ## Changed
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
index af069be2..0ffc5856 100644
--- a/CONTRIBUTING.md
+++ b/CONTRIBUTING.md
@@ -37,7 +37,9 @@ and introduce yourself.
   - [Updating the changelog](#updating-the-changelog)
   - [Merge to `main`](#merge-to-main)
   - [Tag and publish](#tag-and-publish)
-  - [Create a new release on GitHub](create-a-new-relese-on-github)
+  - [Create a new release on Github](#create-a-new-release-on-github)
+  - [What to do if a Github release fails](#what-to-do-if-a-github-release-fails)
+  - [Create a new crates.io release](#create-a-new-cratesio-release)
 
 <!-- END doctoc generated TOC please keep comment here to allow auto update -->
 
diff --git a/bindgen-cli/options.rs b/bindgen-cli/options.rs
index a8780b79..cd5e9bb1 100644
--- a/bindgen-cli/options.rs
+++ b/bindgen-cli/options.rs
@@ -376,7 +376,7 @@ struct BindgenCommand {
     /// Prefix the name of exported symbols.
     #[arg(long)]
     prefix_link_name: Option<String>,
-    /// Makes generated bindings `pub` only for items if the items are publically accessible in C++.
+    /// Makes generated bindings `pub` only for items if the items are publicly accessible in C++.
     #[arg(long)]
     respect_cxx_access_specs: bool,
     /// Always translate enum integer types to native Rust integer types.
diff --git a/bindgen/clang.rs b/bindgen/clang.rs
index 26c02ace..47c7b170 100644
--- a/bindgen/clang.rs
+++ b/bindgen/clang.rs
@@ -876,7 +876,7 @@ impl Cursor {
         unsafe { clang_getCXXAccessSpecifier(self.x) }
     }
 
-    /// Is the cursor's referent publically accessible in C++?
+    /// Is the cursor's referent publicly accessible in C++?
     ///
     /// Returns true if self.access_specifier() is `CX_CXXPublic` or
     /// `CX_CXXInvalidAccessSpecifier`.
diff --git a/bindgen/codegen/helpers.rs b/bindgen/codegen/helpers.rs
index 257ea596..40383401 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 c07318c5..bd6a25cb 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 },
diff --git a/bindgen/ir/comp.rs b/bindgen/ir/comp.rs
index 13a8184f..036e7e5c 100644
--- a/bindgen/ir/comp.rs
+++ b/bindgen/ir/comp.rs
@@ -948,7 +948,7 @@ pub(crate) struct Base {
     pub(crate) kind: BaseKind,
     /// Name of the field in which this base should be stored.
     pub(crate) field_name: String,
-    /// Whether this base is inherited from publically.
+    /// Whether this base is inherited from publicly.
     pub(crate) is_pub: bool,
 }
 
@@ -978,7 +978,7 @@ impl Base {
         true
     }
 
-    /// Whether this base is inherited from publically.
+    /// Whether this base is inherited from publicly.
     pub(crate) fn is_public(&self) -> bool {
         self.is_pub
     }
diff --git a/bindgen/ir/var.rs b/bindgen/ir/var.rs
index 9d46135f..40a061e1 100644
--- a/bindgen/ir/var.rs
+++ b/bindgen/ir/var.rs
@@ -489,7 +489,7 @@ fn duplicated_macro_diagnostic(
     #[cfg(feature = "experimental")]
     // FIXME (pvdrz & amanjeev): This diagnostic message shows way too often to be actually
     // useful. We have to change the logic where this function is called to be able to emit this
-    // message only when the duplication is an actuall issue.
+    // message only when the duplication is an actual issue.
     //
     // If I understood correctly, `bindgen` ignores all `#undef` directives. Meaning that this:
     // ```c


Test patch:
diff --git a/bindgen-tests/tests/expectations/tests/infinite-macro.rs b/bindgen-tests/tests/expectations/tests/infinite-macro.rs
index 455a7ae5..f19879fb 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;