Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tracing::instrument(ret) errors on function with &mut args returning &/&mut #2857

Closed
jdygert-spok opened this issue Jan 18, 2024 · 2 comments

Comments

@jdygert-spok
Copy link

Bug Report

Version

tracing v0.1.40
tracing-attributes v0.1.27 (proc-macro)
tracing-core v0.1.32

Description

When using instrument(ret) (or instrument(err)), the closure in the generated code is inferred to be Fn or FnMut, which causes compile errors when trying to return a reference to an argument.

// error: lifetime may not live long enough
// note: closure implements `Fn`, so references to captured variables can't escape the closure
#[tracing::instrument(ret)]
fn foo(x: &mut ()) -> &() {
    x
}

// error: captured variable cannot escape `FnMut` closure body
#[tracing::instrument(ret)]
fn foo(x: &mut ()) -> &mut () {
    x
}

Possible Solution

Force the generated closure to be FnOnce, either via a function:

fn force_fn_once<T, F: FnOnce() -> T>(f: F) -> F {
    f
}

force_fn_once(move || {
    // ...
})

Or by rebinding a captured variable

let outer = &mut ();
move || {
    { let inner = outer; }
    // ...
}
@Hakuyume
Copy link

Hakuyume commented Mar 6, 2024

Seems similar to #2796 ?

@jdygert-spok
Copy link
Author

Yep. I swear I searched for it... Closing as duplicate

@jdygert-spok jdygert-spok closed this as not planned Won't fix, can't repro, duplicate, stale Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants