Skip to content

Commit f4be202

Browse files
committed
std: Internalize almost all of std::rt
This commit does some refactoring to make almost all of the `std::rt` private. Specifically, the following items are no longer part of its API: * DEFAULT_ERROR_CODE * backtrace * unwind * args * at_exit * cleanup * heap (this is just alloc::heap) * min_stack * util The module is now tagged as `#[doc(hidden)]` as the only purpose it's serve is an entry point for the `panic!` macro via the `begin_unwind` and `begin_unwind_fmt` reexports.
1 parent 192c375 commit f4be202

33 files changed

+272
-431
lines changed

src/doc/nomicon/destructors.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@ this is totally fine.
2626
For instance, a custom implementation of `Box` might write `Drop` like this:
2727

2828
```rust
29-
#![feature(heap_api, core_intrinsics, unique)]
29+
#![feature(alloc, heap_api, core_intrinsics, unique)]
30+
31+
extern crate alloc;
3032

31-
use std::rt::heap;
3233
use std::ptr::Unique;
3334
use std::intrinsics::drop_in_place;
3435
use std::mem;
3536

37+
use alloc::heap;
38+
3639
struct Box<T>{ ptr: Unique<T> }
3740

3841
impl<T> Drop for Box<T> {
@@ -45,6 +48,7 @@ impl<T> Drop for Box<T> {
4548
}
4649
}
4750
}
51+
# fn main() {}
4852
```
4953

5054
and this works fine because when Rust goes to drop the `ptr` field it just sees
@@ -54,13 +58,16 @@ use-after-free the `ptr` because when drop exits, it becomes inacessible.
5458
However this wouldn't work:
5559

5660
```rust
57-
#![feature(heap_api, core_intrinsics, unique)]
61+
#![feature(alloc, heap_api, core_intrinsics, unique)]
62+
63+
extern crate alloc;
5864

59-
use std::rt::heap;
6065
use std::ptr::Unique;
6166
use std::intrinsics::drop_in_place;
6267
use std::mem;
6368

69+
use alloc::heap;
70+
6471
struct Box<T>{ ptr: Unique<T> }
6572

6673
impl<T> Drop for Box<T> {
@@ -87,6 +94,7 @@ impl<T> Drop for SuperBox<T> {
8794
}
8895
}
8996
}
97+
# fn main() {}
9098
```
9199

92100
After we deallocate the `box`'s ptr in SuperBox's destructor, Rust will
@@ -129,13 +137,16 @@ The classic safe solution to overriding recursive drop and allowing moving out
129137
of Self during `drop` is to use an Option:
130138

131139
```rust
132-
#![feature(heap_api, core_intrinsics, unique)]
140+
#![feature(alloc, heap_api, core_intrinsics, unique)]
141+
142+
extern crate alloc;
133143

134-
use std::rt::heap;
135144
use std::ptr::Unique;
136145
use std::intrinsics::drop_in_place;
137146
use std::mem;
138147

148+
use alloc::heap;
149+
139150
struct Box<T>{ ptr: Unique<T> }
140151

141152
impl<T> Drop for Box<T> {
@@ -165,6 +176,7 @@ impl<T> Drop for SuperBox<T> {
165176
}
166177
}
167178
}
179+
# fn main() {}
168180
```
169181

170182
However this has fairly odd semantics: you're saying that a field that *should*

src/doc/nomicon/vec-alloc.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ This is perfectly fine because we already have `cap == 0` as our sentinel for no
99
allocation. We don't even need to handle it specially in almost any code because
1010
we usually need to check if `cap > len` or `len > 0` anyway. The traditional
1111
Rust value to put here is `0x01`. The standard library actually exposes this
12-
as `std::rt::heap::EMPTY`. There are quite a few places where we'll
12+
as `alloc::heap::EMPTY`. There are quite a few places where we'll
1313
want to use `heap::EMPTY` because there's no real allocation to talk about but
1414
`null` would make the compiler do bad things.
1515

@@ -20,11 +20,12 @@ the `heap` API anyway, so let's just get that dependency over with.
2020
So:
2121

2222
```rust,ignore
23-
#![feature(heap_api)]
23+
#![feature(alloc, heap_api)]
2424
25-
use std::rt::heap::EMPTY;
2625
use std::mem;
2726
27+
use alloc::heap::EMPTY;
28+
2829
impl<T> Vec<T> {
2930
fn new() -> Self {
3031
assert!(mem::size_of::<T>() != 0, "We're not ready to handle ZSTs");

src/doc/nomicon/vec-final.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22

33
```rust
44
#![feature(unique)]
5-
#![feature(heap_api)]
5+
#![feature(alloc, heap_api)]
6+
7+
extern crate alloc;
68

79
use std::ptr::{Unique, self};
8-
use std::rt::heap;
910
use std::mem;
1011
use std::ops::{Deref, DerefMut};
1112
use std::marker::PhantomData;
1213

13-
14-
15-
14+
use alloc::heap;
1615

1716
struct RawVec<T> {
1817
ptr: Unique<T>,

src/libarena/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ use std::marker;
4949
use std::mem;
5050
use std::ptr;
5151
use std::rc::Rc;
52-
use std::rt::heap::{allocate, deallocate};
52+
53+
use alloc::heap::{allocate, deallocate};
5354

5455
// The way arena uses arrays is really deeply awful. The arrays are
5556
// allocated, and have capacities reserved, but the fill for the array

src/liblog/lib.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@
174174
#![feature(box_syntax)]
175175
#![feature(const_fn)]
176176
#![feature(iter_cmp)]
177-
#![feature(rt)]
178177
#![feature(staged_api)]
179178
#![feature(static_mutex)]
180179

@@ -185,7 +184,6 @@ use std::io::prelude::*;
185184
use std::mem;
186185
use std::env;
187186
use std::ptr;
188-
use std::rt;
189187
use std::slice;
190188
use std::sync::{Once, StaticMutex};
191189

@@ -292,7 +290,6 @@ pub fn log(level: u32, loc: &'static LogLocation, args: fmt::Arguments) {
292290
let _g = LOCK.lock();
293291
match FILTER as usize {
294292
0 => {}
295-
1 => panic!("cannot log after main thread has exited"),
296293
n => {
297294
let filter = mem::transmute::<_, &String>(n);
298295
if !args.to_string().contains(filter) {
@@ -385,9 +382,6 @@ pub fn mod_enabled(level: u32, module: &str) -> bool {
385382
let _g = LOCK.lock();
386383
unsafe {
387384
assert!(DIRECTIVES as usize != 0);
388-
assert!(DIRECTIVES as usize != 1,
389-
"cannot log after the main thread has exited");
390-
391385
enabled(level, module, (*DIRECTIVES).iter())
392386
}
393387
}
@@ -442,19 +436,6 @@ fn init() {
442436

443437
assert!(DIRECTIVES.is_null());
444438
DIRECTIVES = Box::into_raw(box directives);
445-
446-
// Schedule the cleanup for the globals for when the runtime exits.
447-
let _ = rt::at_exit(move || {
448-
let _g = LOCK.lock();
449-
assert!(!DIRECTIVES.is_null());
450-
let _directives = Box::from_raw(DIRECTIVES);
451-
DIRECTIVES = 1 as *mut _;
452-
453-
if !FILTER.is_null() {
454-
let _filter = Box::from_raw(FILTER);
455-
FILTER = 1 as *mut _;
456-
}
457-
});
458439
}
459440
}
460441

src/libstd/collections/hash/table.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,20 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use self::BucketState::*;
11+
use alloc::heap::{allocate, deallocate, EMPTY};
1212

13-
use clone::Clone;
1413
use cmp;
1514
use hash::{Hash, Hasher};
16-
use iter::{Iterator, ExactSizeIterator};
17-
use marker::{Copy, Send, Sync, Sized, self};
15+
use marker;
1816
use mem::{align_of, size_of};
1917
use mem;
2018
use num::wrapping::OverflowingOps;
21-
use ops::{Deref, DerefMut, Drop};
22-
use option::Option;
23-
use option::Option::{Some, None};
19+
use ops::{Deref, DerefMut};
2420
use ptr::{self, Unique};
25-
use rt::heap::{allocate, deallocate, EMPTY};
2621
use collections::hash_state::HashState;
2722

23+
use self::BucketState::*;
24+
2825
const EMPTY_BUCKET: u64 = 0;
2926

3027
/// The raw hashtable, providing safe-ish access to the unzipped and highly

src/libstd/io/lazy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use prelude::v1::*;
1212

1313
use cell::Cell;
1414
use ptr;
15-
use rt;
1615
use sync::{StaticMutex, Arc};
16+
use sys_common;
1717

1818
pub struct Lazy<T> {
1919
lock: StaticMutex,
@@ -51,7 +51,7 @@ impl<T: Send + Sync + 'static> Lazy<T> {
5151
// `Arc` allocation in our own internal box (it will get deallocated by
5252
// the at exit handler). Otherwise we just return the freshly allocated
5353
// `Arc`.
54-
let registered = rt::at_exit(move || {
54+
let registered = sys_common::at_exit(move || {
5555
let g = self.lock.lock();
5656
let ptr = self.ptr.get();
5757
self.ptr.set(1 as *mut _);

src/libstd/panicking.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ use io::prelude::*;
1313

1414
use any::Any;
1515
use cell::RefCell;
16-
use rt::{backtrace, unwind};
1716
use sys::stdio::Stderr;
17+
use sys_common::backtrace;
1818
use sys_common::thread_info;
19+
use sys_common::unwind;
1920

2021
thread_local! {
2122
pub static LOCAL_STDERR: RefCell<Option<Box<Write + Send>>> = {

src/libstd/process.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ impl Child {
582582
/// to run.
583583
#[stable(feature = "rust1", since = "1.0.0")]
584584
pub fn exit(code: i32) -> ! {
585-
::rt::cleanup();
585+
::sys_common::cleanup();
586586
::sys::os::exit(code)
587587
}
588588

src/libstd/rt.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! Runtime services
12+
//!
13+
//! The `rt` module provides a narrow set of runtime services,
14+
//! including the global heap (exported in `heap`) and unwinding and
15+
//! backtrace support. The APIs in this module are highly unstable,
16+
//! and should be considered as private implementation details for the
17+
//! time being.
18+
19+
#![unstable(feature = "rt",
20+
reason = "this public module should not exist and is highly likely \
21+
to disappear",
22+
issue = "0")]
23+
#![doc(hidden)]
24+
25+
use borrow::ToOwned;
26+
use mem;
27+
use sys;
28+
use sys_common::thread_info::{self, NewThread};
29+
use sys_common;
30+
use thread::{self, Thread};
31+
32+
// Reexport some of our utilities which are expected by other crates.
33+
pub use sys_common::unwind::{begin_unwind, begin_unwind_fmt};
34+
35+
#[cfg(not(test))]
36+
#[lang = "start"]
37+
fn lang_start(main: *const u8, argc: isize, argv: *const *const u8) -> isize {
38+
sys::init();
39+
40+
let failed = unsafe {
41+
let main_guard = sys::thread::guard::init();
42+
sys::stack_overflow::init();
43+
44+
// Next, set up the current Thread with the guard information we just
45+
// created. Note that this isn't necessary in general for new threads,
46+
// but we just do this to name the main thread and to give it correct
47+
// info about the stack bounds.
48+
let thread: Thread = NewThread::new(Some("<main>".to_owned()));
49+
thread_info::set(main_guard, thread);
50+
51+
// Store our args if necessary in a squirreled away location
52+
sys_common::args::init(argc, argv);
53+
54+
// Let's run some code!
55+
let res = thread::catch_panic(mem::transmute::<_, fn()>(main));
56+
sys_common::cleanup();
57+
res.is_err()
58+
};
59+
60+
if failed {
61+
101
62+
} else {
63+
0
64+
}
65+
}

0 commit comments

Comments
 (0)