Skip to content

Commit 6e9a1de

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

File tree

35 files changed

+266
-109
lines changed

35 files changed

+266
-109
lines changed

src/libpanic_unwind/lib.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 8 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 22 additions & 0 deletions
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

Lines changed: 38 additions & 0 deletions
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

Lines changed: 48 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)