Skip to content

Commit a0ca3f9

Browse files
committed
Rework os to avoid using cfg_if! with public items
1 parent e098d27 commit a0ca3f9

File tree

2 files changed

+151
-123
lines changed

2 files changed

+151
-123
lines changed

library/std/src/os/mod.rs

+115-87
Original file line numberDiff line numberDiff line change
@@ -5,91 +5,119 @@
55

66
pub mod raw;
77

8-
cfg_if::cfg_if! {
9-
if #[cfg(all(doc, not(any(target_os = "hermit",
10-
all(target_arch = "wasm32", not(target_os = "wasi")),
11-
all(target_vendor = "fortanix", target_env = "sgx")))))]{
12-
// When documenting std we want to show the `unix`, `windows`, `linux` and `wasi`
13-
// modules as these are the "main modules" that are used across platforms,
14-
// so these modules are enabled when `cfg(doc)` is set.
15-
// This should help show platform-specific functionality in a hopefully cross-platform
16-
// way in the documentation.
17-
18-
pub mod unix;
19-
20-
pub mod linux;
21-
22-
pub mod wasi;
23-
24-
pub mod windows;
25-
} else if #[cfg(doc)] {
26-
// On certain platforms right now the "main modules" modules that are
27-
// documented don't compile (missing things in `libc` which is empty),
28-
// so just omit them with an empty module.
29-
30-
#[unstable(issue = "none", feature = "std_internals")]
31-
pub mod unix {}
32-
33-
#[unstable(issue = "none", feature = "std_internals")]
34-
pub mod linux {}
35-
36-
#[unstable(issue = "none", feature = "std_internals")]
37-
pub mod wasi {}
38-
39-
#[unstable(issue = "none", feature = "std_internals")]
40-
pub mod windows {}
41-
} else {
42-
// If we're not documenting std then we only expose modules appropriate for the
43-
// current platform.
44-
45-
#[cfg(all(target_vendor = "fortanix", target_env = "sgx"))]
46-
pub mod fortanix_sgx;
47-
48-
#[cfg(target_os = "hermit")]
49-
mod hermit;
50-
#[cfg(target_os = "hermit")]
51-
pub use hermit as unix;
52-
53-
#[cfg(unix)]
54-
pub mod unix;
55-
#[cfg(target_os = "android")]
56-
pub mod android;
57-
#[cfg(target_os = "dragonfly")]
58-
pub mod dragonfly;
59-
#[cfg(target_os = "emscripten")]
60-
pub mod emscripten;
61-
#[cfg(target_os = "freebsd")]
62-
pub mod freebsd;
63-
#[cfg(target_os = "fuchsia")]
64-
pub mod fuchsia;
65-
#[cfg(target_os = "haiku")]
66-
pub mod haiku;
67-
#[cfg(target_os = "illumos")]
68-
pub mod illumos;
69-
#[cfg(target_os = "ios")]
70-
pub mod ios;
71-
#[cfg(target_os = "l4re")]
72-
pub mod linux;
73-
#[cfg(target_os = "linux")]
74-
pub mod linux;
75-
#[cfg(target_os = "macos")]
76-
pub mod macos;
77-
#[cfg(target_os = "netbsd")]
78-
pub mod netbsd;
79-
#[cfg(target_os = "openbsd")]
80-
pub mod openbsd;
81-
#[cfg(target_os = "redox")]
82-
pub mod redox;
83-
#[cfg(target_os = "solaris")]
84-
pub mod solaris;
85-
86-
#[cfg(target_os = "vxworks")]
87-
pub mod vxworks;
88-
89-
#[cfg(target_os = "wasi")]
90-
pub mod wasi;
91-
92-
#[cfg(windows)]
93-
pub mod windows;
94-
}
8+
// The code below could be written clearer using `cfg_if!`. However, the items below are
9+
// publicly exported by `std` and external tools can have trouble analysing them because of the use
10+
// of a macro that is not vendored by Rust and included in the toolchain.
11+
// See https://github.com/rust-analyzer/rust-analyzer/issues/6038.
12+
13+
#[cfg(all(
14+
doc,
15+
not(any(
16+
target_os = "hermit",
17+
all(target_arch = "wasm32", not(target_os = "wasi")),
18+
all(target_vendor = "fortanix", target_env = "sgx")
19+
))
20+
))]
21+
#[path = "."]
22+
mod doc {
23+
// When documenting std we want to show the `unix`, `windows`, `linux` and `wasi`
24+
// modules as these are the "main modules" that are used across platforms,
25+
// so these modules are enabled when `cfg(doc)` is set.
26+
// This should help show platform-specific functionality in a hopefully cross-platform
27+
// way in the documentation.
28+
29+
pub mod unix;
30+
31+
pub mod linux;
32+
33+
pub mod wasi;
34+
35+
pub mod windows;
9536
}
37+
#[cfg(all(
38+
doc,
39+
any(
40+
target_os = "hermit",
41+
all(target_arch = "wasm32", not(target_os = "wasi")),
42+
all(target_vendor = "fortanix", target_env = "sgx")
43+
)
44+
))]
45+
mod doc {
46+
// On certain platforms right now the "main modules" modules that are
47+
// documented don't compile (missing things in `libc` which is empty),
48+
// so just omit them with an empty module.
49+
50+
#[unstable(issue = "none", feature = "std_internals")]
51+
pub mod unix {}
52+
53+
#[unstable(issue = "none", feature = "std_internals")]
54+
pub mod linux {}
55+
56+
#[unstable(issue = "none", feature = "std_internals")]
57+
pub mod wasi {}
58+
59+
#[unstable(issue = "none", feature = "std_internals")]
60+
pub mod windows {}
61+
}
62+
#[cfg(doc)]
63+
#[stable(feature = "os", since = "1.0.0")]
64+
pub use doc::*;
65+
66+
#[cfg(not(doc))]
67+
#[path = "."]
68+
mod imp {
69+
// If we're not documenting std then we only expose modules appropriate for the
70+
// current platform.
71+
72+
#[cfg(all(target_vendor = "fortanix", target_env = "sgx"))]
73+
pub mod fortanix_sgx;
74+
75+
#[cfg(target_os = "hermit")]
76+
#[path = "hermit/mod.rs"]
77+
pub mod unix;
78+
79+
#[cfg(target_os = "android")]
80+
pub mod android;
81+
#[cfg(target_os = "dragonfly")]
82+
pub mod dragonfly;
83+
#[cfg(target_os = "emscripten")]
84+
pub mod emscripten;
85+
#[cfg(target_os = "freebsd")]
86+
pub mod freebsd;
87+
#[cfg(target_os = "fuchsia")]
88+
pub mod fuchsia;
89+
#[cfg(target_os = "haiku")]
90+
pub mod haiku;
91+
#[cfg(target_os = "illumos")]
92+
pub mod illumos;
93+
#[cfg(target_os = "ios")]
94+
pub mod ios;
95+
#[cfg(target_os = "l4re")]
96+
pub mod linux;
97+
#[cfg(target_os = "linux")]
98+
pub mod linux;
99+
#[cfg(target_os = "macos")]
100+
pub mod macos;
101+
#[cfg(target_os = "netbsd")]
102+
pub mod netbsd;
103+
#[cfg(target_os = "openbsd")]
104+
pub mod openbsd;
105+
#[cfg(target_os = "redox")]
106+
pub mod redox;
107+
#[cfg(target_os = "solaris")]
108+
pub mod solaris;
109+
#[cfg(unix)]
110+
pub mod unix;
111+
112+
#[cfg(target_os = "vxworks")]
113+
pub mod vxworks;
114+
115+
#[cfg(target_os = "wasi")]
116+
pub mod wasi;
117+
118+
#[cfg(windows)]
119+
pub mod windows;
120+
}
121+
#[cfg(not(doc))]
122+
#[stable(feature = "os", since = "1.0.0")]
123+
pub use imp::*;

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

+36-36
Original file line numberDiff line numberDiff line change
@@ -28,42 +28,42 @@
2828
#![stable(feature = "rust1", since = "1.0.0")]
2929
#![doc(cfg(unix))]
3030

31-
cfg_if::cfg_if! {
32-
if #[cfg(doc)] {
33-
// Use linux as the default platform when documenting on other platforms like Windows
34-
use crate::os::linux as platform;
35-
} else {
36-
#[cfg(target_os = "android")]
37-
use crate::os::android as platform;
38-
#[cfg(target_os = "dragonfly")]
39-
use crate::os::dragonfly as platform;
40-
#[cfg(target_os = "emscripten")]
41-
use crate::os::emscripten as platform;
42-
#[cfg(target_os = "freebsd")]
43-
use crate::os::freebsd as platform;
44-
#[cfg(target_os = "fuchsia")]
45-
use crate::os::fuchsia as platform;
46-
#[cfg(target_os = "haiku")]
47-
use crate::os::haiku as platform;
48-
#[cfg(target_os = "illumos")]
49-
use crate::os::illumos as platform;
50-
#[cfg(target_os = "ios")]
51-
use crate::os::ios as platform;
52-
#[cfg(any(target_os = "linux", target_os = "l4re"))]
53-
use crate::os::linux as platform;
54-
#[cfg(target_os = "macos")]
55-
use crate::os::macos as platform;
56-
#[cfg(target_os = "netbsd")]
57-
use crate::os::netbsd as platform;
58-
#[cfg(target_os = "openbsd")]
59-
use crate::os::openbsd as platform;
60-
#[cfg(target_os = "redox")]
61-
use crate::os::redox as platform;
62-
#[cfg(target_os = "solaris")]
63-
use crate::os::solaris as platform;
64-
#[cfg(target_os = "vxworks")]
65-
use crate::os::vxworks as platform;
66-
}
31+
// Use linux as the default platform when documenting on other platforms like Windows
32+
#[cfg(doc)]
33+
use crate::os::linux as platform;
34+
35+
#[cfg(not(doc))]
36+
mod platform {
37+
#[cfg(target_os = "android")]
38+
pub use crate::os::android::*;
39+
#[cfg(target_os = "dragonfly")]
40+
pub use crate::os::dragonfly::*;
41+
#[cfg(target_os = "emscripten")]
42+
pub use crate::os::emscripten::*;
43+
#[cfg(target_os = "freebsd")]
44+
pub use crate::os::freebsd::*;
45+
#[cfg(target_os = "fuchsia")]
46+
pub use crate::os::fuchsia::*;
47+
#[cfg(target_os = "haiku")]
48+
pub use crate::os::haiku::*;
49+
#[cfg(target_os = "illumos")]
50+
pub use crate::os::illumos::*;
51+
#[cfg(target_os = "ios")]
52+
pub use crate::os::ios::*;
53+
#[cfg(any(target_os = "linux", target_os = "l4re"))]
54+
pub use crate::os::linux::*;
55+
#[cfg(target_os = "macos")]
56+
pub use crate::os::macos::*;
57+
#[cfg(target_os = "netbsd")]
58+
pub use crate::os::netbsd::*;
59+
#[cfg(target_os = "openbsd")]
60+
pub use crate::os::openbsd::*;
61+
#[cfg(target_os = "redox")]
62+
pub use crate::os::redox::*;
63+
#[cfg(target_os = "solaris")]
64+
pub use crate::os::solaris::*;
65+
#[cfg(target_os = "vxworks")]
66+
pub use crate::os::vxworks::*;
6767
}
6868

6969
pub mod ffi;

0 commit comments

Comments
 (0)