Skip to content

Commit

Permalink
Preserve self as segment of path
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Apr 14, 2021
1 parent bb62922 commit 6029cbf
Showing 1 changed file with 11 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

0 comments on commit 6029cbf

Please sign in to comment.