-
Notifications
You must be signed in to change notification settings - Fork 22
Open
Description
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
Labels
No labels