Skip to content

Commit c65e5f8

Browse files
committed
Redesign OverrideCfg to be more type-driven
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.
1 parent f6d708c commit c65e5f8

File tree

3 files changed

+233
-144
lines changed

3 files changed

+233
-144
lines changed

src/cli/rustup_mode.rs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ pub fn main() -> Result<utils::ExitCode> {
111111
cfg.set_toolchain_override(&ResolvableToolchainName::try_from(&t[1..])?);
112112
}
113113

114-
let toolchain = cfg.find_or_install_override_toolchain_or_default(&cwd)?.0;
114+
let toolchain = cfg.find_or_install_active_toolchain(&cwd)?.0;
115115

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

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

11841184
match active_toolchain {
1185-
Ok(atc) => match atc {
1186-
(ref toolchain, Some(ref reason)) => {
1187-
writeln!(t.lock(), "{} ({})", toolchain.name(), reason)?;
1188-
writeln!(t.lock(), "{}", toolchain.rustc_version())?;
1189-
}
1190-
(ref toolchain, None) => {
1191-
writeln!(t.lock(), "{} (default)", toolchain.name())?;
1192-
writeln!(t.lock(), "{}", toolchain.rustc_version())?;
1193-
}
1194-
},
1185+
Ok((ref toolchain, ref reason)) => {
1186+
writeln!(t.lock(), "{} ({})", toolchain.name(), reason)?;
1187+
writeln!(t.lock(), "{}", toolchain.rustc_version())?;
1188+
}
11951189
Err(err) => {
11961190
let root_cause = err.root_cause();
11971191
if let Some(RustupError::ToolchainNotSelected) =
@@ -1230,7 +1224,7 @@ fn show(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
12301224
fn show_active_toolchain(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
12311225
let verbose = m.get_flag("verbose");
12321226
let cwd = utils::current_dir()?;
1233-
match cfg.find_or_install_override_toolchain_or_default(&cwd) {
1227+
match cfg.find_or_install_active_toolchain(&cwd) {
12341228
Err(e) => {
12351229
let root_cause = e.root_cause();
12361230
if let Some(RustupError::ToolchainNotSelected) =
@@ -1241,16 +1235,12 @@ fn show_active_toolchain(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
12411235
}
12421236
}
12431237
Ok((toolchain, reason)) => {
1244-
if let Some(reason) = reason {
1245-
writeln!(
1246-
process().stdout().lock(),
1247-
"{} ({})",
1248-
toolchain.name(),
1249-
reason
1250-
)?;
1251-
} else {
1252-
writeln!(process().stdout().lock(), "{} (default)", toolchain.name())?;
1253-
}
1238+
writeln!(
1239+
process().stdout().lock(),
1240+
"{} ({})",
1241+
toolchain.name(),
1242+
reason
1243+
)?;
12541244
if verbose {
12551245
writeln!(process().stdout().lock(), "{}", toolchain.rustc_version())?;
12561246
}
@@ -1416,7 +1406,7 @@ fn explicit_or_dir_toolchain2(
14161406
}
14171407
None => {
14181408
let cwd = utils::current_dir()?;
1419-
let (toolchain, _) = cfg.find_or_install_override_toolchain_or_default(&cwd)?;
1409+
let (toolchain, _) = cfg.find_or_install_active_toolchain(&cwd)?;
14201410

14211411
Ok(toolchain)
14221412
}

0 commit comments

Comments
 (0)