Skip to content

Commit 4c0766c

Browse files
committed
Move error reporting out of emit_cgu
Error reporting requires a Session, which isn't available on background threads.
1 parent c2f0b3a commit 4c0766c

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

src/driver/aot.rs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl<HCX> HashStable<HCX> for ModuleCodegenResult {
3333
}
3434

3535
pub(crate) struct OngoingCodegen {
36-
modules: Vec<ModuleCodegenResult>,
36+
modules: Vec<Result<ModuleCodegenResult, String>>,
3737
allocator_module: Option<CompiledModule>,
3838
metadata_module: Option<CompiledModule>,
3939
metadata: EncodedMetadata,
@@ -50,6 +50,10 @@ impl OngoingCodegen {
5050
let mut modules = vec![];
5151

5252
for module_codegen_result in self.modules {
53+
let module_codegen_result = match module_codegen_result {
54+
Ok(module_codegen_result) => module_codegen_result,
55+
Err(err) => sess.fatal(&err),
56+
};
5357
let ModuleCodegenResult { module_regular, module_global_asm, existing_work_product } =
5458
module_codegen_result;
5559

@@ -117,7 +121,7 @@ fn emit_cgu(
117121
debug: Option<DebugContext<'_>>,
118122
unwind_context: UnwindContext,
119123
global_asm_object_file: Option<PathBuf>,
120-
) -> ModuleCodegenResult {
124+
) -> Result<ModuleCodegenResult, String> {
121125
let mut product = module.finish();
122126

123127
if let Some(mut debug) = debug {
@@ -126,9 +130,9 @@ fn emit_cgu(
126130

127131
unwind_context.emit(&mut product);
128132

129-
let module_regular = emit_module(tcx, product.object, ModuleKind::Regular, name.clone());
133+
let module_regular = emit_module(tcx, product.object, ModuleKind::Regular, name.clone())?;
130134

131-
ModuleCodegenResult {
135+
Ok(ModuleCodegenResult {
132136
module_regular,
133137
module_global_asm: global_asm_object_file.map(|global_asm_object_file| CompiledModule {
134138
name: format!("{name}.asm"),
@@ -138,31 +142,34 @@ fn emit_cgu(
138142
bytecode: None,
139143
}),
140144
existing_work_product: None,
141-
}
145+
})
142146
}
143147

144148
fn emit_module(
145149
tcx: TyCtxt<'_>,
146150
object: cranelift_object::object::write::Object<'_>,
147151
kind: ModuleKind,
148152
name: String,
149-
) -> CompiledModule {
153+
) -> Result<CompiledModule, String> {
150154
let tmp_file = tcx.output_filenames(()).temp_path(OutputType::Object, Some(&name));
151155
let mut file = match File::create(&tmp_file) {
152156
Ok(file) => file,
153-
Err(err) => tcx.sess.fatal(&format!("error creating object file: {}", err)),
157+
Err(err) => return Err(format!("error creating object file: {}", err)),
154158
};
155159

156160
if let Err(err) = object.write_stream(&mut file) {
157-
tcx.sess.fatal(&format!("error writing object file: {}", err));
161+
return Err(format!("error writing object file: {}", err));
158162
}
159163

160164
tcx.sess.prof.artifact_size("object_file", &*name, file.metadata().unwrap().len());
161165

162-
CompiledModule { name, kind, object: Some(tmp_file), dwarf_object: None, bytecode: None }
166+
Ok(CompiledModule { name, kind, object: Some(tmp_file), dwarf_object: None, bytecode: None })
163167
}
164168

165-
fn reuse_workproduct_for_cgu(tcx: TyCtxt<'_>, cgu: &CodegenUnit<'_>) -> ModuleCodegenResult {
169+
fn reuse_workproduct_for_cgu(
170+
tcx: TyCtxt<'_>,
171+
cgu: &CodegenUnit<'_>,
172+
) -> Result<ModuleCodegenResult, String> {
166173
let work_product = cgu.previous_work_product(tcx);
167174
let obj_out_regular =
168175
tcx.output_filenames(()).temp_path(OutputType::Object, Some(cgu.name().as_str()));
@@ -172,7 +179,7 @@ fn reuse_workproduct_for_cgu(tcx: TyCtxt<'_>, cgu: &CodegenUnit<'_>) -> ModuleCo
172179
);
173180

174181
if let Err(err) = rustc_fs_util::link_or_copy(&source_file_regular, &obj_out_regular) {
175-
tcx.sess.err(&format!(
182+
return Err(format!(
176183
"unable to copy {} to {}: {}",
177184
source_file_regular.display(),
178185
obj_out_regular.display(),
@@ -185,7 +192,7 @@ fn reuse_workproduct_for_cgu(tcx: TyCtxt<'_>, cgu: &CodegenUnit<'_>) -> ModuleCo
185192
let source_file_global_asm = rustc_incremental::in_incr_comp_dir_sess(&tcx.sess, asm_o);
186193
if let Err(err) = rustc_fs_util::link_or_copy(&source_file_global_asm, &obj_out_global_asm)
187194
{
188-
tcx.sess.err(&format!(
195+
return Err(format!(
189196
"unable to copy {} to {}: {}",
190197
source_file_regular.display(),
191198
obj_out_regular.display(),
@@ -197,7 +204,7 @@ fn reuse_workproduct_for_cgu(tcx: TyCtxt<'_>, cgu: &CodegenUnit<'_>) -> ModuleCo
197204
false
198205
};
199206

200-
ModuleCodegenResult {
207+
Ok(ModuleCodegenResult {
201208
module_regular: CompiledModule {
202209
name: cgu.name().to_string(),
203210
kind: ModuleKind::Regular,
@@ -217,7 +224,7 @@ fn reuse_workproduct_for_cgu(tcx: TyCtxt<'_>, cgu: &CodegenUnit<'_>) -> ModuleCo
217224
None
218225
},
219226
existing_work_product: Some((cgu.work_product_id(), work_product)),
220-
}
227+
})
221228
}
222229

223230
fn module_codegen(
@@ -227,7 +234,7 @@ fn module_codegen(
227234
Arc<GlobalAsmConfig>,
228235
rustc_span::Symbol,
229236
),
230-
) -> ModuleCodegenResult {
237+
) -> Result<ModuleCodegenResult, String> {
231238
let cgu = tcx.codegen_unit(cgu_name);
232239
let mono_items = cgu.items_in_deterministic_order(tcx);
233240

@@ -279,7 +286,7 @@ fn module_codegen(
279286

280287
let debug_context = cx.debug_context;
281288
let unwind_context = cx.unwind_context;
282-
let codegen_result = tcx.sess.time("write object file", || {
289+
tcx.sess.time("write object file", || {
283290
emit_cgu(
284291
tcx,
285292
cgu.name().as_str().to_string(),
@@ -288,9 +295,7 @@ fn module_codegen(
288295
unwind_context,
289296
global_asm_object_file,
290297
)
291-
});
292-
293-
codegen_result
298+
})
294299
}
295300

296301
pub(crate) fn run_aot(
@@ -356,7 +361,10 @@ pub(crate) fn run_aot(
356361
let mut product = allocator_module.finish();
357362
allocator_unwind_context.emit(&mut product);
358363

359-
Some(emit_module(tcx, product.object, ModuleKind::Allocator, "allocator_shim".to_owned()))
364+
match emit_module(tcx, product.object, ModuleKind::Allocator, "allocator_shim".to_owned()) {
365+
Ok(allocator_module) => Some(allocator_module),
366+
Err(err) => tcx.sess.fatal(err),
367+
}
360368
} else {
361369
None
362370
};

0 commit comments

Comments
 (0)