Skip to content

Commit

Permalink
Merge pull request #187 from dtolnay/temporaries
Browse files Browse the repository at this point in the history
Drop temporaries as early as possible in $:expr case
  • Loading branch information
dtolnay committed Nov 4, 2021
2 parents fb3e4b7 + 38591a9 commit a32c1d1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,10 @@ macro_rules! anyhow {
});
($err:expr $(,)?) => ({
use $crate::private::kind::*;
match $err {
let error = match $err {
error => (&error).anyhow_kind().new(error),
}
};
error
});
($fmt:expr, $($arg:tt)*) => {
$crate::Error::msg($crate::private::format!($fmt, $($arg)*))
Expand Down
9 changes: 9 additions & 0 deletions tests/test_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod common;

use self::common::*;
use anyhow::{anyhow, ensure};
use std::cell::Cell;
use std::future;

#[test]
Expand Down Expand Up @@ -61,6 +62,14 @@ fn test_temporaries() {
// enclosing future non-Send.
future::ready(anyhow!("...")).await;
});

fn message(cell: Cell<&str>) -> &str {
cell.get()
}

require_send_sync(async {
future::ready(anyhow!(message(Cell::new("...")))).await;
});
}

#[test]
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/temporary-value.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ error[E0716]: temporary value dropped while borrowed
--> tests/ui/temporary-value.rs:4:22
|
4 | let _ = anyhow!(&String::new());
| ---------^^^^^^^^^^^^^-- temporary value is freed at the end of this statement
| ---------^^^^^^^^^^^^^-
| | |
| | creates a temporary which is freed while still in use
| temporary value is freed at the end of this statement
| argument requires that borrow lasts for `'static`

0 comments on commit a32c1d1

Please sign in to comment.