Skip to content

Compiler gets lifetime scope wrong #28970

Closed

Description

@arthurprs

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.

Activity

  1. changed the title [-]Compiler gets lifetime scope wrong in an oneliner[/-] [+]Compiler gets lifetime scope wrong[/+] on Oct 11, 2015
  2. steveklabnik commented on Nov 11, 2015

    @steveklabnik
    Contributor

    @gankro is this the expected behavior of drian?

  3. Gankra commented on Nov 12, 2015

    @Gankra
    Contributor

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

  4. steveklabnik commented on Apr 30, 2017

    @steveklabnik
    Contributor

    Triage: no change, still reproduces

  5. memoryruins commented on Jan 30, 2019

    @memoryruins
    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

  6. pnkfelix commented on Feb 27, 2019

    @pnkfelix
    Contributor

    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.

  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 checkerA-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.fixed-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