Skip to content

Commit 8e3d69f

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 4d296ea commit 8e3d69f

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

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -331,19 +331,12 @@ fn run_compiler(
331331
expanded_args: args,
332332
};
333333

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

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

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

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

524520
/// Whether to stop or continue compilation.

0 commit comments

Comments
 (0)