Skip to content

Commit 8fbe046

Browse files
committed
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.
1 parent b73478b commit 8fbe046

File tree

1 file changed

+34
-38
lines changed
  • compiler/rustc_driver_impl/src

1 file changed

+34
-38
lines changed

compiler/rustc_driver_impl/src/lib.rs

+34-38
Original file line numberDiff line numberDiff line change
@@ -333,19 +333,12 @@ fn run_compiler(
333333
expanded_args: args,
334334
};
335335

336-
let has_input = match make_input(&default_early_dcx, &matches.free) {
337-
Err(reported) => return Err(reported),
338-
Ok(Some(input)) => {
336+
let has_input = match make_input(&default_early_dcx, &matches.free)? {
337+
Some(input) => {
339338
config.input = input;
340339
true // has input: normal compilation
341340
}
342-
Ok(None) => match matches.free.as_slice() {
343-
[] => false, // no input: we will exit early
344-
[_] => panic!("make_input should have provided valid inputs"),
345-
[fst, snd, ..] => default_early_dcx.early_fatal(format!(
346-
"multiple input filenames provided (first two filenames are `{fst}` and `{snd}`)"
347-
)),
348-
},
341+
None => false, // no input: we will exit early
349342
};
350343

351344
drop(default_early_dcx);
@@ -490,37 +483,40 @@ fn make_input(
490483
early_dcx: &EarlyDiagCtxt,
491484
free_matches: &[String],
492485
) -> Result<Option<Input>, ErrorGuaranteed> {
493-
let [input_file] = free_matches else { return Ok(None) };
494-
495-
if input_file != "-" {
496-
// Normal `Input::File`
497-
return Ok(Some(Input::File(PathBuf::from(input_file))));
498-
}
499-
500-
// read from stdin as `Input::Str`
501-
let mut input = String::new();
502-
if io::stdin().read_to_string(&mut input).is_err() {
503-
// Immediately stop compilation if there was an issue reading
504-
// the input (for example if the input stream is not UTF-8).
505-
let reported =
506-
early_dcx.early_err("couldn't read from stdin, as it did not contain valid UTF-8");
507-
return Err(reported);
508-
}
486+
match free_matches {
487+
[] => Ok(None), // no input: we will exit early,
488+
[ifile] if ifile == "-" => {
489+
// read from stdin as `Input::Str`
490+
let mut input = String::new();
491+
if io::stdin().read_to_string(&mut input).is_err() {
492+
// Immediately stop compilation if there was an issue reading
493+
// the input (for example if the input stream is not UTF-8).
494+
let reported = early_dcx
495+
.early_err("couldn't read from stdin, as it did not contain valid UTF-8");
496+
return Err(reported);
497+
}
509498

510-
let name = match env::var("UNSTABLE_RUSTDOC_TEST_PATH") {
511-
Ok(path) => {
512-
let line = env::var("UNSTABLE_RUSTDOC_TEST_LINE").expect(
513-
"when UNSTABLE_RUSTDOC_TEST_PATH is set \
499+
let name = match env::var("UNSTABLE_RUSTDOC_TEST_PATH") {
500+
Ok(path) => {
501+
let line = env::var("UNSTABLE_RUSTDOC_TEST_LINE").expect(
502+
"when UNSTABLE_RUSTDOC_TEST_PATH is set \
514503
UNSTABLE_RUSTDOC_TEST_LINE also needs to be set",
515-
);
516-
let line = isize::from_str_radix(&line, 10)
517-
.expect("UNSTABLE_RUSTDOC_TEST_LINE needs to be an number");
518-
FileName::doc_test_source_code(PathBuf::from(path), line)
519-
}
520-
Err(_) => FileName::anon_source_code(&input),
521-
};
504+
);
505+
let line = isize::from_str_radix(&line, 10)
506+
.expect("UNSTABLE_RUSTDOC_TEST_LINE needs to be an number");
507+
FileName::doc_test_source_code(PathBuf::from(path), line)
508+
}
509+
Err(_) => FileName::anon_source_code(&input),
510+
};
522511

523-
Ok(Some(Input::Str { name, input }))
512+
Ok(Some(Input::Str { name, input }))
513+
}
514+
[ifile] => Ok(Some(Input::File(PathBuf::from(ifile)))),
515+
_ => early_dcx.early_fatal(format!(
516+
"multiple input filenames provided (first two filenames are `{}` and `{}`)",
517+
free_matches[0], free_matches[1],
518+
)),
519+
}
524520
}
525521

526522
/// Whether to stop or continue compilation.

0 commit comments

Comments
 (0)