Skip to content

Commit c0ccab4

Browse files
committed
Fix #44851 by visiting tokens in DefCollector and BuildReducedGraphVisitor
1 parent 1042190 commit c0ccab4

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

src/librustc/hir/map/def_collector.rs

+14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use syntax::ext::hygiene::Mark;
1616
use syntax::visit;
1717
use syntax::symbol::keywords;
1818
use syntax::symbol::Symbol;
19+
use syntax::parse::token::{self, Token};
1920

2021
use hir::map::{ITEM_LIKE_SPACE, REGULAR_SPACE};
2122

@@ -283,4 +284,17 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
283284
_ => visit::walk_stmt(self, stmt),
284285
}
285286
}
287+
288+
fn visit_token(&mut self, t: Token) {
289+
if let Token::Interpolated(nt) = t {
290+
match nt.0 {
291+
token::NtExpr(ref expr) => {
292+
if let ExprKind::Mac(..) = expr.node {
293+
self.visit_macro_invoc(expr.id, false);
294+
}
295+
}
296+
_ => {}
297+
}
298+
}
299+
}
286300
}

src/librustc_resolve/build_reduced_graph.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use syntax::ext::base::SyntaxExtension;
4040
use syntax::ext::base::Determinacy::Undetermined;
4141
use syntax::ext::hygiene::Mark;
4242
use syntax::ext::tt::macro_rules;
43-
use syntax::parse::token;
43+
use syntax::parse::token::{self, Token};
4444
use syntax::symbol::keywords;
4545
use syntax::symbol::Symbol;
4646
use syntax::visit::{self, Visitor};
@@ -830,4 +830,17 @@ impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> {
830830
visit::walk_trait_item(self, item);
831831
self.resolver.current_module = parent;
832832
}
833+
834+
fn visit_token(&mut self, t: Token) {
835+
if let Token::Interpolated(nt) = t {
836+
match nt.0 {
837+
token::NtExpr(ref expr) => {
838+
if let ast::ExprKind::Mac(..) = expr.node {
839+
self.visit_invoc(expr.id);
840+
}
841+
}
842+
_ => {}
843+
}
844+
}
845+
}
833846
}

src/libsyntax/visit.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ use abi::Abi;
2727
use ast::*;
2828
use syntax_pos::Span;
2929
use codemap::Spanned;
30+
use parse::token::Token;
31+
use tokenstream::{TokenTree, TokenStream};
3032

3133
#[derive(Copy, Clone, PartialEq, Eq)]
3234
pub enum FnKind<'a> {
@@ -130,7 +132,16 @@ pub trait Visitor<'ast>: Sized {
130132
fn visit_assoc_type_binding(&mut self, type_binding: &'ast TypeBinding) {
131133
walk_assoc_type_binding(self, type_binding)
132134
}
133-
fn visit_attribute(&mut self, _attr: &'ast Attribute) {}
135+
fn visit_attribute(&mut self, attr: &'ast Attribute) {
136+
walk_attribute(self, attr)
137+
}
138+
fn visit_tt(&mut self, tt: TokenTree) {
139+
walk_tt(self, tt)
140+
}
141+
fn visit_tts(&mut self, tts: TokenStream) {
142+
walk_tts(self, tts)
143+
}
144+
fn visit_token(&mut self, _t: Token) {}
134145
fn visit_vis(&mut self, vis: &'ast Visibility) {
135146
walk_vis(self, vis)
136147
}
@@ -810,3 +821,20 @@ pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) {
810821
visitor.visit_path(path, id);
811822
}
812823
}
824+
825+
pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute) {
826+
visitor.visit_tts(attr.tokens.clone());
827+
}
828+
829+
pub fn walk_tt<'a, V: Visitor<'a>>(visitor: &mut V, tt: TokenTree) {
830+
match tt {
831+
TokenTree::Token(_, tok) => visitor.visit_token(tok),
832+
TokenTree::Delimited(_, delimed) => visitor.visit_tts(delimed.stream()),
833+
}
834+
}
835+
836+
pub fn walk_tts<'a, V: Visitor<'a>>(visitor: &mut V, tts: TokenStream) {
837+
for tt in tts.trees() {
838+
visitor.visit_tt(tt);
839+
}
840+
}

src/test/run-pass/issue-44851.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
macro_rules! a {
12+
() => { "a" }
13+
}
14+
15+
macro_rules! b {
16+
($doc:expr) => {
17+
#[doc = $doc]
18+
pub struct B;
19+
}
20+
}
21+
22+
b!(a!());
23+
24+
fn main() {}

0 commit comments

Comments
 (0)