Skip to content

Commit 9d2c9d2

Browse files
committed
derive: Remove dummy identifier
1 parent 9c9cbe4 commit 9d2c9d2

File tree

3 files changed

+23
-33
lines changed

3 files changed

+23
-33
lines changed

oauth1-request-derive/src/lib.rs

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ mod util;
2323
use proc_macro2::{Span, TokenStream};
2424
use proc_macro_crate::FoundCrate;
2525
use proc_macro_error::{abort, abort_if_dirty, emit_error, proc_macro_error};
26-
use quote::quote;
26+
use quote::{quote, quote_spanned};
2727
use syn::spanned::Spanned;
2828
use syn::{
2929
parse_macro_input, parse_quote, Data, DataStruct, DeriveInput, Fields, GenericParam, Generics,
@@ -50,11 +50,6 @@ pub fn derive_oauth1_authorize(input: proc_macro::TokenStream) -> proc_macro::To
5050
fn expand_derive_oauth1_authorize(mut input: DeriveInput) -> TokenStream {
5151
let name = &input.ident;
5252

53-
// We assume that `dummy` does not conflict with any of call-side identifiers
54-
// and (ab)use it to avoid potential name collisions.
55-
let dummy = format!("_impl_ToOAuth1Request_for_{}", name);
56-
let dummy = Ident::new(&dummy, Span::call_site());
57-
5853
let krate = match proc_macro_crate::crate_name("oauth1-request").unwrap() {
5954
FoundCrate::Name(krate) => krate,
6055
// This is used in `oauth1_request`'s doctests.
@@ -105,36 +100,22 @@ fn expand_derive_oauth1_authorize(mut input: DeriveInput) -> TokenStream {
105100

106101
abort_if_dirty();
107102

108-
let mut fn_generics = input.generics.clone();
109-
fn_generics.params.push(parse_quote! {
110-
#dummy: _oauth1_request::serializer::Serializer
111-
});
112-
let (fn_generics, _, _) = fn_generics.split_for_impl();
113-
114-
let body = MethodBody::new(&fields, &dummy);
103+
let body = MethodBody::new(&fields);
115104

116-
quote! {
105+
quote_spanned! {Span::mixed_site()=>
117106
const _: () = {
118107
extern crate #krate as _oauth1_request;
119108

120-
#[allow(nonstandard_style)]
121-
fn #dummy #fn_generics(mut #dummy: (&#name #ty_generics, #dummy)) -> #dummy::Output
122-
#where_clause
123-
{
124-
#body
125-
}
126-
127109
impl #impl_generics _oauth1_request::Request for #name #ty_generics
128110
#where_clause
129111
{
130-
// We do not want to mess with the signature which appears in the docs
131-
// and do not want to expose the `serializer` and `S` to the macro caller,
132-
// so we are separating the implementation to another function.
133-
fn serialize<S>(&self, serializer: S) -> S::Output
112+
// `_S`'s span resolves at call site so prefix it with underscore to avoid conflict.
113+
// TODO: Use def-site hygiene once it stabilizes.
114+
fn serialize<_S>(&self, mut serializer: _S) -> _S::Output
134115
where
135-
S: _oauth1_request::serializer::Serializer,
116+
_S: _oauth1_request::serializer::Serializer,
136117
{
137-
#dummy((self, serializer))
118+
#body
138119
}
139120
}
140121
};

oauth1-request-derive/src/method_body.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use proc_macro2::TokenStream;
1+
use proc_macro2::{Span, TokenStream};
22
use quote::{quote, quote_spanned, ToTokens};
33
use syn::spanned::Spanned;
44
use syn::{Ident, PathArguments, Type};
@@ -8,19 +8,18 @@ use crate::util::OAuthParameter;
88

99
pub struct MethodBody<'a> {
1010
fields: &'a [Field],
11-
dummy: &'a Ident,
1211
}
1312

1413
impl<'a> MethodBody<'a> {
15-
pub fn new(fields: &'a [Field], dummy: &'a Ident) -> Self {
16-
MethodBody { fields, dummy }
14+
pub fn new(fields: &'a [Field]) -> Self {
15+
MethodBody { fields }
1716
}
1817
}
1918

2019
impl<'a> ToTokens for MethodBody<'a> {
2120
fn to_tokens(&self, tokens: &mut TokenStream) {
22-
let dummy = &self.dummy;
23-
let (this, ser) = (quote! { #dummy.0 }, quote! { #dummy.1 });
21+
let this = Ident::new("self", Span::mixed_site());
22+
let ser = Ident::new("serializer", Span::mixed_site());
2423

2524
let mut next_param = OAuthParameter::default();
2625
for f in self.fields {

oauth1-request-derive/tests/pass.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,16 @@ assert_expand! {
305305
}
306306
}
307307

308+
// Just checking that this compiles.
309+
#[derive(oauth::Request)]
310+
struct Hygiene {
311+
#[oauth1(fmt = serializer)]
312+
a: (),
313+
}
314+
fn serializer(_: &(), _: &mut Formatter<'_>) -> fmt::Result {
315+
unimplemented!();
316+
}
317+
308318
// Just checking that this produces no warnings.
309319
#[derive(oauth::Request)]
310320
#[allow(nonstandard_style)]

0 commit comments

Comments
 (0)