Skip to content

Commit 8dcbc53

Browse files
authored
Merge pull request #124 from taiki-e/self
Fix HasSelf visitor to visit Self keyword inside macro invocations
2 parents 8288d9d + 52b7f31 commit 8dcbc53

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/receiver.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ pub fn has_self_in_block(block: &mut Block) -> bool {
2929
visitor.0
3030
}
3131

32+
fn has_self_in_token_stream(tokens: TokenStream) -> bool {
33+
tokens.into_iter().any(|tt| match tt {
34+
TokenTree::Ident(ident) => ident == "Self",
35+
TokenTree::Group(group) => has_self_in_token_stream(group.stream()),
36+
_ => false,
37+
})
38+
}
39+
3240
struct HasSelf(bool);
3341

3442
impl VisitMut for HasSelf {
@@ -54,6 +62,12 @@ impl VisitMut for HasSelf {
5462
fn visit_item_mut(&mut self, _: &mut Item) {
5563
// Do not recurse into nested items.
5664
}
65+
66+
fn visit_macro_mut(&mut self, mac: &mut Macro) {
67+
if !contains_fn(mac.tokens.clone()) {
68+
self.0 |= has_self_in_token_stream(mac.tokens.clone());
69+
}
70+
}
5771
}
5872

5973
pub struct ReplaceReceiver {
@@ -278,14 +292,14 @@ impl VisitMut for ReplaceReceiver {
278292
}
279293
}
280294

281-
fn visit_macro_mut(&mut self, i: &mut Macro) {
295+
fn visit_macro_mut(&mut self, mac: &mut Macro) {
282296
// We can't tell in general whether `self` inside a macro invocation
283297
// refers to the self in the argument list or a different self
284298
// introduced within the macro. Heuristic: if the macro input contains
285299
// `fn`, then `self` is more likely to refer to something other than the
286300
// outer function's self argument.
287-
if !contains_fn(i.tokens.clone()) {
288-
self.visit_token_stream(&mut i.tokens);
301+
if !contains_fn(mac.tokens.clone()) {
302+
self.visit_token_stream(&mut mac.tokens);
289303
}
290304
}
291305
}

tests/test.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,35 @@ pub mod issue92 {
883883
}
884884
}
885885

886+
// https://github.com/dtolnay/async-trait/issues/92#issuecomment-683370136
887+
pub mod issue92_2 {
888+
use async_trait::async_trait;
889+
890+
macro_rules! mac {
891+
($($tt:tt)*) => {
892+
$($tt)*
893+
};
894+
}
895+
896+
pub trait Trait1 {
897+
fn func1();
898+
}
899+
900+
#[async_trait]
901+
pub trait Trait2: Trait1 {
902+
async fn func2() {
903+
mac!(Self::func1());
904+
905+
macro_rules! mac2 {
906+
($($tt:tt)*) => {
907+
Self::func1();
908+
};
909+
}
910+
mac2!();
911+
}
912+
}
913+
}
914+
886915
// https://github.com/dtolnay/async-trait/issues/104
887916
pub mod issue104 {
888917
use async_trait::async_trait;

0 commit comments

Comments
 (0)