Skip to content

Commit 472f7c9

Browse files
committed
Simplify run_compiler control flow.
I find `Compilation::and_then` hard to read. This commit removes it, simplifying the control flow in `run_compiler`, and reducing the number of lines of code. In particular, `list_metadata` and `process_try_link` (renamed `rlink`) are now only called if the relevant condition is true, rather than that condition being checked within the function.
1 parent 5659cc5 commit 472f7c9

File tree

1 file changed

+54
-72
lines changed
  • compiler/rustc_driver_impl/src

1 file changed

+54
-72
lines changed

compiler/rustc_driver_impl/src/lib.rs

+54-72
Original file line numberDiff line numberDiff line change
@@ -373,20 +373,21 @@ fn run_compiler(
373373

374374
let handler = EarlyErrorHandler::new(sess.opts.error_format);
375375

376-
let should_stop = print_crate_info(&handler, codegen_backend, sess, has_input);
376+
if print_crate_info(&handler, codegen_backend, sess, has_input) == Compilation::Stop {
377+
return sess.compile_status();
378+
}
377379

378380
if !has_input {
379-
if should_stop == Compilation::Continue {
380-
handler.early_error("no input filename given")
381-
}
382-
return sess.compile_status();
381+
handler.early_error("no input filename given"); // this is fatal
383382
}
384383

385-
let should_stop = should_stop
386-
.and_then(|| list_metadata(&handler, sess, &*codegen_backend.metadata_loader()))
387-
.and_then(|| try_process_rlink(sess, compiler));
384+
if !sess.opts.unstable_opts.ls.is_empty() {
385+
list_metadata(&handler, sess, &*codegen_backend.metadata_loader());
386+
return sess.compile_status();
387+
}
388388

389-
if should_stop == Compilation::Stop {
389+
if sess.opts.unstable_opts.link_only {
390+
process_rlink(sess, compiler);
390391
return sess.compile_status();
391392
}
392393

@@ -539,15 +540,6 @@ pub enum Compilation {
539540
Continue,
540541
}
541542

542-
impl Compilation {
543-
fn and_then<F: FnOnce() -> Compilation>(self, next: F) -> Compilation {
544-
match self {
545-
Compilation::Stop => Compilation::Stop,
546-
Compilation::Continue => next(),
547-
}
548-
}
549-
}
550-
551543
fn handle_explain(handler: &EarlyErrorHandler, registry: Registry, code: &str, color: ColorConfig) {
552544
let upper_cased_code = code.to_ascii_uppercase();
553545
let normalised =
@@ -652,70 +644,60 @@ fn show_md_content_with_pager(content: &str, color: ColorConfig) {
652644
}
653645
}
654646

655-
fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Compilation {
656-
if sess.opts.unstable_opts.link_only {
657-
if let Input::File(file) = &sess.io.input {
658-
let outputs = compiler.build_output_filenames(sess, &[]);
659-
let rlink_data = fs::read(file).unwrap_or_else(|err| {
660-
sess.emit_fatal(RlinkUnableToRead { err });
661-
});
662-
let codegen_results = match CodegenResults::deserialize_rlink(sess, rlink_data) {
663-
Ok(codegen) => codegen,
664-
Err(err) => {
665-
match err {
666-
CodegenErrors::WrongFileType => sess.emit_fatal(RLinkWrongFileType),
667-
CodegenErrors::EmptyVersionNumber => {
668-
sess.emit_fatal(RLinkEmptyVersionNumber)
669-
}
670-
CodegenErrors::EncodingVersionMismatch { version_array, rlink_version } => {
671-
sess.emit_fatal(RLinkEncodingVersionMismatch {
672-
version_array,
673-
rlink_version,
674-
})
675-
}
676-
CodegenErrors::RustcVersionMismatch { rustc_version } => {
677-
sess.emit_fatal(RLinkRustcVersionMismatch {
678-
rustc_version,
679-
current_version: sess.cfg_version,
680-
})
681-
}
682-
};
683-
}
684-
};
685-
let result = compiler.codegen_backend().link(sess, codegen_results, &outputs);
686-
abort_on_err(result, sess);
687-
} else {
688-
sess.emit_fatal(RlinkNotAFile {})
689-
}
690-
Compilation::Stop
647+
fn process_rlink(sess: &Session, compiler: &interface::Compiler) {
648+
assert!(sess.opts.unstable_opts.link_only);
649+
if let Input::File(file) = &sess.io.input {
650+
let outputs = compiler.build_output_filenames(sess, &[]);
651+
let rlink_data = fs::read(file).unwrap_or_else(|err| {
652+
sess.emit_fatal(RlinkUnableToRead { err });
653+
});
654+
let codegen_results = match CodegenResults::deserialize_rlink(sess, rlink_data) {
655+
Ok(codegen) => codegen,
656+
Err(err) => {
657+
match err {
658+
CodegenErrors::WrongFileType => sess.emit_fatal(RLinkWrongFileType),
659+
CodegenErrors::EmptyVersionNumber => sess.emit_fatal(RLinkEmptyVersionNumber),
660+
CodegenErrors::EncodingVersionMismatch { version_array, rlink_version } => sess
661+
.emit_fatal(RLinkEncodingVersionMismatch { version_array, rlink_version }),
662+
CodegenErrors::RustcVersionMismatch { rustc_version } => {
663+
sess.emit_fatal(RLinkRustcVersionMismatch {
664+
rustc_version,
665+
current_version: sess.cfg_version,
666+
})
667+
}
668+
};
669+
}
670+
};
671+
let result = compiler.codegen_backend().link(sess, codegen_results, &outputs);
672+
abort_on_err(result, sess);
691673
} else {
692-
Compilation::Continue
674+
sess.emit_fatal(RlinkNotAFile {})
693675
}
694676
}
695677

696678
fn list_metadata(
697679
handler: &EarlyErrorHandler,
698680
sess: &Session,
699681
metadata_loader: &dyn MetadataLoader,
700-
) -> Compilation {
701-
let ls_kinds = &sess.opts.unstable_opts.ls;
702-
if !ls_kinds.is_empty() {
703-
match sess.io.input {
704-
Input::File(ref ifile) => {
705-
let path = &(*ifile);
706-
let mut v = Vec::new();
707-
locator::list_file_metadata(&sess.target, path, metadata_loader, &mut v, ls_kinds)
708-
.unwrap();
709-
safe_println!("{}", String::from_utf8(v).unwrap());
710-
}
711-
Input::Str { .. } => {
712-
handler.early_error("cannot list metadata for stdin");
713-
}
682+
) {
683+
match sess.io.input {
684+
Input::File(ref ifile) => {
685+
let path = &(*ifile);
686+
let mut v = Vec::new();
687+
locator::list_file_metadata(
688+
&sess.target,
689+
path,
690+
metadata_loader,
691+
&mut v,
692+
&sess.opts.unstable_opts.ls,
693+
)
694+
.unwrap();
695+
safe_println!("{}", String::from_utf8(v).unwrap());
696+
}
697+
Input::Str { .. } => {
698+
handler.early_error("cannot list metadata for stdin");
714699
}
715-
return Compilation::Stop;
716700
}
717-
718-
Compilation::Continue
719701
}
720702

721703
fn print_crate_info(

0 commit comments

Comments
 (0)