@@ -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_data_structures:: sync:: Lrc ;
@@ -253,7 +256,6 @@ pub(super) fn transcribe<'a>(
253256 }
254257 }
255258
256- // Replace the meta-var with the matched token tree from the invocation.
257259 mbe:: TokenTree :: MetaVar ( mut sp, mut original_ident) => {
258260 // Find the matched nonterminal from the macro invocation, and use it to replace
259261 // the meta-var.
@@ -273,6 +275,33 @@ pub(super) fn transcribe<'a>(
273275 // some of the unnecessary whitespace.
274276 let ident = MacroRulesNormalizedIdent :: new ( original_ident) ;
275277 if let Some ( cur_matched) = lookup_cur_matched ( ident, interp, & repeats) {
278+ // We wrap the tokens in invisible delimiters, unless they are already wrapped
279+ // in invisible delimiters with the same `MetaVarKind`. Because there is no
280+ // point in having multiple layers of invisible delimiters of the same
281+ // `MetaVarKind`. Indeed, some proc macros can't handle them.
282+ let mut mk_delimited = |mv_kind, mut stream : TokenStream | {
283+ if stream. len ( ) == 1 {
284+ let tree = stream. trees ( ) . next ( ) . unwrap ( ) ;
285+ if let TokenTree :: Delimited ( _, _, delim, inner) = tree
286+ && let Delimiter :: Invisible ( InvisibleOrigin :: MetaVar ( mvk) ) = delim
287+ && mv_kind == * mvk
288+ {
289+ stream = inner. clone ( ) ;
290+ }
291+ }
292+
293+ // Emit as a token stream within `Delimiter::Invisible` to maintain
294+ // parsing priorities.
295+ marker. visit_span ( & mut sp) ;
296+ // Both the open delim and close delim get the same span, which covers the
297+ // `$foo` in the decl macro RHS.
298+ TokenTree :: Delimited (
299+ DelimSpan :: from_single ( sp) ,
300+ DelimSpacing :: new ( Spacing :: Alone , Spacing :: Alone ) ,
301+ Delimiter :: Invisible ( InvisibleOrigin :: MetaVar ( mv_kind) ) ,
302+ stream,
303+ )
304+ } ;
276305 let tt = match cur_matched {
277306 MatchedSingle ( ParseNtResult :: Tt ( tt) ) => {
278307 // `tt`s are emitted into the output stream directly as "raw tokens",
@@ -289,6 +318,9 @@ pub(super) fn transcribe<'a>(
289318 let kind = token:: NtLifetime ( * ident, * is_raw) ;
290319 TokenTree :: token_alone ( kind, sp)
291320 }
321+ MatchedSingle ( ParseNtResult :: Vis ( vis) ) => {
322+ mk_delimited ( MetaVarKind :: Vis , TokenStream :: from_ast ( vis) )
323+ }
292324 MatchedSingle ( ParseNtResult :: Nt ( nt) ) => {
293325 // Other variables are emitted into the output stream as groups with
294326 // `Delimiter::Invisible` to maintain parsing priorities.
0 commit comments