Skip to content

Commit 4af3930

Browse files
authored
Rollup merge of #91641 - dtolnay:cchar-if, r=Mark-Simulacrum
Define c_char using cfg_if rather than repeating 40-line cfg Libstd has a 40-line cfg that defines the targets on which `c_char` is unsigned, and then repeats the same cfg with `not(…)` for the targets on which `c_char` is signed. This PR replaces it with a `cfg_if!` in which an `else` takes care of the signed case. I confirmed that `x.py doc library/std` inlines the type alias because c_char_definition is not a publicly accessible path: ![Screenshot from 2021-12-07 13-42-07](https://user-images.githubusercontent.com/1940490/145110596-f1058406-9f32-44ff-9a81-1dfd19b4a24f.png)
2 parents 21b4a9c + 4e8b91a commit 4af3930

File tree

1 file changed

+62
-88
lines changed

1 file changed

+62
-88
lines changed

library/std/src/os/raw/mod.rs

+62-88
Original file line numberDiff line numberDiff line change
@@ -45,94 +45,13 @@ macro_rules! type_alias {
4545
}
4646
}
4747

48-
type_alias! { "char.md", c_char = u8, NonZero_c_char = NonZeroU8;
49-
#[doc(cfg(all()))]
50-
#[cfg(any(
51-
all(
52-
target_os = "linux",
53-
any(
54-
target_arch = "aarch64",
55-
target_arch = "arm",
56-
target_arch = "hexagon",
57-
target_arch = "powerpc",
58-
target_arch = "powerpc64",
59-
target_arch = "s390x",
60-
target_arch = "riscv64",
61-
target_arch = "riscv32"
62-
)
63-
),
64-
all(target_os = "android", any(target_arch = "aarch64", target_arch = "arm")),
65-
all(target_os = "l4re", target_arch = "x86_64"),
66-
all(
67-
target_os = "freebsd",
68-
any(
69-
target_arch = "aarch64",
70-
target_arch = "arm",
71-
target_arch = "powerpc",
72-
target_arch = "powerpc64",
73-
target_arch = "riscv64"
74-
)
75-
),
76-
all(
77-
target_os = "netbsd",
78-
any(target_arch = "aarch64", target_arch = "arm", target_arch = "powerpc")
79-
),
80-
all(target_os = "openbsd", target_arch = "aarch64"),
81-
all(
82-
target_os = "vxworks",
83-
any(
84-
target_arch = "aarch64",
85-
target_arch = "arm",
86-
target_arch = "powerpc64",
87-
target_arch = "powerpc"
88-
)
89-
),
90-
all(target_os = "fuchsia", target_arch = "aarch64")
91-
))]}
92-
type_alias! { "char.md", c_char = i8, NonZero_c_char = NonZeroI8;
93-
#[doc(cfg(all()))]
94-
#[cfg(not(any(
95-
all(
96-
target_os = "linux",
97-
any(
98-
target_arch = "aarch64",
99-
target_arch = "arm",
100-
target_arch = "hexagon",
101-
target_arch = "powerpc",
102-
target_arch = "powerpc64",
103-
target_arch = "s390x",
104-
target_arch = "riscv64",
105-
target_arch = "riscv32"
106-
)
107-
),
108-
all(target_os = "android", any(target_arch = "aarch64", target_arch = "arm")),
109-
all(target_os = "l4re", target_arch = "x86_64"),
110-
all(
111-
target_os = "freebsd",
112-
any(
113-
target_arch = "aarch64",
114-
target_arch = "arm",
115-
target_arch = "powerpc",
116-
target_arch = "powerpc64",
117-
target_arch = "riscv64"
118-
)
119-
),
120-
all(
121-
target_os = "netbsd",
122-
any(target_arch = "aarch64", target_arch = "arm", target_arch = "powerpc")
123-
),
124-
all(target_os = "openbsd", target_arch = "aarch64"),
125-
all(
126-
target_os = "vxworks",
127-
any(
128-
target_arch = "aarch64",
129-
target_arch = "arm",
130-
target_arch = "powerpc64",
131-
target_arch = "powerpc"
132-
)
133-
),
134-
all(target_os = "fuchsia", target_arch = "aarch64")
135-
)))]}
48+
type_alias! { "char.md", c_char = c_char_definition::c_char, NonZero_c_char = c_char_definition::NonZero_c_char;
49+
// Make this type alias appear cfg-dependent so that Clippy does not suggest
50+
// replacing `0 as c_char` with `0_i8`/`0_u8`. This #[cfg(all())] can be removed
51+
// after the false positive in https://github.com/rust-lang/rust-clippy/issues/8093
52+
// is fixed.
53+
#[cfg(all())]
54+
#[doc(cfg(all()))] }
13655
type_alias! { "schar.md", c_schar = i8, NonZero_c_schar = NonZeroI8; }
13756
type_alias! { "uchar.md", c_uchar = u8, NonZero_c_uchar = NonZeroU8; }
13857
type_alias! { "short.md", c_short = i16, NonZero_c_short = NonZeroI16; }
@@ -180,3 +99,58 @@ pub type c_ptrdiff_t = isize;
18099
/// platforms where this is not the case.
181100
#[unstable(feature = "c_size_t", issue = "88345")]
182101
pub type c_ssize_t = isize;
102+
103+
mod c_char_definition {
104+
cfg_if::cfg_if! {
105+
// These are the targets on which c_char is unsigned.
106+
if #[cfg(any(
107+
all(
108+
target_os = "linux",
109+
any(
110+
target_arch = "aarch64",
111+
target_arch = "arm",
112+
target_arch = "hexagon",
113+
target_arch = "powerpc",
114+
target_arch = "powerpc64",
115+
target_arch = "s390x",
116+
target_arch = "riscv64",
117+
target_arch = "riscv32"
118+
)
119+
),
120+
all(target_os = "android", any(target_arch = "aarch64", target_arch = "arm")),
121+
all(target_os = "l4re", target_arch = "x86_64"),
122+
all(
123+
target_os = "freebsd",
124+
any(
125+
target_arch = "aarch64",
126+
target_arch = "arm",
127+
target_arch = "powerpc",
128+
target_arch = "powerpc64",
129+
target_arch = "riscv64"
130+
)
131+
),
132+
all(
133+
target_os = "netbsd",
134+
any(target_arch = "aarch64", target_arch = "arm", target_arch = "powerpc")
135+
),
136+
all(target_os = "openbsd", target_arch = "aarch64"),
137+
all(
138+
target_os = "vxworks",
139+
any(
140+
target_arch = "aarch64",
141+
target_arch = "arm",
142+
target_arch = "powerpc64",
143+
target_arch = "powerpc"
144+
)
145+
),
146+
all(target_os = "fuchsia", target_arch = "aarch64")
147+
))] {
148+
pub type c_char = u8;
149+
pub type NonZero_c_char = core::num::NonZeroU8;
150+
} else {
151+
// On every other target, c_char is signed.
152+
pub type c_char = i8;
153+
pub type NonZero_c_char = core::num::NonZeroI8;
154+
}
155+
}
156+
}

0 commit comments

Comments
 (0)