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

Add compile error on const fn instrumentation #2418

Merged
merged 5 commits into from
Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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")
andrewpollack marked this conversation as resolved.
Show resolved Hide resolved
}
.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)