Skip to content

Commit cda26a7

Browse files
petrochenkovnnethercote
authored andcommitted
builtin_macros: Strip top-level invisible delimiters in stringify!.
1 parent 4cbaac6 commit cda26a7

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

compiler/rustc_builtin_macros/src/source_util.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_ast as ast;
22
use rustc_ast::ptr::P;
3-
use rustc_ast::token;
4-
use rustc_ast::tokenstream::TokenStream;
3+
use rustc_ast::token::{self, Delimiter};
4+
use rustc_ast::tokenstream::{TokenStream, TokenTree};
55
use rustc_ast_pretty::pprust;
66
use rustc_expand::base::{self, *};
77
use rustc_expand::module::DirOwnership;
@@ -69,8 +69,17 @@ pub fn expand_file(
6969
pub fn expand_stringify(
7070
cx: &mut ExtCtxt<'_>,
7171
sp: Span,
72-
tts: TokenStream,
72+
mut tts: TokenStream,
7373
) -> Box<dyn base::MacResult + 'static> {
74+
// `stringify!` is nearly always called on `macro_rules` meta-variables and the intent is to
75+
// stringify the underlying tokens without meta-variable's own invisible delimiters, so we
76+
// are stripping such delimiters here (possibly multiple layers of them).
77+
while let [TokenTree::Delimited(_, Delimiter::Invisible, inner_tts)] =
78+
&mut tts.clone().into_trees().collect::<Vec<_>>()[..]
79+
{
80+
tts = inner_tts.clone();
81+
}
82+
7483
let sp = cx.with_def_site_ctxt(sp);
7584
let s = pprust::tts_to_string(&tts);
7685
base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&s)))

0 commit comments

Comments
 (0)