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

derive(Parser) fails when using type alias #5019

Closed
2 tasks done
mamekoro opened this issue Jul 19, 2023 · 1 comment
Closed
2 tasks done

derive(Parser) fails when using type alias #5019

mamekoro opened this issue Jul 19, 2023 · 1 comment
Labels
A-derive Area: #[derive]` macro API C-bug Category: Updating dependencies

Comments

@mamekoro
Copy link

Please complete the following tasks

Rust Version

rustc 1.71.0 (8ede3aae2 2023-07-12)

Clap Version

4.3.16

Minimal reproducible code

/*
[dependencies]
clap = { version = "4.3.16", features = ["derive"] }
*/

use clap::Parser;

pub type MountOpt = Vec<String>;

#[derive(Clone, Debug, Parser)]
pub struct Args {
    #[arg(long, value_name = "mount")]
    pub mount: Option<MountOpt>,
}

fn main() {
    println!("{:?}", Args::parse());
}

Steps to reproduce the bug with the above code

cargo build

Actual Behaviour

error[E0599]: the method `value_parser` exists for reference `&&&&&&_AutoValueParser<Vec<String>>`, but its trait bounds were not satisfied
    --> src/main.rs:12:5
     |
12   |     #[arg(long, value_name = "mount")]
     |     ^ method cannot be called on `&&&&&&_AutoValueParser<Vec<String>>` due to unsatisfied trait bounds
     |
    ::: <HOME>/.cargo/registry/src/index.crates.io-6f17d22bba15001f/clap_builder-4.3.16/src/builder/value_parser.rs:2274:1
     |
2274 | pub struct _AutoValueParser<T>(std::marker::PhantomData<T>);
     | ------------------------------ doesn't satisfy `_: _ValueParserViaParse`
     |
    ::: <HOME>/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:396:1
     |
396  | pub struct Vec<T, #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global...
     | ------------------------------------------------------------------------------------------------
     | |
     | doesn't satisfy `Vec<std::string::String>: From<&'s str>`
     | doesn't satisfy `Vec<std::string::String>: From<OsString>`
     | doesn't satisfy `Vec<std::string::String>: FromStr`
     | doesn't satisfy `Vec<std::string::String>: ValueEnum`
     | doesn't satisfy `Vec<std::string::String>: ValueParserFactory`
     | doesn't satisfy `_: From<&OsStr>`
     | doesn't satisfy `_: From<String>`
     |
     = note: the following trait bounds were not satisfied:
             `Vec<std::string::String>: ValueEnum`
             which is required by `&&&&&_AutoValueParser<Vec<std::string::String>>: clap::builder::via_prelude::_ValueParserViaValueEnum`
             `Vec<std::string::String>: ValueParserFactory`
             which is required by `&&&&&&_AutoValueParser<Vec<std::string::String>>: clap::builder::via_prelude::_ValueParserViaFactory`
             `Vec<std::string::String>: From<OsString>`
             which is required by `&&&&_AutoValueParser<Vec<std::string::String>>: clap::builder::via_prelude::_ValueParserViaFromOsString`
             `Vec<std::string::String>: From<&'s std::ffi::OsStr>`
             which is required by `&&&_AutoValueParser<Vec<std::string::String>>: clap::builder::via_prelude::_ValueParserViaFromOsStr`
             `Vec<std::string::String>: From<std::string::String>`
             which is required by `&&_AutoValueParser<Vec<std::string::String>>: clap::builder::via_prelude::_ValueParserViaFromString`
             `Vec<std::string::String>: From<&'s str>`
             which is required by `&_AutoValueParser<Vec<std::string::String>>: clap::builder::via_prelude::_ValueParserViaFromStr`
             `Vec<std::string::String>: FromStr`
             which is required by `_AutoValueParser<Vec<std::string::String>>: clap::builder::via_prelude::_ValueParserViaParse`
     = note: this error originates in the macro `clap::value_parser` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0599`.
error: could not compile `test-clap` (bin "test-clap") due to 2 previous errors

Expected Behaviour

Compilation should success.

Additional Context

I was writing a program to communicate with a Docker server, using Docker CLI (written in Go) as a reference.
I decided to declare type aliases to make the types in my code and Docker CLI similar, and I faced this problem.

Debug Output

The same as "Actual Behaviour" above. (checked with the diff command.)

@mamekoro mamekoro added the C-bug Category: Updating dependencies label Jul 19, 2023
@epage
Copy link
Member

epage commented Jul 19, 2023

This is actually expected behavior and is a documented way to disable the derive from inferring behavior from the type.

Closing in favor of #4626 which is discussing how to improve this

@epage epage closed this as not planned Won't fix, can't repro, duplicate, stale Jul 19, 2023
@epage epage added the A-derive Area: #[derive]` macro API label Jul 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-derive Area: #[derive]` macro API C-bug Category: Updating dependencies
Projects
None yet
Development

No branches or pull requests

2 participants