@@ -1042,6 +1042,13 @@ pub struct OutputFilenames {
10421042 crate_stem : String ,
10431043 /// Typically based on `.rs` input file name. Any '-' is preserved.
10441044 filestem : String ,
1045+ /// A random string generated per invocation of rustc.
1046+ ///
1047+ /// This is prepended to all temporary files so that they do not collide
1048+ /// during concurrent invocations of rustc, or past invocations that were
1049+ /// preserved with a flag like `-C save-temps`, since these files may be
1050+ /// hard-linked.
1051+ invocation_temp : Option < String > ,
10451052 pub single_output_file : Option < OutFileName > ,
10461053 temps_directory : Option < PathBuf > ,
10471054 pub outputs : OutputTypes ,
@@ -1057,13 +1064,15 @@ impl OutputFilenames {
10571064 out_crate_name : String ,
10581065 out_filestem : String ,
10591066 single_output_file : Option < OutFileName > ,
1067+ invocation_temp : Option < String > ,
10601068 temps_directory : Option < PathBuf > ,
10611069 extra : String ,
10621070 outputs : OutputTypes ,
10631071 ) -> Self {
10641072 OutputFilenames {
10651073 out_directory,
10661074 single_output_file,
1075+ invocation_temp,
10671076 temps_directory,
10681077 outputs,
10691078 crate_stem : format ! ( "{out_crate_name}{extra}" ) ,
@@ -1109,6 +1118,12 @@ impl OutputFilenames {
11091118 pub fn temp_path_ext_for_cgu ( & self , ext : & str , codegen_unit_name : & str ) -> PathBuf {
11101119 let mut extension = codegen_unit_name. to_string ( ) ;
11111120
1121+ // Append `.{invocation_temp}` to ensure temporary files are unique.
1122+ if let Some ( rng) = & self . invocation_temp {
1123+ extension. push ( '.' ) ;
1124+ extension. push_str ( rng) ;
1125+ }
1126+
11121127 // FIXME: This is sketchy that we're not appending `.rcgu` when the ext is empty.
11131128 // Append `.rcgu.{ext}`.
11141129 if !ext. is_empty ( ) {
0 commit comments