about summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>2019-06-05 13:54:54 +0300
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>2019-06-06 14:04:02 +0300
commit738e14565deb48800c06abc22f8e35e412f10010 (patch)
tree6b9a2aedad6b40438eb53271705779a7075627af /src/libsyntax
parent67ce3f458939e6fe073bca6128526cb23f0797ba (diff)
downloadrust-738e14565deb48800c06abc22f8e35e412f10010.tar.gz
rust-738e14565deb48800c06abc22f8e35e412f10010.zip
syntax: Use `Token` in visitors and fix a mut visitor test
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/mut_visit.rs24
-rw-r--r--src/libsyntax/visit.rs6
2 files changed, 19 insertions, 11 deletions
diff --git a/src/libsyntax/mut_visit.rs b/src/libsyntax/mut_visit.rs
index 7eb88de2281..4d7f0a97b0f 100644
--- a/src/libsyntax/mut_visit.rs
+++ b/src/libsyntax/mut_visit.rs
@@ -9,7 +9,7 @@
 
 use crate::ast::*;
 use crate::source_map::{Spanned, respan};
-use crate::parse::token::{self, Token, TokenKind};
+use crate::parse::token::{self, Token};
 use crate::ptr::P;
 use crate::ThinVec;
 use crate::tokenstream::*;
@@ -262,7 +262,7 @@ pub trait MutVisitor: Sized {
         noop_visit_tts(tts, self);
     }
 
-    fn visit_token(&mut self, t: &mut TokenKind) {
+    fn visit_token(&mut self, t: &mut Token) {
         noop_visit_token(t, self);
     }
 
@@ -576,9 +576,8 @@ pub fn noop_visit_arg<T: MutVisitor>(Arg { id, pat, ty }: &mut Arg, vis: &mut T)
 
 pub fn noop_visit_tt<T: MutVisitor>(tt: &mut TokenTree, vis: &mut T) {
     match tt {
-        TokenTree::Token(Token { kind, span }) => {
-            vis.visit_token(kind);
-            vis.visit_span(span);
+        TokenTree::Token(token) => {
+            vis.visit_token(token);
         }
         TokenTree::Delimited(DelimSpan { open, close }, _delim, tts) => {
             vis.visit_span(open);
@@ -595,15 +594,24 @@ pub fn noop_visit_tts<T: MutVisitor>(TokenStream(tts): &mut TokenStream, vis: &m
     })
 }
 
-// apply ident visitor if it's an ident, apply other visits to interpolated nodes
-pub fn noop_visit_token<T: MutVisitor>(t: &mut TokenKind, vis: &mut T) {
-    match t {
+// Apply ident visitor if it's an ident, apply other visits to interpolated nodes.
+// In practice the ident part is not actually used by specific visitors right now,
+// but there's a test below checking that it works.
+pub fn noop_visit_token<T: MutVisitor>(t: &mut Token, vis: &mut T) {
+    let Token { kind, span } = t;
+    match kind {
+        token::Ident(name, _) | token::Lifetime(name) => {
+            let mut ident = Ident::new(*name, *span);
+            vis.visit_ident(&mut ident);
+            *name = ident.name;
+        }
         token::Interpolated(nt) => {
             let mut nt = Lrc::make_mut(nt);
             vis.visit_interpolated(&mut nt);
         }
         _ => {}
     }
+    vis.visit_span(span);
 }
 
 /// Apply visitor to elements of interpolated nodes.
diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs
index e32c5f3f3ec..4e6a8274a47 100644
--- a/src/libsyntax/visit.rs
+++ b/src/libsyntax/visit.rs
@@ -14,7 +14,7 @@
 //! those that are created by the expansion of a macro.
 
 use crate::ast::*;
-use crate::parse::token::TokenKind;
+use crate::parse::token::Token;
 use crate::tokenstream::{TokenTree, TokenStream};
 
 use syntax_pos::Span;
@@ -151,7 +151,7 @@ pub trait Visitor<'ast>: Sized {
     fn visit_tts(&mut self, tts: TokenStream) {
         walk_tts(self, tts)
     }
-    fn visit_token(&mut self, _t: TokenKind) {}
+    fn visit_token(&mut self, _t: Token) {}
     // FIXME: add `visit_interpolated` and `walk_interpolated`
     fn visit_vis(&mut self, vis: &'ast Visibility) {
         walk_vis(self, vis)
@@ -855,7 +855,7 @@ pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute)
 
 pub fn walk_tt<'a, V: Visitor<'a>>(visitor: &mut V, tt: TokenTree) {
     match tt {
-        TokenTree::Token(token) => visitor.visit_token(token.kind),
+        TokenTree::Token(token) => visitor.visit_token(token),
         TokenTree::Delimited(_, _, tts) => visitor.visit_tts(tts),
     }
 }