Skip to content

Lifetime error complains about a contravariant argument "declared outside the function" #154350

@theemathas

Description

@theemathas

Code

fn weird<'a>(f: impl FnOnce(fn(&'a i32)), x: fn(&'static i32)) {
    f(x);
}

Current output

error[E0521]: borrowed data escapes outside of function
 --> src/lib.rs:2:5
  |
1 | fn weird<'a>(f: impl FnOnce(fn(&'a i32)), x: fn(&'static i32)) {
  |          -- lifetime `'a` defined here    - `x` declared here, outside of the function body
2 |     f(x);
  |     ^^^^
  |     |
  |     a temporary borrow escapes the function body here
  |     argument requires that `'a` must outlive `'static`
  |
  = help: `x` is declared outside the function, so any data borrowed inside the function cannot be stored into it

For more information about this error, try `rustc --explain E0521`.

Desired output

Rationale and extra context

No response

Other cases

Rust Version

Reproducible on the playground with version 1.96.0-nightly (2026-03-24 362211dc29abc4e8f8cf)

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsA-varianceArea: Variance (https://doc.rust-lang.org/nomicon/subtyping.html)T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.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