rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

Combining dynamic_library with wrap_unsafe_ops still generates un-wrapped access to non-functions

Open WardBrian opened this issue 1 year ago • 0 comments

This means unsafe_op_in_unsafe_fn in edition 2024 complains about the from_library function.

Example:

// file src/foo.h
extern int SOME_CONSTANT;
// file src/main.rs
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#![allow(dead_code)]
#![allow(clippy::all)]
#![allow(rustdoc::broken_intra_doc_links)]
// Include generated bindings file
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

fn main() {
    println!("Hello, world!");
}
//file build.rs
extern crate bindgen;

use std::env;
use std::path::PathBuf;

fn main() {
    println!("cargo:rerun-if-changed=src/foo.h");

    // The bindgen::Builder is the main entry point
    // to bindgen, and lets you build up options for
    // the resulting bindings.
    let bindings = bindgen::Builder::default()
        .header("src/foo.h")
        .dynamic_library_name("foo")
        .wrap_unsafe_ops(true)
        .generate()
        .expect("Unable to generate bindings");

    // Write the bindings to the $OUT_DIR/bindings.rs file.
    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
    bindings
        .write_to_file(out_path.join("bindings.rs"))
        .expect("Couldn't write bindings!");
}

Running cargo fix --edition yields:

$ cargo fix --edition --allow-dirty

   Compiling bindgen-example v0.1.0 (/home/brian/Dev/rust/bindgen-example)
warning[E0133]: call to unsafe function `libloading::Library::new` is unsafe and requires unsafe block
  --> /home/brian/Dev/rust/bindgen-example/target/debug/build/bindgen-example-e99abd65eea52984/out/bindings.rs:12:23
   |
12 |         let library = ::libloading::Library::new(path)?;
   |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function
   |
   = note: for more information, see issue #71668 <https://github.com/rust-lang/rust/issues/71668>
   = note: consult the function's documentation for information on how to avoid undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default
  --> /home/brian/Dev/rust/bindgen-example/target/debug/build/bindgen-example-e99abd65eea52984/out/bindings.rs:8:5
   |
8  | /     pub unsafe fn new<P>(path: P) -> Result<Self, ::libloading::Error>
9  | |     where
10 | |         P: AsRef<::std::ffi::OsStr>,
   | |____________________________________^
   = note: `--force-warn unsafe-op-in-unsafe-fn` implied by `--force-warn rust-2024-compatibility`

warning[E0133]: call to unsafe function `libloading::Library::get` is unsafe and requires unsafe block
  --> /home/brian/Dev/rust/bindgen-example/target/debug/build/bindgen-example-e99abd65eea52984/out/bindings.rs:20:29
   |
20 |           let SOME_CONSTANT = __library
   |  _____________________________^
21 | |             .get::<*mut ::std::os::raw::c_int>(b"SOME_CONSTANT\0")
   | |__________________________________________________________________^ call to unsafe function
   |
   = note: for more information, see issue #71668 <https://github.com/rust-lang/rust/issues/71668>
   = note: consult the function's documentation for information on how to avoid undefined behavior
note: an unsafe function restricts its caller, but its body is safe by default
  --> /home/brian/Dev/rust/bindgen-example/target/debug/build/bindgen-example-e99abd65eea52984/out/bindings.rs:15:5
   |
15 | /     pub unsafe fn from_library<L>(library: L) -> Result<Self, ::libloading::Error>
16 | |     where
17 | |         L: Into<::libloading::Library>,
   | |_______________________________________^

For more information about this error, try `rustc --explain E0133`.

WardBrian avatar Oct 18 '24 14:10 WardBrian