Skip to content

Commit bfe44b3

Browse files
committed
Preserve span of _self tokens
1 parent 6592c88 commit bfe44b3

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

src/expand.rs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use std::mem;
77
use syn::punctuated::Punctuated;
88
use syn::visit_mut::VisitMut;
99
use syn::{
10-
parse_quote, ArgCaptured, ArgSelfRef, Block, FnArg, GenericParam, Generics, Ident, ImplItem,
11-
Lifetime, MethodSig, Pat, PatIdent, Path, ReturnType, Token, TraitItem, Type, TypeParam,
12-
TypeParamBound, WhereClause,
10+
parse_quote, ArgCaptured, ArgSelf, ArgSelfRef, Block, FnArg, GenericParam, Generics, Ident,
11+
ImplItem, Lifetime, MethodSig, Pat, PatIdent, Path, ReturnType, Token, TraitItem, Type,
12+
TypeParam, TypeParamBound, WhereClause,
1313
};
1414

1515
impl ToTokens for Item {
@@ -256,44 +256,53 @@ fn transform_block(context: Context, sig: &mut MethodSig, block: &mut Block, has
256256
let mut self_bound = None::<TypeParamBound>;
257257
match standalone.decl.inputs.iter_mut().next() {
258258
Some(arg @ FnArg::SelfRef(_)) => {
259-
let (lifetime, mutability) = match arg {
259+
let (lifetime, mutability, self_token) = match arg {
260260
FnArg::SelfRef(ArgSelfRef {
261261
lifetime,
262262
mutability,
263+
self_token,
263264
..
264-
}) => (lifetime, mutability),
265+
}) => (lifetime, mutability, self_token),
265266
_ => unreachable!(),
266267
};
268+
let under_self = Ident::new("_self", self_token.span);
267269
match context {
268270
Context::Trait { .. } => {
269271
self_bound = Some(match mutability {
270272
Some(_) => parse_quote!(core::marker::Send),
271273
None => parse_quote!(core::marker::Sync),
272274
});
273275
*arg = parse_quote! {
274-
_self: &#lifetime #mutability AsyncTrait
276+
#under_self: &#lifetime #mutability AsyncTrait
275277
};
276278
}
277279
Context::Impl { receiver, .. } => {
278280
*arg = parse_quote! {
279-
_self: &#lifetime #mutability #receiver
281+
#under_self: &#lifetime #mutability #receiver
280282
};
281283
}
282284
}
283285
}
284-
Some(arg @ FnArg::SelfValue(_)) => match context {
285-
Context::Trait { .. } => {
286-
self_bound = Some(parse_quote!(core::marker::Send));
287-
*arg = parse_quote! {
288-
_self: AsyncTrait
289-
};
290-
}
291-
Context::Impl { receiver, .. } => {
292-
*arg = parse_quote! {
293-
_self: #receiver
294-
};
286+
Some(arg @ FnArg::SelfValue(_)) => {
287+
let self_token = match arg {
288+
FnArg::SelfValue(ArgSelf { self_token, .. }) => self_token,
289+
_ => unreachable!(),
290+
};
291+
let under_self = Ident::new("_self", self_token.span);
292+
match context {
293+
Context::Trait { .. } => {
294+
self_bound = Some(parse_quote!(core::marker::Send));
295+
*arg = parse_quote! {
296+
#under_self: AsyncTrait
297+
};
298+
}
299+
Context::Impl { receiver, .. } => {
300+
*arg = parse_quote! {
301+
#under_self: #receiver
302+
};
303+
}
295304
}
296-
},
305+
}
297306
Some(FnArg::Captured(ArgCaptured {
298307
pat: Pat::Ident(arg),
299308
..

src/receiver.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::mem;
22
use syn::punctuated::Punctuated;
33
use syn::visit_mut::{self, VisitMut};
44
use syn::{
5-
parse_quote, ArgSelf, ArgSelfRef, Block, ExprPath, Item, MethodSig, Path, QSelf, Type, TypePath,
5+
ArgSelf, ArgSelfRef, Block, ExprPath, Ident, Item, MethodSig, Path, QSelf, Type, TypePath,
66
};
77

88
pub fn has_self_in_sig(sig: &mut MethodSig) -> bool {
@@ -127,7 +127,8 @@ impl VisitMut for ReplaceReceiver {
127127
fn visit_expr_path_mut(&mut self, expr: &mut ExprPath) {
128128
if expr.qself.is_none() {
129129
if expr.path.is_ident("self") {
130-
expr.path.segments[0].ident = parse_quote!(_self);
130+
let ident = &mut expr.path.segments[0].ident;
131+
*ident = Ident::new("_self", ident.span());
131132
}
132133
self.self_to_qself_expr(&mut expr.qself, &mut expr.path);
133134
}

0 commit comments

Comments
 (0)