@@ -4,7 +4,7 @@ use crate::errors::{
44 NoSyntaxVarsExprRepeat , VarStillRepeating ,
55} ;
66use crate :: mbe:: macro_parser:: { MatchedNonterminal , MatchedSeq , MatchedTokenTree , NamedMatch } ;
7- use crate :: mbe:: { self , MetaVarExpr } ;
7+ use crate :: mbe:: { self , KleeneOp , MetaVarExpr } ;
88use rustc_ast:: mut_visit:: { self , MutVisitor } ;
99use rustc_ast:: token:: { self , Delimiter , Token , TokenKind } ;
1010use rustc_ast:: tokenstream:: { DelimSpacing , DelimSpan , Spacing , TokenStream , TokenTree } ;
@@ -42,6 +42,7 @@ enum Frame<'a> {
4242 tts : & ' a [ mbe:: TokenTree ] ,
4343 idx : usize ,
4444 sep : Option < Token > ,
45+ kleene_op : KleeneOp ,
4546 } ,
4647}
4748
@@ -207,7 +208,7 @@ pub(super) fn transcribe<'a>(
207208
208209 // Is the repetition empty?
209210 if len == 0 {
210- if seq. kleene . op == mbe :: KleeneOp :: OneOrMore {
211+ if seq. kleene . op == KleeneOp :: OneOrMore {
211212 // FIXME: this really ought to be caught at macro definition
212213 // time... It happens when the Kleene operator in the matcher and
213214 // the body for the same meta-variable do not match.
@@ -227,6 +228,7 @@ pub(super) fn transcribe<'a>(
227228 idx : 0 ,
228229 sep : seq. separator . clone ( ) ,
229230 tts : & delimited. tts ,
231+ kleene_op : seq. kleene . op ,
230232 } ) ;
231233 }
232234 }
@@ -243,7 +245,7 @@ pub(super) fn transcribe<'a>(
243245 MatchedTokenTree ( tt) => {
244246 // `tt`s are emitted into the output stream directly as "raw tokens",
245247 // without wrapping them into groups.
246- result. push ( tt . clone ( ) ) ;
248+ result. push ( maybe_use_metavar_location ( cx , & stack , sp , tt ) ) ;
247249 }
248250 MatchedNonterminal ( nt) => {
249251 // Other variables are emitted into the output stream as groups with
@@ -308,6 +310,62 @@ pub(super) fn transcribe<'a>(
308310 }
309311}
310312
313+ /// Usually metavariables `$var` produce interpolated tokens, which have an additional place for
314+ /// keeping both the original span and the metavariable span. For `tt` metavariables that's not the
315+ /// case however, and there's no place for keeping a second span. So we try to give the single
316+ /// produced span a location that would be most useful in practice (the hygiene part of the span
317+ /// must not be changed).
318+ ///
319+ /// Different locations are useful for different purposes:
320+ /// - The original location is useful when we need to report a diagnostic for the original token in
321+ /// isolation, without combining it with any surrounding tokens. This case occurs, but it is not
322+ /// very common in practice.
323+ /// - The metavariable location is useful when we need to somehow combine the token span with spans
324+ /// of its surrounding tokens. This is the most common way to use token spans.
325+ ///
326+ /// So this function replaces the original location with the metavariable location in all cases
327+ /// except these two:
328+ /// - The metavariable is an element of undelimited sequence `$($tt)*`.
329+ /// These are typically used for passing larger amounts of code, and tokens in that code usually
330+ /// combine with each other and not with tokens outside of the sequence.
331+ /// - The metavariable span comes from a different crate, then we prefer the more local span.
332+ ///
333+ /// FIXME: Find a way to keep both original and metavariable spans for all tokens without
334+ /// regressing compilation time too much. Several experiments for adding such spans were made in
335+ /// the past (PR #95580, #118517, #118671) and all showed some regressions.
336+ fn maybe_use_metavar_location (
337+ cx : & ExtCtxt < ' _ > ,
338+ stack : & [ Frame < ' _ > ] ,
339+ metavar_span : Span ,
340+ orig_tt : & TokenTree ,
341+ ) -> TokenTree {
342+ let undelimited_seq = matches ! (
343+ stack. last( ) ,
344+ Some ( Frame :: Sequence {
345+ tts: [ _] ,
346+ sep: None ,
347+ kleene_op: KleeneOp :: ZeroOrMore | KleeneOp :: OneOrMore ,
348+ ..
349+ } )
350+ ) ;
351+ if undelimited_seq || cx. source_map ( ) . is_imported ( metavar_span) {
352+ return orig_tt. clone ( ) ;
353+ }
354+
355+ match orig_tt {
356+ TokenTree :: Token ( Token { kind, span } , spacing) => {
357+ let span = metavar_span. with_ctxt ( span. ctxt ( ) ) ;
358+ TokenTree :: Token ( Token { kind : kind. clone ( ) , span } , * spacing)
359+ }
360+ TokenTree :: Delimited ( dspan, dspacing, delimiter, tts) => {
361+ let open = metavar_span. shrink_to_lo ( ) . with_ctxt ( dspan. open . ctxt ( ) ) ;
362+ let close = metavar_span. shrink_to_hi ( ) . with_ctxt ( dspan. close . ctxt ( ) ) ;
363+ let dspan = DelimSpan :: from_pair ( open, close) ;
364+ TokenTree :: Delimited ( dspan, * dspacing, * delimiter, tts. clone ( ) )
365+ }
366+ }
367+ }
368+
311369/// Lookup the meta-var named `ident` and return the matched token tree from the invocation using
312370/// the set of matches `interpolations`.
313371///
0 commit comments