Skip to content

Wrong lifetime is inferred in the argument of closure when given more specific type. #51616

@oxalica

Description

@oxalica

I have lifetime bounds to a closure through a trait like this.

trait Closure<'t> {}
impl<'t, F: Fn(&'t ())> Closure<'t> for F {}

fn restrict_trait(_: impl Closure<'static>) {}

When I call restrict_trait(|r| ...), I'll get r: &'static () inside the closure. But when using restrict_trait(|r: &_| ...), the lifetime of r inferred is not 'static anymore.

Here is the detailed example

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.A-inferenceArea: Type inferenceA-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions