Skip to content
New issue

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

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

Already on GitHub? # to your account

Weird lifetime error with closure and early-bound lifetime #28092

Closed
eefriedman opened this issue Aug 29, 2015 · 3 comments · Fixed by #59114
Closed

Weird lifetime error with closure and early-bound lifetime #28092

eefriedman opened this issue Aug 29, 2015 · 3 comments · Fixed by #59114
Labels
A-closures Area: Closures (`|…| { … }`) A-lifetimes Area: Lifetimes / regions C-enhancement Category: An issue proposing an enhancement or a PR with one. fixed-by-NLL Bugs fixed, but only when NLL is enabled.

Comments

@eefriedman
Copy link
Contributor

struct S;
impl S {
    fn f<'a:'a>(_x:&'a i32) {}
}
fn main() {
    let g1: &Fn(&i32) = &|x| S::f(x);
    g1(&1);
    let g2 = &|x| S::f(x);
    g2(&1);
}
<anon>:9:9: 9:10 error: borrowed value does not live long enough
<anon>:9     g2(&1);
                 ^
<anon>:8:27: 10:2 note: reference must be valid for the block suffix following statement 2 at 8:26...
<anon>: 8     let g2 = &|x| S::f(x);
<anon>: 9     g2(&1);
<anon>:10 }
<anon>:9:5: 9:12 note: ...but borrowed value is only valid for the statement at 9:4
<anon>:9     g2(&1);
             ^~~~~~~
<anon>:9:5: 9:12 help: consider using a `let` binding to increase its lifetime
<anon>:9     g2(&1);
             ^~~~~~~
error: aborting due to previous error

Lifetime inference is doing something weird with the second closure.

@steveklabnik steveklabnik added the A-lifetimes Area: Lifetimes / regions label Sep 3, 2015
@steveklabnik steveklabnik added the A-closures Area: Closures (`|…| { … }`) label Mar 1, 2017
@steveklabnik
Copy link
Member

Triage: new error format, but no change

error: borrowed value does not live long enough
  --> foo.rs:9:11
   |
9  |     g2(&1);
   |         - ^ temporary value dropped here while still borrowed
   |         |
   |         temporary value created here
10 | }
   | - temporary value needs to live until here
   |
   = note: consider using a `let` binding to increase its lifetime

@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 22, 2017
@memoryruins
Copy link
Contributor

Triage: now compiles on both editions on stable.
rustc: 1.32.0

@matthewjasper matthewjasper added the fixed-by-NLL Bugs fixed, but only when NLL is enabled. label Jan 30, 2019
@matthewjasper
Copy link
Contributor

This slight variant still errors on the 2015 editiion:

struct S;
impl S {
    fn f<'a:'a>(_x:&'a i32) {}
}
fn main() {
    let g1: &Fn(&mut i32) = &|x| S::f(x);
    g1(&mut 1);
    let g2 = &|x| S::f(x);
    g2(&mut 1);
}

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
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-closures Area: Closures (`|…| { … }`) A-lifetimes Area: Lifetimes / regions C-enhancement Category: An issue proposing an enhancement or a PR with one. fixed-by-NLL Bugs fixed, but only when NLL is enabled.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants