From 126609e460757f83da55def7ffb111cfb415f6df Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 11 Nov 2024 10:28:14 -0800 Subject: [PATCH] Make unsafe attrs conditional on compiler version --- impl/build.rs | 3 +++ impl/src/declaration.rs | 55 ++++++++++++++++++++++++----------------- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/impl/build.rs b/impl/build.rs index beeea10..e6f6d2a 100644 --- a/impl/build.rs +++ b/impl/build.rs @@ -12,10 +12,13 @@ fn main() { if rustc >= 80 { println!("cargo:rustc-check-cfg=cfg(exhaustive)"); + println!("cargo:rustc-check-cfg=cfg(no_unsafe_attributes)"); 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#unsafe-attributes + println!("cargo:rustc-cfg=no_unsafe_attributes"); // 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 e9117c4..1c12c3b 100644 --- a/impl/src/declaration.rs +++ b/impl/src/declaration.rs @@ -119,6 +119,17 @@ pub fn expand(input: TokenStream) -> TokenStream { Some(Token![unsafe](call_site)) }; + let (unsafe_attr, link_section_attr) = if cfg!(no_unsafe_attributes) { + // #[cfg_attr(all(), link_section = ...)] + ( + Ident::new("cfg_attr", call_site), + quote!(all(), link_section), + ) + } else { + // #[unsafe(link_section = ...)] + (Ident::new("unsafe", call_site), quote!(link_section)) + }; + quote! { #(#attrs)* #vis static #ident: #linkme_path::DistributedSlice<#ty> = { @@ -162,19 +173,19 @@ pub fn expand(input: TokenStream) -> TokenStream { } #[cfg(any(target_os = "uefi", target_os = "windows"))] - #[unsafe(link_section = #windows_section_start)] + #[#unsafe_attr(#link_section_attr = #windows_section_start)] static LINKME_START: [<#ty as #linkme_path::__private::Slice>::Element; 0] = []; #[cfg(any(target_os = "uefi", target_os = "windows"))] - #[unsafe(link_section = #windows_section_stop)] + #[#unsafe_attr(#link_section_attr = #windows_section_stop)] static LINKME_STOP: [<#ty as #linkme_path::__private::Slice>::Element; 0] = []; #[cfg(any(target_os = "uefi", target_os = "windows"))] - #[unsafe(link_section = #windows_dupcheck_start)] + #[#unsafe_attr(#link_section_attr = #windows_dupcheck_start)] static DUPCHECK_START: () = (); #[cfg(any(target_os = "uefi", target_os = "windows"))] - #[unsafe(link_section = #windows_dupcheck_stop)] + #[#unsafe_attr(#link_section_attr = #windows_dupcheck_stop)] static DUPCHECK_STOP: () = (); #used @@ -188,17 +199,17 @@ pub fn expand(input: TokenStream) -> TokenStream { target_os = "openbsd", target_os = "psp", ))] - #[cfg_attr(any(target_os = "none", target_os = "linux", target_os = "android", target_os = "fuchsia", target_os = "psp"), unsafe(link_section = #linux_section))] - #[cfg_attr(target_os = "illumos", unsafe(link_section = #illumos_section))] - #[cfg_attr(any(target_os = "freebsd", target_os = "openbsd"), unsafe(link_section = #bsd_section))] + #[cfg_attr(any(target_os = "none", target_os = "linux", target_os = "android", target_os = "fuchsia", target_os = "psp"), #unsafe_attr(#link_section_attr = #linux_section))] + #[cfg_attr(target_os = "illumos", #unsafe_attr(#link_section_attr = #illumos_section))] + #[cfg_attr(any(target_os = "freebsd", target_os = "openbsd"), #unsafe_attr(#link_section_attr = #bsd_section))] static mut LINKME_PLEASE: [<#ty as #linkme_path::__private::Slice>::Element; 0] = []; #used - #[cfg_attr(any(target_os = "none", target_os = "linux", target_os = "android", target_os = "fuchsia", target_os = "psp"), unsafe(link_section = #linux_dupcheck))] - #[cfg_attr(any(target_os = "macos", target_os = "ios", target_os = "tvos"), unsafe(link_section = #macho_dupcheck))] - #[cfg_attr(any(target_os = "uefi", target_os = "windows"), unsafe(link_section = #windows_dupcheck))] - #[cfg_attr(target_os = "illumos", unsafe(link_section = #illumos_dupcheck))] - #[cfg_attr(any(target_os = "freebsd", target_os = "openbsd"), unsafe(link_section = #bsd_dupcheck))] + #[cfg_attr(any(target_os = "none", target_os = "linux", target_os = "android", target_os = "fuchsia", target_os = "psp"), #unsafe_attr(#link_section_attr = #linux_dupcheck))] + #[cfg_attr(any(target_os = "macos", target_os = "ios", target_os = "tvos"), #unsafe_attr(#link_section_attr = #macho_dupcheck))] + #[cfg_attr(any(target_os = "uefi", target_os = "windows"), #unsafe_attr(#link_section_attr = #windows_dupcheck))] + #[cfg_attr(target_os = "illumos", #unsafe_attr(#link_section_attr = #illumos_dupcheck))] + #[cfg_attr(any(target_os = "freebsd", target_os = "openbsd"), #unsafe_attr(#link_section_attr = #bsd_dupcheck))] static DUPCHECK: #linkme_path::__private::usize = 1; #[cfg(not(any( @@ -259,20 +270,20 @@ pub fn expand(input: TokenStream) -> TokenStream { $item:item ) => { #used - #[cfg_attr(any(target_os = "none", target_os = "linux", target_os = "android", target_os = "fuchsia", target_os = "psp"), unsafe(link_section = $linux_section))] - #[cfg_attr(any(target_os = "macos", target_os = "ios", target_os = "tvos"), unsafe(link_section = $macho_section))] - #[cfg_attr(any(target_os = "uefi", target_os = "windows"), unsafe(link_section = $windows_section))] - #[cfg_attr(target_os = "illumos", unsafe(link_section = $illumos_section))] - #[cfg_attr(any(target_os = "freebsd", target_os = "openbsd"), unsafe(link_section = $bsd_section))] + #[cfg_attr(any(target_os = "none", target_os = "linux", target_os = "android", target_os = "fuchsia", target_os = "psp"), #unsafe_attr(#link_section_attr = $linux_section))] + #[cfg_attr(any(target_os = "macos", target_os = "ios", target_os = "tvos"), #unsafe_attr(#link_section_attr = $macho_section))] + #[cfg_attr(any(target_os = "uefi", target_os = "windows"), #unsafe_attr(#link_section_attr = $windows_section))] + #[cfg_attr(target_os = "illumos", #unsafe_attr(#link_section_attr = $illumos_section))] + #[cfg_attr(any(target_os = "freebsd", target_os = "openbsd"), #unsafe_attr(#link_section_attr = $bsd_section))] $item }; ($item:item) => { #used - #[cfg_attr(any(target_os = "none", target_os = "linux", target_os = "android", target_os = "fuchsia", target_os = "psp"), unsafe(link_section = #linux_section))] - #[cfg_attr(any(target_os = "macos", target_os = "ios", target_os = "tvos"), unsafe(link_section = #macho_section))] - #[cfg_attr(any(target_os = "uefi", target_os = "windows"), unsafe(link_section = #windows_section))] - #[cfg_attr(target_os = "illumos", unsafe(link_section = #illumos_section))] - #[cfg_attr(any(target_os = "freebsd", target_os = "openbsd"), unsafe(link_section = #bsd_section))] + #[cfg_attr(any(target_os = "none", target_os = "linux", target_os = "android", target_os = "fuchsia", target_os = "psp"), #unsafe_attr(#link_section_attr = #linux_section))] + #[cfg_attr(any(target_os = "macos", target_os = "ios", target_os = "tvos"), #unsafe_attr(#link_section_attr = #macho_section))] + #[cfg_attr(any(target_os = "uefi", target_os = "windows"), #unsafe_attr(#link_section_attr = #windows_section))] + #[cfg_attr(target_os = "illumos", #unsafe_attr(#link_section_attr = #illumos_section))] + #[cfg_attr(any(target_os = "freebsd", target_os = "openbsd"), #unsafe_attr(#link_section_attr = #bsd_section))] $item }; }