Skip to content

Commit

Permalink
Tweak some spans to try for meaningful click-through on method names
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 19, 2022
1 parent 3b9e32e commit d3c0a42
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,14 @@ fn transform_sig(
has_default: bool,
is_local: bool,
) {
sig.fn_token.span = sig.asyncness.take().unwrap().span;
let default_span = sig.asyncness.take().unwrap().span;
sig.fn_token.span = default_span;

let ret = match &sig.output {
ReturnType::Default => quote!(()),
ReturnType::Type(_, ret) => quote!(#ret),
let (ret_arrow, ret) = match &sig.output {
ReturnType::Default => (Token![->](default_span), quote_spanned!(default_span=> ())),
ReturnType::Type(arrow, ret) => (*arrow, quote!(#ret)),
};

let default_span = sig
.ident
.span()
.join(sig.paren_token.span)
.unwrap_or_else(|| sig.ident.span());

let mut lifetimes = CollectLifetimes::new("'life", default_span);
for arg in sig.inputs.iter_mut() {
match arg {
Expand Down Expand Up @@ -235,13 +230,12 @@ fn transform_sig(
.push(parse_quote_spanned!(default_span=> 'async_trait));

if has_self {
let bound_span = sig.ident.span();
let bound = match sig.inputs.iter().next() {
Some(FnArg::Receiver(Receiver {
reference: Some(_),
mutability: None,
..
})) => Ident::new("Sync", bound_span),
})) => Ident::new("Sync", default_span),
Some(FnArg::Typed(arg))
if match (arg.pat.as_ref(), arg.ty.as_ref()) {
(Pat::Ident(pat), Type::Reference(ty)) => {
Expand All @@ -250,9 +244,9 @@ fn transform_sig(
_ => false,
} =>
{
Ident::new("Sync", bound_span)
Ident::new("Sync", default_span)
}
_ => Ident::new("Send", bound_span),
_ => Ident::new("Send", default_span),
};

let assume_bound = match context {
Expand All @@ -262,9 +256,9 @@ fn transform_sig(

let where_clause = where_clause_or_default(&mut sig.generics.where_clause);
where_clause.predicates.push(if assume_bound || is_local {
parse_quote_spanned!(bound_span=> Self: 'async_trait)
parse_quote_spanned!(default_span=> Self: 'async_trait)
} else {
parse_quote_spanned!(bound_span=> Self: ::core::marker::#bound + 'async_trait)
parse_quote_spanned!(default_span=> Self: ::core::marker::#bound + 'async_trait)
});
}

Expand All @@ -288,14 +282,13 @@ fn transform_sig(
}
}

let ret_span = sig.ident.span();
let bounds = if is_local {
quote_spanned!(ret_span=> 'async_trait)
quote_spanned!(default_span=> 'async_trait)
} else {
quote_spanned!(ret_span=> ::core::marker::Send + 'async_trait)
quote_spanned!(default_span=> ::core::marker::Send + 'async_trait)
};
sig.output = parse_quote_spanned! {ret_span=>
-> ::core::pin::Pin<Box<
sig.output = parse_quote_spanned! {default_span=>
#ret_arrow ::core::pin::Pin<Box<
dyn ::core::future::Future<Output = #ret> + #bounds
>>
};
Expand Down

0 comments on commit d3c0a42

Please sign in to comment.