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

Closure breaks function with higher-ranked trait bound #131677

Open
drewtato opened this issue Oct 14, 2024 · 1 comment
Open

Closure breaks function with higher-ranked trait bound #131677

drewtato opened this issue Oct 14, 2024 · 1 comment
Labels
A-closures Area: Closures (`|…| { … }`) A-higher-ranked Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@drewtato
Copy link

I tried this code:

pub fn f<T>()
where
    for<'a> &'a T:,
{
    || {};
}

I expected this to compile, perhaps with warnings. Instead, I get this error:

error: `T` does not live long enough
 --> src\lib.rs:5:5
  |
5 |     || {};
  |     ^^^^^

Rust version: rustc 1.84.0-nightly (27861c4 2024-10-13)

As best as I can tell, this happens when a generic is involved in a higher-ranked trait bound, and a closure is defined inside the function. It seems like since the closure is somewhat generic over T, the closure needs to satisfy something about the bound. Things that also exhibit the issue:

  • Adding more bounds
  • Using the HRTB with a GAT, e.g. T: for<'a> Trait<Item<'a> = &'a ()>
  • Adding arguments or a return to the closure
  • Using or calling the closure (calling the closure adds another error on the call expression)
  • Replacing the closure with an async block

Things that don't exhibit the issue:

  • A HRTB with a trait generic over a lifetime: T: for<'a> Trait<'a> or for<'a> T: Trait<'a>
  • A HRTB on a concrete type: for<'a> &'a ():
  • Defining the closure inside a function inside f

This is probably the same as #102540 but that issue misidentified the problem and is old. I did some quick checking of old versions and this compiles in 1.20.0 and 1.30.1, throws ICE in 1.40.0, and gives an error in 1.50.0 and 1.60.0. The 1.60.0 error has a little more info.

error: higher-ranked lifetime error
 --> src\lib.rs:5:5
  |
5 |     || {};
  |     ^^^^^
  |
  = note: could not prove for<'a, 'r> &'a T: 'r

The ICE from 1.40.0 looks similar to #59311 (weirdly, predates 1.30) and #71546 and looks like this:

error: internal compiler error: broken MIR in DefId(0:12 ~ rusttest[7b76]::f[0]) (NoSolution): could not prove Binder(OutlivesPredicate(&'a T, ReEmpty))
 --> src\lib.rs:5:5
  |
5 |     || {};
  |     ^^^^^
@drewtato drewtato added the C-bug Category: This is a bug. label Oct 14, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 14, 2024
@lolbinarycat lolbinarycat added A-closures Area: Closures (`|…| { … }`) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-higher-ranked Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs) labels Oct 14, 2024
@saethlin saethlin added T-types Relevant to the types 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 Oct 15, 2024
@drewtato
Copy link
Author

I made some more test cases. The ones that fail to compile are indicated by the comments.

Details
pub fn f1<T>()
where
    for<'a> T: 'a,
{
    || {};
}

pub trait Trait2 {
    type Gat<'a>;
}

pub fn f2<T>()
where
    T: for<'a> Trait2<Gat<'a> = &'a ()>,
{
    || {};
}

pub trait Trait3 {
    type Gat<'a>
    where
        Self: 'a;
}

pub fn f3<T>()
where
    T: for<'a> Trait3<Gat<'a> = &'a ()>,
{
    || {}; // error
}

pub struct Struct4<'a, T> {
    t: T,
    a: &'a (),
}

pub fn f4<T>()
where
    for<'a> Struct4<'a, T>:,
{
    || {};
}

pub struct Struct5<'a, T> {
    t: &'a T,
}

pub fn f5<T>()
where
    for<'a> Struct5<'a, T>:,
{
    || {}; // error
}

pub struct Struct6<'a, T>
where
    T: 'a,
{
    t: T,
    a: &'a (),
}

pub fn f6<T>()
where
    for<'a> Struct6<'a, T>:,
{
    || {}; // error
}

I think, actually, the compiler is almost correct here. f6 is not restricting T like Struct6 is, so it shouldn't compile. It's the same as with normal trait bounds on structs:

pub struct Struct6<T>
where
    T: Default,
{
    t: T,
}

pub fn f6<T>()
where
    Struct6<T>:, // error, the trait bound `T: std::default::Default` is not satisfied
{
}

Except, this isn't what is happening, since removing the closure makes them compile. What I think is occurring is the compiler adds a magic where T: 'a bound (impossible to explicitly specify since it's HRTB) when necessary. And this error occurs because it's not adding where T: 'a to the closure's (invisible) signature.

Hopefully someone more familiar with the compiler will be able to confirm this. I'm hoping the fix will simply be correctly copying the bounds to the inner closure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: Closures (`|…| { … }`) A-higher-ranked Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants