Skip to content

E0195 on where Self: 'a bounds when they are unconditionally satisfied by Self #84021

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

Open
ExpHP opened this issue Apr 8, 2021 · 2 comments
Open
Labels
A-lifetimes Area: Lifetimes / regions A-trait-system Area: Trait system C-bug Category: This is a bug. 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.

Comments

@ExpHP
Copy link
Contributor

ExpHP commented Apr 8, 2021

trait Test {
    fn boo<'ctx>() where Self: 'ctx;
}

impl<A, B> Test for (A, B) {
    fn boo<'ctx>() where A: 'ctx, B: 'ctx { }    // ok
}

impl Test for () {
    fn boo<'ctx>() { }   //  [E0195]: lifetime parameters or bounds on method `boo` do not match the trait declaration
}

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=9d4125a483581bb47d66f002f1074fda

I expected this to succeed. And in fact, the first impl (the generic one) succeeds! (i.e. the compiler is able to successfully determine that Self: 'ctx given A: 'ctx and B: 'ctx)

However, an error is emitted on the impl for ():

error[E0195]: lifetime parameters or bounds on method `boo` do not match the trait declaration
  --> src/lib.rs:10:11
   |
2  |     fn boo<'ctx>() where Self: 'ctx;
   |           ------ lifetimes in impl do not match this method in trait
...
10 |     fn boo<'ctx>() { }
   |           ^^^^^^ lifetimes do not match method in trait

error: aborting due to previous error

In order to make this one compile, we need to add where (): 'ctx to the impl, which is, well, a bit silly!

impl Test for () {
    fn boo<'ctx>() where (): 'ctx { }   //  ok (workaround)
}

Addendum: In fact, any mention of the lifetime inside where bounds causes compilation to succeed.

impl Test for () {
    fn boo<'ctx>() where i32: 'ctx { }  // ok
}

impl Test for () {
    fn boo<'ctx>() where &'ctx i32: Clone { }  // also ok
}
@ExpHP ExpHP added the C-bug Category: This is a bug. label Apr 8, 2021
@ExpHP
Copy link
Contributor Author

ExpHP commented Apr 9, 2021

Possibly related:

trait Test2 {
    fn boo<'ctx>();
}

impl Test2 for () {
    fn boo<'ctx>() where i32: 'ctx { }
}
   Compiling playground v0.0.1 (/playground)
error[E0195]: lifetime parameters or bounds on method `boo` do not match the trait declaration
 --> src/lib.rs:6:11
  |
2 |     fn boo<'ctx>();
  |           ------ lifetimes in impl do not match this method in trait
...
6 |     fn boo<'ctx>() where i32: 'ctx { }
  |           ^^^^^^ lifetimes do not match method in trait

error: aborting due to previous error

Here, deleting the where i32: 'ctx makes this succeed. Perhaps this error could be intentional. Nonetheless, seems related.

@estebank estebank added A-lifetimes Area: Lifetimes / regions A-trait-system Area: Trait system 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. labels Apr 9, 2021
@estebank
Copy link
Contributor

estebank commented Apr 9, 2021

I thought that implied_bounds would make the code not fail, but it also fails. We could reliably detect the case and suggest the extra bound on the lifetime to match the impl, if that's how we want to solve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions A-trait-system Area: Trait system C-bug Category: This is a bug. 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.
Projects
None yet
Development

No branches or pull requests

2 participants