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

Associated type constraints and higher ranked trait bounds require impossible type annotations #87893

Open
patrick-gu opened this issue Aug 9, 2021 · 2 comments
Labels
A-higher-ranked Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs) A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@patrick-gu
Copy link
Contributor

I tried this code (Playground):

trait TraitWithLifetime<'a> {
    type Assoc;
}

struct Implementor<T>(T);

impl<'a, T> TraitWithLifetime<'a> for Implementor<T> where
    T: for<'b> TraitWithLifetime<'b> + TraitWithLifetime<'a, Assoc = ()>
{
}

I expected to see this happen: The code compiles successfully because there are no additional type annotations are needed.

Instead, an invalid suggestion is produced:

error[E0283]: type annotations needed
 --> src\main.rs:8:62
  |
1 | trait TraitWithLifetime<'a> {
  | --------------------------- required by this bound in `TraitWithLifetime`
...
8 |     T: for<'b> TraitWithLifetime<'b> + TraitWithLifetime<'a, Assoc = ()>
  |                                                              ^^^^^^^^^^ cannot infer type for 
type parameter `T`
  |
  = note: cannot satisfy `T: TraitWithLifetime<'a>`
help: consider specifying the type arguments in the function call
  |
8 |     T: for<'b> TraitWithLifetime<'b> + TraitWithLifetime<'a, Assoc = ()::<Self, 'a>>
  |                                                                        ^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0283`.

Meta

rustc --version --verbose:

rustc 1.54.0 (a178d0322 2021-07-26)
binary: rustc
commit-hash: a178d0322ce20e33eac124758e837cbd80a6f633
commit-date: 2021-07-26
host: x86_64-pc-windows-msvc
release: 1.54.0
LLVM version: 12.0.1
@patrick-gu patrick-gu added the C-bug Category: This is a bug. label Aug 9, 2021
@jonas-schievink jonas-schievink added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Aug 9, 2021
@SNCPlay42
Copy link
Contributor

This is similar to #85735 - the actual problem is that there are two bounds on the trait TraitWithLifetime whose generic parameters differ only by lifetimes (see #21974).

A similar fix to the one for #85735 could be applied to avoid the bad suggestion, but I think the compiler should emit a completely different error message from "type annotations needed" when generics on bounds differ only by lifetimes. If you remove the associated type constraint, the error message is still not good:

error[E0283]: type annotations needed
 --> src/lib.rs:8:40
  |
1 | trait TraitWithLifetime<'a> {
  | --------------------------- required by this bound in `TraitWithLifetime`
...
8 |     T: for<'b> TraitWithLifetime<'b> + TraitWithLifetime<'a>
  |                                        ^^^^^^^^^^^^^^^^^^^^^ cannot infer type for type parameter `T`
  |
  = note: cannot satisfy `T: TraitWithLifetime<'a>`

I think the reason there's no special error currently when generics on bounds differ only by lifetimes is that this might not be supposed to be a limitation of the compiler, but #21974 is more than five years old.

@estebank
Copy link
Contributor

estebank commented Aug 3, 2023

Current output:

error[[E0283]](https://doc.rust-lang.org/stable/error_codes/E0283.html): type annotations needed: cannot satisfy `T: TraitWithLifetime<'a>`
 --> src/lib.rs:8:62
  |
8 |     T: for<'b> TraitWithLifetime<'b> + TraitWithLifetime<'a, Assoc = ()>
  |                                                              ^^^^^^^^^^
  |
note: multiple `impl`s or `where` clauses satisfying `T: TraitWithLifetime<'a>` found
 --> src/lib.rs:8:8
  |
8 |     T: for<'b> TraitWithLifetime<'b> + TraitWithLifetime<'a, Assoc = ()>
  |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@estebank estebank added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. labels Aug 3, 2023
@fmease fmease added the A-higher-ranked Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs) label Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-higher-ranked Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs) A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants