From 2532a7e63024a773e832787964922800df13fbdb Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sun, 27 Sep 2020 19:47:52 +0300 Subject: [PATCH 1/2] expand: Stop un-interpolating `NtIdent`s before passing them to built-in macros This was a big hack, and built-in macros should be able to deal with `NtIdents` in the input by themselves like any other parser code. --- .../rustc_builtin_macros/src/concat_idents.rs | 14 +++++----- compiler/rustc_expand/src/base.rs | 27 ++----------------- 2 files changed, 9 insertions(+), 32 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/concat_idents.rs b/compiler/rustc_builtin_macros/src/concat_idents.rs index c4d1c6eee31c4..209158ce39206 100644 --- a/compiler/rustc_builtin_macros/src/concat_idents.rs +++ b/compiler/rustc_builtin_macros/src/concat_idents.rs @@ -27,15 +27,15 @@ pub fn expand_concat_idents<'cx>( } } } else { - match e { - TokenTree::Token(Token { kind: token::Ident(name, _), .. }) => { - res_str.push_str(&name.as_str()) - } - _ => { - cx.span_err(sp, "concat_idents! requires ident args."); - return DummyResult::any(sp); + if let TokenTree::Token(token) = e { + if let Some((ident, _)) = token.ident() { + res_str.push_str(&ident.name.as_str()); + continue; } } + + cx.span_err(sp, "concat_idents! requires ident args."); + return DummyResult::any(sp); } } diff --git a/compiler/rustc_expand/src/base.rs b/compiler/rustc_expand/src/base.rs index 926e3dbfc5266..1ae2be47b140a 100644 --- a/compiler/rustc_expand/src/base.rs +++ b/compiler/rustc_expand/src/base.rs @@ -1,10 +1,9 @@ use crate::expand::{self, AstFragment, Invocation}; use crate::module::DirectoryOwnership; -use rustc_ast::mut_visit::{self, MutVisitor}; use rustc_ast::ptr::P; use rustc_ast::token; -use rustc_ast::tokenstream::{self, TokenStream}; +use rustc_ast::tokenstream::TokenStream; use rustc_ast::visit::{AssocCtxt, Visitor}; use rustc_ast::{self as ast, Attribute, NodeId, PatKind}; use rustc_attr::{self as attr, Deprecation, HasAttrs, Stability}; @@ -364,30 +363,8 @@ where &self, ecx: &'cx mut ExtCtxt<'_>, span: Span, - mut input: TokenStream, + input: TokenStream, ) -> Box { - struct AvoidInterpolatedIdents; - - impl MutVisitor for AvoidInterpolatedIdents { - fn visit_tt(&mut self, tt: &mut tokenstream::TokenTree) { - if let tokenstream::TokenTree::Token(token) = tt { - if let token::Interpolated(nt) = &token.kind { - if let token::NtIdent(ident, is_raw) = **nt { - *tt = tokenstream::TokenTree::token( - token::Ident(ident.name, is_raw), - ident.span, - ); - } - } - } - mut_visit::noop_visit_tt(tt, self) - } - - fn visit_mac(&mut self, mac: &mut ast::MacCall) { - mut_visit::noop_visit_mac(mac, self) - } - } - AvoidInterpolatedIdents.visit_tts(&mut input); (*self)(ecx, span, input) } } From 3b27799c0c00efdb4b94bdba76bcf96956b053ca Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sun, 27 Sep 2020 20:40:47 +0300 Subject: [PATCH 2/2] expand: Minor fn ptr call cleanup --- compiler/rustc_expand/src/base.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_expand/src/base.rs b/compiler/rustc_expand/src/base.rs index 1ae2be47b140a..f7651ca0ba6dd 100644 --- a/compiler/rustc_expand/src/base.rs +++ b/compiler/rustc_expand/src/base.rs @@ -312,7 +312,7 @@ where ts: TokenStream, ) -> Result { // FIXME setup implicit context in TLS before calling self. - Ok((*self)(ts)) + Ok(self(ts)) } } @@ -338,7 +338,7 @@ where annotated: TokenStream, ) -> Result { // FIXME setup implicit context in TLS before calling self. - Ok((*self)(annotation, annotated)) + Ok(self(annotation, annotated)) } } @@ -365,7 +365,7 @@ where span: Span, input: TokenStream, ) -> Box { - (*self)(ecx, span, input) + self(ecx, span, input) } }