Skip to content

Sized check inconsistently checks/ignores lifetimes #156770

@theemathas

Description

@theemathas

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlA-associated-itemsArea: Associated items (types, constants & functions)A-lifetimesArea: Lifetimes / regionsA-trait-systemArea: Trait systemC-bugCategory: This is a bug.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions