Navigation Menu

Skip to content

Commit

Permalink
rustc/driver: improve common patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
ljedrz committed Oct 13, 2018
1 parent 2c482d8 commit d599f5b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 58 deletions.
20 changes: 7 additions & 13 deletions src/librustc_driver/driver.rs
Expand Up @@ -1484,15 +1484,12 @@ fn write_out_deps(sess: &Session, outputs: &OutputFilenames, out_filenames: &[Pa
Ok(())
})();

match result {
Ok(()) => {}
Err(e) => {
sess.fatal(&format!(
"error writing dependencies to `{}`: {}",
deps_filename.display(),
e
));
}
if let Err(e) = result {
sess.fatal(&format!(
"error writing dependencies to `{}`: {}",
deps_filename.display(),
e
));
}
}

Expand Down Expand Up @@ -1650,10 +1647,7 @@ pub fn build_output_filenames(
// "-" as input file will cause the parser to read from stdin so we
// have to make up a name
// We want to toss everything after the final '.'
let dirpath = match *odir {
Some(ref d) => d.clone(),
None => PathBuf::new(),
};
let dirpath = (*odir).as_ref().cloned().unwrap_or_else(|| PathBuf::new());

// If a crate name is present, we use it as the link name
let stem = sess.opts
Expand Down
61 changes: 23 additions & 38 deletions src/librustc_driver/lib.rs
Expand Up @@ -224,15 +224,10 @@ fn load_backend_from_dylib(path: &Path) -> fn() -> Box<dyn CodegenBackend> {
// available for future dynamic libraries opened. This is currently used by
// loading LLVM and then making its symbols available for other dynamic
// libraries.
let lib = match DynamicLibrary::open_global_now(path) {
Ok(lib) => lib,
Err(err) => {
let err = format!("couldn't load codegen backend {:?}: {:?}",
path,
err);
early_error(ErrorOutputType::default(), &err);
}
};
let lib = DynamicLibrary::open_global_now(path).unwrap_or_else(|err| {
let err = format!("couldn't load codegen backend {:?}: {:?}", path, err);
early_error(ErrorOutputType::default(), &err);
});
unsafe {
match lib.symbol("__rustc_codegen_backend") {
Ok(f) => {
Expand Down Expand Up @@ -337,28 +332,22 @@ fn get_codegen_sysroot(backend_name: &str) -> fn() -> Box<dyn CodegenBackend> {
f.exists()
})
.next();
let sysroot = match sysroot {
Some(path) => path,
None => {
let candidates = sysroot_candidates.iter()
.map(|p| p.display().to_string())
.collect::<Vec<_>>()
.join("\n* ");
let err = format!("failed to find a `codegen-backends` folder \
in the sysroot candidates:\n* {}", candidates);
early_error(ErrorOutputType::default(), &err);
}
};
let sysroot = sysroot.unwrap_or_else(|| {
let candidates = sysroot_candidates.iter()
.map(|p| p.display().to_string())
.collect::<Vec<_>>()
.join("\n* ");
let err = format!("failed to find a `codegen-backends` folder \
in the sysroot candidates:\n* {}", candidates);
early_error(ErrorOutputType::default(), &err);
});
info!("probing {} for a codegen backend", sysroot.display());

let d = match sysroot.read_dir() {
Ok(d) => d,
Err(e) => {
let err = format!("failed to load default codegen backend, couldn't \
read `{}`: {}", sysroot.display(), e);
early_error(ErrorOutputType::default(), &err);
}
};
let d = sysroot.read_dir().unwrap_or_else(|e| {
let err = format!("failed to load default codegen backend, couldn't \
read `{}`: {}", sysroot.display(), e);
early_error(ErrorOutputType::default(), &err);
});

let mut file: Option<PathBuf> = None;

Expand Down Expand Up @@ -1055,10 +1044,8 @@ impl RustcDefaultCalls {
Sysroot => println!("{}", sess.sysroot().display()),
TargetSpec => println!("{}", sess.target.target.to_json().pretty()),
FileNames | CrateName => {
let input = match input {
Some(input) => input,
None => early_error(ErrorOutputType::default(), "no input file provided"),
};
let input = input.unwrap_or_else(||
early_error(ErrorOutputType::default(), "no input file provided"));
let attrs = attrs.as_ref().unwrap();
let t_outputs = driver::build_output_filenames(input, odir, ofile, attrs, sess);
let id = rustc_codegen_utils::link::find_crate_name(Some(sess), attrs, input);
Expand Down Expand Up @@ -1406,10 +1393,8 @@ pub fn handle_options(args: &[String]) -> Option<getopts::Matches> {
for option in config::rustc_optgroups() {
(option.apply)(&mut options);
}
let matches = match options.parse(args) {
Ok(m) => m,
Err(f) => early_error(ErrorOutputType::default(), &f.to_string()),
};
let matches = options.parse(args).unwrap_or_else(|f|
early_error(ErrorOutputType::default(), &f.to_string()));

// For all options we just parsed, we check a few aspects:
//
Expand Down Expand Up @@ -1631,7 +1616,7 @@ fn extra_compiler_flags() -> Option<(Vec<String>, bool)> {
}
}

if result.len() > 0 {
if !result.is_empty() {
Some((result, excluded_cargo_defaults))
} else {
None
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_driver/profile/mod.rs
Expand Up @@ -90,7 +90,7 @@ fn profile_queries_thread(r:Receiver<ProfileQueriesMsg>) {
match msg {
ProfileQueriesMsg::Halt => return,
ProfileQueriesMsg::Dump(params) => {
assert!(stack.len() == 0);
assert!(stack.is_empty());
assert!(frame.parse_st == ParseState::Clear);
{
// write log of all messages
Expand Down Expand Up @@ -141,7 +141,7 @@ fn profile_queries_thread(r:Receiver<ProfileQueriesMsg>) {
ProfileQueriesMsg::QueryBegin(span,querymsg)) => {
let start = Instant::now();
frame.parse_st = ParseState::HaveQuery
(Query{span:span, msg:querymsg}, start)
(Query { span, msg: querymsg }, start)
},
(ParseState::Clear,
ProfileQueriesMsg::CacheHit) => {
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_driver/profile/trace.rs
Expand Up @@ -108,11 +108,7 @@ fn html_of_fraction(frac: f64) -> (String, &'static str) {
}

fn total_duration(traces: &[Rec]) -> Duration {
let mut sum : Duration = Duration::new(0,0);
for t in traces.iter() {
sum += t.dur_total;
}
return sum
Duration::new(0, 0) + traces.iter().map(|t| t.dur_total).sum()
}

fn duration_div(nom: Duration, den: Duration) -> f64 {
Expand Down

0 comments on commit d599f5b

Please sign in to comment.