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

attributes: force instrumented function to be FnOnce #2847

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions tracing-attributes/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,27 @@ fn gen_block<B: ToTokens>(
}
);

// Bind a non-Copy value to make the closure FnOnce
// (https://github.com/tokio-rs/tracing/issues/2796).
let fn_once_init = quote!(
let __tracing_attr_fn_once = {
struct TracingAttrFnOnce;
TracingAttrFnOnce
};
);
let fn_once_bind = quote!(
let __tracing_attr_fn_once = __tracing_attr_fn_once;
);

match (err_event, ret_event) {
(Some(err_event), Some(ret_event)) => quote_spanned! {block.span()=>
#span
#fn_once_init
#[allow(clippy::redundant_closure_call)]
match (move || #block)() {
match (move || {
#fn_once_bind
#block
})() {
#[allow(clippy::unit_arg)]
Ok(x) => {
#ret_event;
Expand All @@ -369,8 +385,12 @@ fn gen_block<B: ToTokens>(
},
(Some(err_event), None) => quote_spanned!(block.span()=>
#span
#fn_once_init
#[allow(clippy::redundant_closure_call)]
match (move || #block)() {
match (move || {
#fn_once_bind
#block
})() {
#[allow(clippy::unit_arg)]
Ok(x) => Ok(x),
Err(e) => {
Expand All @@ -381,8 +401,12 @@ fn gen_block<B: ToTokens>(
),
(None, Some(ret_event)) => quote_spanned!(block.span()=>
#span
#fn_once_init
#[allow(clippy::redundant_closure_call)]
let x = (move || #block)();
let x = (move || {
#fn_once_bind
#block
})();
#ret_event;
x
),
Expand Down
15 changes: 15 additions & 0 deletions tracing-attributes/tests/instrument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ fn repro_2294() {
let i = 42;
}

// Reproduces a compile error when an instrumented function signature contains lifetimes
// (https://github.com/tokio-rs/tracing/issues/2796).
#[instrument(err)]
fn repro_2796_err(x: &mut u8) -> Result<&u8, &u8> {
Ok(x)
}
#[instrument(ret)]
fn repro_2796_ret(x: &mut u8) -> Result<&u8, &u8> {
Ok(x)
}
#[instrument(err, ret)]
fn repro_2796_err_ret(x: &mut u8) -> Result<&u8, &u8> {
Ok(x)
}

#[test]
fn override_everything() {
#[instrument(target = "my_target", level = "debug")]
Expand Down