@@ -2,7 +2,10 @@ use std::mem;
22
33use rustc_ast:: ExprKind ;
44use rustc_ast:: mut_visit:: { self , MutVisitor } ;
5- use rustc_ast:: token:: { self , Delimiter , IdentIsRaw , Lit , LitKind , Nonterminal , Token , TokenKind } ;
5+ use rustc_ast:: token:: {
6+ self , Delimiter , IdentIsRaw , InvisibleOrigin , Lit , LitKind , MetaVarKind , Nonterminal , Token ,
7+ TokenKind ,
8+ } ;
69use rustc_ast:: tokenstream:: { DelimSpacing , DelimSpan , Spacing , TokenStream , TokenTree } ;
710use rustc_data_structures:: fx:: FxHashMap ;
811use rustc_errors:: { Diag , DiagCtxtHandle , PResult , pluralize} ;
@@ -252,7 +255,6 @@ pub(super) fn transcribe<'a>(
252255 }
253256 }
254257
255- // Replace the meta-var with the matched token tree from the invocation.
256258 mbe:: TokenTree :: MetaVar ( mut sp, mut original_ident) => {
257259 // Find the matched nonterminal from the macro invocation, and use it to replace
258260 // the meta-var.
@@ -272,6 +274,33 @@ pub(super) fn transcribe<'a>(
272274 // some of the unnecessary whitespace.
273275 let ident = MacroRulesNormalizedIdent :: new ( original_ident) ;
274276 if let Some ( cur_matched) = lookup_cur_matched ( ident, interp, & repeats) {
277+ // We wrap the tokens in invisible delimiters, unless they are already wrapped
278+ // in invisible delimiters with the same `MetaVarKind`. Because there is no
279+ // point in having multiple layers of invisible delimiters of the same
280+ // `MetaVarKind`. Indeed, some proc macros can't handle them.
281+ let mut mk_delimited = |mv_kind, mut stream : TokenStream | {
282+ if stream. len ( ) == 1 {
283+ let tree = stream. trees ( ) . next ( ) . unwrap ( ) ;
284+ if let TokenTree :: Delimited ( _, _, delim, inner) = tree
285+ && let Delimiter :: Invisible ( InvisibleOrigin :: MetaVar ( mvk) ) = delim
286+ && mv_kind == * mvk
287+ {
288+ stream = inner. clone ( ) ;
289+ }
290+ }
291+
292+ // Emit as a token stream within `Delimiter::Invisible` to maintain
293+ // parsing priorities.
294+ marker. visit_span ( & mut sp) ;
295+ // Both the open delim and close delim get the same span, which covers the
296+ // `$foo` in the decl macro RHS.
297+ TokenTree :: Delimited (
298+ DelimSpan :: from_single ( sp) ,
299+ DelimSpacing :: new ( Spacing :: Alone , Spacing :: Alone ) ,
300+ Delimiter :: Invisible ( InvisibleOrigin :: MetaVar ( mv_kind) ) ,
301+ stream,
302+ )
303+ } ;
275304 let tt = match cur_matched {
276305 MatchedSingle ( ParseNtResult :: Tt ( tt) ) => {
277306 // `tt`s are emitted into the output stream directly as "raw tokens",
@@ -288,6 +317,9 @@ pub(super) fn transcribe<'a>(
288317 let kind = token:: NtLifetime ( * ident, * is_raw) ;
289318 TokenTree :: token_alone ( kind, sp)
290319 }
320+ MatchedSingle ( ParseNtResult :: Vis ( vis) ) => {
321+ mk_delimited ( MetaVarKind :: Vis , TokenStream :: from_ast ( vis) )
322+ }
291323 MatchedSingle ( ParseNtResult :: Nt ( nt) ) => {
292324 // Other variables are emitted into the output stream as groups with
293325 // `Delimiter::Invisible` to maintain parsing priorities.
0 commit comments