@@ -318,6 +318,7 @@ enum TestFailure {
318
318
UnexpectedRunPass ,
319
319
}
320
320
321
+ #[ derive( Debug ) ]
321
322
enum DirState {
322
323
Temp ( tempfile:: TempDir ) ,
323
324
Perm ( PathBuf ) ,
@@ -383,10 +384,26 @@ fn run_test(
383
384
edition : Edition ,
384
385
report_unused_externs : impl Fn ( UnusedExterns ) ,
385
386
no_run : bool ,
387
+ // Used to prevent overwriting a binary in case `--persist-doctests` is used.
388
+ binary_extra : Option < & str > ,
386
389
) -> Result < ( ) , TestFailure > {
387
390
// Make sure we emit well-formed executable names for our target.
388
- let rust_out = add_exe_suffix ( "rust_out" . to_owned ( ) , & rustdoc_options. target ) ;
389
- let output_file = outdir. path ( ) . join ( rust_out) ;
391
+ let rust_out =
392
+ add_exe_suffix ( format ! ( "rust_out{}" , binary_extra. unwrap_or( "" ) ) , & rustdoc_options. target ) ;
393
+ let out_dir = outdir. path ( ) ;
394
+ let out_dir = if is_multiple_tests {
395
+ // If this a "multiple tests" case, we want to put it into the parent instead.
396
+ out_dir. parent ( ) . unwrap_or ( out_dir)
397
+ } else {
398
+ out_dir
399
+ } ;
400
+ if matches ! ( * outdir, DirState :: Perm ( ..) ) {
401
+ if let Err ( err) = std:: fs:: create_dir_all ( & out_dir) {
402
+ eprintln ! ( "Couldn't create directory for doctest executables: {err}" ) ;
403
+ panic:: resume_unwind ( Box :: new ( ( ) ) ) ;
404
+ }
405
+ }
406
+ let output_file = out_dir. join ( rust_out) ;
390
407
391
408
let rustc_binary = rustdoc_options
392
409
. test_builder
@@ -753,6 +770,7 @@ impl DocTest {
753
770
edition,
754
771
report_unused_externs,
755
772
no_run,
773
+ None ,
756
774
) ;
757
775
758
776
if let Err ( err) = res {
@@ -897,11 +915,6 @@ pub(crate) fn make_test(
897
915
let mut path = path. clone ( ) ;
898
916
path. push ( & test_id) ;
899
917
900
- if let Err ( err) = std:: fs:: create_dir_all ( & path) {
901
- eprintln ! ( "Couldn't create directory for doctest executables: {err}" ) ;
902
- panic:: resume_unwind ( Box :: new ( ( ) ) ) ;
903
- }
904
-
905
918
DirState :: Perm ( path)
906
919
} else {
907
920
DirState :: Temp ( get_doctest_dir ( ) . expect ( "rustdoc needs a tempdir" ) )
@@ -1390,6 +1403,8 @@ test::test_main(&[{test_args}], vec![{ids}], None);
1390
1403
self . edition ,
1391
1404
|_: UnusedExterns | { } ,
1392
1405
false ,
1406
+ // To prevent writing over an existing doctest
1407
+ Some ( & format ! ( "_{}_{}" , self . edition, * self . ran_edition_tests) ) ,
1393
1408
) ;
1394
1409
if let Err ( TestFailure :: CompileError ) = ret {
1395
1410
// We failed to compile all compatible tests as one so we push them into the
0 commit comments