@@ -485,36 +485,43 @@ fn make_output(matches: &getopts::Matches) -> (Option<PathBuf>, Option<OutFileNa
485
485
( odir, ofile)
486
486
}
487
487
488
- // Extract input (string or file and optional path) from matches.
488
+ /// Extract input (string or file and optional path) from matches.
489
+ /// This handles reading from stdin if `-` is provided.
489
490
fn make_input(
490
491
early_dcx: & EarlyDiagCtxt ,
491
492
free_matches: & [ String ] ,
492
493
) -> Result <Option <Input >, ErrorGuaranteed > {
493
- let [ ifile] = free_matches else { return Ok ( None ) } ;
494
- if ifile == "-" {
495
- let mut src = String :: new( ) ;
496
- if io:: stdin( ) . read_to_string( & mut src) . is_err( ) {
497
- // Immediately stop compilation if there was an issue reading
498
- // the input (for example if the input stream is not UTF-8).
499
- let reported =
500
- early_dcx. early_err( "couldn't read from stdin, as it did not contain valid UTF-8" ) ;
501
- return Err ( reported) ;
502
- }
503
- if let Ok ( path) = env:: var( "UNSTABLE_RUSTDOC_TEST_PATH" ) {
494
+ let [ input_file] = free_matches else { return Ok ( None ) } ;
495
+
496
+ if input_file != "-" {
497
+ // Normal `Input::File`
498
+ return Ok ( Some ( Input :: File ( PathBuf :: from( input_file) ) ) ) ;
499
+ }
500
+
501
+ // read from stdin as `Input::Str`
502
+ let mut input = String :: new( ) ;
503
+ if io:: stdin( ) . read_to_string( & mut input) . is_err( ) {
504
+ // Immediately stop compilation if there was an issue reading
505
+ // the input (for example if the input stream is not UTF-8).
506
+ let reported =
507
+ early_dcx. early_err( "couldn't read from stdin, as it did not contain valid UTF-8" ) ;
508
+ return Err ( reported) ;
509
+ }
510
+
511
+ let name = match env:: var( "UNSTABLE_RUSTDOC_TEST_PATH" ) {
512
+ Ok ( path) => {
504
513
let line = env:: var( "UNSTABLE_RUSTDOC_TEST_LINE" ) . expect(
505
514
"when UNSTABLE_RUSTDOC_TEST_PATH is set \
506
515
UNSTABLE_RUSTDOC_TEST_LINE also needs to be set",
507
516
) ;
508
517
let line = isize :: from_str_radix( & line, 10 )
509
518
. expect( "UNSTABLE_RUSTDOC_TEST_LINE needs to be an number" ) ;
510
- let file_name = FileName :: doc_test_source_code( PathBuf :: from( path) , line) ;
511
- Ok ( Some ( Input :: Str { name: file_name, input: src } ) )
512
- } else {
513
- Ok ( Some ( Input :: Str { name: FileName :: anon_source_code( & src) , input: src } ) )
519
+ FileName :: doc_test_source_code( PathBuf :: from( path) , line)
514
520
}
515
- } else {
516
- Ok ( Some ( Input :: File ( PathBuf :: from( ifile) ) ) )
517
- }
521
+ Err ( _) => FileName :: anon_source_code( & input) ,
522
+ } ;
523
+
524
+ Ok ( Some ( Input :: Str { name, input } ) )
518
525
}
519
526
520
527
/// Whether to stop or continue compilation.
0 commit comments