Skip to content

Commit 738e145

Browse files
committed
syntax: Use Token in visitors and fix a mut visitor test
1 parent 67ce3f4 commit 738e145

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

src/librustc/hir/map/def_collector.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use syntax::ast::*;
66
use syntax::ext::hygiene::Mark;
77
use syntax::visit;
88
use syntax::symbol::{kw, sym};
9-
use syntax::parse::token::{self, TokenKind};
9+
use syntax::parse::token::{self, Token};
1010
use syntax_pos::Span;
1111

1212
/// Creates `DefId`s for nodes in the AST.
@@ -325,8 +325,8 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
325325
}
326326
}
327327

328-
fn visit_token(&mut self, t: TokenKind) {
329-
if let token::Interpolated(nt) = t {
328+
fn visit_token(&mut self, t: Token) {
329+
if let token::Interpolated(nt) = t.kind {
330330
if let token::NtExpr(ref expr) = *nt {
331331
if let ExprKind::Mac(..) = expr.node {
332332
self.visit_macro_invoc(expr.id);

src/librustc_resolve/build_reduced_graph.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use syntax::ext::base::Determinacy::Undetermined;
3434
use syntax::ext::hygiene::Mark;
3535
use syntax::ext::tt::macro_rules;
3636
use syntax::feature_gate::is_builtin_attr;
37-
use syntax::parse::token::{self, TokenKind};
37+
use syntax::parse::token::{self, Token};
3838
use syntax::span_err;
3939
use syntax::std_inject::injected_crate_name;
4040
use syntax::symbol::{kw, sym};
@@ -1052,8 +1052,8 @@ impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> {
10521052
self.resolver.current_module = parent;
10531053
}
10541054

1055-
fn visit_token(&mut self, t: TokenKind) {
1056-
if let token::Interpolated(nt) = t {
1055+
fn visit_token(&mut self, t: Token) {
1056+
if let token::Interpolated(nt) = t.kind {
10571057
if let token::NtExpr(ref expr) = *nt {
10581058
if let ast::ExprKind::Mac(..) = expr.node {
10591059
self.visit_invoc(expr.id);

src/libsyntax/mut_visit.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
1010
use crate::ast::*;
1111
use crate::source_map::{Spanned, respan};
12-
use crate::parse::token::{self, Token, TokenKind};
12+
use crate::parse::token::{self, Token};
1313
use crate::ptr::P;
1414
use crate::ThinVec;
1515
use crate::tokenstream::*;
@@ -262,7 +262,7 @@ pub trait MutVisitor: Sized {
262262
noop_visit_tts(tts, self);
263263
}
264264

265-
fn visit_token(&mut self, t: &mut TokenKind) {
265+
fn visit_token(&mut self, t: &mut Token) {
266266
noop_visit_token(t, self);
267267
}
268268

@@ -576,9 +576,8 @@ pub fn noop_visit_arg<T: MutVisitor>(Arg { id, pat, ty }: &mut Arg, vis: &mut T)
576576

577577
pub fn noop_visit_tt<T: MutVisitor>(tt: &mut TokenTree, vis: &mut T) {
578578
match tt {
579-
TokenTree::Token(Token { kind, span }) => {
580-
vis.visit_token(kind);
581-
vis.visit_span(span);
579+
TokenTree::Token(token) => {
580+
vis.visit_token(token);
582581
}
583582
TokenTree::Delimited(DelimSpan { open, close }, _delim, tts) => {
584583
vis.visit_span(open);
@@ -595,15 +594,24 @@ pub fn noop_visit_tts<T: MutVisitor>(TokenStream(tts): &mut TokenStream, vis: &m
595594
})
596595
}
597596

598-
// apply ident visitor if it's an ident, apply other visits to interpolated nodes
599-
pub fn noop_visit_token<T: MutVisitor>(t: &mut TokenKind, vis: &mut T) {
600-
match t {
597+
// Apply ident visitor if it's an ident, apply other visits to interpolated nodes.
598+
// In practice the ident part is not actually used by specific visitors right now,
599+
// but there's a test below checking that it works.
600+
pub fn noop_visit_token<T: MutVisitor>(t: &mut Token, vis: &mut T) {
601+
let Token { kind, span } = t;
602+
match kind {
603+
token::Ident(name, _) | token::Lifetime(name) => {
604+
let mut ident = Ident::new(*name, *span);
605+
vis.visit_ident(&mut ident);
606+
*name = ident.name;
607+
}
601608
token::Interpolated(nt) => {
602609
let mut nt = Lrc::make_mut(nt);
603610
vis.visit_interpolated(&mut nt);
604611
}
605612
_ => {}
606613
}
614+
vis.visit_span(span);
607615
}
608616

609617
/// Apply visitor to elements of interpolated nodes.

src/libsyntax/visit.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
//! those that are created by the expansion of a macro.
1515
1616
use crate::ast::*;
17-
use crate::parse::token::TokenKind;
17+
use crate::parse::token::Token;
1818
use crate::tokenstream::{TokenTree, TokenStream};
1919

2020
use syntax_pos::Span;
@@ -151,7 +151,7 @@ pub trait Visitor<'ast>: Sized {
151151
fn visit_tts(&mut self, tts: TokenStream) {
152152
walk_tts(self, tts)
153153
}
154-
fn visit_token(&mut self, _t: TokenKind) {}
154+
fn visit_token(&mut self, _t: Token) {}
155155
// FIXME: add `visit_interpolated` and `walk_interpolated`
156156
fn visit_vis(&mut self, vis: &'ast Visibility) {
157157
walk_vis(self, vis)
@@ -855,7 +855,7 @@ pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute)
855855

856856
pub fn walk_tt<'a, V: Visitor<'a>>(visitor: &mut V, tt: TokenTree) {
857857
match tt {
858-
TokenTree::Token(token) => visitor.visit_token(token.kind),
858+
TokenTree::Token(token) => visitor.visit_token(token),
859859
TokenTree::Delimited(_, _, tts) => visitor.visit_tts(tts),
860860
}
861861
}

0 commit comments

Comments
 (0)