Skip to content

Commit

Permalink
Fix missing extern "C" for unsafe functions
Browse files Browse the repository at this point in the history
`unsafe` functions were being matched in a different block that did not
include `extern $abi`. This means that some intrinsics were getting
generated with the Rust ABI rather than C.

Combine the last two blocks using an optional token matcher, which fixes
this problem and is cleaner.
  • Loading branch information
tgross35 committed Jul 18, 2024
1 parent a4805de commit e7fd5be
Showing 1 changed file with 3 additions and 30 deletions.
33 changes: 3 additions & 30 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,41 +449,14 @@ macro_rules! intrinsics {
// input we were given.
(
$(#[$($attr:tt)*])*
pub extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
$($body:tt)*
}

$($rest:tt)*
) => (
$(#[$($attr)*])*
pub extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
$($body)*
}

#[cfg(not(feature = "mangled-names"))]
mod $name {
$(#[$($attr)*])*
#[no_mangle]
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), linkage = "weak")]
extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
super::$name($($argname),*)
}
}

intrinsics!($($rest)*);
);

// Same as the above for unsafe functions.
(
$(#[$($attr:tt)*])*
pub unsafe extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
pub $(unsafe $(@ $empty:tt)?)? extern $abi:tt fn $name:ident( $($argname:ident: $ty:ty),* ) $(-> $ret:ty)? {
$($body:tt)*
}

$($rest:tt)*
) => (
$(#[$($attr)*])*
pub unsafe extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
pub $(unsafe $($empty)?)? extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
$($body)*
}

Expand All @@ -492,7 +465,7 @@ macro_rules! intrinsics {
$(#[$($attr)*])*
#[no_mangle]
#[cfg_attr(all(not(windows), not(target_vendor = "apple")), linkage = "weak")]
unsafe fn $name( $($argname: $ty),* ) $(-> $ret)? {
$(unsafe $($empty)?)? extern $abi fn $name( $($argname: $ty),* ) $(-> $ret)? {
super::$name($($argname),*)
}
}
Expand Down

0 comments on commit e7fd5be

Please sign in to comment.