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
Default value seems to be cached, resulting in wrong default #5127
Comments
Here's a test for this: #[test]
fn default_values_type_arg() {
trait DefaultProvider {
const VALUE: usize;
}
#[derive(PartialEq, Debug)]
struct A;
#[derive(PartialEq, Debug)]
struct B;
impl DefaultProvider for A {
const VALUE: usize = 1;
}
impl DefaultProvider for B {
const VALUE: usize = 2;
}
#[derive(Args, PartialEq, Debug)]
struct Example<T: DefaultProvider> {
#[arg(default_value_t = T::VALUE)]
value: usize,
#[arg(skip)]
_marker: PhantomData<T>,
}
#[derive(Parser, PartialEq, Debug)]
enum Cli {
A(Example<A>),
B(Example<B>),
}
impl Cli {
fn value(&self) -> usize {
match self {
Cli::A(args) => args.value,
Cli::B(args) => args.value,
}
}
}
assert_eq!(
A::VALUE,
Cli::try_parse_from(["test", "a"]).unwrap().value()
);
assert_eq!(
B::VALUE,
Cli::try_parse_from(["test", "b"]).unwrap().value()
);
let help_a = utils::get_subcommand_long_help::<Cli>("a");
assert!(help_a.contains("[default: 1]"));
let help_b = utils::get_subcommand_long_help::<Cli>("b");
assert!(help_b.contains("[default: 2]"));
} |
I think the issue is that default values are created with a static |
Looks like that's why: rust-lang/rust#22991 |
ctron
added a commit
to ctron/trustification
that referenced
this issue
Sep 15, 2023
ctron
added a commit
to ctron/trustification
that referenced
this issue
Sep 15, 2023
ctron
added a commit
to ctron/trustification
that referenced
this issue
Sep 15, 2023
github-merge-queue bot
pushed a commit
to trustification/trustification
that referenced
this issue
Sep 15, 2023
epage
added
A-derive
Area: #[derive]` macro API
S-blocked
Status: Blocked on something else such as an RFC or other implementation work.
labels
Sep 15, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please complete the following tasks
Rust Version
rustc 1.72.0 (5680fa18f 2023-08-23)
Clap Version
4.4.3
Minimal reproducible code
Steps to reproduce the bug with the above code
Run the above example.
Important is that you have the same structure, where a type argument provides the default value, in more than one branch.
Actual Behaviour
Initializes with the wrong default value. Shown in help and used as value.
Expected Behaviour
Use the correct the default value.
Additional Context
No response
Debug Output
No response
The text was updated successfully, but these errors were encountered: