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

Compiler gets lifetime scope wrong #28970

Closed
arthurprs opened this issue Oct 11, 2015 · 5 comments · Fixed by #59114
Closed

Compiler gets lifetime scope wrong #28970

arthurprs opened this issue Oct 11, 2015 · 5 comments · Fixed by #59114
Labels
A-borrow-checker Area: The borrow checker A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. NLL-fixed-by-NLL Bugs fixed, but only when NLL is enabled.

Comments

@arthurprs
Copy link
Contributor

Please check the code bellow

https://play.rust-lang.org/?gist=c0dd3d93bde346c4bd4d&version=nightly

#![feature(drain)]

use std::sync::{Arc, RwLock};

fn main() {
    let files = RwLock::new(vec![Arc::new(1u32)]);

    // WONT compile
    let a: Vec<_> = files.write().unwrap().drain(..1).collect();

    // WONT compile
    let a: Vec<_> = {
        let mut temp = files.write().unwrap();
        temp.drain(..1).collect()
    };

    // compiles
    let a: Vec<_> = {
        let mut temp = files.write().unwrap();
        let res = temp.drain(..1).collect();
        res
    };

    println!("{:?}", a);
}

I suppose all of them should compile as they do the same thing, but only the last one does.

@arthurprs arthurprs changed the title Compiler gets lifetime scope wrong in an oneliner Compiler gets lifetime scope wrong Oct 11, 2015
@steveklabnik steveklabnik added the A-lifetimes Area: Lifetimes / regions label Nov 11, 2015
@steveklabnik
Copy link
Member

@gankro is this the expected behavior of drian?

@Gankra
Copy link
Contributor

Gankra commented Nov 12, 2015

Nothin' to do with Drain in particular. Just the compiler being suboptimal with temporary lifetimes.

@steveklabnik
Copy link
Member

Triage: no change, still reproduces

@Mark-Simulacrum Mark-Simulacrum added C-bug Category: This is a bug. A-borrow-checker Area: The borrow checker labels Jul 24, 2017
@memoryruins
Copy link
Contributor

Triage: now compiles on 2018.
2015 errors:

error[E0597]: borrowed value does not live long enough
 --> src/main.rs:9:21
  |
9 |     let a: Vec<_> = files.write().unwrap().drain(..1).collect();
  |                     ^^^^^^^^^^^^^^^^^^^^^^                     - temporary value dropped here while still borrowed
  |                     |
  |                     temporary value does not live long enough
  |
  = note: values in a scope are dropped in the opposite order they are created
  = note: consider using a `let` binding to increase its lifetime

rustc: 1.32.0

@memoryruins memoryruins added the NLL-fixed-by-NLL Bugs fixed, but only when NLL is enabled. label Jan 30, 2019
@Gankra Gankra closed this as completed Feb 26, 2019
@pnkfelix
Copy link
Member

pnkfelix commented Feb 27, 2019

Reopening: Our current policy is if the 2015 edition alone suffers from a bug that is scheduled to be fixed by NLL (i.e. tagged NLL-fixed-by-NLL) then we leave such issues open until NLL is turned on in the 2015 edition.

@pnkfelix pnkfelix reopened this Feb 27, 2019
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 A-lifetimes Area: Lifetimes / regions C-bug Category: This is a bug. NLL-fixed-by-NLL Bugs fixed, but only when NLL is enabled.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants