Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions conformance/tests/update_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,3 +642,42 @@ fn a_standing_group_member_conflicts_with_a_sibling_flag() {
"standing --yaml still conflicts with --strict",
);
}

/// A counted `u8` whose presence is "not the default". The generated
/// `!= Default::default()` must name `u8`, or `serde_json`'s `PartialEq<Value> for u8`
/// makes the comparison ambiguous — hk's CLI hit that on every build that pulls
/// `serde_json` into the same crate.
#[derive(Cli, Debug, PartialEq)]
#[usage(bin = "counted", completion)]
struct Counted {
#[usage(short, long, global, count, overrides("--quiet"))]
verbose: u8,
#[usage(short, long, global, overrides("--verbose"))]
quiet: bool,
#[usage(subcommand)]
command: CountedCmd,
}

#[derive(Subcommands, Debug, PartialEq)]
enum CountedCmd {
Run,
}

#[test]
fn a_standing_count_compiles_beside_serde_json() {
// Keep `serde_json` live in this module so its `PartialEq` impls stay in scope.
let _ = serde_json::json!({"n": 1u8});

let a = argv(["-vv", "run"]);
let mut counted = Counted::parse_from(&a).expect("two -v");
assert_eq!(counted.verbose, 2);

// A second line that says nothing about `-v` must keep the standing count —
// presence is "not the default", and that comparison has to name `u8`.
let a = argv(["run"]);
counted
.try_update_from(&a)
.expect("the standing count answers for itself");
assert_eq!(counted.verbose, 2);
assert!(!counted.quiet);
}
11 changes: 9 additions & 2 deletions derive/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3233,7 +3233,13 @@ fn standing_presence(field: &Field) -> Option<TokenStream> {
}),
Kind::Flag { .. } | Kind::Arg { .. } => Some(match field.shape {
Shape::Bool => quote!(__usage_s.#ident),
Shape::Count => quote!(__usage_s.#ident != ::std::default::Default::default()),
// Name the field's type: `Default::default()` alone is ambiguous when an
// adopter also brings `serde_json::Value`'s `PartialEq<u8>` into scope
// (hk's `verbose: u8` count under a workspace that pulls serde_json).
Shape::Count => {
let ty = &field.ty;
quote!(__usage_s.#ident != <#ty as ::std::default::Default>::default())
}
Shape::Optional => quote!(__usage_s.#ident.is_some()),
// Nowhere to put "absent", so the field always holds something. The same
// reading of the type that makes it required in the first place.
Expand Down Expand Up @@ -4918,7 +4924,8 @@ fn merge_present(field: &Field) -> TokenStream {
match field.shape {
Shape::Bool => quote!(partial.#given || partial.#ident),
Shape::Count => {
quote!(partial.#given || partial.#ident != ::std::default::Default::default())
let ty = &field.ty;
quote!(partial.#given || partial.#ident != <#ty as ::std::default::Default>::default())
}
Shape::Optional => quote!(partial.#given || partial.#ident.is_some()),
Shape::Required | Shape::Many => quote!(partial.#given || !partial.#ident.is_empty()),
Expand Down