Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up rustdoc startup #102769

Merged
merged 9 commits into from
Oct 19, 2022
24 changes: 9 additions & 15 deletions compiler/rustc_interface/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,33 +130,27 @@ fn get_stack_size() -> Option<usize> {
env::var_os("RUST_MIN_STACK").is_none().then_some(STACK_SIZE)
}

/// Like a `thread::Builder::spawn` followed by a `join()`, but avoids the need
/// for `'static` bounds.
#[cfg(not(parallel_compiler))]
fn scoped_thread<F: FnOnce() -> R + Send, R: Send>(cfg: thread::Builder, f: F) -> R {
// SAFETY: join() is called immediately, so any closure captures are still
// alive.
match unsafe { cfg.spawn_unchecked(f) }.unwrap().join() {
Ok(v) => v,
Err(e) => panic::resume_unwind(e),
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please never inline safe functions that contain unsafe code!

(Also, not as important here, but "single call site" is a really bad metric, unless you can find where other callsites were removed in the past or anything of the worst - often single-call-site functions are separate for semantic reasons!)

The purpose of such a function is to encapsulate the unsafety. Inlining could cause unsound interactions with surrounding code in the caller! And increases complexity in analyzing the correctness of the code, regardless of what you may think about some subject readability metric.

In the end this was fine because of the move to the new scoped threads, but this is pretty dangerous to do in the first place, and IMO should've been a PR on its own anyway (that has "scoped threads" or similar in the PR title, assuming going the full route to the new scoped thread API).


#[cfg(not(parallel_compiler))]
pub fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
edition: Edition,
_threads: usize,
f: F,
) -> R {
// The thread pool is a single thread in the non-parallel compiler.
let mut cfg = thread::Builder::new().name("rustc".to_string());

if let Some(size) = get_stack_size() {
cfg = cfg.stack_size(size);
}

let main_handler = move || rustc_span::create_session_globals_then(edition, f);
let f = move || rustc_span::create_session_globals_then(edition, f);

scoped_thread(cfg, main_handler)
// This avoids the need for `'static` bounds.
//
// SAFETY: join() is called immediately, so any closure captures are still alive.
match unsafe { cfg.spawn_unchecked(f) }.unwrap().join() {
nnethercote marked this conversation as resolved.
Show resolved Hide resolved
Ok(v) => v,
Err(e) => panic::resume_unwind(e),
}
}

/// Creates a new thread and forwards information in thread locals to it.
Expand Down