Skip to content

lifetime inference regression in closure #47524

Closed
@cuviper

Description

@cuviper

The owning_ref crate has a couple test failures starting with Rust 1.23, which worked fine on 1.22. It is fixed by Kimundi/owning-ref-rs#43, but I wanted to see if this is a deliberate compiler change. The only compatibility note for 1.23 that sounds like it might be related is regarding #45852, but I'm not sure.

Reduced example, playground:

fn map<F, T, U>(x: &T, f: F) -> &U
where
    F: FnOnce(&T) -> &U,
{
    f(x)
}

fn borrow<'a>(a: &'a &[i32; 2]) -> &'a i32 {
    &a[0]
}

fn main() {
    let foo = [413, 612];
    let bar = &foo;

    map(&bar, borrow); // OK

    map(&bar, |a: &&[i32; 2]| &a[0]); // does not live long enough
}

This example works on 1.22, but fails on 1.23 through nightly:

error[E0597]: `a[..]` does not live long enough
  --> src/main.rs:18:32
   |
18 |     map(&bar, |a: &&[i32; 2]| &a[0]); // does not live long enough
   |                                ^^^^- borrowed value only lives until here
   |                                |
   |                                does not live long enough
   |
note: borrowed value must be valid for the anonymous lifetime #2 defined on the body at 18:15...
  --> src/main.rs:18:15
   |
18 |     map(&bar, |a: &&[i32; 2]| &a[0]); // does not live long enough
   |               ^^^^^^^^^^^^^^^^^^^^^

xref: https://bugzilla.redhat.com/show_bug.cgi?id=1535396

Metadata

Metadata

Assignees

Labels

A-NLLArea: Non-lexical lifetimes (NLL)A-borrow-checkerArea: The borrow checkerC-bugCategory: This is a bug.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.fixed-by-NLLBugs fixed, but only when NLL is enabled.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions