Skip to content

Commit 32449c3

Browse files
committed
Fix panic in component list --toolchain stable
Fixes a panic caused by the signature of the parser passed to Claps's `Arg.value_parser()` having PartialToolchainDesc, not matching `ArgMatches.get_one::<ResolvableToolchainName>()` in `explicit_or_dir_toolchain()` Also, rename `explicit_or_dir_toolchain()` -> `explicit_desc_or_dir_toolchain()` and consolidate its use across various CLI subcommands. Also fixes a similar panic in `rustup man --toolchain stable`
1 parent 94d41c6 commit 32449c3

File tree

1 file changed

+15
-28
lines changed

1 file changed

+15
-28
lines changed

src/cli/rustup_mode.rs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ pub(crate) fn cli() -> Command {
596596
.help(OFFICIAL_TOOLCHAIN_ARG_HELP)
597597
.long("toolchain")
598598
.num_args(1)
599-
.value_parser( partial_toolchain_desc_parser),
599+
.value_parser(partial_toolchain_desc_parser),
600600
)
601601
.arg(
602602
Arg::new("target")
@@ -614,7 +614,7 @@ pub(crate) fn cli() -> Command {
614614
.help(OFFICIAL_TOOLCHAIN_ARG_HELP)
615615
.long("toolchain")
616616
.num_args(1)
617-
.value_parser( partial_toolchain_desc_parser),
617+
.value_parser(partial_toolchain_desc_parser),
618618
)
619619
.arg(
620620
Arg::new("target")
@@ -1263,10 +1263,7 @@ fn show_rustup_home(cfg: &Cfg) -> Result<utils::ExitCode> {
12631263
}
12641264

12651265
fn target_list(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
1266-
let toolchain = m
1267-
.get_one::<PartialToolchainDesc>("toolchain")
1268-
.map(Into::into);
1269-
let toolchain = explicit_or_dir_toolchain2(cfg, toolchain)?;
1266+
let toolchain = explicit_desc_or_dir_toolchain(cfg, m)?;
12701267
// downcasting required because the toolchain files can name any toolchain
12711268
let distributable = (&toolchain).try_into()?;
12721269

@@ -1278,10 +1275,7 @@ fn target_list(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
12781275
}
12791276

12801277
fn target_add(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
1281-
let toolchain_name = m
1282-
.get_one::<PartialToolchainDesc>("toolchain")
1283-
.map(Into::into);
1284-
let toolchain = explicit_or_dir_toolchain2(cfg, toolchain_name)?;
1278+
let toolchain = explicit_desc_or_dir_toolchain(cfg, m)?;
12851279
// XXX: long term move this error to cli ? the normal .into doesn't work
12861280
// because Result here is the wrong sort and expression type ascription
12871281
// isn't a feature yet.
@@ -1336,10 +1330,7 @@ fn target_add(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
13361330
}
13371331

13381332
fn target_remove(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
1339-
let toolchain = m
1340-
.get_one::<PartialToolchainDesc>("toolchain")
1341-
.map(Into::into);
1342-
let toolchain = explicit_or_dir_toolchain2(cfg, toolchain)?;
1333+
let toolchain = explicit_desc_or_dir_toolchain(cfg, m)?;
13431334
let distributable = DistributableToolchain::try_from(&toolchain)?;
13441335

13451336
for target in m.get_many::<String>("target").unwrap() {
@@ -1355,7 +1346,7 @@ fn target_remove(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
13551346
}
13561347

13571348
fn component_list(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
1358-
let toolchain = explicit_or_dir_toolchain(cfg, m)?;
1349+
let toolchain = explicit_desc_or_dir_toolchain(cfg, m)?;
13591350
// downcasting required because the toolchain files can name any toolchain
13601351
let distributable = (&toolchain).try_into()?;
13611352

@@ -1368,10 +1359,7 @@ fn component_list(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
13681359
}
13691360

13701361
fn component_add(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
1371-
let toolchain = m
1372-
.get_one::<PartialToolchainDesc>("toolchain")
1373-
.map(Into::into);
1374-
let toolchain = explicit_or_dir_toolchain2(cfg, toolchain)?;
1362+
let toolchain = explicit_desc_or_dir_toolchain(cfg, m)?;
13751363
let distributable = DistributableToolchain::try_from(&toolchain)?;
13761364
let target = get_target(m, &distributable);
13771365

@@ -1392,7 +1380,7 @@ fn get_target(m: &ArgMatches, distributable: &DistributableToolchain<'_>) -> Opt
13921380
}
13931381

13941382
fn component_remove(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
1395-
let toolchain = explicit_or_dir_toolchain(cfg, m)?;
1383+
let toolchain = explicit_desc_or_dir_toolchain(cfg, m)?;
13961384
let distributable = DistributableToolchain::try_from(&toolchain)?;
13971385
let target = get_target(m, &distributable);
13981386

@@ -1405,9 +1393,11 @@ fn component_remove(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
14051393
Ok(utils::ExitCode(0))
14061394
}
14071395

1408-
fn explicit_or_dir_toolchain<'a>(cfg: &'a Cfg, m: &ArgMatches) -> Result<Toolchain<'a>> {
1409-
let toolchain = m.get_one::<ResolvableToolchainName>("toolchain");
1410-
explicit_or_dir_toolchain2(cfg, toolchain.cloned())
1396+
// Make *sure* only to use this for a subcommand whose "toolchain" argument
1397+
// has .value_parser(partial_toolchain_desc_parser), or it will panic.
1398+
fn explicit_desc_or_dir_toolchain<'a>(cfg: &'a Cfg, m: &ArgMatches) -> Result<Toolchain<'a>> {
1399+
let toolchain = m.get_one::<PartialToolchainDesc>("toolchain").map(Into::into);
1400+
explicit_or_dir_toolchain2(cfg, toolchain)
14111401
}
14121402

14131403
fn explicit_or_dir_toolchain2(
@@ -1556,10 +1546,7 @@ const DOCS_DATA: &[(&str, &str, &str)] = &[
15561546
];
15571547

15581548
fn doc(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
1559-
let toolchain = m
1560-
.get_one::<PartialToolchainDesc>("toolchain")
1561-
.map(Into::into);
1562-
let toolchain = explicit_or_dir_toolchain2(cfg, toolchain)?;
1549+
let toolchain = explicit_desc_or_dir_toolchain(cfg, m)?;
15631550

15641551
if let Ok(distributable) = DistributableToolchain::try_from(&toolchain) {
15651552
let manifestation = distributable.get_manifestation()?;
@@ -1613,7 +1600,7 @@ fn doc(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
16131600
fn man(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
16141601
let command = m.get_one::<String>("command").unwrap();
16151602

1616-
let toolchain = explicit_or_dir_toolchain(cfg, m)?;
1603+
let toolchain = explicit_desc_or_dir_toolchain(cfg, m)?;
16171604
let mut path = toolchain.path().to_path_buf();
16181605
path.push("share");
16191606
path.push("man");

0 commit comments

Comments
 (0)