@@ -33,7 +33,7 @@ impl<HCX> HashStable<HCX> for ModuleCodegenResult {
33
33
}
34
34
35
35
pub ( crate ) struct OngoingCodegen {
36
- modules : Vec < ModuleCodegenResult > ,
36
+ modules : Vec < Result < ModuleCodegenResult , String > > ,
37
37
allocator_module : Option < CompiledModule > ,
38
38
metadata_module : Option < CompiledModule > ,
39
39
metadata : EncodedMetadata ,
@@ -50,6 +50,10 @@ impl OngoingCodegen {
50
50
let mut modules = vec ! [ ] ;
51
51
52
52
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
+ } ;
53
57
let ModuleCodegenResult { module_regular, module_global_asm, existing_work_product } =
54
58
module_codegen_result;
55
59
@@ -117,7 +121,7 @@ fn emit_cgu(
117
121
debug : Option < DebugContext < ' _ > > ,
118
122
unwind_context : UnwindContext ,
119
123
global_asm_object_file : Option < PathBuf > ,
120
- ) -> ModuleCodegenResult {
124
+ ) -> Result < ModuleCodegenResult , String > {
121
125
let mut product = module. finish ( ) ;
122
126
123
127
if let Some ( mut debug) = debug {
@@ -126,9 +130,9 @@ fn emit_cgu(
126
130
127
131
unwind_context. emit ( & mut product) ;
128
132
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 ( ) ) ? ;
130
134
131
- ModuleCodegenResult {
135
+ Ok ( ModuleCodegenResult {
132
136
module_regular,
133
137
module_global_asm : global_asm_object_file. map ( |global_asm_object_file| CompiledModule {
134
138
name : format ! ( "{name}.asm" ) ,
@@ -138,31 +142,34 @@ fn emit_cgu(
138
142
bytecode : None ,
139
143
} ) ,
140
144
existing_work_product : None ,
141
- }
145
+ } )
142
146
}
143
147
144
148
fn emit_module (
145
149
tcx : TyCtxt < ' _ > ,
146
150
object : cranelift_object:: object:: write:: Object < ' _ > ,
147
151
kind : ModuleKind ,
148
152
name : String ,
149
- ) -> CompiledModule {
153
+ ) -> Result < CompiledModule , String > {
150
154
let tmp_file = tcx. output_filenames ( ( ) ) . temp_path ( OutputType :: Object , Some ( & name) ) ;
151
155
let mut file = match File :: create ( & tmp_file) {
152
156
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) ) ,
154
158
} ;
155
159
156
160
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) ) ;
158
162
}
159
163
160
164
tcx. sess . prof . artifact_size ( "object_file" , & * name, file. metadata ( ) . unwrap ( ) . len ( ) ) ;
161
165
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 } )
163
167
}
164
168
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 > {
166
173
let work_product = cgu. previous_work_product ( tcx) ;
167
174
let obj_out_regular =
168
175
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
172
179
) ;
173
180
174
181
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 ! (
176
183
"unable to copy {} to {}: {}" ,
177
184
source_file_regular. display( ) ,
178
185
obj_out_regular. display( ) ,
@@ -185,7 +192,7 @@ fn reuse_workproduct_for_cgu(tcx: TyCtxt<'_>, cgu: &CodegenUnit<'_>) -> ModuleCo
185
192
let source_file_global_asm = rustc_incremental:: in_incr_comp_dir_sess ( & tcx. sess , asm_o) ;
186
193
if let Err ( err) = rustc_fs_util:: link_or_copy ( & source_file_global_asm, & obj_out_global_asm)
187
194
{
188
- tcx . sess . err ( & format ! (
195
+ return Err ( format ! (
189
196
"unable to copy {} to {}: {}" ,
190
197
source_file_regular. display( ) ,
191
198
obj_out_regular. display( ) ,
@@ -197,7 +204,7 @@ fn reuse_workproduct_for_cgu(tcx: TyCtxt<'_>, cgu: &CodegenUnit<'_>) -> ModuleCo
197
204
false
198
205
} ;
199
206
200
- ModuleCodegenResult {
207
+ Ok ( ModuleCodegenResult {
201
208
module_regular : CompiledModule {
202
209
name : cgu. name ( ) . to_string ( ) ,
203
210
kind : ModuleKind :: Regular ,
@@ -217,7 +224,7 @@ fn reuse_workproduct_for_cgu(tcx: TyCtxt<'_>, cgu: &CodegenUnit<'_>) -> ModuleCo
217
224
None
218
225
} ,
219
226
existing_work_product : Some ( ( cgu. work_product_id ( ) , work_product) ) ,
220
- }
227
+ } )
221
228
}
222
229
223
230
fn module_codegen (
@@ -227,7 +234,7 @@ fn module_codegen(
227
234
Arc < GlobalAsmConfig > ,
228
235
rustc_span:: Symbol ,
229
236
) ,
230
- ) -> ModuleCodegenResult {
237
+ ) -> Result < ModuleCodegenResult , String > {
231
238
let cgu = tcx. codegen_unit ( cgu_name) ;
232
239
let mono_items = cgu. items_in_deterministic_order ( tcx) ;
233
240
@@ -279,7 +286,7 @@ fn module_codegen(
279
286
280
287
let debug_context = cx. debug_context ;
281
288
let unwind_context = cx. unwind_context ;
282
- let codegen_result = tcx. sess . time ( "write object file" , || {
289
+ tcx. sess . time ( "write object file" , || {
283
290
emit_cgu (
284
291
tcx,
285
292
cgu. name ( ) . as_str ( ) . to_string ( ) ,
@@ -288,9 +295,7 @@ fn module_codegen(
288
295
unwind_context,
289
296
global_asm_object_file,
290
297
)
291
- } ) ;
292
-
293
- codegen_result
298
+ } )
294
299
}
295
300
296
301
pub ( crate ) fn run_aot (
@@ -356,7 +361,10 @@ pub(crate) fn run_aot(
356
361
let mut product = allocator_module. finish ( ) ;
357
362
allocator_unwind_context. emit ( & mut product) ;
358
363
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
+ }
360
368
} else {
361
369
None
362
370
} ;
0 commit comments