Skip to content
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

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 opened this issue Sep 22, 2022 · 0 comments · Fixed by #106309
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jruderman
Copy link
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.

@jruderman jruderman added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 22, 2022
@compiler-errors compiler-errors self-assigned this Dec 30, 2022
JohnTitor pushed a commit to JohnTitor/rust that referenced this issue Jan 9, 2023
…didates, r=oli-obk

Prefer non-`[type error]` candidates during selection

Fixes rust-lang#102130
Fixes rust-lang#106351

r? types

note: Alternatively we could filter out error where-clauses during param-env construction? But we still need to filter out impls with errors during `match_impl`, I think.
compiler-errors added a commit to compiler-errors/rust that referenced this issue Jan 9, 2023
…didates, r=oli-obk

Prefer non-`[type error]` candidates during selection

Fixes rust-lang#102130
Fixes rust-lang#106351

r? types

note: Alternatively we could filter out error where-clauses during param-env construction? But we still need to filter out impls with errors during `match_impl`, I think.
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jan 12, 2023
…didates, r=oli-obk

Prefer non-`[type error]` candidates during selection

Fixes rust-lang#102130
Fixes rust-lang#106351

r? types

note: Alternatively we could filter out error where-clauses during param-env construction? But we still need to filter out impls with errors during `match_impl`, I think.
@bors bors closed this as completed in 9b538e8 Jan 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants