Skip to content

Commit

Permalink
Redesign OverrideCfg to be more type-driven
Browse files Browse the repository at this point in the history
Redesigns the `OverrideCfg` type so that validity is represented in
the type system, i.e. "Parse, don't validate". Probably fixes some
subtle bugs when no toolchain is named in a rust-toolchain.toml,
implicitly selecting the default toolchain, but the default toolchain
isn't displayed as active by `rustup list toolchain` and the like.

Also fixes a bug where the `RUSTUP_TOOLCHAIN` erroneously had priority
over a `+toolchain` command line override.
  • Loading branch information
majaha authored and rami3l committed May 4, 2024
1 parent 78f6c8f commit 2522edb
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 144 deletions.
38 changes: 14 additions & 24 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub fn main() -> Result<utils::ExitCode> {
cfg.set_toolchain_override(&ResolvableToolchainName::try_from(&t[1..])?);
}

let toolchain = cfg.find_or_install_override_toolchain_or_default(&cwd)?.0;
let toolchain = cfg.find_or_install_active_toolchain(&cwd)?.0;

Ok(toolchain.rustc_version())
}
Expand Down Expand Up @@ -1082,7 +1082,7 @@ fn show(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
let cwd = utils::current_dir()?;
let installed_toolchains = cfg.list_toolchains()?;
// XXX: we may want a find_without_install capability for show.
let active_toolchain = cfg.find_or_install_override_toolchain_or_default(&cwd);
let active_toolchain = cfg.find_or_install_active_toolchain(&cwd);

// active_toolchain will carry the reason we don't have one in its detail.
let active_targets = if let Ok(ref at) = active_toolchain {
Expand Down Expand Up @@ -1182,16 +1182,10 @@ fn show(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
}

match active_toolchain {
Ok(atc) => match atc {
(ref toolchain, Some(ref reason)) => {
writeln!(t.lock(), "{} ({})", toolchain.name(), reason)?;
writeln!(t.lock(), "{}", toolchain.rustc_version())?;
}
(ref toolchain, None) => {
writeln!(t.lock(), "{} (default)", toolchain.name())?;
writeln!(t.lock(), "{}", toolchain.rustc_version())?;
}
},
Ok((ref toolchain, ref reason)) => {
writeln!(t.lock(), "{} ({})", toolchain.name(), reason)?;
writeln!(t.lock(), "{}", toolchain.rustc_version())?;
}
Err(err) => {
let root_cause = err.root_cause();
if let Some(RustupError::ToolchainNotSelected) =
Expand Down Expand Up @@ -1230,7 +1224,7 @@ fn show(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
fn show_active_toolchain(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
let verbose = m.get_flag("verbose");
let cwd = utils::current_dir()?;
match cfg.find_or_install_override_toolchain_or_default(&cwd) {
match cfg.find_or_install_active_toolchain(&cwd) {
Err(e) => {
let root_cause = e.root_cause();
if let Some(RustupError::ToolchainNotSelected) =
Expand All @@ -1241,16 +1235,12 @@ fn show_active_toolchain(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
}
}
Ok((toolchain, reason)) => {
if let Some(reason) = reason {
writeln!(
process().stdout().lock(),
"{} ({})",
toolchain.name(),
reason
)?;
} else {
writeln!(process().stdout().lock(), "{} (default)", toolchain.name())?;
}
writeln!(
process().stdout().lock(),
"{} ({})",
toolchain.name(),
reason
)?;
if verbose {
writeln!(process().stdout().lock(), "{}", toolchain.rustc_version())?;
}
Expand Down Expand Up @@ -1429,7 +1419,7 @@ fn explicit_or_dir_toolchain2(
}
None => {
let cwd = utils::current_dir()?;
let (toolchain, _) = cfg.find_or_install_override_toolchain_or_default(&cwd)?;
let (toolchain, _) = cfg.find_or_install_active_toolchain(&cwd)?;

Ok(toolchain)
}
Expand Down
Loading

0 comments on commit 2522edb

Please sign in to comment.