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

Incoherent error when a method and the containing impl require the same trait with different lifetimes #78258

Open
elidupree opened this issue Oct 23, 2020 · 3 comments
Labels
A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-lifetimes Area: Lifetimes / regions A-trait-system Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@elidupree
Copy link

elidupree commented Oct 23, 2020

While trying to make the lifetimes work in a -> impl Iterator method, I ran into a weird error. Minimal test case:

trait Trait<'a> {}

struct Struct<'a, T>(&'a T);

impl<'a, T: Trait<'a>> Struct<'a, T> {
    pub fn method<'b>(&self)
    where
        T: Trait<'b>,
    {
    }
}

As far as I can tell, this should be valid code. However, it gives this bizarre error message:

error[E0283]: type annotations needed
 --> src/lib.rs:5:13
  |
1 | trait Trait<'a> {}
  | --------------- required by this bound in `Trait`
...
5 | impl<'a, T: Trait<'a>> Struct<'a, T> {
  |             ^^^^^^^^^ cannot infer type for type parameter `T`
  |
  = note: cannot satisfy `T: Trait<'a>`

This occurs on both stable and nightly.

More details

The error still occurs if we replace one lifetime with 'static - either

trait Trait<'a> {}

struct Struct<T>(T);

impl<'a, T: Trait<'a>> Struct<T> {
    pub fn method(&self)
    where
        T: Trait<'static>,
    {
    }
}

or

trait Trait<'a> {}

struct Struct<T>(T);

impl<T: Trait<'static>> Struct<T> {
    pub fn method<'b>(&self)
    where
        T: Trait<'b>,
    {
    }
}

But it doesn't occur if either bound is removed, or if the same lifetime is used in both bounds, or if the bounds differ by a type parameter rather than a lifetime parameter.

On stable, the error message is even less helpful if the trait has an associated type:

trait Trait<'a> {
    type A;
}

struct Struct<'a, T>(&'a T);

impl<'a, T: Trait<'a>> Struct<'a, T> {
    pub fn method<'b>(&self)
    where
        T: Trait<'b>,
    {
    }
}
error[E0282]: type annotations needed
 --> src/lib.rs:7:13
  |
7 | impl<'a, T: Trait<'a>> Struct<'a, T> {
  |             ^^^^^^^^^ cannot infer type

But on nightly, the error is the same either way.

@elidupree elidupree added the C-bug Category: This is a bug. label Oct 23, 2020
@ohsayan
Copy link
Contributor

ohsayan commented Oct 23, 2020

@rustbot modify labels to +A-traits +A-impl-trait +A-lifetimes +T-compiler
Edit: oops, I accidentally tagged T-lang (seems like it's for T-compiler)

@rustbot rustbot added A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-lifetimes Area: Lifetimes / regions A-trait-system Area: Trait system T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Oct 23, 2020
@elidupree
Copy link
Author

For reference, in my actual code, I wrote this to work around #62849 – the Self type requires associated types from the trait, so I have to have a lifetime parameter on the impl, but the Item type in the returned iterator also requires associated types from the trait, so I have to have a second lifetime parameter on the method or I run into the error `impl Trait` return type cannot contain a projection or `Self` that references lifetimes from a parent scope.

@estebank estebank added D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Oct 23, 2020
@estebank
Copy link
Contributor

Current output:

error[E0283]: type annotations needed: cannot satisfy `T: Trait<'a>`
 --> src/lib.rs:5:13
  |
5 | impl<'a, T: Trait<'a>> Struct<'a, T> {
  |             ^^^^^^^^^
  |
note: multiple `impl`s or `where` clauses satisfying `T: Trait<'a>` found
 --> src/lib.rs:5:13
  |
5 | impl<'a, T: Trait<'a>> Struct<'a, T> {
  |             ^^^^^^^^^
...
8 |         T: Trait<'b>,
  |            ^^^^^^^^^

@estebank estebank removed C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. labels Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-impl-trait Area: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch. A-lifetimes Area: Lifetimes / regions A-trait-system Area: Trait system 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

4 participants