Mut borrow persists after await #106688
Labels
A-async-await
Area: Async & Await
A-borrow-checker
Area: The borrow checker
A-lifetimes
Area: Lifetimes / regions
AsyncAwait-Triaged
Async-await issues that have been triaged during a working group meeting.
C-bug
Category: This is a bug.
E-needs-mcve
Call for participation: This issue has a repro, but needs a Minimal Complete and Verifiable Example
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
WG-async
Working group: Async & await
I tried this code:
I expected to see this happen: Compile fine.
Instead, this happened: Compile fails with error:
Meta
Playground versions:
Notes
I admit I'm still wobbly with a lot that has to do with lifetimes and futures, but it's my understanding that
await
consumes the future so no borrows should exist after that.The error seems very similar to #98077 but that only discusses drops and not awaits, and implies that await resolves the issue (where here await does nothing). This problem is discussed here: https://stackoverflow.com/questions/70538177/rust-async-borrow-lifetimes where it's suggested HRTBs solve the problem, but I couldn't get the example working. There's a similar discussion here: https://www.reddit.com/r/rust/comments/m1sj3b/problems_with_lifetimes_and_async_fns/ which failed to find a solution.
Without
'f
and'b
compilingtx
passes but instead there's a lifetime error in the closure about the future outliving the parameter.My use case is specifically about wrapping transactions in databases, where a user passes in a function and when their function is done executing the transaction (b) is committed or rolled back (I've hit this trying to come up with a transaction wrapper with multiple DB libraries now).
I can work around the issue by moving b into the callback and having the callback return b at the end, but this renders
?
useless and results in a lot more boilerplate.The text was updated successfully, but these errors were encountered: