Skip to content

Commit

Permalink
Make unsafe attrs 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 8979b3d commit 126609e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
3 changes: 3 additions & 0 deletions impl/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
55 changes: 33 additions & 22 deletions impl/src/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> = {
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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
};
}
Expand Down

0 comments on commit 126609e

Please sign in to comment.