Skip to content

Invalid type annotations needed suggestion that leads to E0632 #86162

Closed
@nbdd0121

Description

@nbdd0121

This code produces invalid suggestion:

fn foo(x: impl Clone) {}
fn gen<T>() -> T { todo!() }

fn main() {
    foo(gen());
}
error[E0283]: type annotations needed
 --> src/main.rs:5:5
  |
1 | fn foo(x: impl Clone) {}
  |                ----- required by this bound in `foo`
...
5 |     foo(gen());
  |     ^^^ cannot infer type for type parameter `impl Clone` declared on the function `foo`
  |
  = note: cannot satisfy `_: Clone`
help: consider specifying the type argument in the function call
  |
5 |     foo::<impl Clone>(gen());
  |        ^^^^^^^^^^^^^^

Also this code:

fn foo<const N: usize>(x: impl Clone) {}
fn gen<T>() -> T { todo!() }

fn main() {
    foo(gen());
}
error[E0283]: type annotations needed
 --> src/main.rs:5:5
  |
1 | fn foo<const N: usize>(x: impl Clone) {}
  |                                ----- required by this bound in `foo`
...
5 |     foo(gen());
  |     ^^^ cannot infer type for type parameter `impl Clone` declared on the function `foo`
  |
  = note: cannot satisfy `_: Clone`
help: consider specifying the type arguments in the function call
  |
5 |     foo::<N, impl Clone>(gen());
  |        ^^^^^^^^^^^^^^^^^

None of them produce valid suggestion, as what it suggests will produce error[E0632]: cannot provide explicit generic arguments when impl Trait is used in argument position.

Related to #83606.

@rustbot label: +A-diagnostics +D-invalid-suggestion +T-compiler

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions