Skip to content

Inferring type succeeds with too little information #37188

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

Open
glandium opened this issue Oct 15, 2016 · 2 comments
Open

Inferring type succeeds with too little information #37188

glandium opened this issue Oct 15, 2016 · 2 comments
Labels
A-inference Area: Type inference C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@glandium
Copy link
Contributor

The following code fails to compile:

pub trait Bar {
    fn new() -> Self;
}

pub struct Foo {}

impl Bar for Foo {
    fn new() -> Self {
        Foo {}
    }
}

pub struct Qux<B=Foo> where B: Bar {
    foo: B,
}

impl<B> Qux<B> where B: Bar {
    fn new() -> Self {
        Qux { foo: B::new() }
    }
}

fn main() {
    let q = Qux::new();
}

The error is:

rustc 1.14.0-nightly (6e8f92f11 2016-10-07)
error[E0282]: unable to infer enough type information about `_`
  --> <anon>:24:13
   |
24 |     let q = Qux::new();
   |             ^^^^^^^^ cannot infer type for `_`
   |
   = note: type annotations or generic parameter binding required

error: aborting due to previous error

(from playpen ; same with stable)

Strangely, replacing that failing line with:

    let q: Qux = Qux::new()

makes it compile.

@Mark-Simulacrum Mark-Simulacrum added the A-inference Area: Type inference label May 14, 2017
@Mark-Simulacrum Mark-Simulacrum changed the title error[E0282]: unable to infer enough type information about _ Inferring type succeeds with too little information May 14, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@estebank
Copy link
Contributor

Triage: current output:

error[E0282]: type annotations needed for `Qux<B>`
  --> src/main.rs:24:13
   |
24 |     let q = Qux::new();
   |         -   ^^^^^^^^ cannot infer type for `B`
   |         |
   |         consider giving `q` the explicit type `Qux<B>`, where the type parameter `B` is specified

Giving q the type Qux (without the assoc type set) still works.

@estebank estebank added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Aug 12, 2019
@estebank
Copy link
Contributor

Current output:

error[E0282]: type annotations needed for `Qux<B>`
  --> f71.rs:26:9
   |
26 |     let q = Qux::new();
   |         ^
   |
help: consider giving `q` an explicit type, where the type for type parameter `B` is specified
   |
26 |     let q: Qux<B> = Qux::new();
   |          ++++++++

After #114811:

error[E0283]: type annotations needed for `Qux<B>`
  --> f71.rs:26:9
   |
26 |     let q = Qux::new();
   |         ^   -------- type must be known at this point
   |
   = note: cannot satisfy `_: Bar`
   = help: the trait `Bar` is implemented for `Foo`
note: required by a bound in `Qux::<B>::new`
  --> f71.rs:19:25
   |
19 | impl<B> Qux<B> where B: Bar {
   |                         ^^^ required by this bound in `Qux::<B>::new`
20 |     fn new() -> Self {
   |        --- required by a bound in this associated function
help: consider giving `q` an explicit type, where the type for type parameter `B` is specified
   |
26 |     let q: Qux<B> = Qux::new();
   |          ++++++++

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inference Area: Type inference C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants