Skip to content

Commit 144af3e

Browse files
authored
Auto merge of #36807 - brson:pal, r=brson
Restrict where in the tree platform-specific cfgs may be mentioned With the ports of Rust never ending, it's important that we keep things tidy. The main thing this PR does is introduce a new "pal" (platform abstraction layer) tidy check that limits where platform-specific CFGs may appear. This is intended to maintain existing standards of code organization in hopes that the standard library will continue to be refactored to isolate platform-specific bits, making porting easier; where "standard library" roughly means "all the dependencies of the std and test crates". This generally means placing restrictions on where `cfg(unix)`, `cfg(windows)`, `cfg(target_os)` and `cfg(target_env)` may appear, the basic objective being to isolate platform-specific code to the platform-specific `std::sys` modules, and to the allocation, unwinding, and libc crates. Following are the basic rules, though there are currently exceptions: - core may not have platform-specific code - liballoc_system may have platform-specific code - liballoc_jemalloc may have platform-specific code - libpanic_abort may have platform-specific code - libpanic_unwind may have platform-specific code - other crates in the std facade may not - std may have platform-specific code in the following places - sys/unix/ - sys/windows/ - os/ There are plenty of exceptions today though, noted in the whitelist. The end-state, IMO, is for the standard library to be portable by porting only `std::sys` (possibly extracted to its own crate), an allocator crate, an unwinder crate, and possibly a libc crate (if std depends on it); but that outcome is far off and independent of the utility of enforcing where such code lives today. cc @rust-lang/libs
2 parents 1cdc0fb + 4d76ac8 commit 144af3e

29 files changed

+1191
-880
lines changed

src/libstd/env.rs

+12-185
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use ffi::{OsStr, OsString};
2121
use fmt;
2222
use io;
2323
use path::{Path, PathBuf};
24+
use sys;
2425
use sys::os as os_imp;
2526

2627
/// Returns the current working directory as a `PathBuf`.
@@ -557,7 +558,7 @@ pub struct Args { inner: ArgsOs }
557558
///
558559
/// This structure is created through the `std::env::args_os` method.
559560
#[stable(feature = "env", since = "1.0.0")]
560-
pub struct ArgsOs { inner: os_imp::Args }
561+
pub struct ArgsOs { inner: sys::args::Args }
561562

562563
/// Returns the arguments which this program was started with (normally passed
563564
/// via the command line).
@@ -606,7 +607,7 @@ pub fn args() -> Args {
606607
/// ```
607608
#[stable(feature = "env", since = "1.0.0")]
608609
pub fn args_os() -> ArgsOs {
609-
ArgsOs { inner: os_imp::args() }
610+
ArgsOs { inner: sys::args::args() }
610611
}
611612

612613
#[stable(feature = "env", since = "1.0.0")]
@@ -649,6 +650,8 @@ impl DoubleEndedIterator for ArgsOs {
649650
/// Constants associated with the current target
650651
#[stable(feature = "env", since = "1.0.0")]
651652
pub mod consts {
653+
use sys::env::os;
654+
652655
/// A string describing the architecture of the CPU that is currently
653656
/// in use.
654657
///
@@ -673,7 +676,7 @@ pub mod consts {
673676
/// - unix
674677
/// - windows
675678
#[stable(feature = "env", since = "1.0.0")]
676-
pub const FAMILY: &'static str = super::os::FAMILY;
679+
pub const FAMILY: &'static str = os::FAMILY;
677680

678681
/// A string describing the specific operating system in use.
679682
/// Example value is `linux`.
@@ -692,7 +695,7 @@ pub mod consts {
692695
/// - android
693696
/// - windows
694697
#[stable(feature = "env", since = "1.0.0")]
695-
pub const OS: &'static str = super::os::OS;
698+
pub const OS: &'static str = os::OS;
696699

697700
/// Specifies the filename prefix used for shared libraries on this
698701
/// platform. Example value is `lib`.
@@ -702,7 +705,7 @@ pub mod consts {
702705
/// - lib
703706
/// - `""` (an empty string)
704707
#[stable(feature = "env", since = "1.0.0")]
705-
pub const DLL_PREFIX: &'static str = super::os::DLL_PREFIX;
708+
pub const DLL_PREFIX: &'static str = os::DLL_PREFIX;
706709

707710
/// Specifies the filename suffix used for shared libraries on this
708711
/// platform. Example value is `.so`.
@@ -713,7 +716,7 @@ pub mod consts {
713716
/// - .dylib
714717
/// - .dll
715718
#[stable(feature = "env", since = "1.0.0")]
716-
pub const DLL_SUFFIX: &'static str = super::os::DLL_SUFFIX;
719+
pub const DLL_SUFFIX: &'static str = os::DLL_SUFFIX;
717720

718721
/// Specifies the file extension used for shared libraries on this
719722
/// platform that goes after the dot. Example value is `so`.
@@ -724,7 +727,7 @@ pub mod consts {
724727
/// - dylib
725728
/// - dll
726729
#[stable(feature = "env", since = "1.0.0")]
727-
pub const DLL_EXTENSION: &'static str = super::os::DLL_EXTENSION;
730+
pub const DLL_EXTENSION: &'static str = os::DLL_EXTENSION;
728731

729732
/// Specifies the filename suffix used for executable binaries on this
730733
/// platform. Example value is `.exe`.
@@ -736,7 +739,7 @@ pub mod consts {
736739
/// - .pexe
737740
/// - `""` (an empty string)
738741
#[stable(feature = "env", since = "1.0.0")]
739-
pub const EXE_SUFFIX: &'static str = super::os::EXE_SUFFIX;
742+
pub const EXE_SUFFIX: &'static str = os::EXE_SUFFIX;
740743

741744
/// Specifies the file extension, if any, used for executable binaries
742745
/// on this platform. Example value is `exe`.
@@ -746,183 +749,7 @@ pub mod consts {
746749
/// - exe
747750
/// - `""` (an empty string)
748751
#[stable(feature = "env", since = "1.0.0")]
749-
pub const EXE_EXTENSION: &'static str = super::os::EXE_EXTENSION;
750-
751-
}
752-
753-
#[cfg(target_os = "linux")]
754-
mod os {
755-
pub const FAMILY: &'static str = "unix";
756-
pub const OS: &'static str = "linux";
757-
pub const DLL_PREFIX: &'static str = "lib";
758-
pub const DLL_SUFFIX: &'static str = ".so";
759-
pub const DLL_EXTENSION: &'static str = "so";
760-
pub const EXE_SUFFIX: &'static str = "";
761-
pub const EXE_EXTENSION: &'static str = "";
762-
}
763-
764-
#[cfg(target_os = "macos")]
765-
mod os {
766-
pub const FAMILY: &'static str = "unix";
767-
pub const OS: &'static str = "macos";
768-
pub const DLL_PREFIX: &'static str = "lib";
769-
pub const DLL_SUFFIX: &'static str = ".dylib";
770-
pub const DLL_EXTENSION: &'static str = "dylib";
771-
pub const EXE_SUFFIX: &'static str = "";
772-
pub const EXE_EXTENSION: &'static str = "";
773-
}
774-
775-
#[cfg(target_os = "ios")]
776-
mod os {
777-
pub const FAMILY: &'static str = "unix";
778-
pub const OS: &'static str = "ios";
779-
pub const DLL_PREFIX: &'static str = "lib";
780-
pub const DLL_SUFFIX: &'static str = ".dylib";
781-
pub const DLL_EXTENSION: &'static str = "dylib";
782-
pub const EXE_SUFFIX: &'static str = "";
783-
pub const EXE_EXTENSION: &'static str = "";
784-
}
785-
786-
#[cfg(target_os = "freebsd")]
787-
mod os {
788-
pub const FAMILY: &'static str = "unix";
789-
pub const OS: &'static str = "freebsd";
790-
pub const DLL_PREFIX: &'static str = "lib";
791-
pub const DLL_SUFFIX: &'static str = ".so";
792-
pub const DLL_EXTENSION: &'static str = "so";
793-
pub const EXE_SUFFIX: &'static str = "";
794-
pub const EXE_EXTENSION: &'static str = "";
795-
}
796-
797-
#[cfg(target_os = "dragonfly")]
798-
mod os {
799-
pub const FAMILY: &'static str = "unix";
800-
pub const OS: &'static str = "dragonfly";
801-
pub const DLL_PREFIX: &'static str = "lib";
802-
pub const DLL_SUFFIX: &'static str = ".so";
803-
pub const DLL_EXTENSION: &'static str = "so";
804-
pub const EXE_SUFFIX: &'static str = "";
805-
pub const EXE_EXTENSION: &'static str = "";
806-
}
807-
808-
#[cfg(target_os = "bitrig")]
809-
mod os {
810-
pub const FAMILY: &'static str = "unix";
811-
pub const OS: &'static str = "bitrig";
812-
pub const DLL_PREFIX: &'static str = "lib";
813-
pub const DLL_SUFFIX: &'static str = ".so";
814-
pub const DLL_EXTENSION: &'static str = "so";
815-
pub const EXE_SUFFIX: &'static str = "";
816-
pub const EXE_EXTENSION: &'static str = "";
817-
}
818-
819-
#[cfg(target_os = "netbsd")]
820-
mod os {
821-
pub const FAMILY: &'static str = "unix";
822-
pub const OS: &'static str = "netbsd";
823-
pub const DLL_PREFIX: &'static str = "lib";
824-
pub const DLL_SUFFIX: &'static str = ".so";
825-
pub const DLL_EXTENSION: &'static str = "so";
826-
pub const EXE_SUFFIX: &'static str = "";
827-
pub const EXE_EXTENSION: &'static str = "";
828-
}
829-
830-
#[cfg(target_os = "openbsd")]
831-
mod os {
832-
pub const FAMILY: &'static str = "unix";
833-
pub const OS: &'static str = "openbsd";
834-
pub const DLL_PREFIX: &'static str = "lib";
835-
pub const DLL_SUFFIX: &'static str = ".so";
836-
pub const DLL_EXTENSION: &'static str = "so";
837-
pub const EXE_SUFFIX: &'static str = "";
838-
pub const EXE_EXTENSION: &'static str = "";
839-
}
840-
841-
#[cfg(target_os = "android")]
842-
mod os {
843-
pub const FAMILY: &'static str = "unix";
844-
pub const OS: &'static str = "android";
845-
pub const DLL_PREFIX: &'static str = "lib";
846-
pub const DLL_SUFFIX: &'static str = ".so";
847-
pub const DLL_EXTENSION: &'static str = "so";
848-
pub const EXE_SUFFIX: &'static str = "";
849-
pub const EXE_EXTENSION: &'static str = "";
850-
}
851-
852-
#[cfg(target_os = "solaris")]
853-
mod os {
854-
pub const FAMILY: &'static str = "unix";
855-
pub const OS: &'static str = "solaris";
856-
pub const DLL_PREFIX: &'static str = "lib";
857-
pub const DLL_SUFFIX: &'static str = ".so";
858-
pub const DLL_EXTENSION: &'static str = "so";
859-
pub const EXE_SUFFIX: &'static str = "";
860-
pub const EXE_EXTENSION: &'static str = "";
861-
}
862-
863-
#[cfg(target_os = "windows")]
864-
mod os {
865-
pub const FAMILY: &'static str = "windows";
866-
pub const OS: &'static str = "windows";
867-
pub const DLL_PREFIX: &'static str = "";
868-
pub const DLL_SUFFIX: &'static str = ".dll";
869-
pub const DLL_EXTENSION: &'static str = "dll";
870-
pub const EXE_SUFFIX: &'static str = ".exe";
871-
pub const EXE_EXTENSION: &'static str = "exe";
872-
}
873-
874-
#[cfg(all(target_os = "nacl", not(target_arch = "le32")))]
875-
mod os {
876-
pub const FAMILY: &'static str = "unix";
877-
pub const OS: &'static str = "nacl";
878-
pub const DLL_PREFIX: &'static str = "lib";
879-
pub const DLL_SUFFIX: &'static str = ".so";
880-
pub const DLL_EXTENSION: &'static str = "so";
881-
pub const EXE_SUFFIX: &'static str = ".nexe";
882-
pub const EXE_EXTENSION: &'static str = "nexe";
883-
}
884-
#[cfg(all(target_os = "nacl", target_arch = "le32"))]
885-
mod os {
886-
pub const FAMILY: &'static str = "unix";
887-
pub const OS: &'static str = "pnacl";
888-
pub const DLL_PREFIX: &'static str = "lib";
889-
pub const DLL_SUFFIX: &'static str = ".pso";
890-
pub const DLL_EXTENSION: &'static str = "pso";
891-
pub const EXE_SUFFIX: &'static str = ".pexe";
892-
pub const EXE_EXTENSION: &'static str = "pexe";
893-
}
894-
895-
#[cfg(all(target_os = "emscripten", target_arch = "asmjs"))]
896-
mod os {
897-
pub const FAMILY: &'static str = "unix";
898-
pub const OS: &'static str = "emscripten";
899-
pub const DLL_PREFIX: &'static str = "lib";
900-
pub const DLL_SUFFIX: &'static str = ".so";
901-
pub const DLL_EXTENSION: &'static str = "so";
902-
pub const EXE_SUFFIX: &'static str = ".js";
903-
pub const EXE_EXTENSION: &'static str = "js";
904-
}
905-
906-
#[cfg(all(target_os = "emscripten", target_arch = "wasm32"))]
907-
mod os {
908-
pub const FAMILY: &'static str = "unix";
909-
pub const OS: &'static str = "emscripten";
910-
pub const DLL_PREFIX: &'static str = "lib";
911-
pub const DLL_SUFFIX: &'static str = ".so";
912-
pub const DLL_EXTENSION: &'static str = "so";
913-
pub const EXE_SUFFIX: &'static str = ".js";
914-
pub const EXE_EXTENSION: &'static str = "js";
915-
}
916-
917-
#[cfg(target_os = "haiku")]
918-
mod os {
919-
pub const FAMILY: &'static str = "unix";
920-
pub const OS: &'static str = "haiku";
921-
pub const DLL_PREFIX: &'static str = "lib";
922-
pub const DLL_SUFFIX: &'static str = ".so";
923-
pub const DLL_EXTENSION: &'static str = "so";
924-
pub const EXE_SUFFIX: &'static str = "";
925-
pub const EXE_EXTENSION: &'static str = "";
752+
pub const EXE_EXTENSION: &'static str = os::EXE_EXTENSION;
926753
}
927754

928755
#[cfg(target_arch = "x86")]

src/libstd/ffi/os_str.rs

-11
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,6 @@ impl OsString {
5353
OsString { inner: Buf::from_string(String::new()) }
5454
}
5555

56-
#[cfg(unix)]
57-
fn _from_bytes(vec: Vec<u8>) -> Option<OsString> {
58-
use os::unix::ffi::OsStringExt;
59-
Some(OsString::from_vec(vec))
60-
}
61-
62-
#[cfg(windows)]
63-
fn _from_bytes(vec: Vec<u8>) -> Option<OsString> {
64-
String::from_utf8(vec).ok().map(OsString::from)
65-
}
66-
6756
/// Converts to an `OsStr` slice.
6857
#[stable(feature = "rust1", since = "1.0.0")]
6958
pub fn as_os_str(&self) -> &OsStr {

src/libstd/io/stdio.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,10 @@ impl<R: io::Read> io::Read for Maybe<R> {
125125
}
126126

127127
fn handle_ebadf<T>(r: io::Result<T>, default: T) -> io::Result<T> {
128-
#[cfg(windows)]
129-
const ERR: i32 = ::sys::c::ERROR_INVALID_HANDLE as i32;
130-
#[cfg(not(windows))]
131-
const ERR: i32 = ::libc::EBADF as i32;
128+
use sys::stdio::EBADF_ERR;
132129

133130
match r {
134-
Err(ref e) if e.raw_os_error() == Some(ERR) => Ok(default),
131+
Err(ref e) if e.raw_os_error() == Some(EBADF_ERR) => Ok(default),
135132
r => r
136133
}
137134
}

0 commit comments

Comments
 (0)