Skip to content

Instantly share code, notes, and snippets.

@tesaguri
Created September 17, 2023 23:59
Show Gist options
  • Save tesaguri/67d6cd85b7adde59e180a267e9104f36 to your computer and use it in GitHub Desktop.
Save tesaguri/67d6cd85b7adde59e180a267e9104f36 to your computer and use it in GitHub Desktop.
Inconsistent borrowck behavior between sync/async `fn`s when reassigning function arguments with local borrows
// This doesn't compile (with good reason!):
#[allow(unused)]
fn reassign_and_return_arg(mut arg: &()) -> &() {
let local = ();
arg = &local;
arg
//~^ ERROR cannot return value referencing local variable `local`
}
// This doesn't compile either (seemingly without good reason, but consistent with the previous code):
#[allow(unused)]
fn reassign_arg(mut arg: &()) {
let local = ();
//~^ ERROR `local` does not live long enough
arg = &local;
}
// But this compiles (WHAT):
#[allow(unused)]
async fn reassign_arg_async(mut arg: &()) {
let local = ();
arg = &local;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment