Skip to content

Commit effef88

Browse files
Simplify temp path creation a bit
1 parent e643f59 commit effef88

File tree

13 files changed

+70
-92
lines changed

13 files changed

+70
-92
lines changed

Diff for: compiler/rustc_codegen_cranelift/src/driver/aot.rs

+9-15
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ fn produce_final_output_artifacts(
169169
if codegen_results.modules.len() == 1 {
170170
// 1) Only one codegen unit. In this case it's no difficulty
171171
// to copy `foo.0.x` to `foo.x`.
172-
let module_name = Some(&codegen_results.modules[0].name[..]);
173-
let path = crate_output.temp_path(output_type, module_name);
172+
let path =
173+
crate_output.temp_path_for_cgu(output_type, &codegen_results.modules[0].name);
174174
let output = crate_output.path(output_type);
175175
if !output_type.is_text_output() && output.is_tty() {
176176
sess.dcx()
@@ -183,22 +183,16 @@ fn produce_final_output_artifacts(
183183
ensure_removed(sess.dcx(), &path);
184184
}
185185
} else {
186-
let extension = crate_output
187-
.temp_path(output_type, None)
188-
.extension()
189-
.unwrap()
190-
.to_str()
191-
.unwrap()
192-
.to_owned();
193-
194186
if crate_output.outputs.contains_explicit_name(&output_type) {
195187
// 2) Multiple codegen units, with `--emit foo=some_name`. We have
196188
// no good solution for this case, so warn the user.
197-
sess.dcx().emit_warn(ssa_errors::IgnoringEmitPath { extension });
189+
sess.dcx()
190+
.emit_warn(ssa_errors::IgnoringEmitPath { extension: output_type.extension() });
198191
} else if crate_output.single_output_file.is_some() {
199192
// 3) Multiple codegen units, with `-o some_name`. We have
200193
// no good solution for this case, so warn the user.
201-
sess.dcx().emit_warn(ssa_errors::IgnoringOutput { extension });
194+
sess.dcx()
195+
.emit_warn(ssa_errors::IgnoringOutput { extension: output_type.extension() });
202196
} else {
203197
// 4) Multiple codegen units, but no explicit name. We
204198
// just leave the `foo.0.x` files in place.
@@ -409,7 +403,7 @@ fn emit_module(
409403
object.set_section_data(comment_section, producer, 1);
410404
}
411405

412-
let tmp_file = output_filenames.temp_path(OutputType::Object, Some(&name));
406+
let tmp_file = output_filenames.temp_path_for_cgu(OutputType::Object, &name);
413407
let file = match File::create(&tmp_file) {
414408
Ok(file) => file,
415409
Err(err) => return Err(format!("error creating object file: {}", err)),
@@ -450,7 +444,7 @@ fn reuse_workproduct_for_cgu(
450444
) -> Result<ModuleCodegenResult, String> {
451445
let work_product = cgu.previous_work_product(tcx);
452446
let obj_out_regular =
453-
tcx.output_filenames(()).temp_path(OutputType::Object, Some(cgu.name().as_str()));
447+
tcx.output_filenames(()).temp_path_for_cgu(OutputType::Object, cgu.name().as_str());
454448
let source_file_regular = rustc_incremental::in_incr_comp_dir_sess(
455449
&tcx.sess,
456450
&work_product.saved_files.get("o").expect("no saved object file in work product"),
@@ -627,7 +621,7 @@ fn emit_metadata_module(tcx: TyCtxt<'_>, metadata: &EncodedMetadata) -> Compiled
627621
.to_string();
628622

629623
let tmp_file =
630-
tcx.output_filenames(()).temp_path(OutputType::Metadata, Some(&metadata_cgu_name));
624+
tcx.output_filenames(()).temp_path_for_cgu(OutputType::Metadata, &metadata_cgu_name);
631625

632626
let symbol_name = rustc_middle::middle::exported_symbols::metadata_symbol_name(tcx);
633627
let obj = create_compressed_metadata_file(tcx.sess, metadata, &symbol_name);

Diff for: compiler/rustc_codegen_cranelift/src/global_asm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ pub(crate) fn compile_global_asm(
146146
global_asm.push('\n');
147147

148148
let global_asm_object_file = add_file_stem_postfix(
149-
config.output_filenames.temp_path(OutputType::Object, Some(cgu_name)),
149+
config.output_filenames.temp_path_for_cgu(OutputType::Object, cgu_name),
150150
".asm",
151151
);
152152

Diff for: compiler/rustc_codegen_gcc/src/back/write.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,15 @@ pub(crate) unsafe fn codegen(
2424
{
2525
let context = &module.module_llvm.context;
2626

27-
let module_name = module.name.clone();
28-
2927
let should_combine_object_files = module.module_llvm.should_combine_object_files;
3028

31-
let module_name = Some(&module_name[..]);
32-
3329
// NOTE: Only generate object files with GIMPLE when this environment variable is set for
3430
// now because this requires a particular setup (same gcc/lto1/lto-wrapper commit as libgccjit).
3531
// TODO(antoyo): remove this environment variable.
3632
let fat_lto = env::var("EMBED_LTO_BITCODE").as_deref() == Ok("1");
3733

38-
let bc_out = cgcx.output_filenames.temp_path(OutputType::Bitcode, module_name);
39-
let obj_out = cgcx.output_filenames.temp_path(OutputType::Object, module_name);
34+
let bc_out = cgcx.output_filenames.temp_path_for_cgu(OutputType::Bitcode, &module.name);
35+
let obj_out = cgcx.output_filenames.temp_path_for_cgu(OutputType::Object, &module.name);
4036

4137
if config.bitcode_needed() {
4238
if fat_lto {
@@ -117,14 +113,15 @@ pub(crate) unsafe fn codegen(
117113
}
118114

119115
if config.emit_ir {
120-
let out = cgcx.output_filenames.temp_path(OutputType::LlvmAssembly, module_name);
116+
let out =
117+
cgcx.output_filenames.temp_path_for_cgu(OutputType::LlvmAssembly, &module.name);
121118
std::fs::write(out, "").expect("write file");
122119
}
123120

124121
if config.emit_asm {
125122
let _timer =
126123
cgcx.prof.generic_activity_with_arg("GCC_module_codegen_emit_asm", &*module.name);
127-
let path = cgcx.output_filenames.temp_path(OutputType::Assembly, module_name);
124+
let path = cgcx.output_filenames.temp_path_for_cgu(OutputType::Assembly, &module.name);
128125
context.compile_to_file(OutputKind::Assembler, path.to_str().expect("path to str"));
129126
}
130127

Diff for: compiler/rustc_codegen_llvm/src/back/write.rs

+11-16
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,14 @@ pub(crate) fn create_target_machine(tcx: TyCtxt<'_>, mod_name: &str) -> OwnedTar
119119
tcx.output_filenames(()).split_dwarf_path(
120120
tcx.sess.split_debuginfo(),
121121
tcx.sess.opts.unstable_opts.split_dwarf_kind,
122-
Some(mod_name),
122+
mod_name,
123123
)
124124
} else {
125125
None
126126
};
127127

128128
let output_obj_file =
129-
Some(tcx.output_filenames(()).temp_path(OutputType::Object, Some(mod_name)));
129+
Some(tcx.output_filenames(()).temp_path_for_cgu(OutputType::Object, mod_name));
130130
let config = TargetMachineFactoryConfig { split_dwarf_file, output_obj_file };
131131

132132
target_machine_factory(
@@ -330,8 +330,7 @@ pub(crate) fn save_temp_bitcode(
330330
return;
331331
}
332332
let ext = format!("{name}.bc");
333-
let cgu = Some(&module.name[..]);
334-
let path = cgcx.output_filenames.temp_path_ext(&ext, cgu);
333+
let path = cgcx.output_filenames.temp_path_ext_for_cgu(&ext, &module.name);
335334
write_bitcode_to_file(module, &path)
336335
}
337336

@@ -694,11 +693,8 @@ pub(crate) unsafe fn optimize(
694693
let llcx = &*module.module_llvm.llcx;
695694
let _handlers = DiagnosticHandlers::new(cgcx, dcx, llcx, module, CodegenDiagnosticsStage::Opt);
696695

697-
let module_name = module.name.clone();
698-
let module_name = Some(&module_name[..]);
699-
700696
if config.emit_no_opt_bc {
701-
let out = cgcx.output_filenames.temp_path_ext("no-opt.bc", module_name);
697+
let out = cgcx.output_filenames.temp_path_ext_for_cgu("no-opt.bc", &module.name);
702698
write_bitcode_to_file(module, &out)
703699
}
704700

@@ -744,7 +740,7 @@ pub(crate) unsafe fn optimize(
744740
let thin_lto_buffer = unsafe { ThinBuffer::from_raw_ptr(thin_lto_buffer) };
745741
module.thin_lto_buffer = Some(thin_lto_buffer.data().to_vec());
746742
let bc_summary_out =
747-
cgcx.output_filenames.temp_path(OutputType::ThinLinkBitcode, module_name);
743+
cgcx.output_filenames.temp_path_for_cgu(OutputType::ThinLinkBitcode, &module.name);
748744
if config.emit_thin_lto_summary
749745
&& let Some(thin_link_bitcode_filename) = bc_summary_out.file_name()
750746
{
@@ -801,8 +797,6 @@ pub(crate) unsafe fn codegen(
801797
let llmod = module.module_llvm.llmod();
802798
let llcx = &*module.module_llvm.llcx;
803799
let tm = &*module.module_llvm.tm;
804-
let module_name = module.name.clone();
805-
let module_name = Some(&module_name[..]);
806800
let _handlers =
807801
DiagnosticHandlers::new(cgcx, dcx, llcx, &module, CodegenDiagnosticsStage::Codegen);
808802

@@ -814,8 +808,8 @@ pub(crate) unsafe fn codegen(
814808
// copy it to the .o file, and delete the bitcode if it wasn't
815809
// otherwise requested.
816810

817-
let bc_out = cgcx.output_filenames.temp_path(OutputType::Bitcode, module_name);
818-
let obj_out = cgcx.output_filenames.temp_path(OutputType::Object, module_name);
811+
let bc_out = cgcx.output_filenames.temp_path_for_cgu(OutputType::Bitcode, &module.name);
812+
let obj_out = cgcx.output_filenames.temp_path_for_cgu(OutputType::Object, &module.name);
819813

820814
if config.bitcode_needed() {
821815
if config.emit_bc || config.emit_obj == EmitObj::Bitcode {
@@ -857,7 +851,8 @@ pub(crate) unsafe fn codegen(
857851
if config.emit_ir {
858852
let _timer =
859853
cgcx.prof.generic_activity_with_arg("LLVM_module_codegen_emit_ir", &*module.name);
860-
let out = cgcx.output_filenames.temp_path(OutputType::LlvmAssembly, module_name);
854+
let out =
855+
cgcx.output_filenames.temp_path_for_cgu(OutputType::LlvmAssembly, &module.name);
861856
let out_c = path_to_c_string(&out);
862857

863858
extern "C" fn demangle_callback(
@@ -899,7 +894,7 @@ pub(crate) unsafe fn codegen(
899894
if config.emit_asm {
900895
let _timer =
901896
cgcx.prof.generic_activity_with_arg("LLVM_module_codegen_emit_asm", &*module.name);
902-
let path = cgcx.output_filenames.temp_path(OutputType::Assembly, module_name);
897+
let path = cgcx.output_filenames.temp_path_for_cgu(OutputType::Assembly, &module.name);
903898

904899
// We can't use the same module for asm and object code output,
905900
// because that triggers various errors like invalid IR or broken
@@ -929,7 +924,7 @@ pub(crate) unsafe fn codegen(
929924
.prof
930925
.generic_activity_with_arg("LLVM_module_codegen_emit_obj", &*module.name);
931926

932-
let dwo_out = cgcx.output_filenames.temp_path_dwo(module_name);
927+
let dwo_out = cgcx.output_filenames.temp_path_dwo_for_cgu(&module.name);
933928
let dwo_out = match (cgcx.split_debuginfo, cgcx.split_dwarf_kind) {
934929
// Don't change how DWARF is emitted when disabled.
935930
(SplitDebuginfo::Off, _) => None,

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(
910910
&& let Some(f) = output_filenames.split_dwarf_path(
911911
tcx.sess.split_debuginfo(),
912912
tcx.sess.opts.unstable_opts.split_dwarf_kind,
913-
Some(codegen_unit_name),
913+
codegen_unit_name,
914914
) {
915915
// We get a path relative to the working directory from split_dwarf_path
916916
Some(tcx.sess.source_map().path_mapping().to_real_filename(f))

Diff for: compiler/rustc_codegen_ssa/src/back/link.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ pub fn link_binary(
112112
codegen_results.crate_info.local_crate_name,
113113
);
114114
let crate_name = format!("{}", codegen_results.crate_info.local_crate_name);
115-
let out_filename =
116-
output.file_for_writing(outputs, OutputType::Exe, Some(crate_name.as_str()));
115+
let out_filename = output.file_for_writing(outputs, OutputType::Exe, &crate_name);
117116
match crate_type {
118117
CrateType::Rlib => {
119118
let _timer = sess.timer("link_rlib");

Diff for: compiler/rustc_codegen_ssa/src/back/write.rs

+8-16
Original file line numberDiff line numberDiff line change
@@ -306,14 +306,14 @@ impl TargetMachineFactoryConfig {
306306
cgcx.output_filenames.split_dwarf_path(
307307
cgcx.split_debuginfo,
308308
cgcx.split_dwarf_kind,
309-
Some(module_name),
309+
module_name,
310310
)
311311
} else {
312312
None
313313
};
314314

315315
let output_obj_file =
316-
Some(cgcx.output_filenames.temp_path(OutputType::Object, Some(module_name)));
316+
Some(cgcx.output_filenames.temp_path_for_cgu(OutputType::Object, module_name));
317317
TargetMachineFactoryConfig { split_dwarf_file, output_obj_file }
318318
}
319319
}
@@ -582,8 +582,7 @@ fn produce_final_output_artifacts(
582582
if let [module] = &compiled_modules.modules[..] {
583583
// 1) Only one codegen unit. In this case it's no difficulty
584584
// to copy `foo.0.x` to `foo.x`.
585-
let module_name = Some(&module.name[..]);
586-
let path = crate_output.temp_path(output_type, module_name);
585+
let path = crate_output.temp_path_for_cgu(output_type, &module.name);
587586
let output = crate_output.path(output_type);
588587
if !output_type.is_text_output() && output.is_tty() {
589588
sess.dcx()
@@ -596,22 +595,15 @@ fn produce_final_output_artifacts(
596595
ensure_removed(sess.dcx(), &path);
597596
}
598597
} else {
599-
let extension = crate_output
600-
.temp_path(output_type, None)
601-
.extension()
602-
.unwrap()
603-
.to_str()
604-
.unwrap()
605-
.to_owned();
606-
607598
if crate_output.outputs.contains_explicit_name(&output_type) {
608599
// 2) Multiple codegen units, with `--emit foo=some_name`. We have
609600
// no good solution for this case, so warn the user.
610-
sess.dcx().emit_warn(errors::IgnoringEmitPath { extension });
601+
sess.dcx()
602+
.emit_warn(errors::IgnoringEmitPath { extension: output_type.extension() });
611603
} else if crate_output.single_output_file.is_some() {
612604
// 3) Multiple codegen units, with `-o some_name`. We have
613605
// no good solution for this case, so warn the user.
614-
sess.dcx().emit_warn(errors::IgnoringOutput { extension });
606+
sess.dcx().emit_warn(errors::IgnoringOutput { extension: output_type.extension() });
615607
} else {
616608
// 4) Multiple codegen units, but no explicit name. We
617609
// just leave the `foo.0.x` files in place.
@@ -967,7 +959,7 @@ fn execute_copy_from_cache_work_item<B: ExtraBackendMethods>(
967959
module.source.saved_files.get("dwo").as_ref().and_then(|saved_dwarf_object_file| {
968960
let dwarf_obj_out = cgcx
969961
.output_filenames
970-
.split_dwarf_path(cgcx.split_debuginfo, cgcx.split_dwarf_kind, Some(&module.name))
962+
.split_dwarf_path(cgcx.split_debuginfo, cgcx.split_dwarf_kind, &module.name)
971963
.expect(
972964
"saved dwarf object in work product but `split_dwarf_path` returned `None`",
973965
);
@@ -977,7 +969,7 @@ fn execute_copy_from_cache_work_item<B: ExtraBackendMethods>(
977969
let mut load_from_incr_cache = |perform, output_type: OutputType| {
978970
if perform {
979971
let saved_file = module.source.saved_files.get(output_type.extension())?;
980-
let output_path = cgcx.output_filenames.temp_path(output_type, Some(&module.name));
972+
let output_path = cgcx.output_filenames.temp_path_for_cgu(output_type, &module.name);
981973
load_from_incr_comp_dir(output_path, &saved_file)
982974
} else {
983975
None

Diff for: compiler/rustc_codegen_ssa/src/base.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -640,8 +640,9 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
640640
let metadata_cgu_name =
641641
cgu_name_builder.build_cgu_name(LOCAL_CRATE, &["crate"], Some("metadata")).to_string();
642642
tcx.sess.time("write_compressed_metadata", || {
643-
let file_name =
644-
tcx.output_filenames(()).temp_path(OutputType::Metadata, Some(&metadata_cgu_name));
643+
let file_name = tcx
644+
.output_filenames(())
645+
.temp_path_for_cgu(OutputType::Metadata, &metadata_cgu_name);
645646
let data = create_compressed_metadata_file(
646647
tcx.sess,
647648
&metadata,

Diff for: compiler/rustc_codegen_ssa/src/errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,13 @@ pub struct BinaryOutputToTty {
277277
#[derive(Diagnostic)]
278278
#[diag(codegen_ssa_ignoring_emit_path)]
279279
pub struct IgnoringEmitPath {
280-
pub extension: String,
280+
pub extension: &'static str,
281281
}
282282

283283
#[derive(Diagnostic)]
284284
#[diag(codegen_ssa_ignoring_output)]
285285
pub struct IgnoringOutput {
286-
pub extension: String,
286+
pub extension: &'static str,
287287
}
288288

289289
#[derive(Diagnostic)]

Diff for: compiler/rustc_codegen_ssa/src/lib.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,13 @@ impl<M> ModuleCodegen<M> {
106106
emit_ir: bool,
107107
outputs: &OutputFilenames,
108108
) -> CompiledModule {
109-
let object = emit_obj.then(|| outputs.temp_path(OutputType::Object, Some(&self.name)));
110-
let dwarf_object = emit_dwarf_obj.then(|| outputs.temp_path_dwo(Some(&self.name)));
111-
let bytecode = emit_bc.then(|| outputs.temp_path(OutputType::Bitcode, Some(&self.name)));
112-
let assembly = emit_asm.then(|| outputs.temp_path(OutputType::Assembly, Some(&self.name)));
109+
let object = emit_obj.then(|| outputs.temp_path_for_cgu(OutputType::Object, &self.name));
110+
let dwarf_object = emit_dwarf_obj.then(|| outputs.temp_path_dwo_for_cgu(&self.name));
111+
let bytecode = emit_bc.then(|| outputs.temp_path_for_cgu(OutputType::Bitcode, &self.name));
112+
let assembly =
113+
emit_asm.then(|| outputs.temp_path_for_cgu(OutputType::Assembly, &self.name));
113114
let llvm_ir =
114-
emit_ir.then(|| outputs.temp_path(OutputType::LlvmAssembly, Some(&self.name)));
115+
emit_ir.then(|| outputs.temp_path_for_cgu(OutputType::LlvmAssembly, &self.name));
115116

116117
CompiledModule {
117118
name: self.name.clone(),

Diff for: compiler/rustc_middle/src/ty/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ impl<'tcx> TyCtxt<'tcx> {
279279
p.hash(&mut s);
280280
let hash = s.finish();
281281
*path = Some(path.take().unwrap_or_else(|| {
282-
self.output_filenames(()).temp_path_ext(&format!("long-type-{hash}.txt"), None)
282+
self.output_filenames(()).temp_path_for_diagnostic(&format!("long-type-{hash}.txt"))
283283
}));
284284
let Ok(mut file) =
285285
File::options().create(true).read(true).append(true).open(&path.as_ref().unwrap())

Diff for: compiler/rustc_middle/src/ty/print/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ pub fn shrunk_instance_name<'tcx>(
382382
return (s, None);
383383
}
384384

385-
let path = tcx.output_filenames(()).temp_path_ext("long-type.txt", None);
385+
let path = tcx.output_filenames(()).temp_path_for_diagnostic("long-type.txt");
386386
let written_to_path = std::fs::write(&path, s).ok().map(|_| path);
387387

388388
(shrunk, written_to_path)

0 commit comments

Comments
 (0)