Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
SimonSapin opened this issue Feb 25, 2018 · 6 comments · Fixed by #59114
Labels
A-borrow-checker Area: The borrow checker C-enhancement Category: An issue proposing an enhancement or a PR with one. NLL-fixed-by-NLL Bugs fixed, but only when NLL is enabled. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@SimonSapin
Copy link
Contributor

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
    },
]);
@estebank estebank added the A-borrow-checker Area: The borrow checker label Feb 26, 2018
@ghost
Copy link

ghost commented Feb 26, 2018

Hey I learned something new, like:
adding #![feature(nll)] is, apparently, equivalent to -Z borrowck=mir and to -Z nll

NLL tracking issue

how to pass -Z flags with cargo

Thanks to durka42_ (on IRC) for telling me to how to pass those pesky -Z flags :D
eg. cargo rustc -- -Z borrowck=mir

rustc -Z help

@nikomatsakis
Copy link
Contributor

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

@nikomatsakis
Copy link
Contributor

@SimonSapin is this a regression, do you know?

@SimonSapin
Copy link
Contributor Author

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

@nikomatsakis
Copy link
Contributor

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

@XAMPPRocky XAMPPRocky added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels May 14, 2018
@SimonSapin SimonSapin added the NLL-fixed-by-NLL Bugs fixed, but only when NLL is enabled. label Nov 26, 2018
@SimonSapin
Copy link
Contributor Author

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

bors added a commit that referenced this issue Mar 11, 2019
Enable NLL migrate mode on the 2015 edition

Blocked on #58739

## What is in this PR?

* Remove the `-Zborrowck=ast` flag option from rustc.
* The default in the 2015 edition is now `-Zborrowck=migrate`.
* The 2018 edition default is unchanged: it's still `-Zborrowck=migrate`.
* Enable the `-Ztwo-phase-borrows` flag on all editions.
* Remove most dead code that handled these options.
* Update tests for the above changes.

## What is *not* in this PR?

These are left for future PRs

* Use `-Zborrowck=mir` in NLL compare mode tests
* Remove the `-Zborrowck=compare` option
* Remove the `-Ztwo-phase-borrows` flag. It's kept so that perf.rlo has time to stop using it (cc @Mark-Simulacrum)
* Remove MIR typeck as its own MIR pass - it's now run by NLL.
* Enabling `-Zborrowck=mir` by default

Soundness issues that are fixed by NLL will stay open until full NLL is emitting hard errors. However, these diagnostics and completeness issues can now be closed:

Closes #18330
Closes #22323
Closes #23591
Closes #26736
Closes #27487
Closes #28092
Closes #28970
Closes #29733
Closes #30104
Closes #38915
Closes #39908
Closes #43407
Closes #47524
Closes #48540
Closes #49073
Closes #52614
Closes #55085
Closes #56093
Closes #56496
Closes #57804

cc #43234

r? @pnkfelix
cc @rust-lang/lang
cc @rust-lang/wg-compiler-nll
bors added a commit that referenced this issue Apr 22, 2019
Enable NLL migrate mode on the 2015 edition

## What is in this PR?

* Remove the `-Zborrowck=ast` flag option from rustc.
* The default in the 2015 edition is now `-Zborrowck=migrate`.
* The 2018 edition default is unchanged: it's still `-Zborrowck=migrate`.
* Enable two-phase borrows (currently toggled via the `-Ztwo-phase-borrows` flag) on all editions.
* Remove most dead code that handled these options.
* Update tests for the above changes.

## What is *not* in this PR?

These are left for future PRs

* Use `-Zborrowck=mir` in NLL compare mode tests (#56993)
* Remove the `-Zborrowck=compare` option (#59193)
* Remove the `-Ztwo-phase-borrows` flag. It's kept, as a flag that does nothing so that perf.rlo has time to stop using it (cc @Mark-Simulacrum)
* Remove MIR typeck as its own MIR pass - it's now run by NLL.
* Enabling `-Zborrowck=mir` by default (#58781)
* Replace `allow_bind_by_move_patterns_with_guards` and `check_for_mutation_in_guard_via_ast_walk` with just using the feature gate. (#59192)

Soundness issues that are fixed by NLL will stay open until full NLL is emitting hard errors. However, these diagnostics and completeness issues can now be closed:

Closes #18330
Closes #22323
Closes #23591
Closes #26736
Closes #27487
Closes #28092
Closes #28970
Closes #29733
Closes #30104
Closes #38915
Closes #39908
Closes #43407
Closes #47524
Closes #48540
Closes #49073
Closes #52614
Closes #55085
Closes #56093
Closes #56496
Closes #57804

cc #43234

r? @pnkfelix
cc @rust-lang/lang
cc @rust-lang/wg-compiler-nll
bors added a commit that referenced this issue Apr 22, 2019
Enable NLL migrate mode on the 2015 edition

## What is in this PR?

* Remove the `-Zborrowck=ast` flag option from rustc.
* The default in the 2015 edition is now `-Zborrowck=migrate`.
* The 2018 edition default is unchanged: it's still `-Zborrowck=migrate`.
* Enable two-phase borrows (currently toggled via the `-Ztwo-phase-borrows` flag) on all editions.
* Remove most dead code that handled these options.
* Update tests for the above changes.

## What is *not* in this PR?

These are left for future PRs

* Use `-Zborrowck=mir` in NLL compare mode tests (#56993)
* Remove the `-Zborrowck=compare` option (#59193)
* Remove the `-Ztwo-phase-borrows` flag. It's kept, as a flag that does nothing so that perf.rlo has time to stop using it (cc @Mark-Simulacrum)
* Remove MIR typeck as its own MIR pass - it's now run by NLL.
* Enabling `-Zborrowck=mir` by default (#58781)
* Replace `allow_bind_by_move_patterns_with_guards` and `check_for_mutation_in_guard_via_ast_walk` with just using the feature gate. (#59192)

Soundness issues that are fixed by NLL will stay open until full NLL is emitting hard errors. However, these diagnostics and completeness issues can now be closed:

Closes #18330
Closes #22323
Closes #23591
Closes #26736
Closes #27487
Closes #28092
Closes #28970
Closes #29733
Closes #30104
Closes #38915
Closes #39908
Closes #43407
Closes #47524
Closes #48540
Closes #49073
Closes #52614
Closes #55085
Closes #56093
Closes #56496
Closes #57804

cc #43234

r? @pnkfelix
cc @rust-lang/lang
cc @rust-lang/wg-compiler-nll
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker C-enhancement Category: An issue proposing an enhancement or a PR with one. NLL-fixed-by-NLL Bugs fixed, but only when NLL is enabled. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants