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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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 | ||
|
@@ -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!( | ||
#(#attrs) * | ||
#vis #constness #unsafety #asyncness #abi fn #ident<#gen_params>(#params) #return_type | ||
#where_clause | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, .. | ||
|
@@ -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, .. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 theSpan
type explicitly?I believe that we can use the
Spanned
impl forItemFn
for this.There was a problem hiding this comment.
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 asquote_spanned!(Span::call_site()=> …)
There was a problem hiding this comment.
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!