Skip to content

Commit 525c6a3

Browse files
committed
Directly raise fatal errors inside the codegen backends
As opposed to passing it around through Result.
1 parent 46a7d28 commit 525c6a3

File tree

8 files changed

+117
-124
lines changed

8 files changed

+117
-124
lines changed

compiler/rustc_codegen_gcc/src/back/lto.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use rustc_codegen_ssa::back::write::{CodegenContext, FatLtoInput};
2929
use rustc_codegen_ssa::traits::*;
3030
use rustc_codegen_ssa::{ModuleCodegen, ModuleKind, looks_like_rust_object_file};
3131
use rustc_data_structures::memmap::Mmap;
32-
use rustc_errors::{DiagCtxtHandle, FatalError};
32+
use rustc_errors::DiagCtxtHandle;
3333
use rustc_middle::bug;
3434
use rustc_middle::dep_graph::WorkProduct;
3535
use rustc_session::config::Lto;
@@ -51,12 +51,11 @@ fn prepare_lto(
5151
cgcx: &CodegenContext<GccCodegenBackend>,
5252
each_linked_rlib_for_lto: &[PathBuf],
5353
dcx: DiagCtxtHandle<'_>,
54-
) -> Result<LtoData, FatalError> {
54+
) -> LtoData {
5555
let tmp_path = match tempdir() {
5656
Ok(tmp_path) => tmp_path,
5757
Err(error) => {
58-
eprintln!("Cannot create temporary directory: {}", error);
59-
return Err(FatalError);
58+
dcx.fatal(format!("Cannot create temporary directory: {}", error));
6059
}
6160
};
6261

@@ -91,15 +90,14 @@ fn prepare_lto(
9190
upstream_modules.push((module, CString::new(name).unwrap()));
9291
}
9392
Err(e) => {
94-
dcx.emit_err(e);
95-
return Err(FatalError);
93+
dcx.emit_fatal(e);
9694
}
9795
}
9896
}
9997
}
10098
}
10199

102-
Ok(LtoData { upstream_modules, tmp_path })
100+
LtoData { upstream_modules, tmp_path }
103101
}
104102

105103
fn save_as_file(obj: &[u8], path: &Path) -> Result<(), LtoBitcodeFromRlib> {
@@ -114,10 +112,10 @@ pub(crate) fn run_fat(
114112
cgcx: &CodegenContext<GccCodegenBackend>,
115113
each_linked_rlib_for_lto: &[PathBuf],
116114
modules: Vec<FatLtoInput<GccCodegenBackend>>,
117-
) -> Result<ModuleCodegen<GccContext>, FatalError> {
115+
) -> ModuleCodegen<GccContext> {
118116
let dcx = cgcx.create_dcx();
119117
let dcx = dcx.handle();
120-
let lto_data = prepare_lto(cgcx, each_linked_rlib_for_lto, dcx)?;
118+
let lto_data = prepare_lto(cgcx, each_linked_rlib_for_lto, dcx);
121119
/*let symbols_below_threshold =
122120
lto_data.symbols_below_threshold.iter().map(|c| c.as_ptr()).collect::<Vec<_>>();*/
123121
fat_lto(
@@ -137,7 +135,7 @@ fn fat_lto(
137135
mut serialized_modules: Vec<(SerializedModule<ModuleBuffer>, CString)>,
138136
tmp_path: TempDir,
139137
//symbols_below_threshold: &[String],
140-
) -> Result<ModuleCodegen<GccContext>, FatalError> {
138+
) -> ModuleCodegen<GccContext> {
141139
let _timer = cgcx.prof.generic_activity("GCC_fat_lto_build_monolithic_module");
142140
info!("going for a fat lto");
143141

@@ -261,7 +259,7 @@ fn fat_lto(
261259
// of now.
262260
module.module_llvm.temp_dir = Some(tmp_path);
263261

264-
Ok(module)
262+
module
265263
}
266264

267265
pub struct ModuleBuffer(PathBuf);
@@ -286,10 +284,10 @@ pub(crate) fn run_thin(
286284
each_linked_rlib_for_lto: &[PathBuf],
287285
modules: Vec<(String, ThinBuffer)>,
288286
cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>,
289-
) -> Result<(Vec<ThinModule<GccCodegenBackend>>, Vec<WorkProduct>), FatalError> {
287+
) -> (Vec<ThinModule<GccCodegenBackend>>, Vec<WorkProduct>) {
290288
let dcx = cgcx.create_dcx();
291289
let dcx = dcx.handle();
292-
let lto_data = prepare_lto(cgcx, each_linked_rlib_for_lto, dcx)?;
290+
let lto_data = prepare_lto(cgcx, each_linked_rlib_for_lto, dcx);
293291
if cgcx.opts.cg.linker_plugin_lto.enabled() {
294292
unreachable!(
295293
"We should never reach this case if the LTO step \
@@ -355,7 +353,7 @@ fn thin_lto(
355353
tmp_path: TempDir,
356354
cached_modules: Vec<(SerializedModule<ModuleBuffer>, WorkProduct)>,
357355
//_symbols_below_threshold: &[String],
358-
) -> Result<(Vec<ThinModule<GccCodegenBackend>>, Vec<WorkProduct>), FatalError> {
356+
) -> (Vec<ThinModule<GccCodegenBackend>>, Vec<WorkProduct>) {
359357
let _timer = cgcx.prof.generic_activity("LLVM_thin_lto_global_analysis");
360358
info!("going for that thin, thin LTO");
361359

@@ -518,13 +516,13 @@ fn thin_lto(
518516
// TODO: save the directory so that it gets deleted later.
519517
std::mem::forget(tmp_path);
520518

521-
Ok((opt_jobs, copy_jobs))
519+
(opt_jobs, copy_jobs)
522520
}
523521

524522
pub fn optimize_thin_module(
525523
thin_module: ThinModule<GccCodegenBackend>,
526524
_cgcx: &CodegenContext<GccCodegenBackend>,
527-
) -> Result<ModuleCodegen<GccContext>, FatalError> {
525+
) -> ModuleCodegen<GccContext> {
528526
//let dcx = cgcx.create_dcx();
529527

530528
//let module_name = &thin_module.shared.module_names[thin_module.idx];
@@ -634,7 +632,8 @@ pub fn optimize_thin_module(
634632
save_temp_bitcode(cgcx, &module, "thin-lto-after-pm");
635633
}
636634
}*/
637-
Ok(module)
635+
#[allow(clippy::let_and_return)]
636+
module
638637
}
639638

640639
pub struct ThinBuffer {

compiler/rustc_codegen_gcc/src/back/write.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rustc_codegen_ssa::back::write::{BitcodeSection, CodegenContext, EmitObj, Mo
66
use rustc_codegen_ssa::{CompiledModule, ModuleCodegen};
77
use rustc_fs_util::link_or_copy;
88
use rustc_session::config::OutputType;
9-
use rustc_span::fatal_error::FatalError;
109
use rustc_target::spec::SplitDebuginfo;
1110

1211
use crate::base::add_pic_option;
@@ -17,7 +16,7 @@ pub(crate) fn codegen(
1716
cgcx: &CodegenContext<GccCodegenBackend>,
1817
module: ModuleCodegen<GccContext>,
1918
config: &ModuleConfig,
20-
) -> Result<CompiledModule, FatalError> {
19+
) -> CompiledModule {
2120
let dcx = cgcx.create_dcx();
2221
let dcx = dcx.handle();
2322

@@ -246,15 +245,15 @@ pub(crate) fn codegen(
246245
}
247246
}
248247

249-
Ok(module.into_compiled_module(
248+
module.into_compiled_module(
250249
config.emit_obj != EmitObj::None,
251250
cgcx.target_can_use_split_dwarf && cgcx.split_debuginfo == SplitDebuginfo::Unpacked,
252251
config.emit_bc,
253252
config.emit_asm,
254253
config.emit_ir,
255254
&cgcx.output_filenames,
256255
cgcx.invocation_temp.as_deref(),
257-
))
256+
)
258257
}
259258

260259
pub(crate) fn save_temp_bitcode(

compiler/rustc_codegen_gcc/src/lib.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ use rustc_middle::util::Providers;
110110
use rustc_session::Session;
111111
use rustc_session::config::{OptLevel, OutputFilenames};
112112
use rustc_span::Symbol;
113-
use rustc_span::fatal_error::FatalError;
114113
use rustc_target::spec::RelocModel;
115114
use tempfile::TempDir;
116115

@@ -362,7 +361,7 @@ impl WriteBackendMethods for GccCodegenBackend {
362361
_exported_symbols_for_lto: &[String],
363362
each_linked_rlib_for_lto: &[PathBuf],
364363
modules: Vec<FatLtoInput<Self>>,
365-
) -> Result<ModuleCodegen<Self::Module>, FatalError> {
364+
) -> ModuleCodegen<Self::Module> {
366365
back::lto::run_fat(cgcx, each_linked_rlib_for_lto, modules)
367366
}
368367

@@ -373,7 +372,7 @@ impl WriteBackendMethods for GccCodegenBackend {
373372
each_linked_rlib_for_lto: &[PathBuf],
374373
modules: Vec<(String, Self::ThinBuffer)>,
375374
cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
376-
) -> Result<(Vec<ThinModule<Self>>, Vec<WorkProduct>), FatalError> {
375+
) -> (Vec<ThinModule<Self>>, Vec<WorkProduct>) {
377376
back::lto::run_thin(cgcx, each_linked_rlib_for_lto, modules, cached_modules)
378377
}
379378

@@ -390,23 +389,22 @@ impl WriteBackendMethods for GccCodegenBackend {
390389
_dcx: DiagCtxtHandle<'_>,
391390
module: &mut ModuleCodegen<Self::Module>,
392391
config: &ModuleConfig,
393-
) -> Result<(), FatalError> {
392+
) {
394393
module.module_llvm.context.set_optimization_level(to_gcc_opt_level(config.opt_level));
395-
Ok(())
396394
}
397395

398396
fn optimize_thin(
399397
cgcx: &CodegenContext<Self>,
400398
thin: ThinModule<Self>,
401-
) -> Result<ModuleCodegen<Self::Module>, FatalError> {
399+
) -> ModuleCodegen<Self::Module> {
402400
back::lto::optimize_thin_module(thin, cgcx)
403401
}
404402

405403
fn codegen(
406404
cgcx: &CodegenContext<Self>,
407405
module: ModuleCodegen<Self::Module>,
408406
config: &ModuleConfig,
409-
) -> Result<CompiledModule, FatalError> {
407+
) -> CompiledModule {
410408
back::write::codegen(cgcx, module, config)
411409
}
412410

0 commit comments

Comments
 (0)