Skip to content

Commit 4f5ef04

Browse files
KaiJewsonandrewhickman
authored andcommitted
Force the closure to be FnOnce
1 parent d01c58e commit 4f5ef04

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ use syn::Token;
6363
/// often be fixed by setting the `move` option of the attribute macro. For example:
6464
///
6565
/// ```
66+
/// use fn_error_context::context;
67+
///
6668
/// #[context(move, "context")]
6769
/// fn returns_reference(val: &mut u32) -> anyhow::Result<&mut u32> {
6870
/// Ok(&mut *val)
@@ -90,7 +92,13 @@ pub fn context(args: TokenStream, input: TokenStream) -> TokenStream {
9092
}
9193
} else {
9294
quote! {
93-
(#move_token || #return_ty #body)().map_err(|err| err.context(format!(#format_args)).into())
95+
// Moving a non-`Copy` value into the closure tells borrowck to always treat the closure
96+
// as a `FnOnce`, preventing some borrowing errors.
97+
let force_fn_once = ::core::iter::empty::<()>();
98+
(#move_token || #return_ty {
99+
::core::mem::drop(force_fn_once);
100+
#body
101+
})().map_err(|err| err.context(format!(#format_args)).into())
94102
}
95103
};
96104
input.block.stmts = vec![syn::Stmt::Expr(syn::Expr::Verbatim(new_body))];

tests/borrowing.rs

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/tests.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ fn tests() {
1717
tests.pass("tests/fmt_named_arg.rs");
1818
tests.compile_fail("tests/async_without_return.rs");
1919
tests.compile_fail("tests/preserve_lint.rs");
20-
tests.pass("tests/borrowing.rs");
2120
tests.pass("tests/async_borrowing.rs");
2221
tests.pass("tests/no_move.rs");
2322
tests.pass("tests/async_no_move.rs");

0 commit comments

Comments
 (0)