rust-lang / rust-lang/rust

Inconsistent lifetime checks between function and field access.

Open
#141,481 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lifetimes C-bug T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code (minimized from a real usage):

fn demo<'a, 'b, 'c, T>(x: &'b T, y: Wrap<'c, L2<'a, 'b>>) {
    callback(y, |y| {
        // Doesn't work
        take_both(x, Wrap::new(y.into_inner()));

        // Works
        //take_both(x, Wrap::new(y.0));
    });
}

trait Bad<T> {}

struct L2<'a, 'b>(&'a (), &'b ());

struct Wrap<'want, T>(&'want mut dyn Bad<T>);

impl<'want, T> Wrap<'want, T> {
    fn new(_x: &'want mut dyn Bad<T>) -> Self {
        loop {}
    }

    fn into_inner(self) -> &'want mut dyn Bad<T> {
        self.0
    }
}

fn callback<'a, 'b, 'c>(
    _x: Wrap<'c, L2<'a, 'b>>,
    _f: impl for<'w> FnOnce(Wrap<'w, L2<'a, 'b>>),
) {
}

fn take_both<'a, 'b, 'c, T>(_x: &'c T, _y: Wrap<'a, L2<'b, 'c>>) {}

I expected for both the into_inner function and the field access to behave the same.

Instead, the into_inner function causes a lifetime error that doesn't make any sense.

error: lifetime may not live long enough
  --> src/main.rs:4:5
   |
3  |   fn demo<'a, 'b, 'c, T>(x: &'b T, y: Wrap<'c, L2<'a, 'b>>) {
   |           --  -- lifetime `'b` defined here
   |           |
   |           lifetime `'a` defined here
4  | /     callback(y, |y| {
5  | |         // Doesn't work
6  | |         take_both(x, Wrap::new(y.into_inner()));
...  |
10 | |     });
   | |______^ argument requires that `'b` must outlive `'a`
   |
   = help: consider adding the following bound: `'b: 'a`
   = note: requirement occurs because of the type `Wrap<'_, L2<'_, '_>>`, which makes the generic argument `'_` invariant
   = note: the struct `Wrap<'r, T>` is invariant over the parameter `'r`
   = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance

This did work pre 1.37.0. In 1.37.0 it appears as a warning and 1.40.0 made it a hard error.

Meta

rustc --version --verbose:

rustc 1.87.0 (17067e9ac 2025-05-09)
binary: rustc
commit-hash: 17067e9ac6d7ecb70e50f92c1944e545188d2359
commit-date: 2025-05-09
host: x86_64-unknown-linux-gnu
release: 1.87.0
LLVM version: 20.1.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimized reproduction in src/main.rs and compile it with the reported rustc versions, comparing y.into_inner() with y.0. Trace how the compiler handles the callback, lifetimes, and invariant Wrap parameter, then determine why the two access forms differ. Done means the behavior is explained and, if confirmed as a compiler bug, covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.