diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c35dd2..0a0f9c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # v0.3.23 (unreleased) * Update minimal rust version to 1.46 because of bitflags 1.3 +* Fixed [a bug that occurs when the type of `map` becomes ambiguous](https://github.com/TeXitoi/structopt/issues/490). # v0.3.22 (2021-07-04) diff --git a/structopt-derive/src/lib.rs b/structopt-derive/src/lib.rs index b8e86de..b80ae17 100644 --- a/structopt-derive/src/lib.rs +++ b/structopt-derive/src/lib.rs @@ -330,6 +330,13 @@ fn gen_constructor(fields: &Punctuated, parent_attribute: &Attrs) let flag = *attrs.parser().kind == ParserKind::FromFlag; let occurrences = *attrs.parser().kind == ParserKind::FromOccurrences; let name = attrs.cased_name(); + let convert_type = match **ty { + Ty::Vec | Ty::Option => sub_type(&field.ty).unwrap_or(&field.ty), + Ty::OptionOption | Ty::OptionVec => { + sub_type(&field.ty).and_then(sub_type).unwrap_or(&field.ty) + } + _ => &field.ty, + }; let field_value = match **ty { Ty::Bool => quote_spanned!(ty.span()=> #matches.is_present(#name)), @@ -349,7 +356,7 @@ fn gen_constructor(fields: &Punctuated, parent_attribute: &Attrs) Ty::OptionVec => quote_spanned! { ty.span()=> if #matches.is_present(#name) { Some(#matches.#values_of(#name) - .map_or_else(Vec::new, |v| v.map(#parse).collect())) + .map_or_else(Vec::new, |v| v.map::<#convert_type, _>(#parse).collect())) } else { None } @@ -357,7 +364,7 @@ fn gen_constructor(fields: &Punctuated, parent_attribute: &Attrs) Ty::Vec => quote_spanned! { ty.span()=> #matches.#values_of(#name) - .map_or_else(Vec::new, |v| v.map(#parse).collect()) + .map_or_else(Vec::new, |v| v.map::<#convert_type, _>(#parse).collect()) }, Ty::Other if occurrences => quote_spanned! { ty.span()=> diff --git a/tests/issues.rs b/tests/issues.rs index 8b4ac4b..5860c49 100644 --- a/tests/issues.rs +++ b/tests/issues.rs @@ -115,3 +115,32 @@ fn issue_359() { Opt::from_iter(&["test", "only_one_arg"]) ); } + +#[test] +fn issue_490() { + use std::iter::FromIterator; + use std::str::FromStr; + use structopt::StructOpt; + + struct U16ish; + impl FromStr for U16ish { + type Err = (); + fn from_str(_: &str) -> Result { + unimplemented!() + } + } + impl<'a> FromIterator<&'a U16ish> for Vec { + fn from_iter>(_: T) -> Self { + unimplemented!() + } + } + + #[derive(StructOpt, Debug)] + struct Opt { + opt_vec: Vec, + #[structopt(long)] + opt_opt_vec: Option>, + } + + // Assert that it compiles +}