Skip to content

Compiler gets lifetime scope wrong #28970

Closed
@arthurprs

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.

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