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

[E0282] Cannot infer the result type of map #490

Closed
nullpo-head opened this issue Aug 21, 2021 · 0 comments · Fixed by #491 or clap-rs/clap#2825
Closed

[E0282] Cannot infer the result type of map #490

nullpo-head opened this issue Aug 21, 2021 · 0 comments · Fixed by #491 or clap-rs/clap#2825

Comments

@nullpo-head
Copy link
Contributor

nullpo-head commented Aug 21, 2021

If there is a type T for which FromStr and FromIterator are implemented and is used in an option as Vec<T> or Option<Vec<T>>, the build will fail by the following error.

error[E0282]: type annotations needed
   --> tests/issues.rs:140:18
    |
140 |         opt_vec: Vec<u16>,
    |         ---------^^^^^^^^
    |         |        |
    |         |        cannot infer type for type parameter `B` declared on the associated function `map`
    |         this method call resolves to `T`

The whole example code is

    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>>,
    }

The reason of this build failure is the type of the map in the expanded code becomes ambiguous due to U16ish.

    fn from_clap(matches: &::structopt::clap::ArgMatches) -> Self {
        Opt {
            opt_vec: matches.values_of("opt_vec").map_or_else(Vec::new, |v| {
                v.map(|s| ::std::str::FromStr::from_str(s).unwrap())
                    .collect()
            }),
        }
    }

This map has two possible types, thus the type inference needs an annotation and the build fails.

// u16
            opt_vec: matches.values_of("opt_vec").map_or_else(Vec::new, |v| {
                v.map::<u16, _>(|s| ::std::str::FromStr::from_str(s).unwrap())
                    .collect()
            }),
// Or, U16ish is also possible
            opt_vec: matches.values_of("opt_vec").map_or_else(Vec::new, |v| {
                v.map::<U16ish, _>(|s| ::std::str::FromStr::from_str(s).unwrap())
                    .collect()
            }),

This situation may sound unlikely at first, but it actually happens when you use Structopt with Indicatif crate on Windows. Indicatif defines Utf16Char on Windows and it causes this error.

I've also submitted a PR #491 to fix this. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant