1
- use crate :: base:: ExtCtxt ;
2
1
use crate :: errors:: {
3
2
CountRepetitionMisplaced , MetaVarExprUnrecognizedVar , MetaVarsDifSeqMatchers , MustRepeatOnce ,
4
3
NoSyntaxVarsExprRepeat , VarStillRepeating ,
@@ -9,12 +8,13 @@ use rustc_ast::mut_visit::{self, MutVisitor};
9
8
use rustc_ast:: token:: { self , Delimiter , Token , TokenKind } ;
10
9
use rustc_ast:: tokenstream:: { DelimSpacing , DelimSpan , Spacing , TokenStream , TokenTree } ;
11
10
use rustc_data_structures:: fx:: FxHashMap ;
12
- use rustc_errors:: { pluralize, Diag , PResult } ;
11
+ use rustc_errors:: { pluralize, Diag , DiagCtxt , PResult } ;
13
12
use rustc_parse:: parser:: ParseNtResult ;
14
13
use rustc_span:: hygiene:: { LocalExpnId , Transparency } ;
15
14
use rustc_span:: symbol:: { sym, Ident , MacroRulesNormalizedIdent } ;
16
15
use rustc_span:: { with_metavar_spans, Span , SyntaxContext } ;
17
16
17
+ use rustc_session:: parse:: ParseSess ;
18
18
use smallvec:: { smallvec, SmallVec } ;
19
19
use std:: mem;
20
20
@@ -99,11 +99,12 @@ impl<'a> Iterator for Frame<'a> {
99
99
///
100
100
/// Along the way, we do some additional error checking.
101
101
pub ( super ) fn transcribe < ' a > (
102
- cx : & ExtCtxt < ' a > ,
102
+ psess : & ' a ParseSess ,
103
103
interp : & FxHashMap < MacroRulesNormalizedIdent , NamedMatch > ,
104
104
src : & mbe:: Delimited ,
105
105
src_span : DelimSpan ,
106
106
transparency : Transparency ,
107
+ expand_id : LocalExpnId ,
107
108
) -> PResult < ' a , TokenStream > {
108
109
// Nothing for us to transcribe...
109
110
if src. tts . is_empty ( ) {
@@ -137,8 +138,9 @@ pub(super) fn transcribe<'a>(
137
138
// again, and we are done transcribing.
138
139
let mut result: Vec < TokenTree > = Vec :: new ( ) ;
139
140
let mut result_stack = Vec :: new ( ) ;
140
- let mut marker = Marker ( cx . current_expansion . id , transparency, Default :: default ( ) ) ;
141
+ let mut marker = Marker ( expand_id , transparency, Default :: default ( ) ) ;
141
142
143
+ let dcx = & psess. dcx ;
142
144
loop {
143
145
// Look at the last frame on the stack.
144
146
// If it still has a TokenTree we have not looked at yet, use that tree.
@@ -201,19 +203,17 @@ pub(super) fn transcribe<'a>(
201
203
seq @ mbe:: TokenTree :: Sequence ( _, seq_rep) => {
202
204
match lockstep_iter_size ( seq, interp, & repeats) {
203
205
LockstepIterSize :: Unconstrained => {
204
- return Err ( cx
205
- . dcx ( )
206
- . create_err ( NoSyntaxVarsExprRepeat { span : seq. span ( ) } ) ) ;
206
+ return Err ( dcx. create_err ( NoSyntaxVarsExprRepeat { span : seq. span ( ) } ) ) ;
207
207
}
208
208
209
209
LockstepIterSize :: Contradiction ( msg) => {
210
210
// FIXME: this really ought to be caught at macro definition time... It
211
211
// happens when two meta-variables are used in the same repetition in a
212
212
// sequence, but they come from different sequence matchers and repeat
213
213
// different amounts.
214
- return Err ( cx
215
- . dcx ( )
216
- . create_err ( MetaVarsDifSeqMatchers { span : seq . span ( ) , msg } ) ) ;
214
+ return Err (
215
+ dcx. create_err ( MetaVarsDifSeqMatchers { span : seq . span ( ) , msg } )
216
+ ) ;
217
217
}
218
218
219
219
LockstepIterSize :: Constraint ( len, _) => {
@@ -227,9 +227,7 @@ pub(super) fn transcribe<'a>(
227
227
// FIXME: this really ought to be caught at macro definition
228
228
// time... It happens when the Kleene operator in the matcher and
229
229
// the body for the same meta-variable do not match.
230
- return Err ( cx
231
- . dcx ( )
232
- . create_err ( MustRepeatOnce { span : sp. entire ( ) } ) ) ;
230
+ return Err ( dcx. create_err ( MustRepeatOnce { span : sp. entire ( ) } ) ) ;
233
231
}
234
232
} else {
235
233
// 0 is the initial counter (we have done 0 repetitions so far). `len`
@@ -259,7 +257,7 @@ pub(super) fn transcribe<'a>(
259
257
MatchedSingle ( ParseNtResult :: Tt ( tt) ) => {
260
258
// `tt`s are emitted into the output stream directly as "raw tokens",
261
259
// without wrapping them into groups.
262
- maybe_use_metavar_location ( cx , & stack, sp, tt, & mut marker)
260
+ maybe_use_metavar_location ( psess , & stack, sp, tt, & mut marker)
263
261
}
264
262
MatchedSingle ( ParseNtResult :: Ident ( ident, is_raw) ) => {
265
263
marker. visit_span ( & mut sp) ;
@@ -280,7 +278,7 @@ pub(super) fn transcribe<'a>(
280
278
}
281
279
MatchedSeq ( ..) => {
282
280
// We were unable to descend far enough. This is an error.
283
- return Err ( cx . dcx ( ) . create_err ( VarStillRepeating { span : sp, ident } ) ) ;
281
+ return Err ( dcx. create_err ( VarStillRepeating { span : sp, ident } ) ) ;
284
282
}
285
283
} ;
286
284
result. push ( tt)
@@ -299,7 +297,7 @@ pub(super) fn transcribe<'a>(
299
297
300
298
// Replace meta-variable expressions with the result of their expansion.
301
299
mbe:: TokenTree :: MetaVarExpr ( sp, expr) => {
302
- transcribe_metavar_expr ( cx , expr, interp, & mut marker, & repeats, & mut result, sp) ?;
300
+ transcribe_metavar_expr ( dcx , expr, interp, & mut marker, & repeats, & mut result, sp) ?;
303
301
}
304
302
305
303
// If we are entering a new delimiter, we push its contents to the `stack` to be
@@ -359,7 +357,7 @@ pub(super) fn transcribe<'a>(
359
357
/// combine with each other and not with tokens outside of the sequence.
360
358
/// - The metavariable span comes from a different crate, then we prefer the more local span.
361
359
fn maybe_use_metavar_location (
362
- cx : & ExtCtxt < ' _ > ,
360
+ psess : & ParseSess ,
363
361
stack : & [ Frame < ' _ > ] ,
364
362
mut metavar_span : Span ,
365
363
orig_tt : & TokenTree ,
@@ -397,7 +395,7 @@ fn maybe_use_metavar_location(
397
395
&& insert ( mspans, dspan. entire ( ) , metavar_span)
398
396
} ) ,
399
397
} ;
400
- if no_collision || cx . source_map ( ) . is_imported ( metavar_span) {
398
+ if no_collision || psess . source_map ( ) . is_imported ( metavar_span) {
401
399
return orig_tt. clone ( ) ;
402
400
}
403
401
@@ -558,7 +556,7 @@ fn lockstep_iter_size(
558
556
/// * `[ $( ${count(foo, 1)} ),* ]` will return an error because `${count(foo, 1)}` is
559
557
/// declared inside a single repetition and the index `1` implies two nested repetitions.
560
558
fn count_repetitions < ' a > (
561
- cx : & ExtCtxt < ' a > ,
559
+ dcx : & ' a DiagCtxt ,
562
560
depth_user : usize ,
563
561
mut matched : & NamedMatch ,
564
562
repeats : & [ ( usize , usize ) ] ,
@@ -595,7 +593,7 @@ fn count_repetitions<'a>(
595
593
. and_then ( |el| el. checked_sub ( repeats. len ( ) ) )
596
594
. unwrap_or_default ( ) ;
597
595
if depth_user > depth_max {
598
- return Err ( out_of_bounds_err ( cx , depth_max + 1 , sp. entire ( ) , "count" ) ) ;
596
+ return Err ( out_of_bounds_err ( dcx , depth_max + 1 , sp. entire ( ) , "count" ) ) ;
599
597
}
600
598
601
599
// `repeats` records all of the nested levels at which we are currently
@@ -611,15 +609,15 @@ fn count_repetitions<'a>(
611
609
}
612
610
613
611
if let MatchedSingle ( _) = matched {
614
- return Err ( cx . dcx ( ) . create_err ( CountRepetitionMisplaced { span : sp. entire ( ) } ) ) ;
612
+ return Err ( dcx. create_err ( CountRepetitionMisplaced { span : sp. entire ( ) } ) ) ;
615
613
}
616
614
617
615
count ( depth_user, depth_max, matched)
618
616
}
619
617
620
618
/// Returns a `NamedMatch` item declared on the LHS given an arbitrary [Ident]
621
619
fn matched_from_ident < ' ctx , ' interp , ' rslt > (
622
- cx : & ExtCtxt < ' ctx > ,
620
+ dcx : & ' ctx DiagCtxt ,
623
621
ident : Ident ,
624
622
interp : & ' interp FxHashMap < MacroRulesNormalizedIdent , NamedMatch > ,
625
623
) -> PResult < ' ctx , & ' rslt NamedMatch >
@@ -628,12 +626,12 @@ where
628
626
{
629
627
let span = ident. span ;
630
628
let key = MacroRulesNormalizedIdent :: new ( ident) ;
631
- interp. get ( & key) . ok_or_else ( || cx . dcx ( ) . create_err ( MetaVarExprUnrecognizedVar { span, key } ) )
629
+ interp. get ( & key) . ok_or_else ( || dcx. create_err ( MetaVarExprUnrecognizedVar { span, key } ) )
632
630
}
633
631
634
632
/// Used by meta-variable expressions when an user input is out of the actual declared bounds. For
635
633
/// example, index(999999) in an repetition of only three elements.
636
- fn out_of_bounds_err < ' a > ( cx : & ExtCtxt < ' a > , max : usize , span : Span , ty : & str ) -> Diag < ' a > {
634
+ fn out_of_bounds_err < ' a > ( dcx : & ' a DiagCtxt , max : usize , span : Span , ty : & str ) -> Diag < ' a > {
637
635
let msg = if max == 0 {
638
636
format ! (
639
637
"meta-variable expression `{ty}` with depth parameter \
@@ -645,11 +643,11 @@ fn out_of_bounds_err<'a>(cx: &ExtCtxt<'a>, max: usize, span: Span, ty: &str) ->
645
643
must be less than {max}"
646
644
)
647
645
} ;
648
- cx . dcx ( ) . struct_span_err ( span, msg)
646
+ dcx. struct_span_err ( span, msg)
649
647
}
650
648
651
649
fn transcribe_metavar_expr < ' a > (
652
- cx : & ExtCtxt < ' a > ,
650
+ dcx : & ' a DiagCtxt ,
653
651
expr : & MetaVarExpr ,
654
652
interp : & FxHashMap < MacroRulesNormalizedIdent , NamedMatch > ,
655
653
marker : & mut Marker ,
@@ -664,8 +662,8 @@ fn transcribe_metavar_expr<'a>(
664
662
} ;
665
663
match * expr {
666
664
MetaVarExpr :: Count ( original_ident, depth) => {
667
- let matched = matched_from_ident ( cx , original_ident, interp) ?;
668
- let count = count_repetitions ( cx , depth, matched, repeats, sp) ?;
665
+ let matched = matched_from_ident ( dcx , original_ident, interp) ?;
666
+ let count = count_repetitions ( dcx , depth, matched, repeats, sp) ?;
669
667
let tt = TokenTree :: token_alone (
670
668
TokenKind :: lit ( token:: Integer , sym:: integer ( count) , None ) ,
671
669
visited_span ( ) ,
@@ -674,7 +672,7 @@ fn transcribe_metavar_expr<'a>(
674
672
}
675
673
MetaVarExpr :: Ignore ( original_ident) => {
676
674
// Used to ensure that `original_ident` is present in the LHS
677
- let _ = matched_from_ident ( cx , original_ident, interp) ?;
675
+ let _ = matched_from_ident ( dcx , original_ident, interp) ?;
678
676
}
679
677
MetaVarExpr :: Index ( depth) => match repeats. iter ( ) . nth_back ( depth) {
680
678
Some ( ( index, _) ) => {
@@ -683,7 +681,7 @@ fn transcribe_metavar_expr<'a>(
683
681
visited_span ( ) ,
684
682
) ) ;
685
683
}
686
- None => return Err ( out_of_bounds_err ( cx , repeats. len ( ) , sp. entire ( ) , "index" ) ) ,
684
+ None => return Err ( out_of_bounds_err ( dcx , repeats. len ( ) , sp. entire ( ) , "index" ) ) ,
687
685
} ,
688
686
MetaVarExpr :: Len ( depth) => match repeats. iter ( ) . nth_back ( depth) {
689
687
Some ( ( _, length) ) => {
@@ -692,7 +690,7 @@ fn transcribe_metavar_expr<'a>(
692
690
visited_span ( ) ,
693
691
) ) ;
694
692
}
695
- None => return Err ( out_of_bounds_err ( cx , repeats. len ( ) , sp. entire ( ) , "len" ) ) ,
693
+ None => return Err ( out_of_bounds_err ( dcx , repeats. len ( ) , sp. entire ( ) , "len" ) ) ,
696
694
} ,
697
695
}
698
696
Ok ( ( ) )
0 commit comments