Skip to content

Commit 27f26cf

Browse files
committed
Remove libc_const_extern_fn
This originally existed for MSRV support, then became a necessary hack to support the old ctest's inability to parse `const fn`. The new ctest doesn't have this limitation, so remove the config. Additionally, switch from the `*` kleene to `?` since that is possible now. (backport <rust-lang#4712>) (cherry picked from commit 4894081)
1 parent 4cc380f commit 27f26cf

File tree

2 files changed

+38
-111
lines changed

2 files changed

+38
-111
lines changed

build.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ const ALLOWED_CFGS: &[&str] = &[
1717
"gnu_file_offset_bits64",
1818
// Corresponds to `_TIME_BITS=64` in glibc
1919
"gnu_time_bits64",
20-
// FIXME(ctest): this config shouldn't be needed but ctest can't parse `const extern fn`
21-
"libc_const_extern_fn",
2220
"libc_deny_warnings",
2321
"libc_thread_local",
2422
"libc_ctest",
@@ -153,9 +151,6 @@ fn main() {
153151
set_cfg("libc_thread_local");
154152
}
155153

156-
// Set unconditionally when ctest is not being invoked.
157-
set_cfg("libc_const_extern_fn");
158-
159154
// Since Rust 1.80, configuration that isn't recognized by default needs to be provided to
160155
// avoid warnings.
161156
if rustc_minor_ver >= 80 {

src/macros.rs

Lines changed: 38 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -297,114 +297,46 @@ macro_rules! c_enum {
297297
(@ty) => { $crate::prelude::CEnumRepr };
298298
}
299299

300-
// This is a pretty horrible hack to allow us to conditionally mark some functions as 'const',
301-
// without requiring users of this macro to care "libc_const_extern_fn".
302-
//
303-
// When 'libc_const_extern_fn' is enabled, we emit the captured 'const' keyword in the expanded
304-
// function.
305-
//
306-
// When 'libc_const_extern_fn' is disabled, we always emit a plain 'pub unsafe extern fn'.
307-
// Note that the expression matched by the macro is exactly the same - this allows
308-
// users of this macro to work whether or not 'libc_const_extern_fn' is enabled
309-
//
310-
// Unfortunately, we need to duplicate most of this macro between the 'cfg_if' blocks.
311-
// This is because 'const unsafe extern fn' won't even parse on older compilers,
312-
// so we need to avoid emitting it at all of 'libc_const_extern_fn'.
313-
//
314-
// Specifically, moving the 'cfg_if' into the macro body will *not* work. Doing so would cause the
315-
// '#[cfg(libc_const_extern_fn)]' to be emitted into user code. The 'cfg' gate will not stop Rust
316-
// from trying to parse the 'pub const unsafe extern fn', so users would get a compiler error even
317-
// when the 'libc_const_extern_fn' feature is disabled.
318-
319-
// FIXME(ctest): ctest can't handle `const extern` functions, we should be able to remove this
320-
// cfg completely.
321-
// FIXME(ctest): ctest can't handle `$(,)?` so we use `$(,)*` which isn't quite correct.
322-
cfg_if! {
323-
if #[cfg(libc_const_extern_fn)] {
324-
/// Define an `unsafe` function that is const as long as `libc_const_extern_fn` is enabled.
325-
macro_rules! f {
326-
($(
327-
$(#[$attr:meta])*
328-
pub $({$constness:ident})* fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
329-
$body:block
330-
)*) => ($(
331-
#[inline]
332-
$(#[$attr])*
333-
pub $($constness)* unsafe extern "C" fn $i($($arg: $argty),*) -> $ret
334-
$body
335-
)*)
336-
}
337-
338-
/// Define a safe function that is const as long as `libc_const_extern_fn` is enabled.
339-
macro_rules! safe_f {
340-
($(
341-
$(#[$attr:meta])*
342-
pub $({$constness:ident})* fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
343-
$body:block
344-
)*) => ($(
345-
#[inline]
346-
$(#[$attr])*
347-
pub $($constness)* extern "C" fn $i($($arg: $argty),*) -> $ret
348-
$body
349-
)*)
350-
}
351-
352-
/// A nonpublic function that is const as long as `libc_const_extern_fn` is enabled.
353-
macro_rules! const_fn {
354-
($(
355-
$(#[$attr:meta])*
356-
$({$constness:ident})* fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
357-
$body:block
358-
)*) => ($(
359-
#[inline]
360-
$(#[$attr])*
361-
$($constness)* fn $i($($arg: $argty),*) -> $ret
362-
$body
363-
)*)
364-
}
365-
} else {
366-
/// Define an `unsafe` function that is const as long as `libc_const_extern_fn` is enabled.
367-
macro_rules! f {
368-
($(
369-
$(#[$attr:meta])*
370-
pub $({$constness:ident})* fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
371-
$body:block
372-
)*) => ($(
373-
#[inline]
374-
$(#[$attr])*
375-
pub unsafe extern "C" fn $i($($arg: $argty),*) -> $ret
376-
$body
377-
)*)
378-
}
300+
/// Define a `unsafe` function.
301+
macro_rules! f {
302+
($(
303+
$(#[$attr:meta])*
304+
pub $({$constness:ident})? fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
305+
$body:block
306+
)*) => ($(
307+
#[inline]
308+
$(#[$attr])*
309+
pub $($constness)? unsafe extern "C" fn $i($($arg: $argty),*) -> $ret
310+
$body
311+
)*)
312+
}
379313

380-
/// Define a safe function that is const as long as `libc_const_extern_fn` is enabled.
381-
macro_rules! safe_f {
382-
($(
383-
$(#[$attr:meta])*
384-
pub $({$constness:ident})* fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
385-
$body:block
386-
)*) => ($(
387-
#[inline]
388-
$(#[$attr])*
389-
pub extern "C" fn $i($($arg: $argty),*) -> $ret
390-
$body
391-
)*)
392-
}
314+
/// Define a safe function.
315+
macro_rules! safe_f {
316+
($(
317+
$(#[$attr:meta])*
318+
pub $({$constness:ident})? fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
319+
$body:block
320+
)*) => ($(
321+
#[inline]
322+
$(#[$attr])*
323+
pub $($constness)? extern "C" fn $i($($arg: $argty),*) -> $ret
324+
$body
325+
)*)
326+
}
393327

394-
/// A nonpublic function that is const as long as `libc_const_extern_fn` is enabled.
395-
macro_rules! const_fn {
396-
($(
397-
$(#[$attr:meta])*
398-
$({$constness:ident})* fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
399-
$body:block
400-
)*) => ($(
401-
#[inline]
402-
$(#[$attr])*
403-
fn $i($($arg: $argty),*) -> $ret
404-
$body
405-
)*)
406-
}
407-
}
328+
/// Define a nonpublic function.
329+
macro_rules! const_fn {
330+
($(
331+
$(#[$attr:meta])*
332+
$({$constness:ident})? fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
333+
$body:block
334+
)*) => ($(
335+
#[inline]
336+
$(#[$attr])*
337+
$($constness)? fn $i($($arg: $argty),*) -> $ret
338+
$body
339+
)*)
408340
}
409341

410342
macro_rules! __item {

0 commit comments

Comments
 (0)