Skip to content

Function arguments in nontrivial pattern can be reassigned with a different lifetime. #148463

@theemathas

Description

@theemathas

I tried this code:

#![allow(unused)]

fn works<'a, 'b>(mut x @ _: &'a i32, y: &'b i32) {
    x = y;
}

fn fails<'a, 'b>(mut x: &'a i32, y: &'b i32) {
    x = y;
}

I expected the two functions to either both compile or both error. Instead, only the first function compiles:

error: lifetime may not live long enough
 --> src/lib.rs:8:5
  |
7 | fn fails<'a, 'b>(mut x: &'a i32, y: &'b i32) {
  |          --  -- lifetime `'b` defined here
  |          |
  |          lifetime `'a` defined here
8 |     x = y;
  |     ^^^^^ assignment requires that `'b` must outlive `'a`
  |
  = help: consider adding the following bound: `'b: 'a`

See also #128225, where a variable can be assigned a value with a different lifetime.

Related to #148389, where a similar inconsistency occurs with async fn.

Meta

Reproducible on the playground with version 1.93.0-nightly (2025-11-02 b15a874aafe7eab9ea3a)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-lifetimesArea: Lifetimes / regionsA-patternsRelating to patterns and pattern matchingC-bugCategory: This is a bug.T-typesRelevant to the types team, which will review and decide on the PR/issue.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions