Skip to content

Explicit lifetime required on scoped value #123195

Closed as not planned
Closed as not planned
@aumetra

Description

@aumetra

This is an issue with the scoped-futures library, which is a Future-specific implementation of a technique outlined in this article.

On stable code I wrote compiles just fine, but on beta it fails. In simple cases this is solved by adding a lifetime, but in other cases the fix can get rather gnarly.

I managed to minimize it down into a small sample:

Code

I tried this code:

use std::marker::PhantomData;

pub struct ScopedWrapper<'upper_bound, 'subject> {
    scope: PhantomData<&'subject &'upper_bound ()>,
}

pub fn works_on_stable<'a, F>(reference: &(), scoped: F)
where
    for<'b> F: Fn(&'b ()) -> ScopedWrapper<'b, 'a>,
{
    scoped(reference);
}

I expected to see this happen: It simply compiles.

Instead, this happened: A lifetime-related compiler error happened.

Error message:

aumetra@gazenot:~/regression-min-repr$ cargo +beta build
   Compiling regression-min-repr v0.1.0 (/home/aumetra/regression-min-repr)
error[E0621]: explicit lifetime required in the type of `reference`
  --> src/main.rs:15:5
   |
11 | pub fn works_on_stable<'a, F, Fut>(reference: &(), scoped: F)
   |                                               --- help: add explicit lifetime `'a` to the type of `reference`: `&'a ()`
...
15 |     scoped(reference);
   |     ^^^^^^^^^^^^^^^^^ lifetime `'a` required

For more information about this error, try `rustc --explain E0621`.
error: could not compile `regression-min-repr` (bin "regression-min-repr") due to 1 previous error

Version it worked on

rustc 1.77.0 (aedd173a2 2024-03-17)
binary: rustc
commit-hash: aedd173a2c086e558c2b66d3743b344f977621a7
commit-date: 2024-03-17
host: x86_64-unknown-linux-gnu
release: 1.77.0
LLVM version: 17.0.6

Version with regression

The latest beta

rustc --version --verbose:

rustc 1.78.0-beta.3 (4147533e0 2024-03-27)
binary: rustc
commit-hash: 4147533e05ee20c4fcc432736e7feeafa46521cd
commit-date: 2024-03-27
host: x86_64-unknown-linux-gnu
release: 1.78.0-beta.3
LLVM version: 18.1.2

@rustbot modify labels: +regression-from-stable-to-beta -regression-untriaged

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regionsC-discussionCategory: Discussion or questions that doesn't represent real issues.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions