@@ -5,7 +5,6 @@ use rustc_ast::mut_visit::{self, MutVisitor};
5
5
use rustc_ast:: token:: { self , Token , TokenKind } ;
6
6
use rustc_ast:: tokenstream:: { DelimSpan , TokenStream , TokenTree , TreeAndSpacing } ;
7
7
use rustc_data_structures:: fx:: FxHashMap ;
8
- use rustc_data_structures:: sync:: Lrc ;
9
8
use rustc_errors:: { pluralize, PResult } ;
10
9
use rustc_errors:: { DiagnosticBuilder , ErrorGuaranteed } ;
11
10
use rustc_span:: hygiene:: { LocalExpnId , Transparency } ;
@@ -27,31 +26,35 @@ impl MutVisitor for Marker {
27
26
}
28
27
29
28
/// An iterator over the token trees in a delimited token tree (`{ ... }`) or a sequence (`$(...)`).
30
- enum Frame {
31
- Delimited { forest : Lrc < mbe:: Delimited > , idx : usize , span : DelimSpan } ,
32
- Sequence { forest : Lrc < mbe:: SequenceRepetition > , idx : usize , sep : Option < Token > } ,
29
+ enum Frame < ' a > {
30
+ Delimited {
31
+ tts : & ' a [ mbe:: TokenTree ] ,
32
+ delim_token : token:: DelimToken ,
33
+ idx : usize ,
34
+ span : DelimSpan ,
35
+ } ,
36
+ Sequence {
37
+ tts : & ' a [ mbe:: TokenTree ] ,
38
+ idx : usize ,
39
+ sep : Option < Token > ,
40
+ } ,
33
41
}
34
42
35
- impl Frame {
43
+ impl < ' a > Frame < ' a > {
36
44
/// Construct a new frame around the delimited set of tokens.
37
- fn new ( tts : Vec < mbe:: TokenTree > ) -> Frame {
38
- let forest = Lrc :: new ( mbe:: Delimited { delim : token:: NoDelim , tts } ) ;
39
- Frame :: Delimited { forest, idx : 0 , span : DelimSpan :: dummy ( ) }
45
+ fn new ( tts : & ' a [ mbe:: TokenTree ] ) -> Frame < ' a > {
46
+ Frame :: Delimited { tts, delim_token : token:: NoDelim , idx : 0 , span : DelimSpan :: dummy ( ) }
40
47
}
41
48
}
42
49
43
- impl Iterator for Frame {
44
- type Item = mbe:: TokenTree ;
50
+ impl < ' a > Iterator for Frame < ' a > {
51
+ type Item = & ' a mbe:: TokenTree ;
45
52
46
- fn next ( & mut self ) -> Option < mbe:: TokenTree > {
47
- match * self {
48
- Frame :: Delimited { ref forest, ref mut idx, .. } => {
49
- let res = forest. tts . get ( * idx) . cloned ( ) ;
50
- * idx += 1 ;
51
- res
52
- }
53
- Frame :: Sequence { ref forest, ref mut idx, .. } => {
54
- let res = forest. tts . get ( * idx) . cloned ( ) ;
53
+ fn next ( & mut self ) -> Option < & ' a mbe:: TokenTree > {
54
+ match self {
55
+ Frame :: Delimited { tts, ref mut idx, .. }
56
+ | Frame :: Sequence { tts, ref mut idx, .. } => {
57
+ let res = tts. get ( * idx) ;
55
58
* idx += 1 ;
56
59
res
57
60
}
@@ -92,7 +95,7 @@ pub(super) fn transcribe<'a>(
92
95
93
96
// We descend into the RHS (`src`), expanding things as we go. This stack contains the things
94
97
// we have yet to expand/are still expanding. We start the stack off with the whole RHS.
95
- let mut stack: SmallVec < [ Frame ; 1 ] > = smallvec ! [ Frame :: new( src) ] ;
98
+ let mut stack: SmallVec < [ Frame < ' _ > ; 1 ] > = smallvec ! [ Frame :: new( & src) ] ;
96
99
97
100
// As we descend in the RHS, we will need to be able to match nested sequences of matchers.
98
101
// `repeats` keeps track of where we are in matching at each level, with the last element being
@@ -146,14 +149,14 @@ pub(super) fn transcribe<'a>(
146
149
// We are done processing a Delimited. If this is the top-level delimited, we are
147
150
// done. Otherwise, we unwind the result_stack to append what we have produced to
148
151
// any previous results.
149
- Frame :: Delimited { forest , span, .. } => {
152
+ Frame :: Delimited { delim_token , span, .. } => {
150
153
if result_stack. is_empty ( ) {
151
154
// No results left to compute! We are back at the top-level.
152
155
return Ok ( TokenStream :: new ( result) ) ;
153
156
}
154
157
155
158
// Step back into the parent Delimited.
156
- let tree = TokenTree :: Delimited ( span, forest . delim , TokenStream :: new ( result) ) ;
159
+ let tree = TokenTree :: Delimited ( span, delim_token , TokenStream :: new ( result) ) ;
157
160
result = result_stack. pop ( ) . unwrap ( ) ;
158
161
result. push ( tree. into ( ) ) ;
159
162
}
@@ -167,7 +170,7 @@ pub(super) fn transcribe<'a>(
167
170
// We are descending into a sequence. We first make sure that the matchers in the RHS
168
171
// and the matches in `interp` have the same shape. Otherwise, either the caller or the
169
172
// macro writer has made a mistake.
170
- seq @ mbe:: TokenTree :: Sequence ( .. ) => {
173
+ seq @ mbe:: TokenTree :: Sequence ( _ , delimited ) => {
171
174
match lockstep_iter_size ( & seq, interp, & repeats) {
172
175
LockstepIterSize :: Unconstrained => {
173
176
return Err ( cx. struct_span_err (
@@ -214,7 +217,7 @@ pub(super) fn transcribe<'a>(
214
217
stack. push ( Frame :: Sequence {
215
218
idx : 0 ,
216
219
sep : seq. separator . clone ( ) ,
217
- forest : seq ,
220
+ tts : & delimited . tts ,
218
221
} ) ;
219
222
}
220
223
}
@@ -272,15 +275,21 @@ pub(super) fn transcribe<'a>(
272
275
// the previous results (from outside the Delimited).
273
276
mbe:: TokenTree :: Delimited ( mut span, delimited) => {
274
277
mut_visit:: visit_delim_span ( & mut span, & mut marker) ;
275
- stack. push ( Frame :: Delimited { forest : delimited, idx : 0 , span } ) ;
278
+ stack. push ( Frame :: Delimited {
279
+ tts : & delimited. tts ,
280
+ delim_token : delimited. delim ,
281
+ idx : 0 ,
282
+ span,
283
+ } ) ;
276
284
result_stack. push ( mem:: take ( & mut result) ) ;
277
285
}
278
286
279
287
// Nothing much to do here. Just push the token to the result, being careful to
280
288
// preserve syntax context.
281
289
mbe:: TokenTree :: Token ( token) => {
282
- let mut tt = TokenTree :: Token ( token) ;
283
- mut_visit:: visit_tt ( & mut tt, & mut marker) ;
290
+ let mut token = token. clone ( ) ;
291
+ mut_visit:: visit_token ( & mut token, & mut marker) ;
292
+ let tt = TokenTree :: Token ( token) ;
284
293
result. push ( tt. into ( ) ) ;
285
294
}
286
295
@@ -516,7 +525,7 @@ fn out_of_bounds_err<'a>(
516
525
517
526
fn transcribe_metavar_expr < ' a > (
518
527
cx : & ExtCtxt < ' a > ,
519
- expr : MetaVarExpr ,
528
+ expr : & MetaVarExpr ,
520
529
interp : & FxHashMap < MacroRulesNormalizedIdent , NamedMatch > ,
521
530
marker : & mut Marker ,
522
531
repeats : & [ ( usize , usize ) ] ,
@@ -528,7 +537,7 @@ fn transcribe_metavar_expr<'a>(
528
537
marker. visit_span ( & mut span) ;
529
538
span
530
539
} ;
531
- match expr {
540
+ match * expr {
532
541
MetaVarExpr :: Count ( original_ident, depth_opt) => {
533
542
let matched = matched_from_ident ( cx, original_ident, interp) ?;
534
543
let count = count_repetitions ( cx, depth_opt, matched, & repeats, sp) ?;
0 commit comments