Skip to content

Commit 797b6e8

Browse files
authored
Merge pull request #160 from dtolnay/self
Preserve `self` as segment of path
2 parents 6bff4e0 + 6029cbf commit 797b6e8

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/receiver.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use proc_macro2::{Group, Span, TokenStream, TokenTree};
22
use std::iter::FromIterator;
33
use syn::visit_mut::{self, VisitMut};
44
use syn::{
5-
Block, ExprPath, Ident, Item, Macro, Pat, PatIdent, PatPath, Receiver, Signature, Token,
5+
Block, ExprPath, Ident, Item, Macro, Pat, PatIdent, PatPath, Path, Receiver, Signature, Token,
66
TypePath,
77
};
88

@@ -139,6 +139,16 @@ impl VisitMut for ReplaceSelf {
139139
self.prepend_underscore_to_self(i);
140140
}
141141

142+
fn visit_path_mut(&mut self, p: &mut Path) {
143+
if p.segments.len() == 1 {
144+
// Replace `self`, but not `self::function`.
145+
self.visit_ident_mut(&mut p.segments[0].ident);
146+
}
147+
for segment in &mut p.segments {
148+
self.visit_path_arguments_mut(&mut segment.arguments);
149+
}
150+
}
151+
142152
fn visit_item_mut(&mut self, i: &mut Item) {
143153
// Visit `macro_rules!` because locally defined macros can refer to
144154
// `self`. Otherwise, do not recurse into nested items.

tests/test.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,3 +1321,17 @@ pub mod issue154 {
13211321
}
13221322
}
13231323
}
1324+
1325+
// https://github.com/dtolnay/async-trait/issues/158
1326+
pub mod issue158 {
1327+
use async_trait::async_trait;
1328+
1329+
fn f() {}
1330+
1331+
#[async_trait]
1332+
pub trait Trait {
1333+
async fn f(&self) {
1334+
self::f()
1335+
}
1336+
}
1337+
}

0 commit comments

Comments
 (0)