Skip to content

Commit 51e1ef5

Browse files
authored
rust: Fix compatibility of generated code with the 2024 edition (#1119)
This commit fixes compatibility of generated code with the Rust 2024 edition coming up next month. The new edition requires that `#[export_name]` is notably an `unsafe` attribute and is wrapped in `#[unsafe(export_name = "...")]` so the generated code is updated to do that. Note that this syntax is allowed on the 2021 edition as of Rust 1.82.0 but is not required, it's only required in the 2024 edition.
1 parent 4f52883 commit 51e1ef5

File tree

5 files changed

+18
-18
lines changed

5 files changed

+18
-18
lines changed

ci/rebuild-libcabi-realloc.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
# the `cabi_realloc` symbol from the final binary. This library, however,
88
# is not stable which means that we're publishing new versions of `wit-bindgen`
99
# over its development. This means that if `wit-bindgen`-the-crate were to
10-
# export a `#[no_mangle]` symbol of `cabi_realloc` then it wouldn't work to
11-
# have two versions of `wit-bindgen` in the same project. This can arise
12-
# relatively quickly, however, so this is something we want to solve.
10+
# export a `#[unsafe(no_mangle)]` symbol of `cabi_realloc` then it wouldn't
11+
# work to have two versions of `wit-bindgen` in the same project. This can
12+
# arise relatively quickly, however, so this is something we want to solve.
1313
#
1414
# The general idea of the solution here is to ensure that the `cabi_realloc`
1515
# symbol itself is declared as a weak symbol. A weakly-defined symbol means
@@ -46,7 +46,7 @@ sym=cabi_realloc_wit_bindgen_$version
4646
cat >./crates/guest-rust/rt/src/cabi_realloc.rs <<-EOF
4747
// This file is generated by $0
4848
49-
#[no_mangle]
49+
#[unsafe(no_mangle)]
5050
pub unsafe extern "C" fn $sym(
5151
old_ptr: *mut u8,
5252
old_len: usize,

crates/guest-rust/rt/src/cabi_realloc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This file is generated by ./ci/rebuild-libcabi-realloc.sh
22

3-
#[no_mangle]
3+
#[unsafe(no_mangle)]
44
pub unsafe extern "C" fn cabi_realloc_wit_bindgen_0_36_0(
55
old_ptr: *mut u8,
66
old_len: usize,

crates/guest-rust/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@
310310
/// Additionally a macro is generated by `generate!` (macros generating macros)
311311
/// called `export!`. The `export!` macro is given a component that implements
312312
/// the export `trait`s and then it will itself generate all necessary
313-
/// `#[no_mangle]` functions to implement the ABI required.
313+
/// `#[unsafe(no_mangle)]` functions to implement the ABI required.
314314
///
315315
/// ## Exports: Multiple Interfaces
316316
///
@@ -502,9 +502,9 @@
502502
/// Components are created by having exported WebAssembly functions with
503503
/// specific names, and these functions are not created when `generate!` is
504504
/// invoked. Instead these functions are created afterwards once you've defined
505-
/// your own type an implemented the various `trait`s for it. The `#[no_mangle]`
506-
/// functions that will become the component are created with the generated
507-
/// `export!` macro.
505+
/// your own type an implemented the various `trait`s for it. The
506+
/// `#[unsafe(no_mangle)]` functions that will become the component are created
507+
/// with the generated `export!` macro.
508508
///
509509
/// Each call to `generate!` will itself generate a macro called `export!`.
510510
/// The macro's first argument is the name of a type that implements the traits

crates/rust/src/interface.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ macro_rules! {macro_name} {{
332332
r#"
333333
const _: () = {{
334334
#[doc(hidden)]
335-
#[export_name = "{export_prefix}{module}#[dtor]{name}"]
335+
#[unsafe(export_name = "{export_prefix}{module}#[dtor]{name}")]
336336
#[allow(non_snake_case)]
337337
unsafe extern "C" fn dtor(rep: *mut u8) {{
338338
$($path_to_types)*::{camel}::dtor::<
@@ -575,7 +575,7 @@ pub mod vtable{ordinal} {{
575575
struct Buffer([::core::mem::MaybeUninit::<u8>; {size}]);
576576
let mut buffer = Buffer([::core::mem::MaybeUninit::uninit(); {size}]);
577577
let address = buffer.0.as_mut_ptr() as *mut u8;
578-
578+
579579
#[link(wasm_import_module = "{module}")]
580580
extern "C" {{
581581
#[link_name = "[async][future-read-{index}]{func_name}"]
@@ -1145,7 +1145,7 @@ pub mod vtable{ordinal} {{
11451145
uwrite!(
11461146
self.src,
11471147
"\
1148-
#[export_name = \"{export_prefix}{export_name}\"]
1148+
#[unsafe(export_name = \"{export_prefix}{export_name}\")]
11491149
unsafe extern \"C\" fn export_{name_snake}\
11501150
",
11511151
);
@@ -1164,7 +1164,7 @@ pub mod vtable{ordinal} {{
11641164
uwrite!(
11651165
self.src,
11661166
"\
1167-
#[export_name = \"{export_prefix}[callback]{export_name}\"]
1167+
#[unsafe(export_name = \"{export_prefix}[callback]{export_name}\")]
11681168
unsafe extern \"C\" fn _callback_{name_snake}(ctx: *mut u8, event0: i32, event1: i32, event2: i32) -> i32 {{
11691169
{path_to_self}::__callback_{name_snake}(ctx, event0, event1, event2)
11701170
}}
@@ -1174,7 +1174,7 @@ pub mod vtable{ordinal} {{
11741174
uwrite!(
11751175
self.src,
11761176
"\
1177-
#[export_name = \"{export_prefix}cabi_post_{export_name}\"]
1177+
#[unsafe(export_name = \"{export_prefix}cabi_post_{export_name}\")]
11781178
unsafe extern \"C\" fn _post_return_{name_snake}\
11791179
"
11801180
);

crates/rust/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -826,8 +826,8 @@ impl As{upcase} for {to_convert} {{
826826
uwriteln!(
827827
self.src,
828828
r#"
829-
/// Generates `#[no_mangle]` functions to export the specified type as the
830-
/// root implementation of all generated traits.
829+
/// Generates `#[unsafe(no_mangle)]` functions to export the specified type as
830+
/// the root implementation of all generated traits.
831831
///
832832
/// For more information see the documentation of `wit_bindgen::generate!`.
833833
///
@@ -907,8 +907,8 @@ macro_rules! __export_{world_name}_impl {{
907907
let pkg = &resolve.packages[world.package.unwrap()].name;
908908
let version = env!("CARGO_PKG_VERSION");
909909
self.src.push_str(&format!(
910-
"#[link_section = \"component-type:wit-bindgen:{version}:\
911-
{pkg}:{world_name}:{section_suffix}{opts_suffix}\"]\n"
910+
"#[unsafe(link_section = \"component-type:wit-bindgen:{version}:\
911+
{pkg}:{world_name}:{section_suffix}{opts_suffix}\")]\n"
912912
));
913913

914914
let mut producers = wasm_metadata::Producers::empty();

0 commit comments

Comments
 (0)