Skip to content

Commit b229537

Browse files
authored
Rollup merge of #117212 - clubby789:fix-ternary-recover, r=compiler-errors
Properly restore snapshot when failing to recover parsing ternary If the recovery parsed an expression, then failed to eat a `:`, it would return `false` without restoring the snapshot. Fix this by always restoring the snapshot when returning `false`. Draft for now because I'd like to try and improve this recovery further. Fixes #117208
2 parents df8852a + e81a5c6 commit b229537

File tree

3 files changed

+35
-141
lines changed

3 files changed

+35
-141
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,10 @@ impl<'a> Parser<'a> {
504504

505505
// Special-case "expected `;`" errors
506506
if expected.contains(&TokenType::Token(token::Semi)) {
507-
if self.prev_token == token::Question && self.maybe_recover_from_ternary_operator() {
508-
return Ok(true);
507+
// If the user is trying to write a ternary expression, recover it and
508+
// return an Err to prevent a cascade of irrelevant diagnostics
509+
if self.prev_token == token::Question && let Err(e) = self.maybe_recover_from_ternary_operator() {
510+
return Err(e);
509511
}
510512

511513
if self.token.span == DUMMY_SP || self.prev_token.span == DUMMY_SP {
@@ -1428,10 +1430,10 @@ impl<'a> Parser<'a> {
14281430

14291431
/// Rust has no ternary operator (`cond ? then : else`). Parse it and try
14301432
/// to recover from it if `then` and `else` are valid expressions. Returns
1431-
/// whether it was a ternary operator.
1432-
pub(super) fn maybe_recover_from_ternary_operator(&mut self) -> bool {
1433+
/// an err if this appears to be a ternary expression.
1434+
pub(super) fn maybe_recover_from_ternary_operator(&mut self) -> PResult<'a, ()> {
14331435
if self.prev_token != token::Question {
1434-
return false;
1436+
return PResult::Ok(());
14351437
}
14361438

14371439
let lo = self.prev_token.span.lo();
@@ -1449,20 +1451,18 @@ impl<'a> Parser<'a> {
14491451
if self.eat_noexpect(&token::Colon) {
14501452
match self.parse_expr() {
14511453
Ok(_) => {
1452-
self.sess.emit_err(TernaryOperator { span: self.token.span.with_lo(lo) });
1453-
return true;
1454+
return Err(self
1455+
.sess
1456+
.create_err(TernaryOperator { span: self.token.span.with_lo(lo) }));
14541457
}
14551458
Err(err) => {
14561459
err.cancel();
1457-
self.restore_snapshot(snapshot);
14581460
}
14591461
};
14601462
}
1461-
} else {
1462-
self.restore_snapshot(snapshot);
1463-
};
1464-
1465-
false
1463+
}
1464+
self.restore_snapshot(snapshot);
1465+
Ok(())
14661466
}
14671467

14681468
pub(super) fn maybe_recover_from_bad_type_plus(&mut self, ty: &Ty) -> PResult<'a, ()> {

tests/ui/parser/ternary_operator.rs

+10-49
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,30 @@
1-
// A good chunk of these errors aren't shown to the user, but are still
2-
// required in the test for it to pass.
3-
4-
fn a() { //~ NOTE this function should return `Result` or `Option` to accept `?`
1+
fn a() {
52
let x = 5 > 2 ? true : false;
63
//~^ ERROR Rust has no ternary operator
74
//~| HELP use an `if-else` expression instead
8-
//~| ERROR the `?` operator can only be applied to values that implement `Try` [E0277]
9-
//~| HELP the trait `Try` is not implemented for `{integer}`
10-
//~| ERROR the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) [E0277]
11-
//~| HELP the trait `FromResidual<_>` is not implemented for `()`
12-
//~| NOTE in this expansion of desugaring of operator `?`
13-
//~| NOTE the `?` operator cannot be applied to type `{integer}`
14-
//~| NOTE in this expansion of desugaring of operator `?`
15-
//~| NOTE in this expansion of desugaring of operator `?`
16-
//~| NOTE cannot use the `?` operator in a function that returns `()`
17-
//~| NOTE in this expansion of desugaring of operator `?`
185
}
196

20-
fn b() { //~ NOTE this function should return `Result` or `Option` to accept `?`
7+
fn b() {
218
let x = 5 > 2 ? { true } : { false };
229
//~^ ERROR Rust has no ternary operator
2310
//~| HELP use an `if-else` expression instead
24-
//~| ERROR the `?` operator can only be applied to values that implement `Try` [E0277]
25-
//~| HELP the trait `Try` is not implemented for `{integer}`
26-
//~| ERROR the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) [E0277]
27-
//~| HELP the trait `FromResidual<_>` is not implemented for `()`
28-
//~| NOTE in this expansion of desugaring of operator `?`
29-
//~| NOTE the `?` operator cannot be applied to type `{integer}`
30-
//~| NOTE in this expansion of desugaring of operator `?`
31-
//~| NOTE in this expansion of desugaring of operator `?`
32-
//~| NOTE cannot use the `?` operator in a function that returns `()`
33-
//~| NOTE in this expansion of desugaring of operator `?`
3411
}
3512

36-
fn c() { //~ NOTE this function should return `Result` or `Option` to accept `?`
13+
fn c() {
3714
let x = 5 > 2 ? f32::MAX : f32::MIN;
3815
//~^ ERROR Rust has no ternary operator
3916
//~| HELP use an `if-else` expression instead
40-
//~| ERROR the `?` operator can only be applied to values that implement `Try` [E0277]
41-
//~| HELP the trait `Try` is not implemented for `{integer}`
42-
//~| ERROR the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) [E0277]
43-
//~| HELP the trait `FromResidual<_>` is not implemented for `()`
44-
//~| NOTE in this expansion of desugaring of operator `?`
45-
//~| NOTE the `?` operator cannot be applied to type `{integer}`
46-
//~| NOTE in this expansion of desugaring of operator `?`
47-
//~| NOTE in this expansion of desugaring of operator `?`
48-
//~| NOTE cannot use the `?` operator in a function that returns `()`
49-
//~| NOTE in this expansion of desugaring of operator `?`
5017
}
5118

52-
fn main() { //~ NOTE this function should return `Result` or `Option` to accept `?`
19+
fn bad() {
20+
// regression test for #117208
21+
v ? return;
22+
//~^ ERROR expected one of
23+
}
24+
25+
fn main() {
5326
let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false };
5427
//~^ ERROR Rust has no ternary operator
5528
//~| HELP use an `if-else` expression instead
5629
//~| ERROR expected one of `.`, `;`, `?`, `else`, or an operator, found `:`
57-
//~| NOTE expected one of `.`, `;`, `?`, `else`, or an operator
58-
//~| ERROR the `?` operator can only be applied to values that implement `Try` [E0277]
59-
//~| HELP the trait `Try` is not implemented for `{integer}`
60-
//~| ERROR the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) [E0277]
61-
//~| HELP the trait `FromResidual<_>` is not implemented for `()`
62-
//~| NOTE type ascription syntax has been removed, see issue #101728 <https://github.com/rust-lang/rust/issues/101728>
63-
//~| NOTE in this expansion of desugaring of operator `?`
64-
//~| NOTE the `?` operator cannot be applied to type `{integer}`
65-
//~| NOTE in this expansion of desugaring of operator `?`
66-
//~| NOTE in this expansion of desugaring of operator `?`
67-
//~| NOTE cannot use the `?` operator in a function that returns `()`
68-
//~| NOTE in this expansion of desugaring of operator `?`
6930
}
+12-79
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,48 @@
11
error: Rust has no ternary operator
2-
--> $DIR/ternary_operator.rs:5:19
2+
--> $DIR/ternary_operator.rs:2:19
33
|
44
LL | let x = 5 > 2 ? true : false;
55
| ^^^^^^^^^^^^^^^
66
|
77
= help: use an `if-else` expression instead
88

99
error: Rust has no ternary operator
10-
--> $DIR/ternary_operator.rs:21:19
10+
--> $DIR/ternary_operator.rs:8:19
1111
|
1212
LL | let x = 5 > 2 ? { true } : { false };
1313
| ^^^^^^^^^^^^^^^^^^^^^^^
1414
|
1515
= help: use an `if-else` expression instead
1616

1717
error: Rust has no ternary operator
18-
--> $DIR/ternary_operator.rs:37:19
18+
--> $DIR/ternary_operator.rs:14:19
1919
|
2020
LL | let x = 5 > 2 ? f32::MAX : f32::MIN;
2121
| ^^^^^^^^^^^^^^^^^^^^^^
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:21: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:26: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:26:19
3541
|
3642
LL | let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false };
3743
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3844
|
3945
= help: use an `if-else` expression instead
4046

41-
error[E0277]: the `?` operator can only be applied to values that implement `Try`
42-
--> $DIR/ternary_operator.rs:5:17
43-
|
44-
LL | let x = 5 > 2 ? true : false;
45-
| ^^^ the `?` operator cannot be applied to type `{integer}`
46-
|
47-
= help: the trait `Try` is not implemented for `{integer}`
48-
49-
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
50-
--> $DIR/ternary_operator.rs:5:19
51-
|
52-
LL | fn a() {
53-
| ------ this function should return `Result` or `Option` to accept `?`
54-
LL | let x = 5 > 2 ? true : false;
55-
| ^ cannot use the `?` operator in a function that returns `()`
56-
|
57-
= help: the trait `FromResidual<_>` is not implemented for `()`
58-
59-
error[E0277]: the `?` operator can only be applied to values that implement `Try`
60-
--> $DIR/ternary_operator.rs:21:17
61-
|
62-
LL | let x = 5 > 2 ? { true } : { false };
63-
| ^^^ the `?` operator cannot be applied to type `{integer}`
64-
|
65-
= help: the trait `Try` is not implemented for `{integer}`
66-
67-
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
68-
--> $DIR/ternary_operator.rs:21:19
69-
|
70-
LL | fn b() {
71-
| ------ this function should return `Result` or `Option` to accept `?`
72-
LL | let x = 5 > 2 ? { true } : { false };
73-
| ^ cannot use the `?` operator in a function that returns `()`
74-
|
75-
= help: the trait `FromResidual<_>` is not implemented for `()`
76-
77-
error[E0277]: the `?` operator can only be applied to values that implement `Try`
78-
--> $DIR/ternary_operator.rs:37:17
79-
|
80-
LL | let x = 5 > 2 ? f32::MAX : f32::MIN;
81-
| ^^^ the `?` operator cannot be applied to type `{integer}`
82-
|
83-
= help: the trait `Try` is not implemented for `{integer}`
84-
85-
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
86-
--> $DIR/ternary_operator.rs:37:19
87-
|
88-
LL | fn c() {
89-
| ------ this function should return `Result` or `Option` to accept `?`
90-
LL | let x = 5 > 2 ? f32::MAX : f32::MIN;
91-
| ^ cannot use the `?` operator in a function that returns `()`
92-
|
93-
= help: the trait `FromResidual<_>` is not implemented for `()`
94-
95-
error[E0277]: the `?` operator can only be applied to values that implement `Try`
96-
--> $DIR/ternary_operator.rs:53:17
97-
|
98-
LL | let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false };
99-
| ^^^ the `?` operator cannot be applied to type `{integer}`
100-
|
101-
= help: the trait `Try` is not implemented for `{integer}`
102-
103-
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
105-
|
106-
LL | fn main() {
107-
| --------- this function should return `Result` or `Option` to accept `?`
108-
LL | let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false };
109-
| ^ cannot use the `?` operator in a function that returns `()`
110-
|
111-
= help: the trait `FromResidual<_>` is not implemented for `()`
112-
113-
error: aborting due to 13 previous errors
47+
error: aborting due to 6 previous errors
11448

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

0 commit comments

Comments
 (0)