Skip to content

Commit 26373fb

Browse files
committed
Auto merge of #77275 - petrochenkov:interpid, r=varkor
expand: Stop normalizing `NtIdent`s before passing them to built-in macros Built-in macros should be able to deal with `NtIdents` in the input by themselves like any other parser code. You can't imagine how bad mutable AST visitors are, *especially* if they are modifying tokens. This is one step towards removing token visiting from the visitor infrastructure (#77271 also works in this direction.)
2 parents 48cab67 + 4a4a7f8 commit 26373fb

File tree

3 files changed

+13
-36
lines changed

3 files changed

+13
-36
lines changed

compiler/rustc_builtin_macros/src/asm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ fn parse_reg<'a>(
368368
explicit_reg: &mut bool,
369369
) -> Result<ast::InlineAsmRegOrRegClass, DiagnosticBuilder<'a>> {
370370
p.expect(&token::OpenDelim(token::DelimToken::Paren))?;
371-
let result = match p.token.kind {
371+
let result = match p.token.uninterpolate().kind {
372372
token::Ident(name, false) => ast::InlineAsmRegOrRegClass::RegClass(name),
373373
token::Literal(token::Lit { kind: token::LitKind::Str, symbol, suffix: _ }) => {
374374
*explicit_reg = true;

compiler/rustc_builtin_macros/src/concat_idents.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ pub fn expand_concat_idents<'cx>(
2727
}
2828
}
2929
} else {
30-
match e {
31-
TokenTree::Token(Token { kind: token::Ident(name, _), .. }) => {
32-
res_str.push_str(&name.as_str())
33-
}
34-
_ => {
35-
cx.span_err(sp, "concat_idents! requires ident args.");
36-
return DummyResult::any(sp);
30+
if let TokenTree::Token(token) = e {
31+
if let Some((ident, _)) = token.ident() {
32+
res_str.push_str(&ident.name.as_str());
33+
continue;
3734
}
3835
}
36+
37+
cx.span_err(sp, "concat_idents! requires ident args.");
38+
return DummyResult::any(sp);
3939
}
4040
}
4141

compiler/rustc_expand/src/base.rs

+5-28
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use crate::expand::{self, AstFragment, Invocation};
22
use crate::module::DirectoryOwnership;
33

4-
use rustc_ast::mut_visit::{self, MutVisitor};
54
use rustc_ast::ptr::P;
65
use rustc_ast::token;
7-
use rustc_ast::tokenstream::{self, TokenStream};
6+
use rustc_ast::tokenstream::TokenStream;
87
use rustc_ast::visit::{AssocCtxt, Visitor};
98
use rustc_ast::{self as ast, Attribute, NodeId, PatKind};
109
use rustc_attr::{self as attr, Deprecation, HasAttrs, Stability};
@@ -313,7 +312,7 @@ where
313312
ts: TokenStream,
314313
) -> Result<TokenStream, ErrorReported> {
315314
// FIXME setup implicit context in TLS before calling self.
316-
Ok((*self)(ts))
315+
Ok(self(ts))
317316
}
318317
}
319318

@@ -339,7 +338,7 @@ where
339338
annotated: TokenStream,
340339
) -> Result<TokenStream, ErrorReported> {
341340
// FIXME setup implicit context in TLS before calling self.
342-
Ok((*self)(annotation, annotated))
341+
Ok(self(annotation, annotated))
343342
}
344343
}
345344

@@ -364,31 +363,9 @@ where
364363
&self,
365364
ecx: &'cx mut ExtCtxt<'_>,
366365
span: Span,
367-
mut input: TokenStream,
366+
input: TokenStream,
368367
) -> Box<dyn MacResult + 'cx> {
369-
struct AvoidInterpolatedIdents;
370-
371-
impl MutVisitor for AvoidInterpolatedIdents {
372-
fn visit_tt(&mut self, tt: &mut tokenstream::TokenTree) {
373-
if let tokenstream::TokenTree::Token(token) = tt {
374-
if let token::Interpolated(nt) = &token.kind {
375-
if let token::NtIdent(ident, is_raw) = **nt {
376-
*tt = tokenstream::TokenTree::token(
377-
token::Ident(ident.name, is_raw),
378-
ident.span,
379-
);
380-
}
381-
}
382-
}
383-
mut_visit::noop_visit_tt(tt, self)
384-
}
385-
386-
fn visit_mac(&mut self, mac: &mut ast::MacCall) {
387-
mut_visit::noop_visit_mac(mac, self)
388-
}
389-
}
390-
AvoidInterpolatedIdents.visit_tts(&mut input);
391-
(*self)(ecx, span, input)
368+
self(ecx, span, input)
392369
}
393370
}
394371

0 commit comments

Comments
 (0)