Properly restore snapshot when failing to recover parsing ternary

This commit is contained in:
clubby789 2023-10-26 11:11:36 +00:00
parent 056f5b0f13
commit 041f0313cf
3 changed files with 20 additions and 9 deletions

View File

@ -1454,13 +1454,11 @@ impl<'a> Parser<'a> {
} }
Err(err) => { Err(err) => {
err.cancel(); err.cancel();
self.restore_snapshot(snapshot);
} }
}; };
} }
} else { }
self.restore_snapshot(snapshot); self.restore_snapshot(snapshot);
};
false false
} }

View File

@ -49,6 +49,13 @@ fn c() { //~ NOTE this function should return `Result` or `Option` to accept `?`
//~| NOTE in this expansion of desugaring of operator `?` //~| NOTE in this expansion of desugaring of operator `?`
} }
fn bad() {
// regression test for #117208
v ? return;
//~^ ERROR expected one of
//~| NOTE expected one of
}
fn main() { //~ NOTE this function should return `Result` or `Option` to accept `?` fn main() { //~ NOTE this function should return `Result` or `Option` to accept `?`
let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false }; let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false };
//~^ ERROR Rust has no ternary operator //~^ ERROR Rust has no ternary operator

View File

@ -22,8 +22,14 @@ LL | let x = 5 > 2 ? f32::MAX : f32::MIN;
| |
= help: use an `if-else` expression instead = help: use an `if-else` expression instead
error: expected one of `.`, `;`, `?`, `}`, or an operator, found keyword `return`
--> $DIR/ternary_operator.rs:54:9
|
LL | v ? return;
| ^^^^^^ expected one of `.`, `;`, `?`, `}`, or an operator
error: expected one of `.`, `;`, `?`, `else`, or an operator, found `:` error: expected one of `.`, `;`, `?`, `else`, or an operator, found `:`
--> $DIR/ternary_operator.rs:53:37 --> $DIR/ternary_operator.rs:60:37
| |
LL | let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false }; LL | let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false };
| ^ expected one of `.`, `;`, `?`, `else`, or an operator | ^ expected one of `.`, `;`, `?`, `else`, or an operator
@ -31,7 +37,7 @@ LL | let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false };
= note: type ascription syntax has been removed, see issue #101728 <https://github.com/rust-lang/rust/issues/101728> = note: type ascription syntax has been removed, see issue #101728 <https://github.com/rust-lang/rust/issues/101728>
error: Rust has no ternary operator error: Rust has no ternary operator
--> $DIR/ternary_operator.rs:53:19 --> $DIR/ternary_operator.rs:60:19
| |
LL | let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false }; LL | let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -93,7 +99,7 @@ LL | let x = 5 > 2 ? f32::MAX : f32::MIN;
= help: the trait `FromResidual<_>` is not implemented for `()` = help: the trait `FromResidual<_>` is not implemented for `()`
error[E0277]: the `?` operator can only be applied to values that implement `Try` error[E0277]: the `?` operator can only be applied to values that implement `Try`
--> $DIR/ternary_operator.rs:53:17 --> $DIR/ternary_operator.rs:60:17
| |
LL | let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false }; LL | let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false };
| ^^^ the `?` operator cannot be applied to type `{integer}` | ^^^ the `?` operator cannot be applied to type `{integer}`
@ -101,7 +107,7 @@ LL | let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false };
= help: the trait `Try` is not implemented for `{integer}` = help: the trait `Try` is not implemented for `{integer}`
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
--> $DIR/ternary_operator.rs:53:19 --> $DIR/ternary_operator.rs:60:19
| |
LL | fn main() { LL | fn main() {
| --------- this function should return `Result` or `Option` to accept `?` | --------- 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 };
| |
= help: the trait `FromResidual<_>` is not implemented for `()` = help: the trait `FromResidual<_>` is not implemented for `()`
error: aborting due to 13 previous errors error: aborting due to 14 previous errors
For more information about this error, try `rustc --explain E0277`. For more information about this error, try `rustc --explain E0277`.