From 8979b3d9f6906c51eeec82210aa07b755cb1c9bc Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 11 Nov 2024 10:27:44 -0800 Subject: [PATCH] Make unsafe extern conditional on compiler version --- impl/build.rs | 6 ++++++ impl/src/declaration.rs | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/impl/build.rs b/impl/build.rs index 872296c..beeea10 100644 --- a/impl/build.rs +++ b/impl/build.rs @@ -12,6 +12,12 @@ fn main() { if rustc >= 80 { println!("cargo:rustc-check-cfg=cfg(exhaustive)"); + println!("cargo:rustc-check-cfg=cfg(no_unsafe_extern_blocks)"); + } + + if rustc < 82 { + // https://blog.rust-lang.org/2024/10/17/Rust-1.82.0.html#safe-items-with-unsafe-extern + println!("cargo:rustc-cfg=no_unsafe_extern_blocks"); } } diff --git a/impl/src/declaration.rs b/impl/src/declaration.rs index 03c403a..e9117c4 100644 --- a/impl/src/declaration.rs +++ b/impl/src/declaration.rs @@ -113,6 +113,12 @@ pub fn expand(input: TokenStream) -> TokenStream { let link_section_macro_str = format!("_linkme_macro_{}", ident); let link_section_macro = Ident::new(&link_section_macro_str, call_site); + let unsafe_extern = if cfg!(no_unsafe_extern_blocks) { + None + } else { + Some(Token![unsafe](call_site)) + }; + quote! { #(#attrs)* #vis static #ident: #linkme_path::DistributedSlice<#ty> = { @@ -129,7 +135,7 @@ pub fn expand(input: TokenStream) -> TokenStream { target_os = "openbsd", target_os = "psp", ))] - unsafe extern "Rust" { + #unsafe_extern extern "Rust" { #[cfg_attr(any(target_os = "none", target_os = "linux", target_os = "android", target_os = "fuchsia", target_os = "psp"), link_name = #linux_section_start)] #[cfg_attr(any(target_os = "macos", target_os = "ios", target_os = "tvos"), link_name = #macho_section_start)] #[cfg_attr(target_os = "illumos", link_name = #illumos_section_start)]