Skip to content

Spurious "does not live long enough" for closure coerced to fn pointer in static item (AST borrowck only) #48540

Closed

Description

@SimonSapin

The test case below is reduced from a phf map.

rustc 1.26.0-nightly (28a1e4f 2018-02-24) with -Z borrowck=mir: OK

Without any -Z flag: error below, even though I believe the program to be valid. But maybe it’s not worth fixing if MIR-based borrow-checking will become the default soon? CC @nikomatsakis

A work-around is to have the closure in an intermediate const item.

fn main() {}

struct Static<T: 'static>(&'static [T]);

static FOO: Static<fn(&u32) -> &u32> = Static(&[|x| x]);
error[E0597]: borrowed value does not live long enough
 --> a.rs:5:52
  |
5 | pub static FOO: Static<fn(&u32) -> &u32> = Static(&[|x| x]);
  |                                                    ^^^^^^^- temporary value only lives until here
  |                                                    |
  |                                                    temporary value does not live long enough
  |
  = note: borrowed value must be valid for the static lifetime...

error: aborting due to previous error
fn main() {}

struct Static<T: 'static>(&'static [T]);

static FOO: Static<fn(&u32) -> &u32> = Static(&[
    {
        const F: fn(&u32) -> &u32 = |x| x;  // work around
        F
    },
]);

Activity

  1. nikomatsakis commented on Feb 27, 2018

    @nikomatsakis
    Contributor

    It's actually equivalent to -Zborrowck=mir -Znll -Ztwo-phase-borrows, I think

  2. nikomatsakis commented on Feb 27, 2018

    @nikomatsakis
    Contributor

    @SimonSapin is this a regression, do you know?

  3. SimonSapin commented on Feb 27, 2018

    @SimonSapin
    ContributorAuthor

    @nikomatsakis Not as far as I know. 1.24.0 stable gives the same error. Note that this is fixed by MIR-borrowck.

  4. nikomatsakis commented on Feb 27, 2018

    @nikomatsakis
    Contributor

    OK. In that case I am inclined to wait, yes.

  5. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    T-langRelevant to the language team
    on May 14, 2018
  6. SimonSapin commented on Nov 26, 2018

    @SimonSapin
    ContributorAuthor

    Adding the "fixed by NLL" label as an approximation of fixed by MIR borrowck

  7. added a commit that references this issue on Mar 11, 2019
    65cb35a
  8. added 3 commits that reference this issue on Apr 22, 2019
    bf0ea60
    c21fbfe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-langRelevant to the language teamfixed-by-NLLBugs fixed, but only when NLL is enabled.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions