Skip to content

Commit

Permalink
Merge pull request #160 from dtolnay/self
Browse files Browse the repository at this point in the history
Preserve `self` as segment of path
  • Loading branch information
dtolnay authored Apr 14, 2021
2 parents 6bff4e0 + 6029cbf commit 797b6e8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use proc_macro2::{Group, Span, TokenStream, TokenTree};
use std::iter::FromIterator;
use syn::visit_mut::{self, VisitMut};
use syn::{
Block, ExprPath, Ident, Item, Macro, Pat, PatIdent, PatPath, Receiver, Signature, Token,
Block, ExprPath, Ident, Item, Macro, Pat, PatIdent, PatPath, Path, Receiver, Signature, Token,
TypePath,
};

Expand Down Expand Up @@ -139,6 +139,16 @@ impl VisitMut for ReplaceSelf {
self.prepend_underscore_to_self(i);
}

fn visit_path_mut(&mut self, p: &mut Path) {
if p.segments.len() == 1 {
// Replace `self`, but not `self::function`.
self.visit_ident_mut(&mut p.segments[0].ident);
}
for segment in &mut p.segments {
self.visit_path_arguments_mut(&mut segment.arguments);
}
}

fn visit_item_mut(&mut self, i: &mut Item) {
// Visit `macro_rules!` because locally defined macros can refer to
// `self`. Otherwise, do not recurse into nested items.
Expand Down
14 changes: 14 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1321,3 +1321,17 @@ pub mod issue154 {
}
}
}

// https://github.com/dtolnay/async-trait/issues/158
pub mod issue158 {
use async_trait::async_trait;

fn f() {}

#[async_trait]
pub trait Trait {
async fn f(&self) {
self::f()
}
}
}

0 comments on commit 797b6e8

Please sign in to comment.