Skip to content

Commit c6fedd4

Browse files
committed
Make MatcherPos not derive Clone.
It's only used in one place, and there we clone and then make a bunch of modifications. It's clearer if we duplicate more explicitly, and there's a symmetry now between `sequence()` and `empty_sequence()`.
1 parent f68a044 commit c6fedd4

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

Diff for: compiler/rustc_expand/src/mbe/macro_parser.rs

+24-8
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ struct Parent<'tt> {
132132
/// <--------------> first submatcher; three tts, zero metavars
133133
/// <--------------------------> top-level matcher; two tts, one metavar
134134
/// ```
135-
#[derive(Clone)]
136135
struct MatcherPos<'tt> {
137136
/// The tokens that make up the current matcher. When we are within a `Sequence` or `Delimited`
138137
/// submatcher, this is just the contents of that submatcher.
@@ -177,6 +176,25 @@ impl<'tt> MatcherPos<'tt> {
177176
}
178177
}
179178

179+
fn empty_sequence(
180+
parent_mp: &MatcherPos<'tt>,
181+
seq: &'tt SequenceRepetition,
182+
empty_matches: Lrc<NamedMatchVec>,
183+
) -> Self {
184+
let mut mp = MatcherPos {
185+
tts: parent_mp.tts,
186+
idx: parent_mp.idx + 1,
187+
matches: parent_mp.matches.clone(), // a cheap clone
188+
seq_depth: parent_mp.seq_depth,
189+
match_cur: parent_mp.match_cur + seq.num_captures,
190+
kind: parent_mp.kind.clone(), // an expensive clone
191+
};
192+
for idx in parent_mp.match_cur..parent_mp.match_cur + seq.num_captures {
193+
mp.push_match(idx, MatchedSeq(empty_matches.clone()));
194+
}
195+
mp
196+
}
197+
180198
fn sequence(
181199
parent_mp: Box<MatcherPos<'tt>>,
182200
seq: &'tt SequenceRepetition,
@@ -468,13 +486,11 @@ impl<'tt> TtParser<'tt> {
468486
let op = seq.kleene.op;
469487
if op == mbe::KleeneOp::ZeroOrMore || op == mbe::KleeneOp::ZeroOrOne {
470488
// Allow for the possibility of zero matches of this sequence.
471-
let mut new_mp = mp.clone();
472-
new_mp.match_cur += seq.num_captures;
473-
new_mp.idx += 1;
474-
for idx in mp.match_cur..mp.match_cur + seq.num_captures {
475-
new_mp.push_match(idx, MatchedSeq(self.empty_matches.clone()));
476-
}
477-
self.cur_mps.push(new_mp);
489+
self.cur_mps.push(box MatcherPos::empty_sequence(
490+
&*mp,
491+
&seq,
492+
self.empty_matches.clone(),
493+
));
478494
}
479495

480496
// Allow for the possibility of one or more matches of this sequence.

0 commit comments

Comments
 (0)