Skip to content

Commit 6e9a1de

Browse files
committed
Introduce restricted-std feature.
1 parent 9e58908 commit 6e9a1de

35 files changed

+266
-109
lines changed

src/libpanic_unwind/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ cfg_if::cfg_if! {
4141
if #[cfg(target_os = "emscripten")] {
4242
#[path = "emcc.rs"]
4343
mod real_imp;
44-
} else if #[cfg(target_arch = "wasm32")] {
44+
} else if #[cfg(any(target_arch = "wasm32", target_os = "none"))] {
4545
#[path = "dummy.rs"]
4646
mod real_imp;
4747
} else if #[cfg(target_os = "hermit")] {

src/libproc_macro/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#![feature(in_band_lifetimes)]
2727
#![feature(negative_impls)]
2828
#![feature(optin_builtin_traits)]
29+
#![feature(restricted_std)]
2930
#![feature(rustc_attrs)]
3031
#![feature(min_specialization)]
3132
#![recursion_limit = "256"]

src/librustc_passes/stability.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,8 @@ impl Visitor<'tcx> for Checker<'tcx> {
502502
match item.kind {
503503
hir::ItemKind::ExternCrate(_) => {
504504
// compiler-generated `extern crate` items have a dummy span.
505-
if item.span.is_dummy() {
505+
// `std` is still checked for the `restricted-std` feature.
506+
if item.span.is_dummy() && item.ident.as_str() != "std" {
506507
return;
507508
}
508509

src/libstd/build.rs

+3
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,7 @@ fn main() {
6464
println!("cargo:rustc-link-lib=compiler_rt");
6565
}
6666
println!("cargo:rustc-env=STD_ENV_ARCH={}", env::var("CARGO_CFG_TARGET_ARCH").unwrap());
67+
if target.contains("-none") || target.contains("nvptx") {
68+
println!("cargo:rustc-cfg=feature=\"restricted-std\"");
69+
}
6770
}

src/libstd/lib.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@
197197
//! [primitive types]: ../book/ch03-02-data-types.html
198198
//! [rust-discord]: https://discord.gg/rust-lang
199199
200-
#![stable(feature = "rust1", since = "1.0.0")]
200+
#![cfg_attr(not(feature = "restricted-std"), stable(feature = "rust1", since = "1.0.0"))]
201+
#![cfg_attr(feature = "restricted-std", unstable(feature = "restricted_std", issue = "none"))]
201202
#![doc(
202203
html_root_url = "https://doc.rust-lang.org/nightly/",
203204
html_playground_url = "https://play.rust-lang.org/",
@@ -552,3 +553,9 @@ include!("primitive_docs.rs");
552553
// the rustdoc documentation for the existing keywords. Using `include!`
553554
// because rustdoc only looks for these modules at the crate level.
554555
include!("keyword_docs.rs");
556+
557+
// This is required to avoid an unstable error when `restricted-std` is not
558+
// enabled. The use of #![feature(restricted_std)] in rustc-std-workspace-std
559+
// is unconditional, so the unstable feature needs to be defined somewhere.
560+
#[cfg_attr(not(feature = "restricted-std"), unstable(feature = "restricted_std", issue = "none"))]
561+
mod __restricted_std_workaround {}

src/libstd/sys/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ cfg_if::cfg_if! {
4848
mod sgx;
4949
pub use self::sgx::*;
5050
} else {
51-
compile_error!("libstd doesn't compile for this platform yet");
51+
mod unsupported;
52+
pub use self::unsupported::*;
5253
}
5354
}
5455

src/libstd/sys/unsupported/alloc.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use crate::alloc::{GlobalAlloc, Layout, System};
2+
3+
#[stable(feature = "alloc_system_type", since = "1.28.0")]
4+
unsafe impl GlobalAlloc for System {
5+
#[inline]
6+
unsafe fn alloc(&self, _layout: Layout) -> *mut u8 {
7+
0 as *mut u8
8+
}
9+
10+
#[inline]
11+
unsafe fn alloc_zeroed(&self, _layout: Layout) -> *mut u8 {
12+
0 as *mut u8
13+
}
14+
15+
#[inline]
16+
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
17+
18+
#[inline]
19+
unsafe fn realloc(&self, _ptr: *mut u8, _layout: Layout, _new_size: usize) -> *mut u8 {
20+
0 as *mut u8
21+
}
22+
}

src/libstd/sys/unsupported/args.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use crate::ffi::OsString;
2+
3+
pub unsafe fn init(_argc: isize, _argv: *const *const u8) {}
4+
pub unsafe fn cleanup() {}
5+
6+
pub struct Args {}
7+
8+
pub fn args() -> Args {
9+
Args {}
10+
}
11+
12+
impl Args {
13+
pub fn inner_debug(&self) -> &[OsString] {
14+
&[]
15+
}
16+
}
17+
18+
impl Iterator for Args {
19+
type Item = OsString;
20+
fn next(&mut self) -> Option<OsString> {
21+
None
22+
}
23+
fn size_hint(&self) -> (usize, Option<usize>) {
24+
(0, Some(0))
25+
}
26+
}
27+
28+
impl ExactSizeIterator for Args {
29+
fn len(&self) -> usize {
30+
0
31+
}
32+
}
33+
34+
impl DoubleEndedIterator for Args {
35+
fn next_back(&mut self) -> Option<OsString> {
36+
None
37+
}
38+
}
File renamed without changes.

src/libstd/sys/unsupported/common.rs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use crate::io as std_io;
2+
3+
pub mod memchr {
4+
pub use core::slice::memchr::{memchr, memrchr};
5+
}
6+
7+
pub use crate::sys_common::os_str_bytes as os_str;
8+
9+
// This is not necessarily correct. May want to consider making it part of the
10+
// spec definition?
11+
use crate::os::raw::c_char;
12+
13+
#[cfg(not(test))]
14+
pub fn init() {}
15+
16+
pub fn unsupported<T>() -> std_io::Result<T> {
17+
Err(unsupported_err())
18+
}
19+
20+
pub fn unsupported_err() -> std_io::Error {
21+
std_io::Error::new(std_io::ErrorKind::Other, "operation not supported on this platform")
22+
}
23+
24+
pub fn decode_error_kind(_code: i32) -> crate::io::ErrorKind {
25+
crate::io::ErrorKind::Other
26+
}
27+
28+
pub fn abort_internal() -> ! {
29+
core::intrinsics::abort();
30+
}
31+
32+
pub fn hashmap_random_keys() -> (u64, u64) {
33+
(1, 2)
34+
}
35+
36+
// This enum is used as the storage for a bunch of types which can't actually
37+
// exist.
38+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
39+
pub enum Void {}
40+
41+
pub unsafe fn strlen(mut s: *const c_char) -> usize {
42+
let mut n = 0;
43+
while *s != 0 {
44+
n += 1;
45+
s = s.offset(1);
46+
}
47+
return n;
48+
}

src/libstd/sys/wasm/condvar.rs renamed to src/libstd/sys/unsupported/condvar.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ impl Condvar {
1818
pub unsafe fn notify_all(&self) {}
1919

2020
pub unsafe fn wait(&self, _mutex: &Mutex) {
21-
panic!("can't block with web assembly")
21+
panic!("condvar wait not supported")
2222
}
2323

2424
pub unsafe fn wait_timeout(&self, _mutex: &Mutex, _dur: Duration) -> bool {
25-
panic!("can't block with web assembly");
25+
panic!("condvar wait not supported");
2626
}
2727

2828
#[inline]

src/libstd/sys/unsupported/env.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pub mod os {
2+
pub const FAMILY: &str = "";
3+
pub const OS: &str = "";
4+
pub const DLL_PREFIX: &str = "";
5+
pub const DLL_SUFFIX: &str = "";
6+
pub const DLL_EXTENSION: &str = "";
7+
pub const EXE_SUFFIX: &str = "";
8+
pub const EXE_EXTENSION: &str = "";
9+
}
File renamed without changes.
File renamed without changes.

src/libstd/sys/unsupported/mod.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
pub mod alloc;
2+
pub mod args;
3+
pub mod cmath;
4+
pub mod condvar;
5+
pub mod env;
6+
pub mod fs;
7+
pub mod io;
8+
pub mod mutex;
9+
pub mod net;
10+
pub mod os;
11+
pub mod path;
12+
pub mod pipe;
13+
pub mod process;
14+
pub mod rwlock;
15+
pub mod stack_overflow;
16+
pub mod stdio;
17+
pub mod thread;
18+
#[cfg(target_thread_local)]
19+
pub mod thread_local_dtor;
20+
pub mod thread_local_key;
21+
pub mod time;
22+
23+
mod common;
24+
pub use common::*;

src/libstd/sys/wasm/mutex.rs renamed to src/libstd/sys/unsupported/mutex.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ pub struct Mutex {
55
}
66

77
unsafe impl Send for Mutex {}
8-
unsafe impl Sync for Mutex {} // no threads on wasm
8+
unsafe impl Sync for Mutex {} // no threads on this platform
99

1010
impl Mutex {
11+
#[rustc_const_stable(feature = "const_sys_mutex_new", since = "1.0.0")]
1112
pub const fn new() -> Mutex {
1213
Mutex { locked: UnsafeCell::new(false) }
1314
}
@@ -42,8 +43,8 @@ impl Mutex {
4243
pub unsafe fn destroy(&self) {}
4344
}
4445

45-
// All empty stubs because wasm has no threads yet, so lock acquisition always
46-
// succeeds.
46+
// All empty stubs because this platform does not yet support threads, so lock
47+
// acquisition always succeeds.
4748
pub struct ReentrantMutex {}
4849

4950
impl ReentrantMutex {
File renamed without changes.

src/libstd/sys/wasm/os.rs renamed to src/libstd/sys/unsupported/os.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
use super::{unsupported, Void};
12
use crate::error::Error as StdError;
23
use crate::ffi::{OsStr, OsString};
34
use crate::fmt;
45
use crate::io;
56
use crate::path::{self, PathBuf};
6-
use crate::str;
7-
use crate::sys::{unsupported, Void};
87

98
pub fn errno() -> i32 {
109
0
@@ -48,14 +47,14 @@ where
4847

4948
impl fmt::Display for JoinPathsError {
5049
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51-
"not supported on wasm yet".fmt(f)
50+
"not supported on this platform yet".fmt(f)
5251
}
5352
}
5453

5554
impl StdError for JoinPathsError {
5655
#[allow(deprecated)]
5756
fn description(&self) -> &str {
58-
"not supported on wasm yet"
57+
"not supported on this platform yet"
5958
}
6059
}
6160

@@ -73,35 +72,33 @@ impl Iterator for Env {
7372
}
7473

7574
pub fn env() -> Env {
76-
panic!("not supported on web assembly")
75+
panic!("not supported on this platform")
7776
}
7877

7978
pub fn getenv(_: &OsStr) -> io::Result<Option<OsString>> {
8079
Ok(None)
8180
}
8281

8382
pub fn setenv(_: &OsStr, _: &OsStr) -> io::Result<()> {
84-
Err(io::Error::new(io::ErrorKind::Other, "cannot set env vars on wasm32-unknown-unknown"))
83+
Err(io::Error::new(io::ErrorKind::Other, "cannot set env vars on this platform"))
8584
}
8685

8786
pub fn unsetenv(_: &OsStr) -> io::Result<()> {
88-
Err(io::Error::new(io::ErrorKind::Other, "cannot unset env vars on wasm32-unknown-unknown"))
87+
Err(io::Error::new(io::ErrorKind::Other, "cannot unset env vars on this platform"))
8988
}
9089

9190
pub fn temp_dir() -> PathBuf {
92-
panic!("no filesystem on wasm")
91+
panic!("no filesystem on this platform")
9392
}
9493

9594
pub fn home_dir() -> Option<PathBuf> {
9695
None
9796
}
9897

9998
pub fn exit(_code: i32) -> ! {
100-
unsafe {
101-
crate::arch::wasm32::unreachable();
102-
}
99+
crate::intrinsics::abort()
103100
}
104101

105102
pub fn getpid() -> u32 {
106-
panic!("no pids on wasm")
103+
panic!("no pids on this platform")
107104
}
File renamed without changes.
File renamed without changes.

src/libstd/sys/wasm/rwlock.rs renamed to src/libstd/sys/unsupported/rwlock.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub struct RWLock {
55
}
66

77
unsafe impl Send for RWLock {}
8-
unsafe impl Sync for RWLock {} // no threads on wasm
8+
unsafe impl Sync for RWLock {} // no threads on this platform
99

1010
impl RWLock {
1111
pub const fn new() -> RWLock {
File renamed without changes.

src/libstd/sys/unsupported/thread.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use super::{unsupported, Void};
2+
use crate::ffi::CStr;
3+
use crate::io;
4+
use crate::time::Duration;
5+
6+
pub struct Thread(Void);
7+
8+
pub const DEFAULT_MIN_STACK_SIZE: usize = 4096;
9+
10+
impl Thread {
11+
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
12+
pub unsafe fn new(_stack: usize, _p: Box<dyn FnOnce()>) -> io::Result<Thread> {
13+
unsupported()
14+
}
15+
16+
pub fn yield_now() {
17+
// do nothing
18+
}
19+
20+
pub fn set_name(_name: &CStr) {
21+
// nope
22+
}
23+
24+
pub fn sleep(_dur: Duration) {
25+
panic!("can't sleep");
26+
}
27+
28+
pub fn join(self) {
29+
match self.0 {}
30+
}
31+
}
32+
33+
pub mod guard {
34+
pub type Guard = !;
35+
pub unsafe fn current() -> Option<Guard> {
36+
None
37+
}
38+
pub unsafe fn init() -> Option<Guard> {
39+
None
40+
}
41+
}

0 commit comments

Comments
 (0)