Skip to content

Confusing spurious E0283 when a generic trait is implemented over &'a T and a nonexistent type #106351

Closed
@Heliozoa

Description

@Heliozoa

Inspired by https://users.rust-lang.org/t/creating-daemon-processes/86832/3

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=112302a0ad4af6da9c2acf936c4defea

pub struct S;

trait Generic<T> {}

impl<'a, T> Generic<&'a T> for S {}
impl Generic<Type> for S {}

The current output is:

Compiling playground v0.0.1 (/playground)
error[[E0412]](https://doc.rust-lang.org/stable/error-index.html#E0412): cannot find type `Type` in this scope
 --> src/lib.rs:6:14
  |
6 | impl Generic<Type> for S {}
  |              ^^^^ not found in this scope
  |
help: consider importing one of these items
  |
1 | [use postgres::types::Type;](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=112302a0ad4af6da9c2acf936c4defea#)
  |
1 | [use postgres_types::Type;](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=112302a0ad4af6da9c2acf936c4defea#)
  |
1 | [use psl_types::Type;](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=112302a0ad4af6da9c2acf936c4defea#)
  |
1 | [use publicsuffix::Type;](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=112302a0ad4af6da9c2acf936c4defea#)
  |
    and 6 other candidates

error[[E0283]](https://doc.rust-lang.org/stable/error-index.html#E0283): type annotations needed: cannot satisfy `S: Generic<&'a T>`
 --> src/lib.rs:5:13
  |
5 | impl<'a, T> Generic<&'a T> for S {}
  |             ^^^^^^^^^^^^^^
  |
note: multiple `impl`s satisfying `S: Generic<&'a T>` found
 --> src/lib.rs:5:1
  |
5 | impl<'a, T> Generic<&'a T> for S {}
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6 | impl Generic<Type> for S {}
  | ^^^^^^^^^^^^^^^^^^^^^^^^

Some errors have detailed explanations: E0283, E0412.
For more information about an error, try `rustc --explain E0283`.
error: could not compile `playground` due to 2 previous errors

Ideally the output should not include the second error E0283 as it doesn't make sense.

Interestingly, this problem disappears if the lifetime parameter is dropped from the first impl. The code

pub struct S;

trait Generic<T> {}

impl Generic<u32> for S {}
impl Generic<&'static u32> for S {}
impl Generic<Type> for S {}

results in the expected compiler output:

Compiling playground v0.0.1 (/playground)
error[[E0412]](https://doc.rust-lang.org/stable/error-index.html#E0412): cannot find type `Type` in this scope
 --> src/lib.rs:7:14
  |
7 | impl Generic<Type> for S {}
  |              ^^^^ not found in this scope
  |
help: consider importing one of these items
  |
1 | [use postgres::types::Type;](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=112302a0ad4af6da9c2acf936c4defea#)
  |
1 | [use postgres_types::Type;](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=112302a0ad4af6da9c2acf936c4defea#)
  |
1 | [use psl_types::Type;](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=112302a0ad4af6da9c2acf936c4defea#)
  |
1 | [use publicsuffix::Type;](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=112302a0ad4af6da9c2acf936c4defea#)
  |
    and 6 other candidates

For more information about this error, try `rustc --explain E0412`.
error: could not compile `playground` due to previous error

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