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
Contributor

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.

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Sep 22, 2022
added a commit that references this issue on Jan 9, 2023

Rollup merge of rust-lang#106309 - compiler-errors:prefer-non-err-can…

a006aa5
added a commit that references this issue on Jan 9, 2023

Rollup merge of rust-lang#106309 - compiler-errors:prefer-non-err-can…

500261f
added a commit that references this issue on Jan 12, 2023

Rollup merge of rust-lang#106309 - compiler-errors:prefer-non-err-can…

9b538e8
added a commit that references this issue on Jan 12, 2023

Rollup merge of rust-lang#106309 - compiler-errors:prefer-non-err-can…

11e4a4f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

    Participants

    @jruderman@compiler-errors

    Issue actions

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