mirror of
https://github.com/tokio-rs/tracing.git
synced 2025-09-28 21:42:15 +00:00
attributes: add compile error on const fn instrumentation (#2418)
## 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
This commit is contained in:
parent
10c32f23b3
commit
7cceb79b11
@ -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};
|
||||
|
||||
@ -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
|
||||
@ -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) {
|
||||
|
@ -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
tracing-attributes/tests/ui/const_instrument.rs
Normal file
8
tracing-attributes/tests/ui/const_instrument.rs
Normal file
@ -0,0 +1,8 @@
|
||||
#![allow(unreachable_code)]
|
||||
|
||||
#[tracing::instrument]
|
||||
const fn unit() {
|
||||
""
|
||||
}
|
||||
|
||||
fn main() {}
|
15
tracing-attributes/tests/ui/const_instrument.stderr
Normal file
15
tracing-attributes/tests/ui/const_instrument.stderr
Normal file
@ -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)
|
Loading…
x
Reference in New Issue
Block a user