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: remove direct dependency on proc-macro2 #296

Merged
merged 1 commit into from Aug 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion tracing-attributes/Cargo.toml
Expand Up @@ -41,7 +41,6 @@ async-await = []
[dependencies]
syn = { version = "1", features = ["full", "extra-traits"] }
quote = "1"
proc-macro2 = "1"

[dev-dependencies]
tracing = "0.1"
Expand Down
10 changes: 4 additions & 6 deletions tracing-attributes/src/lib.rs
Expand Up @@ -40,8 +40,7 @@
extern crate proc_macro;

use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::{quote, quote_spanned};
use quote::{quote, quote_spanned, ToTokens};
use syn::{
spanned::Spanned, AttributeArgs, FnArg, Ident, ItemFn, Lit, LitInt, Meta, MetaNameValue,
NestedMeta, Pat, PatIdent, PatType, Signature,
Expand Down Expand Up @@ -112,7 +111,6 @@ use syn::{
pub fn instrument(args: TokenStream, item: TokenStream) -> TokenStream {
let input: ItemFn = syn::parse_macro_input!(item as ItemFn);
let args = syn::parse_macro_input!(args as AttributeArgs);
let call_site = Span::call_site();

// these are needed ahead of time, as ItemFn contains the function body _and_
// isn't representable inside a quote!/quote_spanned! macro
Expand Down Expand Up @@ -187,7 +185,7 @@ pub fn instrument(args: TokenStream, item: TokenStream) -> TokenStream {
let level = level(&args);
let target = target(&args);

quote_spanned!(call_site=>
quote!(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think propagating the span of the original function is important, so that any errors are displayed correctly. Is there a way to do that using syn::spanned::Spanned, so that we don't need to import the Span type explicitly?

I believe that we can use the Spanned impl for ItemFn for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be misunderstanding the documentation, but this seems to indicate that quote!(…) does the same thing as quote_spanned!(Span::call_site()=> …)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I think I missed that in the docs, that's great. Nevermind my previous comment!

#(#attrs) *
#vis #constness #unsafety #asyncness #abi fn #ident<#gen_params>(#params) #return_type
#where_clause
Expand All @@ -204,7 +202,7 @@ pub fn instrument(args: TokenStream, item: TokenStream) -> TokenStream {
.into()
}

fn level(args: &AttributeArgs) -> proc_macro2::TokenStream {
fn level(args: &AttributeArgs) -> impl ToTokens {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

let mut levels = args.iter().filter_map(|arg| match arg {
NestedMeta::Meta(Meta::NameValue(MetaNameValue {
ref path, ref lit, ..
Expand Down Expand Up @@ -258,7 +256,7 @@ fn level(args: &AttributeArgs) -> proc_macro2::TokenStream {
}
}

fn target(args: &AttributeArgs) -> proc_macro2::TokenStream {
fn target(args: &AttributeArgs) -> impl ToTokens {
let mut levels = args.iter().filter_map(|arg| match arg {
NestedMeta::Meta(Meta::NameValue(MetaNameValue {
ref path, ref lit, ..
Expand Down