Skip to content

Commit b252924

Browse files
committed
Simplify make_input
1 parent 4a6cfc6 commit b252924

File tree

1 file changed

+21
-22
lines changed
  • compiler/rustc_driver/src

1 file changed

+21
-22
lines changed

compiler/rustc_driver/src/lib.rs

+21-22
Original file line numberDiff line numberDiff line change
@@ -235,21 +235,15 @@ fn run_compiler(
235235
registry: diagnostics_registry(),
236236
};
237237

238-
match make_input(&matches.free) {
239-
Some((input, input_file_path, input_err)) => {
240-
if let Some(err) = input_err {
241-
// Immediately stop compilation if there was an issue reading
242-
// the input (for example if the input stream is not UTF-8).
243-
early_error_no_abort(config.opts.error_format, &err.to_string());
244-
return Err(ErrorReported);
245-
}
246-
238+
match make_input(config.opts.error_format, &matches.free) {
239+
Err(ErrorReported) => return Err(ErrorReported),
240+
Ok(Some((input, input_file_path))) => {
247241
config.input = input;
248242
config.input_path = input_file_path;
249243

250244
callbacks.config(&mut config);
251245
}
252-
None => match matches.free.len() {
246+
Ok(None) => match matches.free.len() {
253247
0 => {
254248
callbacks.config(&mut config);
255249
interface::run_compiler(config, |compiler| {
@@ -469,19 +463,23 @@ fn make_output(matches: &getopts::Matches) -> (Option<PathBuf>, Option<PathBuf>)
469463
}
470464

471465
// Extract input (string or file and optional path) from matches.
472-
fn make_input(free_matches: &[String]) -> Option<(Input, Option<PathBuf>, Option<io::Error>)> {
466+
fn make_input(
467+
error_format: ErrorOutputType,
468+
free_matches: &[String],
469+
) -> Result<Option<(Input, Option<PathBuf>)>, ErrorReported> {
473470
if free_matches.len() == 1 {
474471
let ifile = &free_matches[0];
475472
if ifile == "-" {
476473
let mut src = String::new();
477-
let err = if io::stdin().read_to_string(&mut src).is_err() {
478-
Some(io::Error::new(
479-
io::ErrorKind::InvalidData,
474+
if io::stdin().read_to_string(&mut src).is_err() {
475+
// Immediately stop compilation if there was an issue reading
476+
// the input (for example if the input stream is not UTF-8).
477+
early_error_no_abort(
478+
error_format,
480479
"couldn't read from stdin, as it did not contain valid UTF-8",
481-
))
482-
} else {
483-
None
484-
};
480+
);
481+
return Err(ErrorReported);
482+
}
485483
if let Ok(path) = env::var("UNSTABLE_RUSTDOC_TEST_PATH") {
486484
let line = env::var("UNSTABLE_RUSTDOC_TEST_LINE").expect(
487485
"when UNSTABLE_RUSTDOC_TEST_PATH is set \
@@ -490,14 +488,15 @@ fn make_input(free_matches: &[String]) -> Option<(Input, Option<PathBuf>, Option
490488
let line = isize::from_str_radix(&line, 10)
491489
.expect("UNSTABLE_RUSTDOC_TEST_LINE needs to be an number");
492490
let file_name = FileName::doc_test_source_code(PathBuf::from(path), line);
493-
return Some((Input::Str { name: file_name, input: src }, None, err));
491+
Ok(Some((Input::Str { name: file_name, input: src }, None)))
492+
} else {
493+
Ok(Some((Input::Str { name: FileName::anon_source_code(&src), input: src }, None)))
494494
}
495-
Some((Input::Str { name: FileName::anon_source_code(&src), input: src }, None, err))
496495
} else {
497-
Some((Input::File(PathBuf::from(ifile)), Some(PathBuf::from(ifile)), None))
496+
Ok(Some((Input::File(PathBuf::from(ifile)), Some(PathBuf::from(ifile)))))
498497
}
499498
} else {
500-
None
499+
Ok(None)
501500
}
502501
}
503502

0 commit comments

Comments
 (0)