Rollup merge of #125530 - SparrowLii:expand2, r=petrochenkov · rust-lang/rust@f50b4f5 (original) (raw)
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:🪥:{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 = 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
``
`@@ -274,7 +272,7 @@ pub(super) fn transcribe<'a>(
`
274
272
`MatchedSingle(ParseNtResult::Tt(tt)) => {
`
275
273
`` // tt
s are emitted into the output stream directly as "raw tokens",
``
276
274
`// without wrapping them into groups.
`
277
``
`-
maybe_use_metavar_location(cx, &stack, sp, tt, &mut marker)
`
``
275
`+
maybe_use_metavar_location(psess, &stack, sp, tt, &mut marker)
`
278
276
`}
`
279
277
`MatchedSingle(ParseNtResult::Ident(ident, is_raw)) => {
`
280
278
` marker.visit_span(&mut sp);
`
`@@ -295,7 +293,7 @@ pub(super) fn transcribe<'a>(
`
295
293
`}
`
296
294
`MatchedSeq(..) => {
`
297
295
`// We were unable to descend far enough. This is an error.
`
298
``
`-
return Err(cx.dcx().create_err(VarStillRepeating { span: sp, ident }));
`
``
296
`+
return Err(dcx.create_err(VarStillRepeating { span: sp, ident }));
`
299
297
`}
`
300
298
`};
`
301
299
` result.push(tt)
`
`@@ -314,7 +312,7 @@ pub(super) fn transcribe<'a>(
`
314
312
``
315
313
`// Replace meta-variable expressions with the result of their expansion.
`
316
314
` mbe::TokenTree::MetaVarExpr(sp, expr) => {
`
317
``
`-
transcribe_metavar_expr(cx, expr, interp, &mut marker, &repeats, &mut result, sp)?;
`
``
315
`+
transcribe_metavar_expr(dcx, expr, interp, &mut marker, &repeats, &mut result, sp)?;
`
318
316
`}
`
319
317
``
320
318
`` // If we are entering a new delimiter, we push its contents to the stack
to be
``
`@@ -374,7 +372,7 @@ pub(super) fn transcribe<'a>(
`
374
372
`/// combine with each other and not with tokens outside of the sequence.
`
375
373
`/// - The metavariable span comes from a different crate, then we prefer the more local span.
`
376
374
`fn maybe_use_metavar_location(
`
377
``
`-
cx: &ExtCtxt<'_>,
`
``
375
`+
psess: &ParseSess,
`
378
376
`stack: &[Frame<'_>],
`
379
377
`mut metavar_span: Span,
`
380
378
`orig_tt: &TokenTree,
`
`@@ -412,7 +410,7 @@ fn maybe_use_metavar_location(
`
412
410
` && insert(mspans, dspan.entire(), metavar_span)
`
413
411
`}),
`
414
412
`};
`
415
``
`-
if no_collision || cx.source_map().is_imported(metavar_span) {
`
``
413
`+
if no_collision || psess.source_map().is_imported(metavar_span) {
`
416
414
`return orig_tt.clone();
`
417
415
`}
`
418
416
``
`@@ -573,7 +571,7 @@ fn lockstep_iter_size(
`
573
571
`` /// * [ <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">( </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">(</span></span></span></span>{count(foo, 1)} ),* ]
will return an error because ${count(foo, 1)}
is
``
574
572
`` /// declared inside a single repetition and the index 1
implies two nested repetitions.
``
575
573
`fn count_repetitions<'a>(
`
576
``
`-
cx: &ExtCtxt<'a>,
`
``
574
`+
dcx: &'a DiagCtxt,
`
577
575
`depth_user: usize,
`
578
576
`mut matched: &NamedMatch,
`
579
577
`repeats: &[(usize, usize)],
`
`@@ -610,7 +608,7 @@ fn count_repetitions<'a>(
`
610
608
`.and_then(|el| el.checked_sub(repeats.len()))
`
611
609
`.unwrap_or_default();
`
612
610
`if depth_user > depth_max {
`
613
``
`-
return Err(out_of_bounds_err(cx, depth_max + 1, sp.entire(), "count"));
`
``
611
`+
return Err(out_of_bounds_err(dcx, depth_max + 1, sp.entire(), "count"));
`
614
612
`}
`
615
613
``
616
614
`` // repeats
records all of the nested levels at which we are currently
``
`@@ -626,15 +624,15 @@ fn count_repetitions<'a>(
`
626
624
`}
`
627
625
``
628
626
`if let MatchedSingle(_) = matched {
`
629
``
`-
return Err(cx.dcx().create_err(CountRepetitionMisplaced { span: sp.entire() }));
`
``
627
`+
return Err(dcx.create_err(CountRepetitionMisplaced { span: sp.entire() }));
`
630
628
`}
`
631
629
``
632
630
`count(depth_user, depth_max, matched)
`
633
631
`}
`
634
632
``
635
633
`` /// Returns a NamedMatch
item declared on the LHS given an arbitrary [Ident]
``
636
634
`fn matched_from_ident<'ctx, 'interp, 'rslt>(
`
637
``
`-
cx: &ExtCtxt<'ctx>,
`
``
635
`+
dcx: &'ctx DiagCtxt,
`
638
636
`ident: Ident,
`
639
637
`interp: &'interp FxHashMap<MacroRulesNormalizedIdent, NamedMatch>,
`
640
638
`) -> PResult<'ctx, &'rslt NamedMatch>
`
`@@ -643,12 +641,12 @@ where
`
643
641
`{
`
644
642
`let span = ident.span;
`
645
643
`let key = MacroRulesNormalizedIdent::new(ident);
`
646
``
`-
interp.get(&key).ok_or_else(|| cx.dcx().create_err(MetaVarExprUnrecognizedVar { span, key }))
`
``
644
`+
interp.get(&key).ok_or_else(|| dcx.create_err(MetaVarExprUnrecognizedVar { span, key }))
`
647
645
`}
`
648
646
``
649
647
`/// Used by meta-variable expressions when an user input is out of the actual declared bounds. For
`
650
648
`/// example, index(999999) in an repetition of only three elements.
`
651
``
`-
fn out_of_bounds_err<'a>(cx: &ExtCtxt<'a>, max: usize, span: Span, ty: &str) -> Diag<'a> {
`
``
649
`+
fn out_of_bounds_err<'a>(dcx: &'a DiagCtxt, max: usize, span: Span, ty: &str) -> Diag<'a> {
`
652
650
`let msg = if max == 0 {
`
653
651
`format!(
`
654
652
`` "meta-variable expression {ty}
with depth parameter \
``
`@@ -660,11 +658,11 @@ fn out_of_bounds_err<'a>(cx: &ExtCtxt<'a>, max: usize, span: Span, ty: &str) ->
`
660
658
` must be less than {max}"
`
661
659
`)
`
662
660
`};
`
663
``
`-
cx.dcx().struct_span_err(span, msg)
`
``
661
`+
dcx.struct_span_err(span, msg)
`
664
662
`}
`
665
663
``
666
664
`fn transcribe_metavar_expr<'a>(
`
667
``
`-
cx: &ExtCtxt<'a>,
`
``
665
`+
dcx: &'a DiagCtxt,
`
668
666
`expr: &MetaVarExpr,
`
669
667
`interp: &FxHashMap<MacroRulesNormalizedIdent, NamedMatch>,
`
670
668
`marker: &mut Marker,
`
`@@ -679,8 +677,8 @@ fn transcribe_metavar_expr<'a>(
`
679
677
`};
`
680
678
`match *expr {
`
681
679
`MetaVarExpr::Count(original_ident, depth) => {
`
682
``
`-
let matched = matched_from_ident(cx, original_ident, interp)?;
`
683
``
`-
let count = count_repetitions(cx, depth, matched, repeats, sp)?;
`
``
680
`+
let matched = matched_from_ident(dcx, original_ident, interp)?;
`
``
681
`+
let count = count_repetitions(dcx, depth, matched, repeats, sp)?;
`
684
682
`let tt = TokenTree::token_alone(
`
685
683
`TokenKind::lit(token::Integer, sym::integer(count), None),
`
686
684
`visited_span(),
`
`@@ -689,7 +687,7 @@ fn transcribe_metavar_expr<'a>(
`
689
687
`}
`
690
688
`MetaVarExpr::Ignore(original_ident) => {
`
691
689
`` // Used to ensure that original_ident
is present in the LHS
``
692
``
`-
let _ = matched_from_ident(cx, original_ident, interp)?;
`
``
690
`+
let _ = matched_from_ident(dcx, original_ident, interp)?;
`
693
691
`}
`
694
692
`MetaVarExpr::Index(depth) => match repeats.iter().nth_back(depth) {
`
695
693
`Some((index, _)) => {
`
`@@ -698,7 +696,7 @@ fn transcribe_metavar_expr<'a>(
`
698
696
`visited_span(),
`
699
697
`));
`
700
698
`}
`
701
``
`-
None => return Err(out_of_bounds_err(cx, repeats.len(), sp.entire(), "index")),
`
``
699
`+
None => return Err(out_of_bounds_err(dcx, repeats.len(), sp.entire(), "index")),
`
702
700
`},
`
703
701
`MetaVarExpr::Len(depth) => match repeats.iter().nth_back(depth) {
`
704
702
`Some((_, length)) => {
`
`@@ -707,7 +705,7 @@ fn transcribe_metavar_expr<'a>(
`
707
705
`visited_span(),
`
708
706
`));
`
709
707
`}
`
710
``
`-
None => return Err(out_of_bounds_err(cx, repeats.len(), sp.entire(), "len")),
`
``
708
`+
None => return Err(out_of_bounds_err(dcx, repeats.len(), sp.entire(), "len")),
`
711
709
`},
`
712
710
`}
`
713
711
`Ok(())
`