Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not downcast to type alias #4636

Closed
2 tasks done
2moe opened this issue Jan 13, 2023 · 4 comments
Closed
2 tasks done

Could not downcast to type alias #4636

2moe opened this issue Jan 13, 2023 · 4 comments
Labels
C-bug Category: Updating dependencies

Comments

@2moe
Copy link

2moe commented Jan 13, 2023

Please complete the following tasks

Rust Version

rustc 1.68.0-nightly (61a415be5 2023-01-12)

Clap Version

4.0.32

Minimal reproducible code

use clap::Parser;
type OptVal = Option<String>;

#[derive(Parser, Debug)]
struct Cli {
    #[arg(long, value_parser = ["turbo", "auto", "slow"])]
    mode: OptVal,
}

fn main() {
    let args = Cli::parse();
    dbg!(&args.mode);
}

Steps to reproduce the bug with the above code

cargo r -- --mode auto

Actual Behaviour

thread 'main' panicked at 'Mismatch between definition and access of `mode`. Could not downcast to core::option::Option<alloc::string::String>, need to downcast to alloc::string::String
', src/main.rs:117:11
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Expected Behaviour

[src/main.rs:21] &args.mode = Some(
    "auto",
)

Additional Context

If I don't use OptVal type, and use Option<String>, I won't have this problem.

Debug Output

[dependencies]
clap = { version = "4.0.32", features = ["derive", "wrap_help", "unicode"] }
@2moe 2moe added the C-bug Category: Updating dependencies label Jan 13, 2023
@epage
Copy link
Member

epage commented Jan 13, 2023

That application is defined to require a subcommand, so if one isn't specified you get that error.

To make something optional (arg, subcommand), you need to wrap it in an Option. One way to think of it is there isn't a way to construct Cli right now as what will we put in the run field when no subcommand is specified.

@2moe 2moe changed the title value_parser conflicts with subcommand Could not downcast to type alias Jan 13, 2023
@2moe
Copy link
Author

2moe commented Jan 13, 2023

@epage Thanks.
I have amended this issue and I am not sure if this is appropriate.
For type OptVal = Option<String>, clap will not resolve this type alias(OptVal), but will resolve Option<String>.
Is this an intentional approach?

@epage
Copy link
Member

epage commented Jan 13, 2023

Clap does a textual comparison on the type to determine what behavior is assumed so people can override it, so type aliases are intentionally treated as unique types. #4626 is discussing changing / improving the situation.

@epage
Copy link
Member

epage commented Jan 16, 2023

As this is expected behavior, I'm closing this out.

@epage epage closed this as not planned Won't fix, can't repro, duplicate, stale Jan 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Updating dependencies
Projects
None yet
Development

No branches or pull requests

2 participants