@@ -318,6 +318,7 @@ enum TestFailure {
318318 UnexpectedRunPass ,
319319}
320320
321+ #[ derive( Debug ) ]
321322enum DirState {
322323 Temp ( tempfile:: TempDir ) ,
323324 Perm ( PathBuf ) ,
@@ -383,10 +384,26 @@ fn run_test(
383384 edition : Edition ,
384385 report_unused_externs : impl Fn ( UnusedExterns ) ,
385386 no_run : bool ,
387+ // Used to prevent overwriting a binary in case `--persist-doctests` is used.
388+ binary_extra : Option < & str > ,
386389) -> Result < ( ) , TestFailure > {
387390 // 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) ;
390407
391408 let rustc_binary = rustdoc_options
392409 . test_builder
@@ -753,6 +770,7 @@ impl DocTest {
753770 edition,
754771 report_unused_externs,
755772 no_run,
773+ None ,
756774 ) ;
757775
758776 if let Err ( err) = res {
@@ -897,11 +915,6 @@ pub(crate) fn make_test(
897915 let mut path = path. clone ( ) ;
898916 path. push ( & test_id) ;
899917
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-
905918 DirState :: Perm ( path)
906919 } else {
907920 DirState :: Temp ( get_doctest_dir ( ) . expect ( "rustdoc needs a tempdir" ) )
@@ -1390,6 +1403,8 @@ test::test_main(&[{test_args}], vec![{ids}], None);
13901403 self . edition ,
13911404 |_: UnusedExterns | { } ,
13921405 false ,
1406+ // To prevent writing over an existing doctest
1407+ Some ( & format ! ( "_{}_{}" , self . edition, * self . ran_edition_tests) ) ,
13931408 ) ;
13941409 if let Err ( TestFailure :: CompileError ) = ret {
13951410 // We failed to compile all compatible tests as one so we push them into the
0 commit comments