about summary refs log tree commit diff
path: root/compiler/rustc_driver_impl/src
diff options
context:
space:
mode:
authorOrion Gonzalez <ardi@ardis.dev>2024-08-27 18:46:44 +0200
committerOrion Gonzalez <ardi@ardis.dev>2024-08-28 13:03:18 +0200
commitb218623ea030885e88730413c61e3d47ed2905da (patch)
tree750f44e1800ab198972e64cb12e59e8832376669 /compiler/rustc_driver_impl/src
parentddcb073c53db68500e31874529595e6c7658b670 (diff)
downloadrust-b218623ea030885e88730413c61e3d47ed2905da.tar.gz
rust-b218623ea030885e88730413c61e3d47ed2905da.zip
cleanup make_input
Diffstat (limited to 'compiler/rustc_driver_impl/src')
-rw-r--r--compiler/rustc_driver_impl/src/lib.rs45
1 files changed, 26 insertions, 19 deletions
diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs
index 540523aa7b4..76bff6a87f3 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.