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

Fix the problem where the build fails due to the ambiguous type of map #491

Merged
merged 4 commits into from
Aug 30, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
11 changes: 9 additions & 2 deletions structopt-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,13 @@ fn gen_constructor(fields: &Punctuated<Field, Comma>, 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)),

Expand All @@ -349,15 +356,15 @@ fn gen_constructor(fields: &Punctuated<Field, Comma>, 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
}
},

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()=>
Expand Down
29 changes: 29 additions & 0 deletions tests/issues.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Self::Err> {
unimplemented!()
}
}
impl<'a> FromIterator<&'a U16ish> for Vec<u16> {
fn from_iter<T: IntoIterator<Item = &'a U16ish>>(_: T) -> Self {
unimplemented!()
}
}

#[derive(StructOpt, Debug)]
struct Opt {
opt_vec: Vec<u16>,
#[structopt(long)]
opt_opt_vec: Option<Vec<u16>>,
}

// Assert that it compiles
}