Skip to content

Commit b398805

Browse files
committed
cleanup: Simplify the syntax of f! and similar macros
We no longer need to support gating `const` behind `libc_const_extern_fn`, so remove the awkward `{const}` syntax. (backport <#4714>) (cherry picked from commit e860257)
1 parent b0013c5 commit b398805

File tree

30 files changed

+264
-265
lines changed

30 files changed

+264
-265
lines changed

ci/style.sh

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ while IFS= read -r file; do
3131
# wouldn't be correct for something like `all(any(...), ...)`).
3232
perl -pi -0777 -e 's/if #\[cfg\((.*?)\)\]/if cfg_tmp!([$1])/gms' "$file"
3333

34-
# We have some instances of `{const}` that make macros happy but aren't
35-
# valid syntax. Replace this with just the keyword, plus an indicator
36-
# comment on the preceding line (which is where rustfmt puts it. Also
37-
# rust-lang/rustfmt#5464).
38-
perl -pi -e 's/^(\s*)(.*)\{const\}/$1\/\* FMT-CONST \*\/\n$1$2const/g' "$file"
39-
4034
# Format the file. We need to invoke `rustfmt` directly since `cargo fmt`
4135
# can't figure out the module tree with the hacks in place.
4236
failed=false
@@ -45,7 +39,6 @@ while IFS= read -r file; do
4539
# Restore all changes to the files.
4640
perl -pi -e 's/fn (\w+)_fmt_tmp\(\)/$1!/g' "$file"
4741
perl -pi -0777 -e 's/cfg_tmp!\(\[(.*?)\]\)/#[cfg($1)]/gms' "$file"
48-
perl -pi -0777 -e 's/\/\* FMT-CONST \*\/(?:\n\s*)?(.*?)const/$1\{const\}/gms' "$file"
4942

5043
# Defer emitting the failure until after the files get reset
5144
if [ "$failed" != "false" ]; then

src/fuchsia/mod.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3326,57 +3326,57 @@ f! {
33263326
}
33273327
}
33283328

3329-
pub {const} fn CMSG_ALIGN(len: size_t) -> size_t {
3329+
pub const fn CMSG_ALIGN(len: size_t) -> size_t {
33303330
(len + size_of::<size_t>() - 1) & !(size_of::<size_t>() - 1)
33313331
}
33323332

3333-
pub {const} fn CMSG_SPACE(len: c_uint) -> c_uint {
3333+
pub const fn CMSG_SPACE(len: c_uint) -> c_uint {
33343334
(CMSG_ALIGN(len as size_t) + CMSG_ALIGN(size_of::<cmsghdr>())) as c_uint
33353335
}
33363336

3337-
pub {const} fn CMSG_LEN(len: c_uint) -> c_uint {
3337+
pub const fn CMSG_LEN(len: c_uint) -> c_uint {
33383338
(CMSG_ALIGN(size_of::<cmsghdr>()) + len as size_t) as c_uint
33393339
}
33403340
}
33413341

33423342
safe_f! {
3343-
pub {const} fn WIFSTOPPED(status: c_int) -> bool {
3343+
pub const fn WIFSTOPPED(status: c_int) -> bool {
33443344
(status & 0xff) == 0x7f
33453345
}
33463346

3347-
pub {const} fn WSTOPSIG(status: c_int) -> c_int {
3347+
pub const fn WSTOPSIG(status: c_int) -> c_int {
33483348
(status >> 8) & 0xff
33493349
}
33503350

3351-
pub {const} fn WIFCONTINUED(status: c_int) -> bool {
3351+
pub const fn WIFCONTINUED(status: c_int) -> bool {
33523352
status == 0xffff
33533353
}
33543354

3355-
pub {const} fn WIFSIGNALED(status: c_int) -> bool {
3355+
pub const fn WIFSIGNALED(status: c_int) -> bool {
33563356
((status & 0x7f) + 1) as i8 >= 2
33573357
}
33583358

3359-
pub {const} fn WTERMSIG(status: c_int) -> c_int {
3359+
pub const fn WTERMSIG(status: c_int) -> c_int {
33603360
status & 0x7f
33613361
}
33623362

3363-
pub {const} fn WIFEXITED(status: c_int) -> bool {
3363+
pub const fn WIFEXITED(status: c_int) -> bool {
33643364
(status & 0x7f) == 0
33653365
}
33663366

3367-
pub {const} fn WEXITSTATUS(status: c_int) -> c_int {
3367+
pub const fn WEXITSTATUS(status: c_int) -> c_int {
33683368
(status >> 8) & 0xff
33693369
}
33703370

3371-
pub {const} fn WCOREDUMP(status: c_int) -> bool {
3371+
pub const fn WCOREDUMP(status: c_int) -> bool {
33723372
(status & 0x80) != 0
33733373
}
33743374

3375-
pub {const} fn QCMD(cmd: c_int, type_: c_int) -> c_int {
3375+
pub const fn QCMD(cmd: c_int, type_: c_int) -> c_int {
33763376
(cmd << 8) | (type_ & 0x00ff)
33773377
}
33783378

3379-
pub {const} fn makedev(major: c_uint, minor: c_uint) -> crate::dev_t {
3379+
pub const fn makedev(major: c_uint, minor: c_uint) -> crate::dev_t {
33803380
let major = major as crate::dev_t;
33813381
let minor = minor as crate::dev_t;
33823382
let mut dev = 0;
@@ -3387,14 +3387,14 @@ safe_f! {
33873387
dev
33883388
}
33893389

3390-
pub {const} fn major(dev: crate::dev_t) -> c_uint {
3390+
pub const fn major(dev: crate::dev_t) -> c_uint {
33913391
let mut major = 0;
33923392
major |= (dev & 0x00000000000fff00) >> 8;
33933393
major |= (dev & 0xfffff00000000000) >> 32;
33943394
major as c_uint
33953395
}
33963396

3397-
pub {const} fn minor(dev: crate::dev_t) -> c_uint {
3397+
pub const fn minor(dev: crate::dev_t) -> c_uint {
33983398
let mut minor = 0;
33993399
minor |= (dev & 0x00000000000000ff) >> 0;
34003400
minor |= (dev & 0x00000ffffff00000) >> 12;

src/macros.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -301,40 +301,46 @@ macro_rules! c_enum {
301301
macro_rules! f {
302302
($(
303303
$(#[$attr:meta])*
304-
pub $({$constness:ident})? fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
304+
// Less than ideal hack to match either `fn` or `const fn`.
305+
pub $(fn $i:ident)? $(const fn $const_i:ident)?
306+
($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
305307
$body:block
306-
)*) => ($(
308+
)+) => {$(
307309
#[inline]
308310
$(#[$attr])*
309-
pub $($constness)? unsafe extern "C" fn $i($($arg: $argty),*) -> $ret
311+
pub $(unsafe extern "C" fn $i)? $(const unsafe extern "C" fn $const_i)?
312+
($($arg: $argty),*) -> $ret
310313
$body
311-
)*)
314+
)+};
312315
}
313316

314317
/// Define a safe function.
315318
macro_rules! safe_f {
316319
($(
317320
$(#[$attr:meta])*
318-
pub $({$constness:ident})? fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
321+
// Less than ideal hack to match either `fn` or `const fn`.
322+
pub $(fn $i:ident)? $(const fn $const_i:ident)?
323+
($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
319324
$body:block
320-
)*) => ($(
325+
)+) => {$(
321326
#[inline]
322327
$(#[$attr])*
323-
pub $($constness)? extern "C" fn $i($($arg: $argty),*) -> $ret
328+
pub $(extern "C" fn $i)? $(const extern "C" fn $const_i)?
329+
($($arg: $argty),*) -> $ret
324330
$body
325-
)*)
331+
)+};
326332
}
327333

328334
/// Define a nonpublic function.
329335
macro_rules! const_fn {
330336
($(
331337
$(#[$attr:meta])*
332-
$({$constness:ident})? fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
338+
const fn $i:ident($($arg:ident: $argty:ty),* $(,)*) -> $ret:ty
333339
$body:block
334340
)*) => ($(
335341
#[inline]
336342
$(#[$attr])*
337-
$($constness)? fn $i($($arg: $argty),*) -> $ret
343+
const fn $i($($arg: $argty),*) -> $ret
338344
$body
339345
)*)
340346
}

src/unix/aix/mod.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2457,11 +2457,11 @@ f! {
24572457
(cmsg as *mut c_uchar).offset(size_of::<cmsghdr>() as isize)
24582458
}
24592459

2460-
pub {const} fn CMSG_LEN(length: c_uint) -> c_uint {
2460+
pub const fn CMSG_LEN(length: c_uint) -> c_uint {
24612461
size_of::<cmsghdr>() as c_uint + length
24622462
}
24632463

2464-
pub {const} fn CMSG_SPACE(length: c_uint) -> c_uint {
2464+
pub const fn CMSG_SPACE(length: c_uint) -> c_uint {
24652465
size_of::<cmsghdr>() as c_uint + length
24662466
}
24672467

@@ -2493,62 +2493,62 @@ f! {
24932493
}
24942494

24952495
safe_f! {
2496-
pub {const} fn WIFSTOPPED(status: c_int) -> bool {
2496+
pub const fn WIFSTOPPED(status: c_int) -> bool {
24972497
(status & _W_STOPPED) != 0
24982498
}
24992499

2500-
pub {const} fn WSTOPSIG(status: c_int) -> c_int {
2500+
pub const fn WSTOPSIG(status: c_int) -> c_int {
25012501
if WIFSTOPPED(status) {
25022502
(((status as c_uint) >> 8) & 0xff) as c_int
25032503
} else {
25042504
-1
25052505
}
25062506
}
25072507

2508-
pub {const} fn WIFEXITED(status: c_int) -> bool {
2508+
pub const fn WIFEXITED(status: c_int) -> bool {
25092509
(status & 0xFF) == 0
25102510
}
25112511

2512-
pub {const} fn WEXITSTATUS(status: c_int) -> c_int {
2512+
pub const fn WEXITSTATUS(status: c_int) -> c_int {
25132513
if WIFEXITED(status) {
25142514
(((status as c_uint) >> 8) & 0xff) as c_int
25152515
} else {
25162516
-1
25172517
}
25182518
}
25192519

2520-
pub {const} fn WIFSIGNALED(status: c_int) -> bool {
2520+
pub const fn WIFSIGNALED(status: c_int) -> bool {
25212521
!WIFEXITED(status) && !WIFSTOPPED(status)
25222522
}
25232523

2524-
pub {const} fn WTERMSIG(status: c_int) -> c_int {
2524+
pub const fn WTERMSIG(status: c_int) -> c_int {
25252525
if WIFSIGNALED(status) {
25262526
(((status as c_uint) >> 16) & 0xff) as c_int
25272527
} else {
25282528
-1
25292529
}
25302530
}
25312531

2532-
pub {const} fn WIFCONTINUED(status: c_int) -> bool {
2532+
pub const fn WIFCONTINUED(status: c_int) -> bool {
25332533
(status & WCONTINUED) != 0
25342534
}
25352535

25362536
// AIX doesn't have native WCOREDUMP.
2537-
pub {const} fn WCOREDUMP(_status: c_int) -> bool {
2537+
pub const fn WCOREDUMP(_status: c_int) -> bool {
25382538
false
25392539
}
25402540

2541-
pub {const} fn major(dev: crate::dev_t) -> c_uint {
2541+
pub const fn major(dev: crate::dev_t) -> c_uint {
25422542
let x = dev >> 16;
25432543
x as c_uint
25442544
}
25452545

2546-
pub {const} fn minor(dev: crate::dev_t) -> c_uint {
2546+
pub const fn minor(dev: crate::dev_t) -> c_uint {
25472547
let y = dev & 0xFFFF;
25482548
y as c_uint
25492549
}
25502550

2551-
pub {const} fn makedev(major: c_uint, minor: c_uint) -> crate::dev_t {
2551+
pub const fn makedev(major: c_uint, minor: c_uint) -> crate::dev_t {
25522552
let major = major as crate::dev_t;
25532553
let minor = minor as crate::dev_t;
25542554
let mut dev = 0;

src/unix/bsd/apple/mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5214,49 +5214,49 @@ f! {
52145214
(cmsg as *mut c_uchar).add(__DARWIN_ALIGN32(size_of::<cmsghdr>()))
52155215
}
52165216

5217-
pub {const} fn CMSG_SPACE(length: c_uint) -> c_uint {
5217+
pub const fn CMSG_SPACE(length: c_uint) -> c_uint {
52185218
(__DARWIN_ALIGN32(size_of::<cmsghdr>()) + __DARWIN_ALIGN32(length as usize)) as c_uint
52195219
}
52205220

5221-
pub {const} fn CMSG_LEN(length: c_uint) -> c_uint {
5221+
pub const fn CMSG_LEN(length: c_uint) -> c_uint {
52225222
(__DARWIN_ALIGN32(size_of::<cmsghdr>()) + length as usize) as c_uint
52235223
}
52245224

5225-
pub {const} fn VM_MAKE_TAG(id: u8) -> u32 {
5225+
pub const fn VM_MAKE_TAG(id: u8) -> u32 {
52265226
(id as u32) << 24u32
52275227
}
52285228
}
52295229

52305230
safe_f! {
5231-
pub {const} fn WSTOPSIG(status: c_int) -> c_int {
5231+
pub const fn WSTOPSIG(status: c_int) -> c_int {
52325232
status >> 8
52335233
}
52345234

5235-
pub {const} fn _WSTATUS(status: c_int) -> c_int {
5235+
pub const fn _WSTATUS(status: c_int) -> c_int {
52365236
status & 0x7f
52375237
}
52385238

5239-
pub {const} fn WIFCONTINUED(status: c_int) -> bool {
5239+
pub const fn WIFCONTINUED(status: c_int) -> bool {
52405240
_WSTATUS(status) == _WSTOPPED && WSTOPSIG(status) == 0x13
52415241
}
52425242

5243-
pub {const} fn WIFSIGNALED(status: c_int) -> bool {
5243+
pub const fn WIFSIGNALED(status: c_int) -> bool {
52445244
_WSTATUS(status) != _WSTOPPED && _WSTATUS(status) != 0
52455245
}
52465246

5247-
pub {const} fn WIFSTOPPED(status: c_int) -> bool {
5247+
pub const fn WIFSTOPPED(status: c_int) -> bool {
52485248
_WSTATUS(status) == _WSTOPPED && WSTOPSIG(status) != 0x13
52495249
}
52505250

5251-
pub {const} fn makedev(major: i32, minor: i32) -> dev_t {
5251+
pub const fn makedev(major: i32, minor: i32) -> dev_t {
52525252
(major << 24) | minor
52535253
}
52545254

5255-
pub {const} fn major(dev: dev_t) -> i32 {
5255+
pub const fn major(dev: dev_t) -> i32 {
52565256
(dev >> 24) & 0xff
52575257
}
52585258

5259-
pub {const} fn minor(dev: dev_t) -> i32 {
5259+
pub const fn minor(dev: dev_t) -> i32 {
52605260
dev & 0xffffff
52615261
}
52625262
}

src/unix/bsd/freebsdlike/dragonfly/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,7 @@ pub const RTAX_MPLS3: c_int = 10;
14201420
pub const RTAX_MAX: c_int = 11;
14211421

14221422
const_fn! {
1423-
{const} fn _CMSG_ALIGN(n: usize) -> usize {
1423+
const fn _CMSG_ALIGN(n: usize) -> usize {
14241424
(n + (size_of::<c_long>() - 1)) & !(size_of::<c_long>() - 1)
14251425
}
14261426
}
@@ -1430,7 +1430,7 @@ f! {
14301430
(cmsg as *mut c_uchar).offset(_CMSG_ALIGN(size_of::<cmsghdr>()) as isize)
14311431
}
14321432

1433-
pub {const} fn CMSG_LEN(length: c_uint) -> c_uint {
1433+
pub const fn CMSG_LEN(length: c_uint) -> c_uint {
14341434
(_CMSG_ALIGN(size_of::<cmsghdr>()) + length as usize) as c_uint
14351435
}
14361436

@@ -1446,7 +1446,7 @@ f! {
14461446
}
14471447
}
14481448

1449-
pub {const} fn CMSG_SPACE(length: c_uint) -> c_uint {
1449+
pub const fn CMSG_SPACE(length: c_uint) -> c_uint {
14501450
(_CMSG_ALIGN(size_of::<cmsghdr>()) + _CMSG_ALIGN(length as usize)) as c_uint
14511451
}
14521452

@@ -1475,11 +1475,11 @@ f! {
14751475
}
14761476

14771477
safe_f! {
1478-
pub {const} fn WIFSIGNALED(status: c_int) -> bool {
1478+
pub const fn WIFSIGNALED(status: c_int) -> bool {
14791479
(status & 0o177) != 0o177 && (status & 0o177) != 0
14801480
}
14811481

1482-
pub {const} fn makedev(major: c_uint, minor: c_uint) -> crate::dev_t {
1482+
pub const fn makedev(major: c_uint, minor: c_uint) -> crate::dev_t {
14831483
let major = major as crate::dev_t;
14841484
let minor = minor as crate::dev_t;
14851485
let mut dev = 0;
@@ -1488,11 +1488,11 @@ safe_f! {
14881488
dev
14891489
}
14901490

1491-
pub {const} fn major(dev: crate::dev_t) -> c_int {
1491+
pub const fn major(dev: crate::dev_t) -> c_int {
14921492
((dev >> 8) & 0xff) as c_int
14931493
}
14941494

1495-
pub {const} fn minor(dev: crate::dev_t) -> c_int {
1495+
pub const fn minor(dev: crate::dev_t) -> c_int {
14961496
(dev & 0xffff00ff) as c_int
14971497
}
14981498
}

src/unix/bsd/freebsdlike/freebsd/freebsd11/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,17 +385,17 @@ pub const MINCORE_SUPER: c_int = 0x20;
385385
pub const SPECNAMELEN: c_int = 63;
386386

387387
safe_f! {
388-
pub {const} fn makedev(major: c_uint, minor: c_uint) -> crate::dev_t {
388+
pub const fn makedev(major: c_uint, minor: c_uint) -> crate::dev_t {
389389
let major = major as crate::dev_t;
390390
let minor = minor as crate::dev_t;
391391
(major << 8) | minor
392392
}
393393

394-
pub {const} fn major(dev: crate::dev_t) -> c_int {
394+
pub const fn major(dev: crate::dev_t) -> c_int {
395395
((dev >> 8) & 0xff) as c_int
396396
}
397397

398-
pub {const} fn minor(dev: crate::dev_t) -> c_int {
398+
pub const fn minor(dev: crate::dev_t) -> c_int {
399399
(dev & 0xffff00ff) as c_int
400400
}
401401
}

0 commit comments

Comments
 (0)