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
clap 4.3: Implementation of FnOnce
is not general enough
#4939
Comments
I just ran with #!/usr/bin/env rust-script
//! ```cargo
//! [dependencies]
//! clap = { version = "=4.2.0", features = ["derive", "debug"] }
//! anyhow = "1"
//! ```
use anyhow::Result;
use clap::Parser;
#[derive(Clone, Debug)]
struct Offset {}
impl Offset {
pub fn parse(s: impl AsRef<str>) -> Result<Self> {
Ok(Self {})
}
}
#[derive(Clone, Debug, Parser)]
struct Command {
#[arg(long, value_parser = Offset::parse)]
pub offset: Offset,
}
fn main() {
println!("Hello, world!");
} and I'm seeing the failure even if I drop to #!/usr/bin/env rust-script
//! ```cargo
//! [dependencies]
//! clap = { version = "=4.0.0", features = ["derive", "debug"] }
//! anyhow = "1"
//! ```
use anyhow::Result;
use clap::Parser;
#[derive(Clone, Debug)]
struct Offset {}
impl Offset {
pub fn parse(s: impl AsRef<str>) -> Result<Self> {
Ok(Self {})
}
}
#[derive(Clone, Debug, Parser)]
struct Command {
#[arg(long, value_parser = Offset::parse)]
pub offset: Offset,
}
fn main() {
println!("Hello, world!");
} I see the failure. |
Rust Version: 1.74.1 @epage @lukesneeringer I did a bit of diving into the the problem since I ran into something similar on latest clap & latest rust. I made the smallest reproducible example I could: Problem#[derive(Clone)]
struct Offset;
pub fn offset_parse(_: impl AsRef<str>) -> Result<Offset, String> {
Ok(Offset)
}
fn main() {
let arg = clapArg::new("offset").value_parser(offset_parse);
let _cmd = clap::Command::new("command").arg(arg);
} the compiler (nightly or stable) still seems to have a hard time at showing the actual origin of the problem when dealing with higher ranked trait bounds, the actual source of the error is the line below: clap/clap_builder/src/builder/value_parser.rs Line 910 in d092896
Partial solutionI found a reddit comment that touches very closely on the problem encountered in this issue and managed to reproduce most of the pattern: However, the last step in the post using the nested function that returns an
I've made a separate feature branch to test out the issue that includes the modified clap/clap_builder/src/builder/value_parser.rs Lines 908 to 935 in 9920bfa
As well as a unit test to reproduce the issue: clap/clap_builder/src/builder/tests.rs Lines 58 to 69 in 9920bfa
There is still a compilation error but I do believe this is closer to being compiled:
|
I've experimented with this for a while now and I think this is some limitation in the compiler - I've reported it here, we'll see: I think the best solution for now is to use a closure, i.e.: let arg = Arg::new("offset").value_parser(|a: &str| offset_parse(a)); |
Please complete the following tasks
Rust Version
1.69.0
Clap Version
4.3.0
Minimal reproducible code
Steps to reproduce the bug with the above code
Actual Behaviour
Expected Behaviour
Successful compilation.
Additional Context
This program compiled successfully with clap 4.2, and broke in clap 4.3.
Debug Output
Same as "actual behavior" above.
The text was updated successfully, but these errors were encountered: