From b218623ea030885e88730413c61e3d47ed2905da Mon Sep 17 00:00:00 2001 From: Orion Gonzalez Date: Tue, 27 Aug 2024 18:46:44 +0200 Subject: [PATCH] cleanup make_input --- compiler/rustc_driver_impl/src/lib.rs | 45 ++++++++++++++++----------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index 540523aa7b4..76bff6a87f3 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -481,36 +481,43 @@ fn make_output(matches: &getopts::Matches) -> (Option, Option Result, ErrorGuaranteed> { - let [ifile] = free_matches else { return Ok(None) }; - if ifile == "-" { - let mut src = String::new(); - if io::stdin().read_to_string(&mut src).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 let Ok(path) = env::var("UNSTABLE_RUSTDOC_TEST_PATH") { + let [input_file] = free_matches else { return Ok(None) }; + + if input_file != "-" { + // Normal `Input::File` + return Ok(Some(Input::File(PathBuf::from(input_file)))); + } + + // 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", ); let line = isize::from_str_radix(&line, 10) .expect("UNSTABLE_RUSTDOC_TEST_LINE needs to be an number"); - let file_name = FileName::doc_test_source_code(PathBuf::from(path), line); - Ok(Some(Input::Str { name: file_name, input: src })) - } else { - Ok(Some(Input::Str { name: FileName::anon_source_code(&src), input: src })) + FileName::doc_test_source_code(PathBuf::from(path), line) } - } else { - Ok(Some(Input::File(PathBuf::from(ifile)))) - } + Err(_) => FileName::anon_source_code(&input), + }; + + Ok(Some(Input::Str { name, input })) } /// Whether to stop or continue compilation.