Skip to content

Commit b218623

Browse files
author
Orion Gonzalez
committed
cleanup make_input
1 parent ddcb073 commit b218623

File tree

1 file changed

+26
-19
lines changed
  • compiler/rustc_driver_impl/src

1 file changed

+26
-19
lines changed

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -481,36 +481,43 @@ fn make_output(matches: &getopts::Matches) -> (Option<PathBuf>, Option<OutFileNa
481481
(odir, ofile)
482482
}
483483

484-
// Extract input (string or file and optional path) from matches.
484+
/// Extract input (string or file and optional path) from matches.
485+
/// This handles reading from stdin if `-` is provided.
485486
fn make_input(
486487
early_dcx: &EarlyDiagCtxt,
487488
free_matches: &[String],
488489
) -> Result<Option<Input>, ErrorGuaranteed> {
489-
let [ifile] = free_matches else { return Ok(None) };
490-
if ifile == "-" {
491-
let mut src = String::new();
492-
if io::stdin().read_to_string(&mut src).is_err() {
493-
// Immediately stop compilation if there was an issue reading
494-
// the input (for example if the input stream is not UTF-8).
495-
let reported =
496-
early_dcx.early_err("couldn't read from stdin, as it did not contain valid UTF-8");
497-
return Err(reported);
498-
}
499-
if let Ok(path) = env::var("UNSTABLE_RUSTDOC_TEST_PATH") {
490+
let [input_file] = free_matches else { return Ok(None) };
491+
492+
if input_file != "-" {
493+
// Normal `Input::File`
494+
return Ok(Some(Input::File(PathBuf::from(input_file))));
495+
}
496+
497+
// read from stdin as `Input::Str`
498+
let mut input = String::new();
499+
if io::stdin().read_to_string(&mut input).is_err() {
500+
// Immediately stop compilation if there was an issue reading
501+
// the input (for example if the input stream is not UTF-8).
502+
let reported =
503+
early_dcx.early_err("couldn't read from stdin, as it did not contain valid UTF-8");
504+
return Err(reported);
505+
}
506+
507+
let name = match env::var("UNSTABLE_RUSTDOC_TEST_PATH") {
508+
Ok(path) => {
500509
let line = env::var("UNSTABLE_RUSTDOC_TEST_LINE").expect(
501510
"when UNSTABLE_RUSTDOC_TEST_PATH is set \
502511
UNSTABLE_RUSTDOC_TEST_LINE also needs to be set",
503512
);
504513
let line = isize::from_str_radix(&line, 10)
505514
.expect("UNSTABLE_RUSTDOC_TEST_LINE needs to be an number");
506-
let file_name = FileName::doc_test_source_code(PathBuf::from(path), line);
507-
Ok(Some(Input::Str { name: file_name, input: src }))
508-
} else {
509-
Ok(Some(Input::Str { name: FileName::anon_source_code(&src), input: src }))
515+
FileName::doc_test_source_code(PathBuf::from(path), line)
510516
}
511-
} else {
512-
Ok(Some(Input::File(PathBuf::from(ifile))))
513-
}
517+
Err(_) => FileName::anon_source_code(&input),
518+
};
519+
520+
Ok(Some(Input::Str { name, input }))
514521
}
515522

516523
/// Whether to stop or continue compilation.

0 commit comments

Comments
 (0)