Skip to content

Commit 9459806

Browse files
author
Orion Gonzalez
committed
cleanup make_input
1 parent 9ce759d commit 9459806

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
@@ -485,36 +485,43 @@ fn make_output(matches: &getopts::Matches) -> (Option<PathBuf>, Option<OutFileNa
485485
(odir, ofile)
486486
}
487487

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

520527
/// Whether to stop or continue compilation.

0 commit comments

Comments
 (0)