Skip to content

Commit be29631

Browse files
authored
Rollup merge of rust-lang#89104 - Mark-Simulacrum:spawn-unchecked, r=nagisa,bjorn3
Simplify scoped_thread Avoids a bunch of manual pointer manipulation in favor of using the appropriate std API.
2 parents 6b12f3e + 0222556 commit be29631

File tree

2 files changed

+6
-19
lines changed

2 files changed

+6
-19
lines changed

compiler/rustc_interface/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![feature(bool_to_option)]
22
#![feature(box_patterns)]
33
#![feature(internal_output_capture)]
4+
#![feature(thread_spawn_unchecked)]
45
#![feature(nll)]
56
#![feature(once_cell)]
67
#![recursion_limit = "256"]

compiler/rustc_interface/src/util.rs

+5-19
Original file line numberDiff line numberDiff line change
@@ -115,25 +115,11 @@ fn get_stack_size() -> Option<usize> {
115115
/// for `'static` bounds.
116116
#[cfg(not(parallel_compiler))]
117117
pub fn scoped_thread<F: FnOnce() -> R + Send, R: Send>(cfg: thread::Builder, f: F) -> R {
118-
struct Ptr(*mut ());
119-
unsafe impl Send for Ptr {}
120-
unsafe impl Sync for Ptr {}
121-
122-
let mut f = Some(f);
123-
let run = Ptr(&mut f as *mut _ as *mut ());
124-
let mut result = None;
125-
let result_ptr = Ptr(&mut result as *mut _ as *mut ());
126-
127-
let thread = cfg.spawn(move || {
128-
let _ = (&run, &result_ptr);
129-
let run = unsafe { (*(run.0 as *mut Option<F>)).take().unwrap() };
130-
let result = unsafe { &mut *(result_ptr.0 as *mut Option<R>) };
131-
*result = Some(run());
132-
});
133-
134-
match thread.unwrap().join() {
135-
Ok(()) => result.unwrap(),
136-
Err(p) => panic::resume_unwind(p),
118+
// SAFETY: join() is called immediately, so any closure captures are still
119+
// alive.
120+
match unsafe { cfg.spawn_unchecked(f) }.unwrap().join() {
121+
Ok(v) => v,
122+
Err(e) => panic::resume_unwind(e),
137123
}
138124
}
139125

0 commit comments

Comments
 (0)