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

Generate unsafe extern blocks (Edition 2024) #2901

Open
marmitar opened this issue Aug 18, 2024 · 2 comments
Open

Generate unsafe extern blocks (Edition 2024) #2901

marmitar opened this issue Aug 18, 2024 · 2 comments

Comments

@marmitar
Copy link

marmitar commented Aug 18, 2024

I know Edition 2024 is very experimental at the moment, so this is more of a heads-up than an actual issue.

Description

Rust has just implemented RFC 3484, requiring all extern blocks to be marked unsafe extern in Edition 2024. Older editions don't yet compile this unsafe extern construct (as of the latest stable, rustc-1.80.0), but they will eventually accept the unsafe keyword for compatibility reasons. This crate needs some way of generating bindings with this future requirements, either via Builder configuration, or by detecting the current edition.

I've implemented a minimal (but complete) example that triggers the error here: bindgen-unsafe-extern.

Input C/C++ Header

void cool_function(int i, char c);

Bindgen Invocation

bindgen::Builder::default()
    .header("src/cool.h")
    .generate()
    .unwrap()
    .write_to_file("src/cool.rs")
    .unwrap()

Actual Results

Generated file:

extern "C" {
    pub fn cool_function(i: ::std::os::raw::c_int, c: ::std::os::raw::c_char);
}

Compiler error:

error: extern blocks must be unsafe
 --> src/cool.rs:3:1
  |
3 | / extern "C" {
4 | |     pub fn cool_function(i: ::std::os::raw::c_int, c: ::std::os::raw::c_char);
5 | | }
  | |_^

Expected Results

For Edition 2024, the generated file should be:

unsafe extern "C" {
    pub fn cool_function(i: ::std::os::raw::c_int, c: ::std::os::raw::c_char);
}
@GKFX
Copy link
Contributor

GKFX commented Aug 18, 2024

It's worth noting that Edition 2024 safe extern functions, plus either the proposed [[safe]] attribute for C (N2659) or an equivalent, would solve #2774.

@ojeda
Copy link
Contributor

ojeda commented Aug 18, 2024

The proposed [[safe]] attribute did not get enough support from the committee back then, but lately memory safety is talked about more in WG14 and is in the latest charter, so perhaps things change. Rust safe items within unsafe extern blocks could help there.

Or perhaps a C implementation could add something like that to improve interop with Rust for their users... :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants