Skip to content

Extra "type annotations needed" error after trying to constrain a non-existent type parameter with the same trait bound as another type parameter #102130

Closed
@jruderman

Description

@jruderman

Given the following code:

use core::ops::Add;

fn dbl<T>(x: T) -> <T as Add>::Output
where
    T: Copy + Add,
    UUU: Copy
{
    x + x
}

fn main() {
    println!("{}", dbl(3));
}

The current output is:

error[[E0412]](https://doc.rust-lang.org/nightly/error-index.html#E0412): cannot find type `UUU` in this scope
 --> src/main.rs:6:5
  |
6 |     UUU: Copy
  |     ^^^ not found in this scope

error[[E0282]](https://doc.rust-lang.org/nightly/error-index.html#E0282): type annotations needed
 --> src/main.rs:3:8
  |
3 | fn dbl<T>(x: T) -> <T as Add>::Output
  |        ^ cannot infer type for type parameter `T`

Similarly, this code:

use core::ops::Add;

fn dbl<T>(x: T) -> <T as Add>::Output
where
    T: Copy + Add,
    UUU: Add
{
    x + x
}

fn main() {
    println!("{}", dbl(3));
}

produces

error[[E0412]](https://doc.rust-lang.org/nightly/error-index.html#E0412): cannot find type `UUU` in this scope
 --> src/main.rs:6:5
  |
6 |     UUU: Add
  |     ^^^ not found in this scope

error[[E0284]](https://doc.rust-lang.org/nightly/error-index.html#E0284): type annotations needed
 --> src/main.rs:3:1
  |
3 | / fn dbl<T>(x: T) -> <T as Add>::Output
4 | | where
5 | |     T: Copy + Add,
6 | |     UUU: Add
7 | | {
8 | |     x + x
9 | | }
  | |_^ cannot infer type
  |
  = note: cannot satisfy `<T as Add>::Output == _`

In both cases, the "type annotation needed" error (E0282 or E0284) shouldn't be shown.

Ideally, the erroneous UUU line should not prevent type inference from succeeding, because UUU does not refer to anything. Alternatively (as in #88630, #75331, #68507) the first error could cancel the attempt at inferring types.

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-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