Skip to content

Borrow checker false dropped while still borrowed #38915

Closed
@letheed

Description

@letheed

The following does not compile:

use std::ffi::OsString;
use std::io::{Result as IoResult};
use std::path::Path;

fn main() {
    let paths: Vec<OsString> = vec!["filename".into()];
// Switching these two lines appeases our lord and savior the borrow checker
    let f = std::fs::rename;
    let dests = paths.clone();
// ---------------------------
    let pairs = paths.iter().zip(dests.iter());
//                               ----- borrow occurs here
    foo(f, pairs);
}
// `dests` does not live long enough.
// ^ `dests` dropped here while still borrowed

fn foo<P, I>(f: fn(P, P) -> IoResult<()>, pairs: I)
    where P: AsRef<Path>,
          I: Iterator<Item=(P, P)>,
{
    for (src, dest) in pairs {
        let _ = f(src, dest);
    }
}

EDIT: simplified code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerC-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