Skip to content

Commit 23e1b0e

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 08d7498 commit 23e1b0e

File tree

1 file changed

+56
-79
lines changed
  • compiler/rustc_driver_impl/src

1 file changed

+56
-79
lines changed

compiler/rustc_driver_impl/src/lib.rs

+56-79
Original file line numberDiff line numberDiff line change
@@ -372,27 +372,23 @@ fn run_compiler(
372372

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

375-
let should_stop = print_crate_info(
376-
&handler,
377-
&**compiler.codegen_backend(),
378-
compiler.session(),
379-
has_input,
380-
);
375+
if print_crate_info(&handler, &**compiler.codegen_backend(), compiler.session(), has_input)
376+
== Compilation::Stop
377+
{
378+
return sess.compile_status();
379+
}
381380

382381
if !has_input {
383-
if should_stop == Compilation::Continue {
384-
handler.early_error("no input filename given")
385-
}
386-
return sess.compile_status();
382+
handler.early_error("no input filename given");
387383
}
388384

389-
let should_stop = should_stop
390-
.and_then(|| {
391-
list_metadata(&handler, sess, &*compiler.codegen_backend().metadata_loader())
392-
})
393-
.and_then(|| try_process_rlink(sess, compiler));
385+
if !sess.opts.unstable_opts.ls.is_empty() {
386+
list_metadata(&handler, &sess, &*compiler.codegen_backend().metadata_loader());
387+
return sess.compile_status();
388+
}
394389

395-
if should_stop == Compilation::Stop {
390+
if sess.opts.unstable_opts.link_only {
391+
rlink(sess, compiler);
396392
return sess.compile_status();
397393
}
398394

@@ -545,15 +541,6 @@ pub enum Compilation {
545541
Continue,
546542
}
547543

548-
impl Compilation {
549-
fn and_then<F: FnOnce() -> Compilation>(self, next: F) -> Compilation {
550-
match self {
551-
Compilation::Stop => Compilation::Stop,
552-
Compilation::Continue => next(),
553-
}
554-
}
555-
}
556-
557544
fn handle_explain(handler: &EarlyErrorHandler, registry: Registry, code: &str, color: ColorConfig) {
558545
let upper_cased_code = code.to_ascii_uppercase();
559546
let normalised =
@@ -658,70 +645,60 @@ fn show_md_content_with_pager(content: &str, color: ColorConfig) {
658645
}
659646
}
660647

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

702679
fn list_metadata(
703680
handler: &EarlyErrorHandler,
704681
sess: &Session,
705682
metadata_loader: &dyn MetadataLoader,
706-
) -> Compilation {
707-
let ls_kinds = &sess.opts.unstable_opts.ls;
708-
if !ls_kinds.is_empty() {
709-
match sess.io.input {
710-
Input::File(ref ifile) => {
711-
let path = &(*ifile);
712-
let mut v = Vec::new();
713-
locator::list_file_metadata(&sess.target, path, metadata_loader, &mut v, ls_kinds)
714-
.unwrap();
715-
safe_println!("{}", String::from_utf8(v).unwrap());
716-
}
717-
Input::Str { .. } => {
718-
handler.early_error("cannot list metadata for stdin");
719-
}
683+
) {
684+
match sess.io.input {
685+
Input::File(ref ifile) => {
686+
let path = &(*ifile);
687+
let mut v = Vec::new();
688+
locator::list_file_metadata(
689+
&sess.target,
690+
path,
691+
metadata_loader,
692+
&mut v,
693+
&sess.opts.unstable_opts.ls,
694+
)
695+
.unwrap();
696+
safe_println!("{}", String::from_utf8(v).unwrap());
697+
}
698+
Input::Str { .. } => {
699+
handler.early_error("cannot list metadata for stdin");
720700
}
721-
return Compilation::Stop;
722701
}
723-
724-
Compilation::Continue
725702
}
726703

727704
fn print_crate_info(

0 commit comments

Comments
 (0)