Generate unsafe extern blocks (Edition 2024) · Issue #2901 · rust-lang/rust-bindgen (original) (raw)

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); }