Skip to content

Commit

Permalink
attributes: add compile error on const fn instrumentation (#2418)
Browse files Browse the repository at this point in the history
## Motivation

The `#[instrument]` macro cannot be used on `const fn`s, because the
generated code will perform runtime tracing behavior. However, when
adding the attribute to a `const fn`, the compiler errors generated
currently are somewhat unclear (see #2414). It would be better if we
generated a less verbose error that simply states that `#[instrument]`
is not supported on `const fn`s.

## Solution

This branch changes the `#[instrument]` macro to detect when the
annotated function is a `const fn`, and emit a simpler, more descritpive
error message. The new error simply states that the `#[instrument]`
attribute cannot be used on `const fn`s, and should be much less
confusing to the user.

Fixes #2414
  • Loading branch information
andrewpollack authored and hawkw committed Apr 21, 2023
1 parent 10c32f2 commit 7cceb79
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
17 changes: 16 additions & 1 deletion tracing-attributes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
extern crate proc_macro;

use proc_macro2::TokenStream;
use quote::ToTokens;
use quote::{quote, ToTokens};
use syn::parse::{Parse, ParseStream};
use syn::{Attribute, ItemFn, Signature, Visibility};

Expand Down Expand Up @@ -535,6 +535,14 @@ mod expand;
/// }
/// ```
///
/// `const fn` cannot be instrumented, and will result in a compilation failure:
///
/// ```compile_fail
/// # use tracing_attributes::instrument;
/// #[instrument]
/// const fn my_const_function() {}
/// ```
///
/// [span]: https://docs.rs/tracing/latest/tracing/span/index.html
/// [name]: https://docs.rs/tracing/latest/tracing/struct.Metadata.html#method.name
/// [target]: https://docs.rs/tracing/latest/tracing/struct.Metadata.html#method.target
Expand Down Expand Up @@ -585,6 +593,13 @@ fn instrument_precise(
let input = syn::parse::<ItemFn>(item)?;
let instrumented_function_name = input.sig.ident.to_string();

if input.sig.constness.is_some() {
return Ok(quote! {
compile_error!("the `#[instrument]` attribute may not be used with `const fn`s")
}
.into());
}

// check for async_trait-like patterns in the block, and instrument
// the future instead of the wrapper
if let Some(async_like) = expand::AsyncInfo::from_fn(&input) {
Expand Down
7 changes: 7 additions & 0 deletions tracing-attributes/tests/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ fn async_instrument() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/ui/async_instrument.rs");
}

#[rustversion::stable]
#[test]
fn const_instrument() {
let t = trybuild::TestCases::new();
t.compile_fail("tests/ui/const_instrument.rs");
}
8 changes: 8 additions & 0 deletions tracing-attributes/tests/ui/const_instrument.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![allow(unreachable_code)]

#[tracing::instrument]
const fn unit() {
""
}

fn main() {}
15 changes: 15 additions & 0 deletions tracing-attributes/tests/ui/const_instrument.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error: macros that expand to items must be delimited with braces or followed by a semicolon
--> tests/ui/const_instrument.rs:3:1
|
3 | #[tracing::instrument]
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the attribute macro `tracing::instrument` (in Nightly builds, run with -Z macro-backtrace for more info)

error: the `#[instrument]` attribute may not be used with `const fn`s
--> tests/ui/const_instrument.rs:3:1
|
3 | #[tracing::instrument]
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the attribute macro `tracing::instrument` (in Nightly builds, run with -Z macro-backtrace for more info)

0 comments on commit 7cceb79

Please sign in to comment.