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

HRTBs with two lifetimes: implementation is not general enough when impl uses lifetime bound #113967

Open
JanBeh opened this issue Jul 22, 2023 · 1 comment
Labels
A-lifetimes Area: lifetime related C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@JanBeh
Copy link
Contributor

JanBeh commented Jul 22, 2023

I tried this code:

fn foo<'a, 'b: 'a>(_arg: &'a &'b i32) {}

trait Trait<T> {
    fn method(&self, arg: T);
}

impl<'a, 'b> Trait<&'a &'b i32> for ()
where
    'b: 'a, // `main` compiles if this line is commented out
{
    fn method(&self, arg: &'a &'b i32) {
        foo(arg);
    }
}

fn works_fine1<'a, 'b>(arg: &'a &'b i32) { foo(arg) }
fn works_fine2<T: for<'a, 'b> FnMut(&'a &'b i32)>(_arg: T) {}
fn fails<T: for<'a, 'b> Trait<&'a &'b i32>>(_arg: T) {}

fn main() {
    works_fine1(&&0);
    works_fine2(|x| foo(x));
    fails(());
}

(Playground)

I expected the program to compile and run without errors because 'b: 'a irregardless of explicitly adding that bound (as shown by calling foo).

Instead, this compilation error occurred:

error: implementation of `Trait` is not general enough
  --> src/main.rs:23:5
   |
23 |     fails(());
   |     ^^^^^^^^^ implementation of `Trait` is not general enough
   |
   = note: `()` must implement `Trait<&'0 &'1 i32>`, for any two lifetimes `'0` and `'1`...
   = note: ...but it actually implements `Trait<&&'2 i32>`, for some specific lifetime `'2`

Minimal example:

trait Trait<T> {}
impl<'a, 'b: 'a> Trait<&'a &'b i32> for () {}

fn fails<T: for<'a, 'b> Trait<&'a &'b i32>>(_arg: T) {}

fn main() {
    fails(());
}

(Playground)

The minimal example will compile if we replace:

-impl<'a, 'b: 'a> Trait<&'a &'b i32> for () {}
+impl<'a, 'b> Trait<&'a &'b i32> for () {}

Meta

rustc --version --verbose:

rustc 1.73.0-nightly (0308df23e 2023-07-21)
binary: rustc
commit-hash: 0308df23e621e783e31a27ca5beaa01b9df60d4a
commit-date: 2023-07-21
host: x86_64-unknown-freebsd
release: 1.73.0-nightly
LLVM version: 16.0.5

See also

See also: Why can’t I use lifetime bounds in HRTBs? on URLO.

I ran into that problem in a PR #1048 for regex where it wasn't trivial to work around. See also comment below in that matter.

@JanBeh JanBeh added the C-bug Category: This is a bug. label Jul 22, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 22, 2023
@JanBeh
Copy link
Contributor Author

JanBeh commented Jul 22, 2023

Note that it's not always possible to omit the explicit lifetime bound, as shown in the following example:

// we can't omit the `'b: 'a` bound here:
trait OtherTrait<'a, 'b: 'a>: FnMut(&'a &'b i32) {}

impl<'a, 'b, F> OtherTrait<'a, 'b> for F
where
    'b: 'a, // and we can't comment this out
    F: ?Sized + FnMut(&'a &'b i32)
{
}

fn other_func<T: for<'a, 'b> OtherTrait<'a, 'b>>(_arg: T) {}

fn main() {
    other_func(|_x: &&i32| ());
}

Fails with:

error: implementation of `OtherTrait` is not general enough
  --> src/main.rs:14:5
   |
14 |     other_func(|_x: &&i32| ());
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `OtherTrait` is not general enough
   |
   = note: `[closure@src/main.rs:14:16: 14:27]` must implement `OtherTrait<'0, '1>`, for any two lifetimes `'0` and `'1`...
   = note: ...but it actually implements `OtherTrait<'_, '2>`, for some specific lifetime `'2`

(Playground)

If we try to comment out this:

-    'b: 'a, // and we can't comment this out
+    //'b: 'a, // and we can't comment this out

We get:

error[E0478]: lifetime bound not satisfied
 --> src/main.rs:4:17
  |
4 | impl<'a, 'b, F> OtherTrait<'a, 'b> for F
  |                 ^^^^^^^^^^^^^^^^^^
  |
note: lifetime parameter instantiated with the lifetime `'b` as defined here
 --> src/main.rs:4:10
  |
4 | impl<'a, 'b, F> OtherTrait<'a, 'b> for F
  |          ^^
note: but lifetime parameter must outlive the lifetime `'a` as defined here
 --> src/main.rs:4:6
  |
4 | impl<'a, 'b, F> OtherTrait<'a, 'b> for F
  |      ^^

And if we try to fix that:

-trait OtherTrait<'a, 'b: 'a>: FnMut(&'a &'b i32) {}
+trait OtherTrait<'a, 'b>: FnMut(&'a &'b i32) {}

Then we get:

error[E0491]: in type `&'a &'b i32`, reference has a longer lifetime than the data it references
 --> src/main.rs:2:27
  |
2 | trait OtherTrait<'a, 'b>: FnMut(&'a &'b i32) {}
  |                           ^^^^^^^^^^^^^^^^^^
  |
note: the pointer is valid for the lifetime `'a` as defined here
 --> src/main.rs:2:18
  |
2 | trait OtherTrait<'a, 'b>: FnMut(&'a &'b i32) {}
  |                  ^^
note: but the referenced data is only valid for the lifetime `'b` as defined here
 --> src/main.rs:2:22
  |
2 | trait OtherTrait<'a, 'b>: FnMut(&'a &'b i32) {}
  |                      ^^

error[E0491]: in type `&'a &'b i32`, reference has a longer lifetime than the data it references
 --> src/main.rs:7:17
  |
7 |     F: ?Sized + FnMut(&'a &'b i32)
  |                 ^^^^^^^^^^^^^^^^^^
  |
note: the pointer is valid for the lifetime `'a` as defined here
 --> src/main.rs:4:6
  |
4 | impl<'a, 'b, F> OtherTrait<'a, 'b> for F
  |      ^^
note: but the referenced data is only valid for the lifetime `'b` as defined here
 --> src/main.rs:4:10
  |
4 | impl<'a, 'b, F> OtherTrait<'a, 'b> for F
  |          ^^

(Playground)

But this compiles:

fn direct<T: for<'a, 'b> FnMut(&'a &'b i32)>(_arg: T) {}

fn main() {
    direct(|_x: &&i32| ());
}

(Playground)

The examples in this comment have also been run using rustc 1.73.0-nightly (0308df23e 2023-07-21).

@workingjubilee workingjubilee added the A-lifetimes Area: lifetime related label Jul 23, 2023
@saethlin saethlin added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Aug 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: lifetime related 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

4 participants