Skip to content

Commit 401dd84

Browse files
committed
Remove all threading through of ErrorGuaranteed from the driver
It was inconsistently done (sometimes even within a single function) and most of the rest of the compiler uses fatal errors instead, which need to be caught using catch_with_exit_code anyway. Using fatal errors instead of ErrorGuaranteed everywhere in the driver simplifies things a bit.
1 parent 030545d commit 401dd84

File tree

19 files changed

+175
-216
lines changed

19 files changed

+175
-216
lines changed

compiler/rustc_codegen_llvm/src/lib.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use rustc_codegen_ssa::back::write::{
3636
use rustc_codegen_ssa::traits::*;
3737
use rustc_codegen_ssa::{CodegenResults, CompiledModule, ModuleCodegen};
3838
use rustc_data_structures::fx::FxIndexMap;
39-
use rustc_errors::{DiagCtxtHandle, ErrorGuaranteed, FatalError};
39+
use rustc_errors::{DiagCtxtHandle, FatalError};
4040
use rustc_metadata::EncodedMetadata;
4141
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
4242
use rustc_middle::ty::TyCtxt;
@@ -370,19 +370,14 @@ impl CodegenBackend for LlvmCodegenBackend {
370370
(codegen_results, work_products)
371371
}
372372

373-
fn link(
374-
&self,
375-
sess: &Session,
376-
codegen_results: CodegenResults,
377-
outputs: &OutputFilenames,
378-
) -> Result<(), ErrorGuaranteed> {
373+
fn link(&self, sess: &Session, codegen_results: CodegenResults, outputs: &OutputFilenames) {
379374
use rustc_codegen_ssa::back::link::link_binary;
380375

381376
use crate::back::archive::LlvmArchiveBuilderBuilder;
382377

383378
// Run the linker on any artifacts that resulted from the LLVM run.
384379
// This should produce either a finished executable or library.
385-
link_binary(sess, &LlvmArchiveBuilderBuilder, codegen_results, outputs)
380+
link_binary(sess, &LlvmArchiveBuilderBuilder, codegen_results, outputs);
386381
}
387382
}
388383

compiler/rustc_codegen_ssa/src/back/link.rs

+24-32
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_ast::CRATE_NODE_ID;
1515
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
1616
use rustc_data_structures::memmap::Mmap;
1717
use rustc_data_structures::temp_dir::MaybeTempDir;
18-
use rustc_errors::{DiagCtxtHandle, ErrorGuaranteed, FatalError};
18+
use rustc_errors::{DiagCtxtHandle, FatalError};
1919
use rustc_fs_util::{fix_windows_verbatim_for_gcc, try_canonicalize};
2020
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
2121
use rustc_metadata::fs::{METADATA_FILENAME, copy_to_stdout, emit_wrapper_file};
@@ -71,7 +71,7 @@ pub fn link_binary(
7171
archive_builder_builder: &dyn ArchiveBuilderBuilder,
7272
codegen_results: CodegenResults,
7373
outputs: &OutputFilenames,
74-
) -> Result<(), ErrorGuaranteed> {
74+
) {
7575
let _timer = sess.timer("link_binary");
7676
let output_metadata = sess.opts.output_types.contains_key(&OutputType::Metadata);
7777
let mut tempfiles_for_stdout_output: Vec<PathBuf> = Vec::new();
@@ -119,7 +119,7 @@ pub fn link_binary(
119119
&codegen_results,
120120
RlibFlavor::Normal,
121121
&path,
122-
)?
122+
)
123123
.build(&out_filename);
124124
}
125125
CrateType::Staticlib => {
@@ -129,7 +129,7 @@ pub fn link_binary(
129129
&codegen_results,
130130
&out_filename,
131131
&path,
132-
)?;
132+
);
133133
}
134134
_ => {
135135
link_natively(
@@ -139,7 +139,7 @@ pub fn link_binary(
139139
&out_filename,
140140
&codegen_results,
141141
path.as_ref(),
142-
)?;
142+
);
143143
}
144144
}
145145
if sess.opts.json_artifact_notifications {
@@ -225,8 +225,6 @@ pub fn link_binary(
225225
maybe_remove_temps_from_module(preserve_objects, preserve_dwarf_objects, module);
226226
}
227227
});
228-
229-
Ok(())
230228
}
231229

232230
// Crate type is not passed when calculating the dylibs to include for LTO. In that case all
@@ -298,7 +296,7 @@ fn link_rlib<'a>(
298296
codegen_results: &CodegenResults,
299297
flavor: RlibFlavor,
300298
tmpdir: &MaybeTempDir,
301-
) -> Result<Box<dyn ArchiveBuilder + 'a>, ErrorGuaranteed> {
299+
) -> Box<dyn ArchiveBuilder + 'a> {
302300
let mut ab = archive_builder_builder.new_archive_builder(sess);
303301

304302
let trailing_metadata = match flavor {
@@ -374,7 +372,7 @@ fn link_rlib<'a>(
374372
{
375373
let path = find_native_static_library(filename.as_str(), true, sess);
376374
let src = read(path)
377-
.map_err(|e| sess.dcx().emit_fatal(errors::ReadFileError { message: e }))?;
375+
.unwrap_or_else(|e| sess.dcx().emit_fatal(errors::ReadFileError { message: e }));
378376
let (data, _) = create_wrapper_file(sess, ".bundled_lib".to_string(), &src);
379377
let wrapper_file = emit_wrapper_file(sess, &data, tmpdir, filename.as_str());
380378
packed_bundled_libs.push(wrapper_file);
@@ -392,7 +390,7 @@ fn link_rlib<'a>(
392390
codegen_results.crate_info.used_libraries.iter(),
393391
tmpdir.as_ref(),
394392
true,
395-
)? {
393+
) {
396394
ab.add_archive(&output_path, Box::new(|_| false)).unwrap_or_else(|error| {
397395
sess.dcx().emit_fatal(errors::AddNativeLibrary { library_path: output_path, error });
398396
});
@@ -433,7 +431,7 @@ fn link_rlib<'a>(
433431
ab.add_file(&lib)
434432
}
435433

436-
Ok(ab)
434+
ab
437435
}
438436

439437
/// Extract all symbols defined in raw-dylib libraries, collated by library name.
@@ -445,7 +443,7 @@ fn link_rlib<'a>(
445443
fn collate_raw_dylibs<'a>(
446444
sess: &Session,
447445
used_libraries: impl IntoIterator<Item = &'a NativeLib>,
448-
) -> Result<Vec<(String, Vec<DllImport>)>, ErrorGuaranteed> {
446+
) -> Vec<(String, Vec<DllImport>)> {
449447
// Use index maps to preserve original order of imports and libraries.
450448
let mut dylib_table = FxIndexMap::<String, FxIndexMap<Symbol, &DllImport>>::default();
451449

@@ -469,15 +467,13 @@ fn collate_raw_dylibs<'a>(
469467
}
470468
}
471469
}
472-
if let Some(guar) = sess.dcx().has_errors() {
473-
return Err(guar);
474-
}
475-
Ok(dylib_table
470+
sess.dcx().abort_if_errors();
471+
dylib_table
476472
.into_iter()
477473
.map(|(name, imports)| {
478474
(name, imports.into_iter().map(|(_, import)| import.clone()).collect())
479475
})
480-
.collect())
476+
.collect()
481477
}
482478

483479
fn create_dll_import_libs<'a>(
@@ -486,8 +482,8 @@ fn create_dll_import_libs<'a>(
486482
used_libraries: impl IntoIterator<Item = &'a NativeLib>,
487483
tmpdir: &Path,
488484
is_direct_dependency: bool,
489-
) -> Result<Vec<PathBuf>, ErrorGuaranteed> {
490-
Ok(collate_raw_dylibs(sess, used_libraries)?
485+
) -> Vec<PathBuf> {
486+
collate_raw_dylibs(sess, used_libraries)
491487
.into_iter()
492488
.map(|(raw_dylib_name, raw_dylib_imports)| {
493489
let name_suffix = if is_direct_dependency { "_imports" } else { "_imports_indirect" };
@@ -537,7 +533,7 @@ fn create_dll_import_libs<'a>(
537533

538534
output_path
539535
})
540-
.collect())
536+
.collect()
541537
}
542538

543539
/// Create a static archive.
@@ -557,15 +553,15 @@ fn link_staticlib(
557553
codegen_results: &CodegenResults,
558554
out_filename: &Path,
559555
tempdir: &MaybeTempDir,
560-
) -> Result<(), ErrorGuaranteed> {
556+
) {
561557
info!("preparing staticlib to {:?}", out_filename);
562558
let mut ab = link_rlib(
563559
sess,
564560
archive_builder_builder,
565561
codegen_results,
566562
RlibFlavor::StaticlibBase,
567563
tempdir,
568-
)?;
564+
);
569565
let mut all_native_libs = vec![];
570566

571567
let res = each_linked_rlib(
@@ -656,8 +652,6 @@ fn link_staticlib(
656652
print_native_static_libs(sess, &print.out, &all_native_libs, &all_rust_dylibs);
657653
}
658654
}
659-
660-
Ok(())
661655
}
662656

663657
/// Use `thorin` (rust implementation of a dwarf packaging utility) to link DWARF objects into a
@@ -773,7 +767,7 @@ fn link_natively(
773767
out_filename: &Path,
774768
codegen_results: &CodegenResults,
775769
tmpdir: &Path,
776-
) -> Result<(), ErrorGuaranteed> {
770+
) {
777771
info!("preparing {:?} to {:?}", crate_type, out_filename);
778772
let (linker_path, flavor) = linker_and_flavor(sess);
779773
let self_contained_components = self_contained_components(sess, crate_type);
@@ -797,7 +791,7 @@ fn link_natively(
797791
temp_filename,
798792
codegen_results,
799793
self_contained_components,
800-
)?;
794+
);
801795

802796
linker::disable_localization(&mut cmd);
803797

@@ -1177,8 +1171,6 @@ fn link_natively(
11771171
ab.add_file(temp_filename);
11781172
ab.build(out_filename);
11791173
}
1180-
1181-
Ok(())
11821174
}
11831175

11841176
fn strip_symbols_with_external_utility(
@@ -2232,7 +2224,7 @@ fn linker_with_args(
22322224
out_filename: &Path,
22332225
codegen_results: &CodegenResults,
22342226
self_contained_components: LinkSelfContainedComponents,
2235-
) -> Result<Command, ErrorGuaranteed> {
2227+
) -> Command {
22362228
let self_contained_crt_objects = self_contained_components.is_crt_objects_enabled();
22372229
let cmd = &mut *super::linker::get_linker(
22382230
sess,
@@ -2356,7 +2348,7 @@ fn linker_with_args(
23562348
codegen_results.crate_info.used_libraries.iter(),
23572349
tmpdir,
23582350
true,
2359-
)? {
2351+
) {
23602352
cmd.add_object(&output_path);
23612353
}
23622354
// As with add_upstream_native_libraries, we need to add the upstream raw-dylib symbols in case
@@ -2388,7 +2380,7 @@ fn linker_with_args(
23882380
native_libraries_from_nonstatics,
23892381
tmpdir,
23902382
false,
2391-
)? {
2383+
) {
23922384
cmd.add_object(&output_path);
23932385
}
23942386

@@ -2435,7 +2427,7 @@ fn linker_with_args(
24352427
// to it and remove the option. Currently the last holdout is wasm32-unknown-emscripten.
24362428
add_post_link_args(cmd, sess, flavor);
24372429

2438-
Ok(cmd.take_cmd())
2430+
cmd.take_cmd()
24392431
}
24402432

24412433
fn add_order_independent_options(

compiler/rustc_codegen_ssa/src/traits/backend.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::hash::Hash;
44
use rustc_ast::expand::allocator::AllocatorKind;
55
use rustc_data_structures::fx::FxIndexMap;
66
use rustc_data_structures::sync::{DynSend, DynSync};
7-
use rustc_errors::ErrorGuaranteed;
87
use rustc_metadata::EncodedMetadata;
98
use rustc_metadata::creader::MetadataLoaderDyn;
109
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
@@ -84,13 +83,8 @@ pub trait CodegenBackend {
8483
) -> (CodegenResults, FxIndexMap<WorkProductId, WorkProduct>);
8584

8685
/// This is called on the returned [`CodegenResults`] from [`join_codegen`](Self::join_codegen).
87-
fn link(
88-
&self,
89-
sess: &Session,
90-
codegen_results: CodegenResults,
91-
outputs: &OutputFilenames,
92-
) -> Result<(), ErrorGuaranteed> {
93-
link_binary(sess, &ArArchiveBuilderBuilder, codegen_results, outputs)
86+
fn link(&self, sess: &Session, codegen_results: CodegenResults, outputs: &OutputFilenames) {
87+
link_binary(sess, &ArArchiveBuilderBuilder, codegen_results, outputs);
9488
}
9589

9690
/// Returns `true` if this backend can be safely called from multiple threads.

compiler/rustc_driver_impl/src/args.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,18 @@ impl Expander {
9999
/// If this function is intended to be used with command line arguments,
100100
/// `argv[0]` must be removed prior to calling it manually.
101101
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
102-
pub fn arg_expand_all(
103-
early_dcx: &EarlyDiagCtxt,
104-
at_args: &[String],
105-
) -> Result<Vec<String>, ErrorGuaranteed> {
102+
pub fn arg_expand_all(early_dcx: &EarlyDiagCtxt, at_args: &[String]) -> Vec<String> {
106103
let mut expander = Expander::default();
107104
let mut result = Ok(());
108105
for arg in at_args {
109106
if let Err(err) = expander.arg(arg) {
110107
result = Err(early_dcx.early_err(format!("failed to load argument file: {err}")));
111108
}
112109
}
113-
result.map(|()| expander.finish())
110+
if let Err(guar) = result {
111+
guar.raise_fatal();
112+
}
113+
expander.finish()
114114
}
115115

116116
/// Gets the raw unprocessed command-line arguments as Unicode strings, without doing any further

0 commit comments

Comments
 (0)