rust-lang / rust-lang/rust

`Sized` check inconsistently checks/ignores lifetimes

Open
#156,770 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-associated-items A-lifetimes A-MIR A-trait-system C-bug T-types
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

This issue is similar to #156694.

I'm not sure if this is a bug.

pub trait Trait {
    type Assoc<'a>: ?Sized;
}

// This compiles
pub fn works<'a, 'b, T: Trait<Assoc<'a>: Sized>>(_x: T::Assoc<'b>) {
}

// This errors
pub fn fails<'a, 'b, T: Trait<Assoc<'a>: Sized>>(x: T::Assoc<'b>) {
    let _x = x;
}

In the above code, I've specified that T::Assoc<'a>: Sized. However, the code tries to use T::Assoc<'b>: Sized. A function argument needs to be Sized, which makes it a bit weird that works compiles. However, once we've accepted the argument, moving the variable somehow requires Sized in a way such that the compiler checks lifetimes, making fails compile error. This is strange.

pub trait Trait {
    type Assoc<'a>: ?Sized;
}

// This compiles
pub fn works<'a, 'b, T: Trait<Assoc<'a>: Sized>>() {
    let f = || -> T::Assoc<'b> { todo!() };
    let _x = f();
}

// This errors
pub fn fails<'a, 'b, T: Trait<Assoc<'a>: Sized>>() {
    let f = || -> T::Assoc<'b> { todo!() };
    let x = f();
    let _y = x;
}

In the above code, I've again specified that T::Assoc<'a>: Sized, but required T::Assoc<'b>: Sized. Normally, declaring a closure to return a type requires that type to implement Sized, but that check seems to ignore lifetimes. Even calling the closure and putting the result into a variable is allowed. However, for some reason, moving that result into a second variable causes the compiler to check the lifetimes. This is strange.

cc @lcnr

Meta

Reproducible on the playground with version 1.97.0-nightly (2026-05-19 e50aa6fba4e63ab34c72)

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

Reproduce both pairs of works and fails on the Rust playground using nightly 1.97.0 (2026-05-19 e50aa6fba4e63ab34c72). Compare when the compiler checks Sized for the associated type across the two lifetimes and across the closure and variable moves. Done means the intended lifetime behavior is established and the inconsistent diagnostics or acceptance is resolved.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.