Repository: rust-lang/rust-bindgen
Pull request #2413 (2023-02-11 19:55:38)

Add warning section to build dependency tutorial doc
Users (such as myself) that have enough experience to know to copy dependencies into the `Cargo.toml` file, but not enough to know that they should not add it under `[dependencies]`. Note that searching for the error does point you directly to [this thread](https://users.rust-lang.org/t/bindgen-basic-help/11478) which has a solution, so not like it's particularly difficult to resolve.

I chose this style of block for the warning since it looks alright and by default, neither standard Markdown nor mdbook supports admonishments (though if we really wanted, we could add a preprocessor like [mdbook-admonish](https://tommilligan.github.io/mdbook-admonish/), but that seems like overkill).

Also added the actual error message both for reference and to boost the likelihood that this will capture search traffic

Finally, there is the question of whether we want to add this at all, or if it just adds noise to the docs. I will defer to reviewers judgement, but I would argue that `bindgen` would be a very common first build-time crate that a dev will encounter when learning Rust, so this is a great opportunity to highlight the distinction and link to the Cargo docs.

Included is a screenshot of what this looks like on localhost:
<img width="1043" alt="image" src="https://user-images.githubusercontent.com/102886067/218278498-29679393-eec8-498f-84be-8fd827ef8277.png">



jahs-tech: r? @emilio

Patch:
diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs
index b6fb70eb..b5a7b27c 100644
--- a/bindgen/codegen/mod.rs
+++ b/bindgen/codegen/mod.rs
@@ -4029,14 +4029,10 @@ impl CodeGenerator for Function {
 
         let is_internal = matches!(self.linkage(), Linkage::Internal);
 
-        if is_internal {
-            if ctx.options().wrap_static_fns {
-                result.items_to_serialize.push(item.id());
-            } else {
-                // We can't do anything with Internal functions if we are not wrapping them so just
-                // avoid generating anything for them.
-                return None;
-            }
+        if is_internal && !ctx.options().wrap_static_fns {
+            // We can't do anything with Internal functions if we are not wrapping them so just
+            // avoid generating anything for them.
+            return None;
         }
 
         // Pure virtual methods have no actual symbol, so we can't generate
@@ -4139,6 +4135,10 @@ impl CodeGenerator for Function {
             abi => abi,
         };
 
+        if is_internal && ctx.options().wrap_static_fns {
+            result.items_to_serialize.push(item.id());
+        }
+
         // Handle overloaded functions by giving each overload its own unique
         // suffix.
         let times_seen = result.overload_number(&canonical_name);
diff --git a/bindgen/codegen/serialize.rs b/bindgen/codegen/serialize.rs
index e521c703..13106484 100644
--- a/bindgen/codegen/serialize.rs
+++ b/bindgen/codegen/serialize.rs
@@ -311,6 +311,14 @@ impl<'a> CSerialize<'a> for Type {
                     CompKind::Union => write!(writer, "union {}", name)?,
                 };
             }
+            TypeKind::Enum(_enum_ty) => {
+                if self.is_const() {
+                    write!(writer, "const ")?;
+                }
+
+                let name = item.canonical_name(ctx);
+                write!(writer, "enum {}", name)?;
+            }
             ty => {
                 return Err(CodegenError::Serialize {
                     msg: format!("Cannot serialize type kind {:?}", ty),
diff --git a/book/src/tutorial-1.md b/book/src/tutorial-1.md
index cac4f255..182a777f 100644
--- a/book/src/tutorial-1.md
+++ b/book/src/tutorial-1.md
@@ -13,3 +13,9 @@ you're unsure.
 [build-dependencies]
 bindgen = "0.53.1"
 ```
+
+> ⚠️ **Warning**
+>
+> `bindgen` needs to be added to the `[build-dependencies]` section, not the normal
+> `[dependencies]` section. If you add it as a regular dependency, you will get
+> errors like the following: `` error[E0463]: can't find crate for `bindgen` ``
diff --git a/book/src/using-bitfields.md b/book/src/using-bitfields.md
index 41e07ad7..0ee9f032 100644
--- a/book/src/using-bitfields.md
+++ b/book/src/using-bitfields.md
@@ -5,7 +5,7 @@
 As Rust does not support bitfields, Bindgen generates a struct for each with the following characteristics
 * Immutable getter functions for each bitfield named ```<bitfield>```
 * Setter functions for each contiguous block of bitfields named ```set_<bitfield>```
-* Far each contiguous block of bitfields, Bindgen emits an opaque physical field that contains one or more logical bitfields
+* For each contiguous block of bitfields, Bindgen emits an opaque physical field that contains one or more logical bitfields
 * A static constructor  ```new_bitfield_{1, 2, ...}``` with a parameter for each bitfield contained within the opaque physical field.
 
 ## Bitfield examples


Test patch:
diff --git a/bindgen-tests/tests/expectations/tests/generated/wrap_static_fns.c b/bindgen-tests/tests/expectations/tests/generated/wrap_static_fns.c
index 22b2f67f..f5f33f84 100644
--- a/bindgen-tests/tests/expectations/tests/generated/wrap_static_fns.c
+++ b/bindgen-tests/tests/expectations/tests/generated/wrap_static_fns.c
@@ -12,3 +12,5 @@ int takes_alias__extern(func f) asm("takes_alias__extern");
 int takes_alias__extern(func f) { return takes_alias(f); }
 int takes_qualified__extern(const int *const *arg) asm("takes_qualified__extern");
 int takes_qualified__extern(const int *const *arg) { return takes_qualified(arg); }
+enum foo takes_enum__extern(const enum foo f) asm("takes_enum__extern");
+enum foo takes_enum__extern(const enum foo f) { return takes_enum(f); }
diff --git a/bindgen-tests/tests/expectations/tests/wrap-static-fns.rs b/bindgen-tests/tests/expectations/tests/wrap-static-fns.rs
index 54ed9fd4..fbfdcb13 100644
--- a/bindgen-tests/tests/expectations/tests/wrap-static-fns.rs
+++ b/bindgen-tests/tests/expectations/tests/wrap-static-fns.rs
@@ -50,3 +50,9 @@ extern "C" {
         arg: *const *const ::std::os::raw::c_int,
     ) -> ::std::os::raw::c_int;
 }
+pub const foo_BAR: foo = 0;
+pub type foo = ::std::os::raw::c_uint;
+extern "C" {
+    #[link_name = "\u{1}takes_enum__extern"]
+    pub fn takes_enum(f: foo) -> foo;
+}
diff --git a/bindgen-tests/tests/headers/wrap-static-fns.h b/bindgen-tests/tests/headers/wrap-static-fns.h
index 8b90c7bc..faa7b7f1 100644
--- a/bindgen-tests/tests/headers/wrap-static-fns.h
+++ b/bindgen-tests/tests/headers/wrap-static-fns.h
@@ -3,6 +3,7 @@
 static inline int foo() {
     return 11;
 }
+static int bar();
 static int bar() {
     return 1;
 }
@@ -31,3 +32,11 @@ static inline int takes_alias(func f) {
 static inline int takes_qualified(const int *const *arg) {
     return **arg;
 }
+
+enum foo {
+    BAR = 0x0,
+};
+
+static inline enum foo takes_enum(const enum foo f) {
+    return f;
+}