1
Fork 0

Let make_input immediately report an error for multiple input filenames

This allows simplifying the call site and make_input by using a single
match instead of two levels of if's.
This commit is contained in:
bjorn3 2023-11-26 18:16:23 +00:00
parent b73478b6ee
commit 8fbe0466f5

View file

@ -333,19 +333,12 @@ fn run_compiler(
expanded_args: args, expanded_args: args,
}; };
let has_input = match make_input(&default_early_dcx, &matches.free) { let has_input = match make_input(&default_early_dcx, &matches.free)? {
Err(reported) => return Err(reported), Some(input) => {
Ok(Some(input)) => {
config.input = input; config.input = input;
true // has input: normal compilation true // has input: normal compilation
} }
Ok(None) => match matches.free.as_slice() { None => false, // no input: we will exit early
[] => false, // no input: we will exit early
[_] => panic!("make_input should have provided valid inputs"),
[fst, snd, ..] => default_early_dcx.early_fatal(format!(
"multiple input filenames provided (first two filenames are `{fst}` and `{snd}`)"
)),
},
}; };
drop(default_early_dcx); drop(default_early_dcx);
@ -490,37 +483,40 @@ fn make_input(
early_dcx: &EarlyDiagCtxt, early_dcx: &EarlyDiagCtxt,
free_matches: &[String], free_matches: &[String],
) -> Result<Option<Input>, ErrorGuaranteed> { ) -> Result<Option<Input>, ErrorGuaranteed> {
let [input_file] = free_matches else { return Ok(None) }; match free_matches {
[] => Ok(None), // no input: we will exit early,
[ifile] if ifile == "-" => {
// 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);
}
if input_file != "-" { let name = match env::var("UNSTABLE_RUSTDOC_TEST_PATH") {
// Normal `Input::File` Ok(path) => {
return Ok(Some(Input::File(PathBuf::from(input_file)))); let line = env::var("UNSTABLE_RUSTDOC_TEST_LINE").expect(
} "when UNSTABLE_RUSTDOC_TEST_PATH is set \
// 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", UNSTABLE_RUSTDOC_TEST_LINE also needs to be set",
); );
let line = isize::from_str_radix(&line, 10) let line = isize::from_str_radix(&line, 10)
.expect("UNSTABLE_RUSTDOC_TEST_LINE needs to be an number"); .expect("UNSTABLE_RUSTDOC_TEST_LINE needs to be an number");
FileName::doc_test_source_code(PathBuf::from(path), line) FileName::doc_test_source_code(PathBuf::from(path), line)
} }
Err(_) => FileName::anon_source_code(&input), Err(_) => FileName::anon_source_code(&input),
}; };
Ok(Some(Input::Str { name, input })) Ok(Some(Input::Str { name, input }))
}
[ifile] => Ok(Some(Input::File(PathBuf::from(ifile)))),
_ => early_dcx.early_fatal(format!(
"multiple input filenames provided (first two filenames are `{}` and `{}`)",
free_matches[0], free_matches[1],
)),
}
} }
/// Whether to stop or continue compilation. /// Whether to stop or continue compilation.