Skip to content

Commit 2dac168

Browse files
authored
Unrolled build for rust-lang#129667
Rollup merge of rust-lang#129667 - dev-ardi:rustc_driver-cleanup, r=michaelwoerister Rustc driver cleanup This adds a few comments to the driver to clarify a bit what's happening and does some cleanup.
2 parents ac77e88 + c35e01e commit 2dac168

File tree

3 files changed

+40
-30
lines changed

3 files changed

+40
-30
lines changed

compiler/rustc_driver_impl/src/lib.rs

+36-29
Original file line numberDiff line numberDiff line change
@@ -393,13 +393,17 @@ fn run_compiler(
393393

394394
let linker = compiler.enter(|queries| {
395395
let early_exit = || early_exit().map(|_| None);
396+
397+
// Parse the crate root source code (doesn't parse submodules yet)
398+
// Everything else is parsed during macro expansion.
396399
queries.parse()?;
397400

398-
if let Some(ppm) = &sess.opts.pretty {
399-
if ppm.needs_ast_map() {
401+
// If pretty printing is requested: Figure out the representation, print it and exit
402+
if let Some(pp_mode) = sess.opts.pretty {
403+
if pp_mode.needs_ast_map() {
400404
queries.global_ctxt()?.enter(|tcx| {
401405
tcx.ensure().early_lint_checks(());
402-
pretty::print(sess, *ppm, pretty::PrintExtra::NeedsAstMap { tcx });
406+
pretty::print(sess, pp_mode, pretty::PrintExtra::NeedsAstMap { tcx });
403407
Ok(())
404408
})?;
405409

@@ -410,7 +414,7 @@ fn run_compiler(
410414
let krate = queries.parse()?;
411415
pretty::print(
412416
sess,
413-
*ppm,
417+
pp_mode,
414418
pretty::PrintExtra::AfterParsing { krate: &*krate.borrow() },
415419
);
416420
}
@@ -465,12 +469,8 @@ fn run_compiler(
465469
linker.link(sess, codegen_backend)?
466470
}
467471

468-
if sess.opts.unstable_opts.print_fuel.is_some() {
469-
eprintln!(
470-
"Fuel used by {}: {}",
471-
sess.opts.unstable_opts.print_fuel.as_ref().unwrap(),
472-
sess.print_fuel.load(Ordering::SeqCst)
473-
);
472+
if let Some(fuel) = sess.opts.unstable_opts.print_fuel.as_deref() {
473+
eprintln!("Fuel used by {}: {}", fuel, sess.print_fuel.load(Ordering::SeqCst));
474474
}
475475

476476
Ok(())
@@ -487,36 +487,43 @@ fn make_output(matches: &getopts::Matches) -> (Option<PathBuf>, Option<OutFileNa
487487
(odir, ofile)
488488
}
489489

490-
// Extract input (string or file and optional path) from matches.
490+
/// Extract input (string or file and optional path) from matches.
491+
/// This handles reading from stdin if `-` is provided.
491492
fn make_input(
492493
early_dcx: &EarlyDiagCtxt,
493494
free_matches: &[String],
494495
) -> Result<Option<Input>, ErrorGuaranteed> {
495-
let [ifile] = free_matches else { return Ok(None) };
496-
if ifile == "-" {
497-
let mut src = String::new();
498-
if io::stdin().read_to_string(&mut src).is_err() {
499-
// Immediately stop compilation if there was an issue reading
500-
// the input (for example if the input stream is not UTF-8).
501-
let reported =
502-
early_dcx.early_err("couldn't read from stdin, as it did not contain valid UTF-8");
503-
return Err(reported);
504-
}
505-
if let Ok(path) = env::var("UNSTABLE_RUSTDOC_TEST_PATH") {
496+
let [input_file] = free_matches else { return Ok(None) };
497+
498+
if input_file != "-" {
499+
// Normal `Input::File`
500+
return Ok(Some(Input::File(PathBuf::from(input_file))));
501+
}
502+
503+
// read from stdin as `Input::Str`
504+
let mut input = String::new();
505+
if io::stdin().read_to_string(&mut input).is_err() {
506+
// Immediately stop compilation if there was an issue reading
507+
// the input (for example if the input stream is not UTF-8).
508+
let reported =
509+
early_dcx.early_err("couldn't read from stdin, as it did not contain valid UTF-8");
510+
return Err(reported);
511+
}
512+
513+
let name = match env::var("UNSTABLE_RUSTDOC_TEST_PATH") {
514+
Ok(path) => {
506515
let line = env::var("UNSTABLE_RUSTDOC_TEST_LINE").expect(
507516
"when UNSTABLE_RUSTDOC_TEST_PATH is set \
508517
UNSTABLE_RUSTDOC_TEST_LINE also needs to be set",
509518
);
510519
let line = isize::from_str_radix(&line, 10)
511520
.expect("UNSTABLE_RUSTDOC_TEST_LINE needs to be an number");
512-
let file_name = FileName::doc_test_source_code(PathBuf::from(path), line);
513-
Ok(Some(Input::Str { name: file_name, input: src }))
514-
} else {
515-
Ok(Some(Input::Str { name: FileName::anon_source_code(&src), input: src }))
521+
FileName::doc_test_source_code(PathBuf::from(path), line)
516522
}
517-
} else {
518-
Ok(Some(Input::File(PathBuf::from(ifile))))
519-
}
523+
Err(_) => FileName::anon_source_code(&input),
524+
};
525+
526+
Ok(Some(Input::Str { name, input }))
520527
}
521528

522529
/// Whether to stop or continue compilation.

compiler/rustc_parse/src/parser/item.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ impl<'a> Parser<'a> {
5151
}
5252

5353
/// Parses the contents of a module (inner attributes followed by module items).
54-
/// We exit once we hit `term`
54+
/// We exit once we hit `term` which can be either
55+
/// - EOF (for files)
56+
/// - `}` for mod items
5557
pub fn parse_mod(
5658
&mut self,
5759
term: &TokenKind,

compiler/rustc_session/src/config.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2893,6 +2893,7 @@ pub enum PpHirMode {
28932893
}
28942894

28952895
#[derive(Copy, Clone, PartialEq, Debug)]
2896+
/// Pretty print mode
28962897
pub enum PpMode {
28972898
/// Options that print the source code, i.e.
28982899
/// `-Zunpretty=normal` and `-Zunpretty=expanded`

0 commit comments

Comments
 (0)