11use std:: mem;
22
33use rustc_ast:: mut_visit:: { self , MutVisitor } ;
4- use rustc_ast:: token:: { self , Delimiter , IdentIsRaw , Lit , LitKind , Nonterminal , Token , TokenKind } ;
4+ use rustc_ast:: token:: {
5+ self , Delimiter , IdentIsRaw , InvisibleOrigin , Lit , LitKind , MetaVarKind , Nonterminal , Token ,
6+ TokenKind ,
7+ } ;
58use rustc_ast:: tokenstream:: { DelimSpacing , DelimSpan , Spacing , TokenStream , TokenTree } ;
69use rustc_ast:: ExprKind ;
710use rustc_data_structures:: fx:: FxHashMap ;
@@ -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,19 @@ 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+ let mut mk_delimited = |mv_kind, stream| {
278+ // Emit as a token stream within `Delimiter::Invisible` to maintain parsing
279+ // priorities.
280+ marker. visit_span ( & mut sp) ;
281+ // Both the open delim and close delim get the same span, which covers the
282+ // `$foo` in the decl macro RHS.
283+ TokenTree :: Delimited (
284+ DelimSpan :: from_single ( sp) ,
285+ DelimSpacing :: new ( Spacing :: Alone , Spacing :: Alone ) ,
286+ Delimiter :: Invisible ( InvisibleOrigin :: MetaVar ( mv_kind) ) ,
287+ stream,
288+ )
289+ } ;
275290 let tt = match cur_matched {
276291 MatchedSingle ( ParseNtResult :: Tt ( tt) ) => {
277292 // `tt`s are emitted into the output stream directly as "raw tokens",
@@ -288,6 +303,9 @@ pub(super) fn transcribe<'a>(
288303 let kind = token:: NtLifetime ( * ident, * is_raw) ;
289304 TokenTree :: token_alone ( kind, sp)
290305 }
306+ MatchedSingle ( ParseNtResult :: Vis ( vis) ) => {
307+ mk_delimited ( MetaVarKind :: Vis , TokenStream :: from_ast ( vis) )
308+ }
291309 MatchedSingle ( ParseNtResult :: Nt ( nt) ) => {
292310 // Other variables are emitted into the output stream as groups with
293311 // `Delimiter::Invisible` to maintain parsing priorities.
0 commit comments