Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visit attribute tokens in DefCollector and BuildReducedGraphVisitor #45464

Merged
merged 2 commits into from
Oct 26, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/librustc/hir/map/def_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use syntax::ext::hygiene::Mark;
use syntax::visit;
use syntax::symbol::keywords;
use syntax::symbol::Symbol;
use syntax::parse::token::{self, Token};

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

Expand Down Expand Up @@ -283,4 +284,17 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
_ => visit::walk_stmt(self, stmt),
}
}

fn visit_token(&mut self, t: Token) {
if let Token::Interpolated(nt) = t {
match nt.0 {
token::NtExpr(ref expr) => {
if let ExprKind::Mac(..) = expr.node {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this should just be token::Expr(ref expr) => self.visit_expr(expr),.

Ideally, we'd add visit_interpolated and walk_interpolated (a la https://github.com/sinkuu/rust/blob/c0ccab4c2330659b67edb5889d7f9ee702aebbe3/src/libsyntax/fold.rs#L618) -- could you add a FIXME for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should, but doesn't work due to lifetimes:(

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you can fix the lifetime error by chaging fn visit_token to take the token by reference instead of by value.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, nvm -- you can get a reference to the token with an appropriate lifetime from a token stream, so that won't work. I think what you have is fine for now.

Copy link
Contributor Author

@sinkuu sinkuu Oct 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(comment removed)

Nvm, I didn't reload the browser

self.visit_macro_invoc(expr.id, false);
}
}
_ => {}
}
}
}
}
15 changes: 14 additions & 1 deletion src/librustc_resolve/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use syntax::ext::base::SyntaxExtension;
use syntax::ext::base::Determinacy::Undetermined;
use syntax::ext::hygiene::Mark;
use syntax::ext::tt::macro_rules;
use syntax::parse::token;
use syntax::parse::token::{self, Token};
use syntax::symbol::keywords;
use syntax::symbol::Symbol;
use syntax::visit::{self, Visitor};
Expand Down Expand Up @@ -830,4 +830,17 @@ impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> {
visit::walk_trait_item(self, item);
self.resolver.current_module = parent;
}

fn visit_token(&mut self, t: Token) {
if let Token::Interpolated(nt) = t {
match nt.0 {
token::NtExpr(ref expr) => {
if let ast::ExprKind::Mac(..) = expr.node {
self.visit_invoc(expr.id);
}
}
_ => {}
}
}
}
}
30 changes: 29 additions & 1 deletion src/libsyntax/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ use abi::Abi;
use ast::*;
use syntax_pos::Span;
use codemap::Spanned;
use parse::token::Token;
use tokenstream::{TokenTree, TokenStream};

#[derive(Copy, Clone, PartialEq, Eq)]
pub enum FnKind<'a> {
Expand Down Expand Up @@ -130,7 +132,16 @@ pub trait Visitor<'ast>: Sized {
fn visit_assoc_type_binding(&mut self, type_binding: &'ast TypeBinding) {
walk_assoc_type_binding(self, type_binding)
}
fn visit_attribute(&mut self, _attr: &'ast Attribute) {}
fn visit_attribute(&mut self, attr: &'ast Attribute) {
walk_attribute(self, attr)
}
fn visit_tt(&mut self, tt: TokenTree) {
walk_tt(self, tt)
}
fn visit_tts(&mut self, tts: TokenStream) {
walk_tts(self, tts)
}
fn visit_token(&mut self, _t: Token) {}
fn visit_vis(&mut self, vis: &'ast Visibility) {
walk_vis(self, vis)
}
Expand Down Expand Up @@ -810,3 +821,20 @@ pub fn walk_vis<'a, V: Visitor<'a>>(visitor: &mut V, vis: &'a Visibility) {
visitor.visit_path(path, id);
}
}

pub fn walk_attribute<'a, V: Visitor<'a>>(visitor: &mut V, attr: &'a Attribute) {
visitor.visit_tts(attr.tokens.clone());
}

pub fn walk_tt<'a, V: Visitor<'a>>(visitor: &mut V, tt: TokenTree) {
match tt {
TokenTree::Token(_, tok) => visitor.visit_token(tok),
TokenTree::Delimited(_, delimed) => visitor.visit_tts(delimed.stream()),
}
}

pub fn walk_tts<'a, V: Visitor<'a>>(visitor: &mut V, tts: TokenStream) {
for tt in tts.trees() {
visitor.visit_tt(tt);
}
}
24 changes: 24 additions & 0 deletions src/test/run-pass/issue-44851.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

macro_rules! a {
() => { "a" }
}

macro_rules! b {
($doc:expr) => {
#[doc = $doc]
pub struct B;
}
}

b!(a!());

fn main() {}