Skip to content

Commit 9a65322

Browse files
committed
replace self.clone() with self.create_snapshot_for_diagnostic()
1 parent 2db8236 commit 9a65322

File tree

4 files changed

+36
-36
lines changed

4 files changed

+36
-36
lines changed

compiler/rustc_parse/src/parser/attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl<'a> Parser<'a> {
151151
span: Span,
152152
attr_type: OuterAttributeType,
153153
) -> Option<Span> {
154-
let mut snapshot = self.clone();
154+
let mut snapshot = self.create_snapshot_for_diagnostic();
155155
let lo = span.lo()
156156
+ BytePos(match attr_type {
157157
OuterAttributeType::Attribute => 1,

compiler/rustc_parse/src/parser/diagnostics.rs

+22-22
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ impl<'a> Parser<'a> {
481481
// fn foo() -> Foo {
482482
// field: value,
483483
// }
484-
let mut snapshot = self.clone();
484+
let mut snapshot = self.create_snapshot_for_diagnostic();
485485
let path =
486486
Path { segments: vec![], span: self.prev_token.span.shrink_to_lo(), tokens: None };
487487
let struct_expr = snapshot.parse_struct_expr(None, path, AttrVec::new(), false);
@@ -507,7 +507,7 @@ impl<'a> Parser<'a> {
507507
Applicability::MaybeIncorrect,
508508
)
509509
.emit();
510-
*self = snapshot;
510+
self.restore_snapshot(snapshot);
511511
let mut tail = self.mk_block(
512512
vec![self.mk_stmt_err(expr.span)],
513513
s,
@@ -721,7 +721,7 @@ impl<'a> Parser<'a> {
721721
/// angle brackets.
722722
pub(super) fn check_turbofish_missing_angle_brackets(&mut self, segment: &mut PathSegment) {
723723
if token::ModSep == self.token.kind && segment.args.is_none() {
724-
let snapshot = self.clone();
724+
let snapshot = self.create_snapshot_for_diagnostic();
725725
self.bump();
726726
let lo = self.token.span;
727727
match self.parse_angle_args(None) {
@@ -755,14 +755,14 @@ impl<'a> Parser<'a> {
755755
.emit();
756756
} else {
757757
// This doesn't look like an invalid turbofish, can't recover parse state.
758-
*self = snapshot;
758+
self.restore_snapshot(snapshot);
759759
}
760760
}
761761
Err(err) => {
762762
// We couldn't parse generic parameters, unlikely to be a turbofish. Rely on
763763
// generic parse error instead.
764764
err.cancel();
765-
*self = snapshot;
765+
self.restore_snapshot(snapshot);
766766
}
767767
}
768768
}
@@ -868,7 +868,7 @@ impl<'a> Parser<'a> {
868868
// `x == y < z`
869869
(BinOpKind::Eq, AssocOp::Less | AssocOp::LessEqual | AssocOp::Greater | AssocOp::GreaterEqual) => {
870870
// Consume `z`/outer-op-rhs.
871-
let snapshot = self.clone();
871+
let snapshot = self.create_snapshot_for_diagnostic();
872872
match self.parse_expr() {
873873
Ok(r2) => {
874874
// We are sure that outer-op-rhs could be consumed, the suggestion is
@@ -878,14 +878,14 @@ impl<'a> Parser<'a> {
878878
}
879879
Err(expr_err) => {
880880
expr_err.cancel();
881-
*self = snapshot;
881+
self.restore_snapshot(snapshot);
882882
false
883883
}
884884
}
885885
}
886886
// `x > y == z`
887887
(BinOpKind::Lt | BinOpKind::Le | BinOpKind::Gt | BinOpKind::Ge, AssocOp::Equal) => {
888-
let snapshot = self.clone();
888+
let snapshot = self.create_snapshot_for_diagnostic();
889889
// At this point it is always valid to enclose the lhs in parentheses, no
890890
// further checks are necessary.
891891
match self.parse_expr() {
@@ -895,7 +895,7 @@ impl<'a> Parser<'a> {
895895
}
896896
Err(expr_err) => {
897897
expr_err.cancel();
898-
*self = snapshot;
898+
self.restore_snapshot(snapshot);
899899
false
900900
}
901901
}
@@ -960,7 +960,7 @@ impl<'a> Parser<'a> {
960960
|| outer_op.node == AssocOp::Greater
961961
{
962962
if outer_op.node == AssocOp::Less {
963-
let snapshot = self.clone();
963+
let snapshot = self.create_snapshot_for_diagnostic();
964964
self.bump();
965965
// So far we have parsed `foo<bar<`, consume the rest of the type args.
966966
let modifiers =
@@ -972,15 +972,15 @@ impl<'a> Parser<'a> {
972972
{
973973
// We don't have `foo< bar >(` or `foo< bar >::`, so we rewind the
974974
// parser and bail out.
975-
*self = snapshot.clone();
975+
self.restore_snapshot(snapshot);
976976
}
977977
}
978978
return if token::ModSep == self.token.kind {
979979
// We have some certainty that this was a bad turbofish at this point.
980980
// `foo< bar >::`
981981
suggest(&mut err);
982982

983-
let snapshot = self.clone();
983+
let snapshot = self.create_snapshot_for_diagnostic();
984984
self.bump(); // `::`
985985

986986
// Consume the rest of the likely `foo<bar>::new()` or return at `foo<bar>`.
@@ -997,7 +997,7 @@ impl<'a> Parser<'a> {
997997
expr_err.cancel();
998998
// Not entirely sure now, but we bubble the error up with the
999999
// suggestion.
1000-
*self = snapshot;
1000+
self.restore_snapshot(snapshot);
10011001
Err(err)
10021002
}
10031003
}
@@ -1051,7 +1051,7 @@ impl<'a> Parser<'a> {
10511051
}
10521052

10531053
fn consume_fn_args(&mut self) -> Result<(), ()> {
1054-
let snapshot = self.clone();
1054+
let snapshot = self.create_snapshot_for_diagnostic();
10551055
self.bump(); // `(`
10561056

10571057
// Consume the fn call arguments.
@@ -1061,7 +1061,7 @@ impl<'a> Parser<'a> {
10611061

10621062
if self.token.kind == token::Eof {
10631063
// Not entirely sure that what we consumed were fn arguments, rollback.
1064-
*self = snapshot;
1064+
self.restore_snapshot(snapshot);
10651065
Err(())
10661066
} else {
10671067
// 99% certain that the suggestion is correct, continue parsing.
@@ -2002,12 +2002,12 @@ impl<'a> Parser<'a> {
20022002
}
20032003

20042004
fn recover_const_param_decl(&mut self, ty_generics: Option<&Generics>) -> Option<GenericArg> {
2005-
let snapshot = self.clone();
2005+
let snapshot = self.create_snapshot_for_diagnostic();
20062006
let param = match self.parse_const_param(vec![]) {
20072007
Ok(param) => param,
20082008
Err(err) => {
20092009
err.cancel();
2010-
*self = snapshot;
2010+
self.restore_snapshot(snapshot);
20112011
return None;
20122012
}
20132013
};
@@ -2099,7 +2099,7 @@ impl<'a> Parser<'a> {
20992099
// We perform these checks and early return to avoid taking a snapshot unnecessarily.
21002100
return Err(err);
21012101
}
2102-
let snapshot = self.clone();
2102+
let snapshot = self.create_snapshot_for_diagnostic();
21032103
if is_op_or_dot {
21042104
self.bump();
21052105
}
@@ -2131,7 +2131,7 @@ impl<'a> Parser<'a> {
21312131
err.cancel();
21322132
}
21332133
}
2134-
*self = snapshot;
2134+
self.restore_snapshot(snapshot);
21352135
Err(err)
21362136
}
21372137

@@ -2191,7 +2191,7 @@ impl<'a> Parser<'a> {
21912191
let span = self.token.span;
21922192
// We only emit "unexpected `:`" error here if we can successfully parse the
21932193
// whole pattern correctly in that case.
2194-
let snapshot = self.clone();
2194+
let snapshot = self.create_snapshot_for_diagnostic();
21952195

21962196
// Create error for "unexpected `:`".
21972197
match self.expected_one_of_not_found(&[], &[]) {
@@ -2203,7 +2203,7 @@ impl<'a> Parser<'a> {
22032203
// reasonable error.
22042204
inner_err.cancel();
22052205
err.cancel();
2206-
*self = snapshot;
2206+
self.restore_snapshot(snapshot);
22072207
}
22082208
Ok(mut pat) => {
22092209
// We've parsed the rest of the pattern.
@@ -2282,7 +2282,7 @@ impl<'a> Parser<'a> {
22822282
}
22832283
_ => {
22842284
// Carry on as if we had not done anything. This should be unreachable.
2285-
*self = snapshot;
2285+
self.restore_snapshot(snapshot);
22862286
}
22872287
};
22882288
first_pat

compiler/rustc_parse/src/parser/expr.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ impl<'a> Parser<'a> {
704704
ExprKind::Path(None, ast::Path { segments, .. }),
705705
TokenKind::Ident(kw::For | kw::Loop | kw::While, false),
706706
) if segments.len() == 1 => {
707-
let snapshot = self.clone();
707+
let snapshot = self.create_snapshot_for_diagnostic();
708708
let label = Label {
709709
ident: Ident::from_str_and_span(
710710
&format!("'{}", segments[0].ident),
@@ -726,7 +726,7 @@ impl<'a> Parser<'a> {
726726
}
727727
Err(err) => {
728728
err.cancel();
729-
*self = snapshot;
729+
self.restore_snapshot(snapshot);
730730
}
731731
}
732732
}
@@ -1886,7 +1886,7 @@ impl<'a> Parser<'a> {
18861886
lo: Span,
18871887
attrs: AttrVec,
18881888
) -> Option<P<Expr>> {
1889-
let mut snapshot = self.clone();
1889+
let mut snapshot = self.create_snapshot_for_diagnostic();
18901890
match snapshot.parse_array_or_repeat_expr(attrs, token::Brace) {
18911891
Ok(arr) => {
18921892
let hi = snapshot.prev_token.span;
@@ -1902,7 +1902,7 @@ impl<'a> Parser<'a> {
19021902
.note("to define an array, one would use square brackets instead of curly braces")
19031903
.emit();
19041904

1905-
*self = snapshot;
1905+
self.restore_snapshot(snapshot);
19061906
Some(self.mk_expr_err(arr.span))
19071907
}
19081908
Err(e) => {
@@ -2370,7 +2370,7 @@ impl<'a> Parser<'a> {
23702370
if self.token.kind != token::Semi {
23712371
return None;
23722372
}
2373-
let start_snapshot = self.clone();
2373+
let start_snapshot = self.create_snapshot_for_diagnostic();
23742374
let semi_sp = self.token.span;
23752375
self.bump(); // `;`
23762376
let mut stmts =
@@ -2418,15 +2418,15 @@ impl<'a> Parser<'a> {
24182418
return Some(err(self, stmts));
24192419
}
24202420
if self.token.kind == token::Comma {
2421-
*self = start_snapshot;
2421+
self.restore_snapshot(start_snapshot);
24222422
return None;
24232423
}
2424-
let pre_pat_snapshot = self.clone();
2424+
let pre_pat_snapshot = self.create_snapshot_for_diagnostic();
24252425
match self.parse_pat_no_top_alt(None) {
24262426
Ok(_pat) => {
24272427
if self.token.kind == token::FatArrow {
24282428
// Reached arm end.
2429-
*self = pre_pat_snapshot;
2429+
self.restore_snapshot(pre_pat_snapshot);
24302430
return Some(err(self, stmts));
24312431
}
24322432
}
@@ -2435,21 +2435,21 @@ impl<'a> Parser<'a> {
24352435
}
24362436
}
24372437

2438-
*self = pre_pat_snapshot;
2438+
self.restore_snapshot(pre_pat_snapshot);
24392439
match self.parse_stmt_without_recovery(true, ForceCollect::No) {
24402440
// Consume statements for as long as possible.
24412441
Ok(Some(stmt)) => {
24422442
stmts.push(stmt);
24432443
}
24442444
Ok(None) => {
2445-
*self = start_snapshot;
2445+
self.restore_snapshot(start_snapshot);
24462446
break;
24472447
}
24482448
// We couldn't parse either yet another statement missing it's
24492449
// enclosing block nor the next arm's pattern or closing brace.
24502450
Err(stmt_err) => {
24512451
stmt_err.cancel();
2452-
*self = start_snapshot;
2452+
self.restore_snapshot(start_snapshot);
24532453
break;
24542454
}
24552455
}

compiler/rustc_parse/src/parser/path.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ impl<'a> Parser<'a> {
645645
} else {
646646
// Fall back by trying to parse a const-expr expression. If we successfully do so,
647647
// then we should report an error that it needs to be wrapped in braces.
648-
let snapshot = self.clone();
648+
let snapshot = self.create_snapshot_for_diagnostic();
649649
match self.parse_expr_res(Restrictions::CONST_EXPR, None) {
650650
Ok(expr) => {
651651
return Ok(Some(self.dummy_const_arg_needs_braces(
@@ -654,7 +654,7 @@ impl<'a> Parser<'a> {
654654
)));
655655
}
656656
Err(err) => {
657-
*self = snapshot;
657+
self.restore_snapshot(snapshot);
658658
err.cancel();
659659
return Ok(None);
660660
}

0 commit comments

Comments
 (0)