From ddcb073c53db68500e31874529595e6c7658b670 Mon Sep 17 00:00:00 2001
From: Orion Gonzalez <ardi@ardis.dev>
Date: Tue, 27 Aug 2024 18:28:59 +0200
Subject: [PATCH 1/4] replace is_some() -> unwrap with if let

---
 compiler/rustc_driver_impl/src/lib.rs | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs
index 2b7dc040f6411..540523aa7b450 100644
--- a/compiler/rustc_driver_impl/src/lib.rs
+++ b/compiler/rustc_driver_impl/src/lib.rs
@@ -463,12 +463,8 @@ fn run_compiler(
             linker.link(sess, codegen_backend)?
         }
 
-        if sess.opts.unstable_opts.print_fuel.is_some() {
-            eprintln!(
-                "Fuel used by {}: {}",
-                sess.opts.unstable_opts.print_fuel.as_ref().unwrap(),
-                sess.print_fuel.load(Ordering::SeqCst)
-            );
+        if let Some(fuel) = sess.opts.unstable_opts.print_fuel.as_deref() {
+            eprintln!("Fuel used by {}: {}", fuel, sess.print_fuel.load(Ordering::SeqCst));
         }
 
         Ok(())

From b218623ea030885e88730413c61e3d47ed2905da Mon Sep 17 00:00:00 2001
From: Orion Gonzalez <ardi@ardis.dev>
Date: Tue, 27 Aug 2024 18:46:44 +0200
Subject: [PATCH 2/4] cleanup make_input

---
 compiler/rustc_driver_impl/src/lib.rs | 45 ++++++++++++++++-----------
 1 file changed, 26 insertions(+), 19 deletions(-)

diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs
index 540523aa7b450..76bff6a87f32d 100644
--- a/compiler/rustc_driver_impl/src/lib.rs
+++ b/compiler/rustc_driver_impl/src/lib.rs
@@ -481,36 +481,43 @@ fn make_output(matches: &getopts::Matches) -> (Option<PathBuf>, Option<OutFileNa
     (odir, ofile)
 }
 
-// Extract input (string or file and optional path) from matches.
+/// Extract input (string or file and optional path) from matches.
+/// This handles reading from stdin if `-` is provided.
 fn make_input(
     early_dcx: &EarlyDiagCtxt,
     free_matches: &[String],
 ) -> Result<Option<Input>, ErrorGuaranteed> {
-    let [ifile] = free_matches else { return Ok(None) };
-    if ifile == "-" {
-        let mut src = String::new();
-        if io::stdin().read_to_string(&mut src).is_err() {
-            // Immediately stop compilation if there was an issue reading
-            // the input (for example if the input stream is not UTF-8).
-            let reported =
-                early_dcx.early_err("couldn't read from stdin, as it did not contain valid UTF-8");
-            return Err(reported);
-        }
-        if let Ok(path) = env::var("UNSTABLE_RUSTDOC_TEST_PATH") {
+    let [input_file] = free_matches else { return Ok(None) };
+
+    if input_file != "-" {
+        // Normal `Input::File`
+        return Ok(Some(Input::File(PathBuf::from(input_file))));
+    }
+
+    // read from stdin as `Input::Str`
+    let mut input = String::new();
+    if io::stdin().read_to_string(&mut input).is_err() {
+        // Immediately stop compilation if there was an issue reading
+        // the input (for example if the input stream is not UTF-8).
+        let reported =
+            early_dcx.early_err("couldn't read from stdin, as it did not contain valid UTF-8");
+        return Err(reported);
+    }
+
+    let name = match env::var("UNSTABLE_RUSTDOC_TEST_PATH") {
+        Ok(path) => {
             let line = env::var("UNSTABLE_RUSTDOC_TEST_LINE").expect(
                 "when UNSTABLE_RUSTDOC_TEST_PATH is set \
                                     UNSTABLE_RUSTDOC_TEST_LINE also needs to be set",
             );
             let line = isize::from_str_radix(&line, 10)
                 .expect("UNSTABLE_RUSTDOC_TEST_LINE needs to be an number");
-            let file_name = FileName::doc_test_source_code(PathBuf::from(path), line);
-            Ok(Some(Input::Str { name: file_name, input: src }))
-        } else {
-            Ok(Some(Input::Str { name: FileName::anon_source_code(&src), input: src }))
+            FileName::doc_test_source_code(PathBuf::from(path), line)
         }
-    } else {
-        Ok(Some(Input::File(PathBuf::from(ifile))))
-    }
+        Err(_) => FileName::anon_source_code(&input),
+    };
+
+    Ok(Some(Input::Str { name, input }))
 }
 
 /// Whether to stop or continue compilation.

From a007d349a1ccd6f998049e3acbf99e0df17a5c49 Mon Sep 17 00:00:00 2001
From: Orion Gonzalez <ardi@ardis.dev>
Date: Tue, 27 Aug 2024 20:04:47 +0200
Subject: [PATCH 3/4] clarify a few things

---
 compiler/rustc_driver_impl/src/lib.rs | 12 ++++++++----
 compiler/rustc_session/src/config.rs  |  1 +
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs
index 76bff6a87f32d..f77551475366c 100644
--- a/compiler/rustc_driver_impl/src/lib.rs
+++ b/compiler/rustc_driver_impl/src/lib.rs
@@ -391,13 +391,17 @@ fn run_compiler(
 
         let linker = compiler.enter(|queries| {
             let early_exit = || early_exit().map(|_| None);
+
+            // Parse the crate root source code (doesn't parse submodules yet)
+            // Everything else is parsed during macro expansion.
             queries.parse()?;
 
-            if let Some(ppm) = &sess.opts.pretty {
-                if ppm.needs_ast_map() {
+            // If pretty printing is requested: Figure out the representation, print it and exit
+            if let Some(pp_mode) = sess.opts.pretty {
+                if pp_mode.needs_ast_map() {
                     queries.global_ctxt()?.enter(|tcx| {
                         tcx.ensure().early_lint_checks(());
-                        pretty::print(sess, *ppm, pretty::PrintExtra::NeedsAstMap { tcx });
+                        pretty::print(sess, pp_mode, pretty::PrintExtra::NeedsAstMap { tcx });
                         Ok(())
                     })?;
 
@@ -408,7 +412,7 @@ fn run_compiler(
                     let krate = queries.parse()?;
                     pretty::print(
                         sess,
-                        *ppm,
+                        pp_mode,
                         pretty::PrintExtra::AfterParsing { krate: &*krate.borrow() },
                     );
                 }
diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs
index 95d171409d86d..4fb3702b05dc5 100644
--- a/compiler/rustc_session/src/config.rs
+++ b/compiler/rustc_session/src/config.rs
@@ -2893,6 +2893,7 @@ pub enum PpHirMode {
 }
 
 #[derive(Copy, Clone, PartialEq, Debug)]
+/// Pretty print mode
 pub enum PpMode {
     /// Options that print the source code, i.e.
     /// `-Zunpretty=normal` and `-Zunpretty=expanded`

From c35e01e48e4eb6850f20628dde49f1204c2242c8 Mon Sep 17 00:00:00 2001
From: Orion Gonzalez <ardi@ardis.dev>
Date: Tue, 27 Aug 2024 20:05:53 +0200
Subject: [PATCH 4/4] clarify what term can be

---
 compiler/rustc_parse/src/parser/item.rs | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs
index 8775d792c3d2b..7bdcac454558a 100644
--- a/compiler/rustc_parse/src/parser/item.rs
+++ b/compiler/rustc_parse/src/parser/item.rs
@@ -49,7 +49,9 @@ impl<'a> Parser<'a> {
     }
 
     /// Parses the contents of a module (inner attributes followed by module items).
-    /// We exit once we hit `term`
+    /// We exit once we hit `term` which can be either
+    /// - EOF (for files)
+    /// - `}` for mod items
     pub fn parse_mod(
         &mut self,
         term: &TokenKind,