Skip to content

Commit 5441523

Browse files
committed
Refactor out a repeating pattern with get_or_default_sysroot
1 parent a03d19e commit 5441523

File tree

5 files changed

+15
-29
lines changed

5 files changed

+15
-29
lines changed

compiler/rustc_driver_impl/src/lib.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -888,9 +888,7 @@ pub fn version_at_macro_invocation(
888888
let debug_flags = matches.opt_strs("Z");
889889
let backend_name = debug_flags.iter().find_map(|x| x.strip_prefix("codegen-backend="));
890890
let opts = config::Options::default();
891-
let sysroot = opts.maybe_sysroot.clone().unwrap_or_else(|| {
892-
filesearch::get_or_default_sysroot().expect("Failed finding sysroot")
893-
});
891+
let sysroot = filesearch::materialize_sysroot(opts.maybe_sysroot.clone());
894892
let target = config::build_target_config(early_dcx, &opts, None, &sysroot);
895893

896894
get_codegen_backend(early_dcx, &sysroot, backend_name, &target).print_version();
@@ -1100,9 +1098,7 @@ pub fn describe_flag_categories(early_dcx: &EarlyDiagCtxt, matches: &Matches) ->
11001098
let backend_name = debug_flags.iter().find_map(|x| x.strip_prefix("codegen-backend="));
11011099

11021100
let opts = config::Options::default();
1103-
let sysroot = opts.maybe_sysroot.clone().unwrap_or_else(|| {
1104-
filesearch::get_or_default_sysroot().expect("Failed finding sysroot")
1105-
});
1101+
let sysroot = filesearch::materialize_sysroot(opts.maybe_sysroot.clone());
11061102
let target = config::build_target_config(early_dcx, &opts, None, &sysroot);
11071103

11081104
get_codegen_backend(early_dcx, &sysroot, backend_name, &target).print_passes();

compiler/rustc_interface/src/interface.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,7 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
336336

337337
let early_dcx = EarlyDiagCtxt::new(config.opts.error_format);
338338

339-
let sysroot = match &config.opts.maybe_sysroot {
340-
Some(sysroot) => sysroot.clone(),
341-
None => filesearch::get_or_default_sysroot().expect("Failed finding sysroot"),
342-
};
339+
let sysroot = filesearch::materialize_sysroot(config.opts.maybe_sysroot.clone());
343340

344341
let (codegen_backend, target_cfg) = match config.make_codegen_backend {
345342
None => {

compiler/rustc_interface/src/tests.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_session::config::{
1313
use rustc_session::lint::Level;
1414
use rustc_session::search_paths::SearchPath;
1515
use rustc_session::utils::{CanonicalizedPath, NativeLib, NativeLibKind};
16-
use rustc_session::{build_session, getopts, CompilerIO, EarlyDiagCtxt, Session};
16+
use rustc_session::{build_session, filesearch, getopts, CompilerIO, EarlyDiagCtxt, Session};
1717
use rustc_span::edition::{Edition, DEFAULT_EDITION};
1818
use rustc_span::symbol::sym;
1919
use rustc_span::{FileName, SourceFileHashAlgorithm};
@@ -38,12 +38,7 @@ fn mk_session(matches: getopts::Matches) -> (Session, Cfg) {
3838
temps_dir,
3939
};
4040

41-
let sysroot = match &sessopts.maybe_sysroot {
42-
Some(sysroot) => sysroot.clone(),
43-
None => {
44-
rustc_session::filesearch::get_or_default_sysroot().expect("Failed finding sysroot")
45-
}
46-
};
41+
let sysroot = filesearch::materialize_sysroot(sessopts.maybe_sysroot.clone());
4742

4843
let target_cfg =
4944
rustc_session::config::build_target_config(&early_dcx, &sessopts, None, &sysroot);

compiler/rustc_session/src/config.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use crate::options::*;
66
use crate::errors::FileWriteFail;
77
use crate::search_paths::SearchPath;
88
use crate::utils::{CanonicalizedPath, NativeLib, NativeLibKind};
9-
use crate::{lint, HashStableContext};
9+
use crate::{filesearch, lint, HashStableContext};
1010
use crate::{EarlyDiagCtxt, Session};
1111
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
1212
use rustc_data_structures::stable_hasher::{StableOrd, ToStableHashKey};
@@ -2856,16 +2856,8 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
28562856

28572857
let logical_env = parse_logical_env(early_dcx, matches);
28582858

2859-
// Try to find a directory containing the Rust `src`, for more details see
2860-
// the doc comment on the `real_rust_source_base_dir` field.
2861-
let tmp_buf;
2862-
let sysroot = match &sysroot_opt {
2863-
Some(s) => s,
2864-
None => {
2865-
tmp_buf = crate::filesearch::get_or_default_sysroot().expect("Failed finding sysroot");
2866-
&tmp_buf
2867-
}
2868-
};
2859+
let sysroot = filesearch::materialize_sysroot(sysroot_opt);
2860+
28692861
let real_rust_source_base_dir = {
28702862
// This is the location used by the `rust-src` `rustup` component.
28712863
let mut candidate = sysroot.join("lib/rustlib/src/rust");
@@ -2909,7 +2901,7 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
29092901
describe_lints,
29102902
output_types,
29112903
search_paths,
2912-
maybe_sysroot: sysroot_opt,
2904+
maybe_sysroot: Some(sysroot),
29132905
target_triple,
29142906
test,
29152907
incremental,

compiler/rustc_session/src/filesearch.rs

+6
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ pub fn sysroot_candidates() -> SmallVec<[PathBuf; 2]> {
194194
return sysroot_candidates;
195195
}
196196

197+
/// Returns the provided sysroot or calls [`get_or_default_sysroot`] if it's none.
198+
/// Panics if [`get_or_default_sysroot`] returns an error.
199+
pub fn materialize_sysroot(maybe_sysroot: Option<PathBuf>) -> PathBuf {
200+
maybe_sysroot.unwrap_or_else(|| get_or_default_sysroot().expect("Failed finding sysroot"))
201+
}
202+
197203
/// This function checks if sysroot is found using env::args().next(), and if it
198204
/// is not found, finds sysroot from current rustc_driver dll.
199205
pub fn get_or_default_sysroot() -> Result<PathBuf, String> {

0 commit comments

Comments
 (0)