Skip to content

Commit e1306b3

Browse files
majaharami3l
authored andcommitted
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 08ef1b9 commit e1306b3

File tree

3 files changed

+233
-144
lines changed

3 files changed

+233
-144
lines changed

src/cli/rustup_mode.rs

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

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

115115
Ok(toolchain.rustc_version())
116116
}
@@ -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 {
@@ -1175,16 +1175,10 @@ fn show(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
11751175
}
11761176

11771177
match active_toolchain {
1178-
Ok(atc) => match atc {
1179-
(ref toolchain, Some(ref reason)) => {
1180-
writeln!(t.lock(), "{} ({})", toolchain.name(), reason)?;
1181-
writeln!(t.lock(), "{}", toolchain.rustc_version())?;
1182-
}
1183-
(ref toolchain, None) => {
1184-
writeln!(t.lock(), "{} (default)", toolchain.name())?;
1185-
writeln!(t.lock(), "{}", toolchain.rustc_version())?;
1186-
}
1187-
},
1178+
Ok((ref toolchain, ref reason)) => {
1179+
writeln!(t.lock(), "{} ({})", toolchain.name(), reason)?;
1180+
writeln!(t.lock(), "{}", toolchain.rustc_version())?;
1181+
}
11881182
Err(err) => {
11891183
let root_cause = err.root_cause();
11901184
if let Some(RustupError::ToolchainNotSelected) =
@@ -1223,7 +1217,7 @@ fn show(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
12231217
fn show_active_toolchain(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
12241218
let verbose = m.get_flag("verbose");
12251219
let cwd = utils::current_dir()?;
1226-
match cfg.find_or_install_override_toolchain_or_default(&cwd) {
1220+
match cfg.find_or_install_active_toolchain(&cwd) {
12271221
Err(e) => {
12281222
let root_cause = e.root_cause();
12291223
if let Some(RustupError::ToolchainNotSelected) =
@@ -1234,16 +1228,12 @@ fn show_active_toolchain(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
12341228
}
12351229
}
12361230
Ok((toolchain, reason)) => {
1237-
if let Some(reason) = reason {
1238-
writeln!(
1239-
process().stdout().lock(),
1240-
"{} ({})",
1241-
toolchain.name(),
1242-
reason
1243-
)?;
1244-
} else {
1245-
writeln!(process().stdout().lock(), "{} (default)", toolchain.name())?;
1246-
}
1231+
writeln!(
1232+
process().stdout().lock(),
1233+
"{} ({})",
1234+
toolchain.name(),
1235+
reason
1236+
)?;
12471237
if verbose {
12481238
writeln!(process().stdout().lock(), "{}", toolchain.rustc_version())?;
12491239
}
@@ -1408,7 +1398,7 @@ fn explicit_or_dir_toolchain2(
14081398
}
14091399
None => {
14101400
let cwd = utils::current_dir()?;
1411-
let (toolchain, _) = cfg.find_or_install_override_toolchain_or_default(&cwd)?;
1401+
let (toolchain, _) = cfg.find_or_install_active_toolchain(&cwd)?;
14121402

14131403
Ok(toolchain)
14141404
}

0 commit comments

Comments
 (0)