Skip to content

Commit 74cbeef

Browse files
Rollup merge of rust-lang#102960 - Dylan-DPC:rollup-ud1dlfl, r=Dylan-DPC
Rollup of 7 pull requests Successful merges: - rust-lang#102623 (translation: eager translation) - rust-lang#102769 (Clean up rustdoc startup) - rust-lang#102830 (Unify `tcx.constness` query and param env constness checks) - rust-lang#102847 (impl AsFd and AsRawFd for io::{Stdin, Stdout, Stderr}, not the sys versions) - rust-lang#102883 (Fix stabilization of `feature(half_open_range_patterns)`) - rust-lang#102936 (rustdoc: remove unused CSS `nav.sum`) - rust-lang#102940 (Update books) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 909d03d + 6f1de0b commit 74cbeef

File tree

8 files changed

+163
-219
lines changed

8 files changed

+163
-219
lines changed

compiler/rustc_interface/src/interface.rs

+48-54
Original file line numberDiff line numberDiff line change
@@ -276,67 +276,61 @@ pub struct Config {
276276
pub registry: Registry,
277277
}
278278

279-
pub fn create_compiler_and_run<R>(config: Config, f: impl FnOnce(&Compiler) -> R) -> R {
280-
crate::callbacks::setup_callbacks();
281-
282-
let registry = &config.registry;
283-
let (mut sess, codegen_backend) = util::create_session(
284-
config.opts,
285-
config.crate_cfg,
286-
config.crate_check_cfg,
287-
config.diagnostic_output,
288-
config.file_loader,
289-
config.input_path.clone(),
290-
config.lint_caps,
291-
config.make_codegen_backend,
292-
registry.clone(),
293-
);
294-
295-
if let Some(parse_sess_created) = config.parse_sess_created {
296-
parse_sess_created(
297-
&mut Lrc::get_mut(&mut sess)
298-
.expect("create_session() should never share the returned session")
299-
.parse_sess,
300-
);
301-
}
302-
303-
let temps_dir = sess.opts.unstable_opts.temps_dir.as_ref().map(|o| PathBuf::from(&o));
304-
305-
let compiler = Compiler {
306-
sess,
307-
codegen_backend,
308-
input: config.input,
309-
input_path: config.input_path,
310-
output_dir: config.output_dir,
311-
output_file: config.output_file,
312-
temps_dir,
313-
register_lints: config.register_lints,
314-
override_queries: config.override_queries,
315-
};
316-
317-
rustc_span::with_source_map(compiler.sess.parse_sess.clone_source_map(), move || {
318-
let r = {
319-
let _sess_abort_error = OnDrop(|| {
320-
compiler.sess.finish_diagnostics(registry);
321-
});
322-
323-
f(&compiler)
324-
};
325-
326-
let prof = compiler.sess.prof.clone();
327-
prof.generic_activity("drop_compiler").run(move || drop(compiler));
328-
r
329-
})
330-
}
331-
332279
// JUSTIFICATION: before session exists, only config
333280
#[allow(rustc::bad_opt_access)]
334281
pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Send) -> R {
335282
trace!("run_compiler");
336283
util::run_in_thread_pool_with_globals(
337284
config.opts.edition,
338285
config.opts.unstable_opts.threads,
339-
|| create_compiler_and_run(config, f),
286+
|| {
287+
crate::callbacks::setup_callbacks();
288+
289+
let registry = &config.registry;
290+
let (mut sess, codegen_backend) = util::create_session(
291+
config.opts,
292+
config.crate_cfg,
293+
config.crate_check_cfg,
294+
config.diagnostic_output,
295+
config.file_loader,
296+
config.input_path.clone(),
297+
config.lint_caps,
298+
config.make_codegen_backend,
299+
registry.clone(),
300+
);
301+
302+
if let Some(parse_sess_created) = config.parse_sess_created {
303+
parse_sess_created(&mut sess.parse_sess);
304+
}
305+
306+
let temps_dir = sess.opts.unstable_opts.temps_dir.as_ref().map(|o| PathBuf::from(&o));
307+
308+
let compiler = Compiler {
309+
sess: Lrc::new(sess),
310+
codegen_backend: Lrc::new(codegen_backend),
311+
input: config.input,
312+
input_path: config.input_path,
313+
output_dir: config.output_dir,
314+
output_file: config.output_file,
315+
temps_dir,
316+
register_lints: config.register_lints,
317+
override_queries: config.override_queries,
318+
};
319+
320+
rustc_span::with_source_map(compiler.sess.parse_sess.clone_source_map(), move || {
321+
let r = {
322+
let _sess_abort_error = OnDrop(|| {
323+
compiler.sess.finish_diagnostics(registry);
324+
});
325+
326+
f(&compiler)
327+
};
328+
329+
let prof = compiler.sess.prof.clone();
330+
prof.generic_activity("drop_compiler").run(move || drop(compiler));
331+
r
332+
})
333+
},
340334
)
341335
}
342336

compiler/rustc_interface/src/util.rs

+21-24
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use rustc_codegen_ssa::traits::CodegenBackend;
55
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
66
#[cfg(parallel_compiler)]
77
use rustc_data_structures::jobserver;
8-
use rustc_data_structures::sync::Lrc;
98
use rustc_errors::registry::Registry;
109
#[cfg(parallel_compiler)]
1110
use rustc_middle::ty::tls;
@@ -73,7 +72,7 @@ pub fn create_session(
7372
Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>,
7473
>,
7574
descriptions: Registry,
76-
) -> (Lrc<Session>, Lrc<Box<dyn CodegenBackend>>) {
75+
) -> (Session, Box<dyn CodegenBackend>) {
7776
let codegen_backend = if let Some(make_codegen_backend) = make_codegen_backend {
7877
make_codegen_backend(&sopts)
7978
} else {
@@ -121,7 +120,7 @@ pub fn create_session(
121120
sess.parse_sess.config = cfg;
122121
sess.parse_sess.check_config = check_cfg;
123122

124-
(Lrc::new(sess), Lrc::new(codegen_backend))
123+
(sess, codegen_backend)
125124
}
126125

127126
const STACK_SIZE: usize = 8 * 1024 * 1024;
@@ -132,33 +131,31 @@ fn get_stack_size() -> Option<usize> {
132131
env::var_os("RUST_MIN_STACK").is_none().then_some(STACK_SIZE)
133132
}
134133

135-
/// Like a `thread::Builder::spawn` followed by a `join()`, but avoids the need
136-
/// for `'static` bounds.
137134
#[cfg(not(parallel_compiler))]
138-
fn scoped_thread<F: FnOnce() -> R + Send, R: Send>(cfg: thread::Builder, f: F) -> R {
139-
// SAFETY: join() is called immediately, so any closure captures are still
140-
// alive.
141-
match unsafe { cfg.spawn_unchecked(f) }.unwrap().join() {
142-
Ok(v) => v,
143-
Err(e) => panic::resume_unwind(e),
144-
}
145-
}
146-
147-
#[cfg(not(parallel_compiler))]
148-
pub fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
135+
pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
149136
edition: Edition,
150137
_threads: usize,
151138
f: F,
152139
) -> R {
153-
let mut cfg = thread::Builder::new().name("rustc".to_string());
154-
155-
if let Some(size) = get_stack_size() {
156-
cfg = cfg.stack_size(size);
157-
}
140+
// The thread pool is a single thread in the non-parallel compiler.
141+
thread::scope(|s| {
142+
let mut builder = thread::Builder::new().name("rustc".to_string());
143+
if let Some(size) = get_stack_size() {
144+
builder = builder.stack_size(size);
145+
}
158146

159-
let main_handler = move || rustc_span::create_session_globals_then(edition, f);
147+
// `unwrap` is ok here because `spawn_scoped` only panics if the thread
148+
// name contains null bytes.
149+
let r = builder
150+
.spawn_scoped(s, move || rustc_span::create_session_globals_then(edition, f))
151+
.unwrap()
152+
.join();
160153

161-
scoped_thread(cfg, main_handler)
154+
match r {
155+
Ok(v) => v,
156+
Err(e) => panic::resume_unwind(e),
157+
}
158+
})
162159
}
163160

164161
/// Creates a new thread and forwards information in thread locals to it.
@@ -177,7 +174,7 @@ unsafe fn handle_deadlock() {
177174
}
178175

179176
#[cfg(parallel_compiler)]
180-
pub fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
177+
pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
181178
edition: Edition,
182179
threads: usize,
183180
f: F,

library/std/src/os/fd/owned.rs

+52
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use super::raw::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
77
use crate::fmt;
88
use crate::fs;
9+
use crate::io;
910
use crate::marker::PhantomData;
1011
use crate::mem::forget;
1112
#[cfg(not(any(target_arch = "wasm32", target_env = "sgx")))]
@@ -385,3 +386,54 @@ impl<T: AsFd> AsFd for Box<T> {
385386
(**self).as_fd()
386387
}
387388
}
389+
390+
#[stable(feature = "io_safety", since = "1.63.0")]
391+
impl AsFd for io::Stdin {
392+
#[inline]
393+
fn as_fd(&self) -> BorrowedFd<'_> {
394+
unsafe { BorrowedFd::borrow_raw(0) }
395+
}
396+
}
397+
398+
#[stable(feature = "io_safety", since = "1.63.0")]
399+
impl<'a> AsFd for io::StdinLock<'a> {
400+
#[inline]
401+
fn as_fd(&self) -> BorrowedFd<'_> {
402+
// SAFETY: user code should not close stdin out from under the standard library
403+
unsafe { BorrowedFd::borrow_raw(0) }
404+
}
405+
}
406+
407+
#[stable(feature = "io_safety", since = "1.63.0")]
408+
impl AsFd for io::Stdout {
409+
#[inline]
410+
fn as_fd(&self) -> BorrowedFd<'_> {
411+
unsafe { BorrowedFd::borrow_raw(1) }
412+
}
413+
}
414+
415+
#[stable(feature = "io_safety", since = "1.63.0")]
416+
impl<'a> AsFd for io::StdoutLock<'a> {
417+
#[inline]
418+
fn as_fd(&self) -> BorrowedFd<'_> {
419+
// SAFETY: user code should not close stdout out from under the standard library
420+
unsafe { BorrowedFd::borrow_raw(1) }
421+
}
422+
}
423+
424+
#[stable(feature = "io_safety", since = "1.63.0")]
425+
impl AsFd for io::Stderr {
426+
#[inline]
427+
fn as_fd(&self) -> BorrowedFd<'_> {
428+
unsafe { BorrowedFd::borrow_raw(2) }
429+
}
430+
}
431+
432+
#[stable(feature = "io_safety", since = "1.63.0")]
433+
impl<'a> AsFd for io::StderrLock<'a> {
434+
#[inline]
435+
fn as_fd(&self) -> BorrowedFd<'_> {
436+
// SAFETY: user code should not close stderr out from under the standard library
437+
unsafe { BorrowedFd::borrow_raw(2) }
438+
}
439+
}

library/std/src/sys/unix/stdio.rs

+1-49
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::io::{self, IoSlice, IoSliceMut};
22
use crate::mem::ManuallyDrop;
3-
use crate::os::unix::io::{AsFd, BorrowedFd, FromRawFd};
3+
use crate::os::unix::io::FromRawFd;
44
use crate::sys::fd::FileDesc;
55

66
pub struct Stdin(());
@@ -91,51 +91,3 @@ pub const STDIN_BUF_SIZE: usize = crate::sys_common::io::DEFAULT_BUF_SIZE;
9191
pub fn panic_output() -> Option<impl io::Write> {
9292
Some(Stderr::new())
9393
}
94-
95-
#[stable(feature = "io_safety", since = "1.63.0")]
96-
impl AsFd for io::Stdin {
97-
#[inline]
98-
fn as_fd(&self) -> BorrowedFd<'_> {
99-
unsafe { BorrowedFd::borrow_raw(libc::STDIN_FILENO) }
100-
}
101-
}
102-
103-
#[stable(feature = "io_safety", since = "1.63.0")]
104-
impl<'a> AsFd for io::StdinLock<'a> {
105-
#[inline]
106-
fn as_fd(&self) -> BorrowedFd<'_> {
107-
unsafe { BorrowedFd::borrow_raw(libc::STDIN_FILENO) }
108-
}
109-
}
110-
111-
#[stable(feature = "io_safety", since = "1.63.0")]
112-
impl AsFd for io::Stdout {
113-
#[inline]
114-
fn as_fd(&self) -> BorrowedFd<'_> {
115-
unsafe { BorrowedFd::borrow_raw(libc::STDOUT_FILENO) }
116-
}
117-
}
118-
119-
#[stable(feature = "io_safety", since = "1.63.0")]
120-
impl<'a> AsFd for io::StdoutLock<'a> {
121-
#[inline]
122-
fn as_fd(&self) -> BorrowedFd<'_> {
123-
unsafe { BorrowedFd::borrow_raw(libc::STDOUT_FILENO) }
124-
}
125-
}
126-
127-
#[stable(feature = "io_safety", since = "1.63.0")]
128-
impl AsFd for io::Stderr {
129-
#[inline]
130-
fn as_fd(&self) -> BorrowedFd<'_> {
131-
unsafe { BorrowedFd::borrow_raw(libc::STDERR_FILENO) }
132-
}
133-
}
134-
135-
#[stable(feature = "io_safety", since = "1.63.0")]
136-
impl<'a> AsFd for io::StderrLock<'a> {
137-
#[inline]
138-
fn as_fd(&self) -> BorrowedFd<'_> {
139-
unsafe { BorrowedFd::borrow_raw(libc::STDERR_FILENO) }
140-
}
141-
}

0 commit comments

Comments
 (0)