Skip to content

Extend lifetime of temporaries #28

@dtolnay

Description

@dtolnay

The behavior of maplit's macros when temporaries are involved is inconsistent with vec! and inconsistent with how temporary lifetimes usually work. Temporaries are supposed to outlive the current statement i.e. live "until the semicolon".

use maplit::hashset;
use std::collections::HashSet;
use std::path::Path;

fn v(_: Vec<&str>) {}
fn s(_: HashSet<&str>) {}

fn main() {
    let path = Path::new("/");

    // fine
    v(vec![path.to_string_lossy().as_ref()]);

    // does not compile
    s(hashset![path.to_string_lossy().as_ref()]);
}
error[E0716]: temporary value dropped while borrowed
  --> src/main.rs:15:16
   |
15 |     s(hashset![path.to_string_lossy().as_ref()]);
   |       ---------^^^^^^^^^^^^^^^^^^^^^^----------
   |       |        |
   |       |        creates a temporary which is freed while still in use
   |       temporary value is freed at the end of this statement
   |       borrow later used here
   |
   = note: consider using a `let` binding to create a longer lived value
   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

As a proof of concept, an expansion like this would behave correctly with lifetimes of temporaries:

s({
    match (path.to_string_lossy().as_ref(), /* ... */) {
        (x0, /* ... */) => {
            let mut set = HashSet::new();
            set.insert(x0);
            /* ... */
            set
        }
    }
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions