Skip to content

Commit 041f031

Browse files
committed
Properly restore snapshot when failing to recover parsing ternary
1 parent 056f5b0 commit 041f031

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1454,13 +1454,11 @@ impl<'a> Parser<'a> {
14541454
}
14551455
Err(err) => {
14561456
err.cancel();
1457-
self.restore_snapshot(snapshot);
14581457
}
14591458
};
14601459
}
1461-
} else {
1462-
self.restore_snapshot(snapshot);
1463-
};
1460+
}
1461+
self.restore_snapshot(snapshot);
14641462

14651463
false
14661464
}

tests/ui/parser/ternary_operator.rs

+7
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ fn c() { //~ NOTE this function should return `Result` or `Option` to accept `?`
4949
//~| NOTE in this expansion of desugaring of operator `?`
5050
}
5151

52+
fn bad() {
53+
// regression test for #117208
54+
v ? return;
55+
//~^ ERROR expected one of
56+
//~| NOTE expected one of
57+
}
58+
5259
fn main() { //~ NOTE this function should return `Result` or `Option` to accept `?`
5360
let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false };
5461
//~^ ERROR Rust has no ternary operator

tests/ui/parser/ternary_operator.stderr

+11-5
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,22 @@ LL | let x = 5 > 2 ? f32::MAX : f32::MIN;
2222
|
2323
= help: use an `if-else` expression instead
2424

25+
error: expected one of `.`, `;`, `?`, `}`, or an operator, found keyword `return`
26+
--> $DIR/ternary_operator.rs:54:9
27+
|
28+
LL | v ? return;
29+
| ^^^^^^ expected one of `.`, `;`, `?`, `}`, or an operator
30+
2531
error: expected one of `.`, `;`, `?`, `else`, or an operator, found `:`
26-
--> $DIR/ternary_operator.rs:53:37
32+
--> $DIR/ternary_operator.rs:60:37
2733
|
2834
LL | let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false };
2935
| ^ expected one of `.`, `;`, `?`, `else`, or an operator
3036
|
3137
= note: type ascription syntax has been removed, see issue #101728 <https://github.com/rust-lang/rust/issues/101728>
3238

3339
error: Rust has no ternary operator
34-
--> $DIR/ternary_operator.rs:53:19
40+
--> $DIR/ternary_operator.rs:60:19
3541
|
3642
LL | let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false };
3743
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -93,15 +99,15 @@ LL | let x = 5 > 2 ? f32::MAX : f32::MIN;
9399
= help: the trait `FromResidual<_>` is not implemented for `()`
94100

95101
error[E0277]: the `?` operator can only be applied to values that implement `Try`
96-
--> $DIR/ternary_operator.rs:53:17
102+
--> $DIR/ternary_operator.rs:60:17
97103
|
98104
LL | let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false };
99105
| ^^^ the `?` operator cannot be applied to type `{integer}`
100106
|
101107
= help: the trait `Try` is not implemented for `{integer}`
102108

103109
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
104-
--> $DIR/ternary_operator.rs:53:19
110+
--> $DIR/ternary_operator.rs:60:19
105111
|
106112
LL | fn main() {
107113
| --------- this function should return `Result` or `Option` to accept `?`
@@ -110,6 +116,6 @@ LL | let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false };
110116
|
111117
= help: the trait `FromResidual<_>` is not implemented for `()`
112118

113-
error: aborting due to 13 previous errors
119+
error: aborting due to 14 previous errors
114120

115121
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)