Skip to content

Commit 2782123

Browse files
committed
cleanup dependence of ExtCtxt in transcribe when macro expansion
1 parent 9cb6bb8 commit 2782123

2 files changed

Lines changed: 31 additions & 32 deletions

File tree

‎compiler/rustc_expand/src/mbe/macro_rules.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ fn expand_macro<'cx>(
224224
let arm_span = rhses[i].span();
225225

226226
// rhs has holes ( `$id` and `$(...)` that need filled)
227-
let tts = matchtranscribe(cx,&named_matches, rhs, rhs_span, transparency){
227+
let id = cx.current_expansion.id;
228+
let tts = matchtranscribe(psess,&named_matches, rhs, rhs_span, transparency, id){
228229
Ok(tts) => tts,
229230
Err(err) => {
230231
let guar = err.emit();

‎compiler/rustc_expand/src/mbe/transcribe.rs‎

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
usecrate::base::ExtCtxt;
21
usecrate::errors::{
32
CountRepetitionMisplaced,MetaVarExprUnrecognizedVar,MetaVarsDifSeqMatchers,MustRepeatOnce,
43
NoSyntaxVarsExprRepeat,VarStillRepeating,
@@ -9,12 +8,13 @@ use rustc_ast::mut_visit::{self, MutVisitor};
98
use rustc_ast::token::{self,Delimiter,Token,TokenKind};
109
use rustc_ast::tokenstream::{DelimSpacing,DelimSpan,Spacing,TokenStream,TokenTree};
1110
use rustc_data_structures::fx::FxHashMap;
12-
use rustc_errors::{pluralize,Diag,PResult};
11+
use rustc_errors::{pluralize,Diag,DiagCtxt,PResult};
1312
use rustc_parse::parser::ParseNtResult;
1413
use rustc_span::hygiene::{LocalExpnId,Transparency};
1514
use rustc_span::symbol::{sym,Ident,MacroRulesNormalizedIdent};
1615
use rustc_span::{with_metavar_spans,Span,SyntaxContext};
1716

17+
use rustc_session::parse::ParseSess;
1818
use smallvec::{smallvec,SmallVec};
1919
use std::mem;
2020

@@ -99,11 +99,12 @@ impl<'a> Iterator for Frame<'a> {
9999
///
100100
/// Along the way, we do some additional error checking.
101101
pub(super)fntranscribe<'a>(
102-
cx:&ExtCtxt<'a>,
102+
psess:&'aParseSess,
103103
interp:&FxHashMap<MacroRulesNormalizedIdent,NamedMatch>,
104104
src:&mbe::Delimited,
105105
src_span:DelimSpan,
106106
transparency:Transparency,
107+
expand_id:LocalExpnId,
107108
) -> PResult<'a,TokenStream>{
108109
// Nothing for us to transcribe...
109110
if src.tts.is_empty(){
@@ -137,8 +138,9 @@ pub(super) fn transcribe<'a>(
137138
// again, and we are done transcribing.
138139
letmut result:Vec<TokenTree> = Vec::new();
139140
letmut result_stack = Vec::new();
140-
letmut marker = Marker(cx.current_expansion.id, transparency,Default::default());
141+
letmut marker = Marker(expand_id, transparency,Default::default());
141142

143+
let dcx = &psess.dcx;
142144
loop{
143145
// Look at the last frame on the stack.
144146
// If it still has a TokenTree we have not looked at yet, use that tree.
@@ -201,19 +203,17 @@ pub(super) fn transcribe<'a>(
201203
seq @ mbe::TokenTree::Sequence(_, seq_rep) => {
202204
matchlockstep_iter_size(seq, interp,&repeats){
203205
LockstepIterSize::Unconstrained => {
204-
returnErr(cx
205-
.dcx()
206-
.create_err(NoSyntaxVarsExprRepeat{span: seq.span()}));
206+
returnErr(dcx.create_err(NoSyntaxVarsExprRepeat{span: seq.span()}));
207207
}
208208

209209
LockstepIterSize::Contradiction(msg) => {
210210
// FIXME: this really ought to be caught at macro definition time... It
211211
// happens when two meta-variables are used in the same repetition in a
212212
// sequence, but they come from different sequence matchers and repeat
213213
// different amounts.
214-
returnErr(cx
215-
.dcx()
216-
.create_err(MetaVarsDifSeqMatchers{span: seq.span(), msg }));
214+
returnErr(
215+
dcx.create_err(MetaVarsDifSeqMatchers{span: seq.span(), msg })
216+
);
217217
}
218218

219219
LockstepIterSize::Constraint(len, _) => {
@@ -227,9 +227,7 @@ pub(super) fn transcribe<'a>(
227227
// FIXME: this really ought to be caught at macro definition
228228
// time... It happens when the Kleene operator in the matcher and
229229
// the body for the same meta-variable do not match.
230-
returnErr(cx
231-
.dcx()
232-
.create_err(MustRepeatOnce{span: sp.entire()}));
230+
returnErr(dcx.create_err(MustRepeatOnce{span: sp.entire()}));
233231
}
234232
}else{
235233
// 0 is the initial counter (we have done 0 repetitions so far). `len`
@@ -259,7 +257,7 @@ pub(super) fn transcribe<'a>(
259257
MatchedSingle(ParseNtResult::Tt(tt)) => {
260258
// `tt`s are emitted into the output stream directly as "raw tokens",
261259
// 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)
263261
}
264262
MatchedSingle(ParseNtResult::Ident(ident, is_raw)) => {
265263
marker.visit_span(&mut sp);
@@ -280,7 +278,7 @@ pub(super) fn transcribe<'a>(
280278
}
281279
MatchedSeq(..) => {
282280
// We were unable to descend far enough. This is an error.
283-
returnErr(cx.dcx().create_err(VarStillRepeating{span: sp, ident }));
281+
returnErr(dcx.create_err(VarStillRepeating{span: sp, ident }));
284282
}
285283
};
286284
result.push(tt)
@@ -299,7 +297,7 @@ pub(super) fn transcribe<'a>(
299297

300298
// Replace meta-variable expressions with the result of their expansion.
301299
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)?;
303301
}
304302

305303
// 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>(
359357
/// combine with each other and not with tokens outside of the sequence.
360358
/// - The metavariable span comes from a different crate, then we prefer the more local span.
361359
fnmaybe_use_metavar_location(
362-
cx:&ExtCtxt<'_>,
360+
psess:&ParseSess,
363361
stack:&[Frame<'_>],
364362
mutmetavar_span:Span,
365363
orig_tt:&TokenTree,
@@ -397,7 +395,7 @@ fn maybe_use_metavar_location(
397395
&& insert(mspans, dspan.entire(), metavar_span)
398396
}),
399397
};
400-
if no_collision || cx.source_map().is_imported(metavar_span){
398+
if no_collision || psess.source_map().is_imported(metavar_span){
401399
return orig_tt.clone();
402400
}
403401

@@ -558,7 +556,7 @@ fn lockstep_iter_size(
558556
/// * `[ $( ${count(foo, 1)} ),* ]` will return an error because `${count(foo, 1)}` is
559557
/// declared inside a single repetition and the index `1` implies two nested repetitions.
560558
fncount_repetitions<'a>(
561-
cx:&ExtCtxt<'a>,
559+
dcx:&'aDiagCtxt,
562560
depth_user:usize,
563561
mutmatched:&NamedMatch,
564562
repeats:&[(usize,usize)],
@@ -595,7 +593,7 @@ fn count_repetitions<'a>(
595593
.and_then(|el| el.checked_sub(repeats.len()))
596594
.unwrap_or_default();
597595
if depth_user > depth_max {
598-
returnErr(out_of_bounds_err(cx, depth_max + 1, sp.entire(),"count"));
596+
returnErr(out_of_bounds_err(dcx, depth_max + 1, sp.entire(),"count"));
599597
}
600598

601599
// `repeats` records all of the nested levels at which we are currently
@@ -611,15 +609,15 @@ fn count_repetitions<'a>(
611609
}
612610

613611
ifletMatchedSingle(_) = matched {
614-
returnErr(cx.dcx().create_err(CountRepetitionMisplaced{span: sp.entire()}));
612+
returnErr(dcx.create_err(CountRepetitionMisplaced{span: sp.entire()}));
615613
}
616614

617615
count(depth_user, depth_max, matched)
618616
}
619617

620618
/// Returns a `NamedMatch` item declared on the LHS given an arbitrary [Ident]
621619
fnmatched_from_ident<'ctx,'interp,'rslt>(
622-
cx:&ExtCtxt<'ctx>,
620+
dcx:&'ctxDiagCtxt,
623621
ident:Ident,
624622
interp:&'interpFxHashMap<MacroRulesNormalizedIdent,NamedMatch>,
625623
) -> PResult<'ctx,&'rsltNamedMatch>
@@ -628,12 +626,12 @@ where
628626
{
629627
let span = ident.span;
630628
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 }))
632630
}
633631

634632
/// Used by meta-variable expressions when an user input is out of the actual declared bounds. For
635633
/// example, index(999999) in an repetition of only three elements.
636-
fnout_of_bounds_err<'a>(cx:&ExtCtxt<'a>,max:usize,span:Span,ty:&str) -> Diag<'a>{
634+
fnout_of_bounds_err<'a>(dcx:&'aDiagCtxt,max:usize,span:Span,ty:&str) -> Diag<'a>{
637635
let msg = if max == 0{
638636
format!(
639637
"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) ->
645643
must be less than {max}"
646644
)
647645
};
648-
cx.dcx().struct_span_err(span, msg)
646+
dcx.struct_span_err(span, msg)
649647
}
650648

651649
fntranscribe_metavar_expr<'a>(
652-
cx:&ExtCtxt<'a>,
650+
dcx:&'aDiagCtxt,
653651
expr:&MetaVarExpr,
654652
interp:&FxHashMap<MacroRulesNormalizedIdent,NamedMatch>,
655653
marker:&mutMarker,
@@ -664,8 +662,8 @@ fn transcribe_metavar_expr<'a>(
664662
};
665663
match*expr {
666664
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)?;
669667
let tt = TokenTree::token_alone(
670668
TokenKind::lit(token::Integer, sym::integer(count),None),
671669
visited_span(),
@@ -674,7 +672,7 @@ fn transcribe_metavar_expr<'a>(
674672
}
675673
MetaVarExpr::Ignore(original_ident) => {
676674
// 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)?;
678676
}
679677
MetaVarExpr::Index(depth) => match repeats.iter().nth_back(depth){
680678
Some((index, _)) => {
@@ -683,7 +681,7 @@ fn transcribe_metavar_expr<'a>(
683681
visited_span(),
684682
));
685683
}
686-
None => returnErr(out_of_bounds_err(cx, repeats.len(), sp.entire(),"index")),
684+
None => returnErr(out_of_bounds_err(dcx, repeats.len(), sp.entire(),"index")),
687685
},
688686
MetaVarExpr::Len(depth) => match repeats.iter().nth_back(depth){
689687
Some((_, length)) => {
@@ -692,7 +690,7 @@ fn transcribe_metavar_expr<'a>(
692690
visited_span(),
693691
));
694692
}
695-
None => returnErr(out_of_bounds_err(cx, repeats.len(), sp.entire(),"len")),
693+
None => returnErr(out_of_bounds_err(dcx, repeats.len(), sp.entire(),"len")),
696694
},
697695
}
698696
Ok(())

0 commit comments

Comments
 (0)