Skip to content

Commit

Permalink
Extract update_override()
Browse files Browse the repository at this point in the history
  • Loading branch information
rami3l committed Sep 25, 2023
1 parent ddb5a4b commit b7d9c87
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,26 +487,18 @@ impl Cfg {
}

fn find_override_config(&self, path: &Path) -> Result<Option<(OverrideCfg, OverrideReason)>> {
let mut override_ = None::<(OverrideFile, OverrideReason)>;

let mut update_override = |file, reason| {
if let Some((file1, reason1)) = &mut override_ {
if file1.has_toolchain() {
// Update the reason only if the override file has a toolchain.
*reason1 = reason
}
file1.merge(file);
} else {
override_ = Some((file, reason))
};
};
let mut override_ = None;

// Check for all possible toolchain overrides below...
// See: <https://rust-lang.github.io/rustup/overrides.html>

// 1. Check toolchain override from command
if let Some(ref name) = self.toolchain_override {
update_override(name.to_string().into(), OverrideReason::CommandLine);
update_override(
&mut override_,
name.to_string().into(),
OverrideReason::CommandLine,
);
}

// 2. Check RUSTUP_TOOLCHAIN
Expand All @@ -515,15 +507,19 @@ impl Cfg {
// custom, distributable, and absolute path toolchains otherwise
// rustup's export of a RUSTUP_TOOLCHAIN when running a process will
// error when a nested rustup invocation occurs
update_override(name.to_string().into(), OverrideReason::Environment);
update_override(
&mut override_,
name.to_string().into(),
OverrideReason::Environment,
);
}

// 3. walk up the directory tree from 'path' looking for either the
// directory in override database, or
// 4. a `rust-toolchain` file.
self.settings_file.with(|s| {
if let Some((file, reason)) = self.find_override_from_dir_walk(path, s)? {
update_override(file, reason);
update_override(&mut override_, file, reason);
}
Ok(())
})?;
Expand Down Expand Up @@ -1006,6 +1002,22 @@ impl Cfg {
}
}

fn update_override(
override_: &mut Option<(OverrideFile, OverrideReason)>,
file: OverrideFile,
reason: OverrideReason,
) {
if let Some((file1, reason1)) = override_ {
if file1.has_toolchain() {
// Update the reason only if the override file has a toolchain.
*reason1 = reason
}
file1.merge(file);
} else {
*override_ = Some((file, reason))
};
}

fn get_default_host_triple(s: &Settings) -> dist::TargetTriple {
s.default_host_triple
.as_ref()
Expand Down

0 comments on commit b7d9c87

Please sign in to comment.