Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
WardBrian opened this issue Oct 18, 2024 · 0 comments · Fixed by #2961

Comments

@WardBrian
Copy link
Contributor

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`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant