Skip to content

Commit

Permalink
Add compile error on const fn instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewpollack committed Dec 20, 2022
1 parent 02903cb commit 0e3399a
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 @@ -79,7 +79,7 @@
)]

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 @@ -342,6 +342,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
/// [`follows_from`]: https://docs.rs/tracing/latest/tracing/struct.Span.html#method.follows_from
/// [`tracing`]: https://github.com/tokio-rs/tracing
Expand Down Expand Up @@ -385,6 +393,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!("instrumenting const fn not supported")
}
.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/async_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: instrumenting const fn not supported
--> tests/ui/async_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 0e3399a

Please sign in to comment.