Skip to content

Error uses parameter name out of context: cannot infer type for U #40944

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

Closed
kornelski opened this issue Mar 31, 2017 · 2 comments
Closed

Error uses parameter name out of context: cannot infer type for U #40944

kornelski opened this issue Mar 31, 2017 · 2 comments

Comments

@kornelski
Copy link
Contributor

kornelski commented Mar 31, 2017

The message is technically correct, but it refers to a generic name U that is not present in user's source code, and has no meaning in the current context.

fn main() {
    assert_eq!("bar".into(), "foo".into());
}

error[E0282]: unable to infer enough type information about U
cannot infer type for U

It's even weirder that the type U is not in Into<T>'s definition, but comes from somewhere else inside stdlib.

pub trait Into<T> {
    fn into(self) -> T;
}

It would be clearer if rustc has shown Into<U> instead, or perhaps the stdlib implementation could rename the cryptic U to something self-explanatory like TypeToBeConvertedInto.

@tatref
Copy link

tatref commented May 4, 2017

I think this can be troublesome for beginners, since this can pop up anywhere. Other example:

fn main() {
   use std::collections::HashMap;
   
   let h = HashMap::new();
}

Error message using nightly:

error[E0282]: type annotations needed
 --> <anon>:4:12
  |
4 |    let h = HashMap::new();
  |        -   ^^^^^^^^^^^^ cannot infer type for `K`
  |        |
  |        consider giving `h` a type

@Mark-Simulacrum
Copy link
Member

I'm going to close this in favor of #25633.

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

No branches or pull requests

3 participants