Skip to content

Commit

Permalink
Make unsafe extern conditional on compiler version
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Nov 11, 2024
1 parent 2cc2232 commit 8979b3d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 6 additions & 0 deletions impl/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down
8 changes: 7 additions & 1 deletion impl/src/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> = {
Expand All @@ -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)]
Expand Down

0 comments on commit 8979b3d

Please sign in to comment.