Skip to content

Commit ff801a6

Browse files
committed
fix: Ensure that const extern fn is always enabled
In [1] this conditional was dropped in favor of a Cargo feature, which was turned on by default in [2]. However, this did not help the case where `--no-default-features` is passed. Unfortunately we still can't drop this config entirely since `ctest` cannot parse the syntax, so change back to useing a `cfg` to control constness rather than a Cargo feature. Additionally, remove a portion of the macro's comment that is no longer relevant. Fixes: rust-lang#4149 [1]: rust-lang#4105 [2]: rust-lang#4134 (backport <rust-lang#4151>) (cherry picked from commit e18ee8c)
1 parent 5fd2c84 commit ff801a6

File tree

3 files changed

+24
-28
lines changed

3 files changed

+24
-28
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ cargo-args = ["-Zbuild-std=core"]
137137
rustc-std-workspace-core = { version = "1.0.0", optional = true }
138138

139139
[features]
140-
default = ["const-extern-fn", "std"]
140+
default = ["std"]
141141
std = []
142142
rustc-dep-of-std = ['align', 'rustc-std-workspace-core']
143143
extra_traits = []

build.rs

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const ALLOWED_CFGS: &'static [&'static str] = &[
1313
"freebsd13",
1414
"freebsd14",
1515
"freebsd15",
16+
// FIXME(ctest): this config shouldn't be needed but ctest can't parse `const extern fn`
17+
"libc_const_extern_fn",
1618
"libc_deny_warnings",
1719
"libc_thread_local",
1820
"libc_ctest",
@@ -80,6 +82,9 @@ fn main() {
8082
set_cfg("libc_thread_local");
8183
}
8284

85+
// Set unconditionally when ctest is not being invoked.
86+
set_cfg("libc_const_extern_fn");
87+
8388
// check-cfg is a nightly cargo/rustc feature to warn when unknown cfgs are used across the
8489
// codebase. libc can configure it if the appropriate environment variable is passed. Since
8590
// rust-lang/rust enforces it, this is useful when using a custom libc fork there.

src/macros.rs

+18-27
Original file line numberDiff line numberDiff line change
@@ -167,40 +167,31 @@ macro_rules! e {
167167
)*);
168168
}
169169

170-
// This is a pretty horrible hack to allow us to conditionally mark
171-
// some functions as 'const', without requiring users of this macro
172-
// to care about the "const-extern-fn" feature.
170+
// This is a pretty horrible hack to allow us to conditionally mark some functions as 'const',
171+
// without requiring users of this macro to care "libc_const_extern_fn".
173172
//
174-
// When 'const-extern-fn' is enabled, we emit the captured 'const' keyword
175-
// in the expanded function.
173+
// When 'libc_const_extern_fn' is enabled, we emit the captured 'const' keyword in the expanded
174+
// function.
176175
//
177-
// When 'const-extern-fn' is disabled, we always emit a plain 'pub unsafe extern fn'.
176+
// When 'libc_const_extern_fn' is disabled, we always emit a plain 'pub unsafe extern fn'.
178177
// Note that the expression matched by the macro is exactly the same - this allows
179-
// users of this macro to work whether or not 'const-extern-fn' is enabled
178+
// users of this macro to work whether or not 'libc_const_extern_fn' is enabled
180179
//
181180
// Unfortunately, we need to duplicate most of this macro between the 'cfg_if' blocks.
182181
// This is because 'const unsafe extern fn' won't even parse on older compilers,
183-
// so we need to avoid emitting it at all of 'const-extern-fn'.
182+
// so we need to avoid emitting it at all of 'libc_const_extern_fn'.
184183
//
185-
// Specifically, moving the 'cfg_if' into the macro body will *not* work.
186-
// Doing so would cause the '#[cfg(feature = "const-extern-fn")]' to be emitted
187-
// into user code. The 'cfg' gate will not stop Rust from trying to parse the
188-
// 'pub const unsafe extern fn', so users would get a compiler error even when
189-
// the 'const-extern-fn' feature is disabled
190-
//
191-
// Note that users of this macro need to place 'const' in a weird position
192-
// (after the closing ')' for the arguments, but before the return type).
193-
// This was the only way I could satisfy the following two requirements:
194-
// 1. Avoid ambiguity errors from 'macro_rules!' (which happen when writing '$foo:ident fn'
195-
// 2. Allow users of this macro to mix 'pub fn foo' and 'pub const fn bar' within the same
196-
// 'f!' block
184+
// Specifically, moving the 'cfg_if' into the macro body will *not* work. Doing so would cause the
185+
// '#[cfg(libc_const_extern_fn)]' to be emitted into user code. The 'cfg' gate will not stop Rust
186+
// from trying to parse the 'pub const unsafe extern fn', so users would get a compiler error even
187+
// when the 'libc_const_extern_fn' feature is disabled.
197188

198189
// FIXME(ctest): ctest can't handle `const extern` functions, we should be able to remove this
199190
// cfg completely.
200191
// FIXME(ctest): ctest can't handle `$(,)?` so we use `$(,)*` which isn't quite correct.
201192
cfg_if! {
202-
if #[cfg(feature = "const-extern-fn")] {
203-
/// Define an `unsafe` function that is const as long as `const-extern-fn` is enabled.
193+
if #[cfg(libc_const_extern_fn)] {
194+
/// Define an `unsafe` function that is const as long as `libc_const_extern_fn` is enabled.
204195
macro_rules! f {
205196
($(
206197
$(#[$attr:meta])*
@@ -214,7 +205,7 @@ cfg_if! {
214205
)*)
215206
}
216207

217-
/// Define a safe function that is const as long as `const-extern-fn` is enabled.
208+
/// Define a safe function that is const as long as `libc_const_extern_fn` is enabled.
218209
macro_rules! safe_f {
219210
($(
220211
$(#[$attr:meta])*
@@ -228,7 +219,7 @@ cfg_if! {
228219
)*)
229220
}
230221

231-
/// A nonpublic function that is const as long as `const-extern-fn` is enabled.
222+
/// A nonpublic function that is const as long as `libc_const_extern_fn` is enabled.
232223
macro_rules! const_fn {
233224
($(
234225
$(#[$attr:meta])*
@@ -242,7 +233,7 @@ cfg_if! {
242233
)*)
243234
}
244235
} else {
245-
/// Define an `unsafe` function that is const as long as `const-extern-fn` is enabled.
236+
/// Define an `unsafe` function that is const as long as `libc_const_extern_fn` is enabled.
246237
macro_rules! f {
247238
($(
248239
$(#[$attr:meta])*
@@ -256,7 +247,7 @@ cfg_if! {
256247
)*)
257248
}
258249

259-
/// Define a safe function that is const as long as `const-extern-fn` is enabled.
250+
/// Define a safe function that is const as long as `libc_const_extern_fn` is enabled.
260251
macro_rules! safe_f {
261252
($(
262253
$(#[$attr:meta])*
@@ -270,7 +261,7 @@ cfg_if! {
270261
)*)
271262
}
272263

273-
/// A nonpublic function that is const as long as `const-extern-fn` is enabled.
264+
/// A nonpublic function that is const as long as `libc_const_extern_fn` is enabled.
274265
macro_rules! const_fn {
275266
($(
276267
$(#[$attr:meta])*

0 commit comments

Comments
 (0)