Skip to content

Odd lifetime error with closures and HRTBs #147618

@comex

Description

@comex

This code:

#![feature(closure_lifetime_binder)]

struct Foo<'a, 'b: 'a, 'c: 'a>(*mut &'a (), *mut &'b (), *mut &'c ());

fn bar<'b, 'c>(callback: impl for<'a> Fn(Foo<'a, 'b, 'c>) + 'static) {
    let _ = for<'a> |foo: Foo<'a, 'b, 'c>| -> () {
        callback(foo);
    };
}

produces on latest nightly:

error: lifetime may not live long enough
 --> src/lib.rs:7:9
  |
5 | fn bar<'b, 'c>(callback: impl for<'a> Fn(Foo<'a, 'b, 'c>) + 'static) {
  |        --  -- lifetime `'c` defined here
  |        |
  |        lifetime `'b` defined here
6 |     let _ = for<'a> |foo: Foo<'a, 'b, 'c>| -> () {
7 |         callback(foo);
  |         ^^^^^^^^^^^^^ argument requires that `'b` must outlive `'c`
  |
  = help: consider adding the following bound: `'b: 'c`

error: lifetime may not live long enough
 --> src/lib.rs:7:9
  |
5 | fn bar<'b, 'c>(callback: impl for<'a> Fn(Foo<'a, 'b, 'c>) + 'static) {
  |        --  -- lifetime `'c` defined here
  |        |
  |        lifetime `'b` defined here
6 |     let _ = for<'a> |foo: Foo<'a, 'b, 'c>| -> () {
7 |         callback(foo);
  |         ^^^^^^^^^^^^^ argument requires that `'c` must outlive `'b`
  |
  = help: consider adding the following bound: `'c: 'b`

help: `'b` and `'c` must be the same: replace one with the other

I think this code should be valid. After all, the type signatures on the closure itself and on the function it's trying to call are identical. I would expect both the closure and the function to implicitly establish bounds 'b: 'a and 'c: 'a due to the use of Foo<'a, 'b, 'c> as an argument. But this shouldn't require 'b: 'c or 'c: 'b.

Disclaimer: I don't really understand how implied bounds work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)A-higher-rankedArea: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs)A-implied-boundsArea: Implied bounds / inferred outlives-boundsA-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types 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