Skip to content
/ rust Public
forked from rust-lang/rust

Commit faa7395

Browse files
committed
Remove unnecessary maybe_ternary_lo field
1 parent 1648180 commit faa7395

File tree

5 files changed

+55
-37
lines changed

5 files changed

+55
-37
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -501,10 +501,8 @@ impl<'a> Parser<'a> {
501501
// Special-case "expected `;`" errors
502502
if expected.contains(&TokenType::Token(token::Semi)) {
503503
if self.prev_token.kind == token::Question {
504-
self.maybe_ternary_lo = Some(self.prev_token.span.lo());
505-
let result = self.maybe_recover_from_ternary_operator().map(|_| true);
506-
self.maybe_ternary_lo = None;
507-
return result;
504+
self.maybe_recover_from_ternary_operator();
505+
return Ok(true);
508506
}
509507

510508
if self.token.span == DUMMY_SP || self.prev_token.span == DUMMY_SP {
@@ -1339,7 +1337,7 @@ impl<'a> Parser<'a> {
13391337

13401338
/// Rust has no ternary operator (`cond ? then : else`). Parse it and try
13411339
/// to recover from it if `then` and `else` are valid expressions.
1342-
pub(super) fn maybe_recover_from_ternary_operator(&mut self) -> PResult<'a, ()> {
1340+
pub(super) fn maybe_recover_from_ternary_operator(&mut self) {
13431341
let snapshot = self.create_snapshot_for_diagnostic();
13441342
let lo = self.prev_token.span.lo();
13451343

@@ -1368,8 +1366,6 @@ impl<'a> Parser<'a> {
13681366
} else {
13691367
self.restore_snapshot(snapshot);
13701368
};
1371-
1372-
Ok(())
13731369
}
13741370

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

compiler/rustc_parse/src/parser/mod.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ use rustc_errors::{
3737
use rustc_session::parse::ParseSess;
3838
use rustc_span::source_map::{Span, DUMMY_SP};
3939
use rustc_span::symbol::{kw, sym, Ident, Symbol};
40-
use rustc_span::BytePos;
4140
use std::ops::Range;
4241
use std::{cmp, mem, slice};
4342
use thin_vec::ThinVec;
@@ -158,17 +157,12 @@ pub struct Parser<'a> {
158157
/// Whether the parser is allowed to do recovery.
159158
/// This is disabled when parsing macro arguments, see #103534
160159
pub recovery: Recovery,
161-
/// The low part of a ternary operator (`cond ? then : else`).
162-
/// FIXME(Centri3): This is currently only used so that type ascription is
163-
/// not mentioned in the error. Once the error in `stmt.rs` is removed, this
164-
/// can be removed.
165-
maybe_ternary_lo: Option<BytePos>,
166160
}
167161

168162
// This type is used a lot, e.g. it's cloned when matching many declarative macro rules with nonterminals. Make sure
169163
// it doesn't unintentionally get bigger.
170164
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
171-
rustc_data_structures::static_assert_size!(Parser<'_>, 280);
165+
rustc_data_structures::static_assert_size!(Parser<'_>, 272);
172166

173167
/// Stores span information about a closure.
174168
#[derive(Clone)]
@@ -481,7 +475,6 @@ impl<'a> Parser<'a> {
481475
},
482476
current_closure: None,
483477
recovery: Recovery::Allowed,
484-
maybe_ternary_lo: None,
485478
};
486479

487480
// Make parser point to the first token.

compiler/rustc_parse/src/parser/stmt.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -576,9 +576,7 @@ impl<'a> Parser<'a> {
576576
Applicability::MaybeIncorrect,
577577
);
578578
}
579-
if self.sess.unstable_features.is_nightly_build()
580-
&& self.maybe_ternary_lo.is_none()
581-
{
579+
if self.sess.unstable_features.is_nightly_build() {
582580
// FIXME(Nilstrieb): Remove this again after a few months.
583581
err.note("type ascription syntax has been removed, see issue #101728 <https://github.com/rust-lang/rust/issues/101728>");
584582
}

tests/ui/parser/ternary_operator.rs

+35-6
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,69 @@
1-
fn a() {
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 `?`
25
let x = 5 > 2 ? true : false;
36
//~^ ERROR Rust has no ternary operator
47
//~| HELP use an `if-else` expression instead
58
//~| ERROR the `?` operator can only be applied to values that implement `Try` [E0277]
69
//~| HELP the trait `Try` is not implemented for `{integer}`
710
//~| ERROR the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) [E0277]
811
//~| 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 `?`
918
}
1019

11-
fn b() {
20+
fn b() { //~ NOTE this function should return `Result` or `Option` to accept `?`
1221
let x = 5 > 2 ? { true } : { false };
1322
//~^ ERROR Rust has no ternary operator
1423
//~| HELP use an `if-else` expression instead
1524
//~| ERROR the `?` operator can only be applied to values that implement `Try` [E0277]
1625
//~| HELP the trait `Try` is not implemented for `{integer}`
1726
//~| ERROR the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) [E0277]
1827
//~| 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 `?`
1934
}
2035

21-
fn c() {
36+
fn c() { //~ NOTE this function should return `Result` or `Option` to accept `?`
2237
let x = 5 > 2 ? f32::MAX : f32::MIN;
2338
//~^ ERROR Rust has no ternary operator
2439
//~| HELP use an `if-else` expression instead
2540
//~| ERROR the `?` operator can only be applied to values that implement `Try` [E0277]
2641
//~| HELP the trait `Try` is not implemented for `{integer}`
2742
//~| ERROR the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) [E0277]
2843
//~| 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 `?`
2950
}
3051

31-
fn main() {
52+
fn main() { //~ NOTE this function should return `Result` or `Option` to accept `?`
3253
let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false };
3354
//~^ ERROR Rust has no ternary operator
3455
//~| HELP use an `if-else` expression instead
35-
//~| expected one of `.`, `;`, `?`, `else`, or an operator, found `:`
36-
//~| ERROR the `?` operator can only be applied to values that implement `Try` [E0277]
56+
//~| 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]
3759
//~| HELP the trait `Try` is not implemented for `{integer}`
3860
//~| ERROR the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) [E0277]
3961
//~| 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 `?`
4069
}

tests/ui/parser/ternary_operator.stderr

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,53 @@
11
error: Rust has no ternary operator
2-
--> $DIR/ternary_operator.rs:2:19
2+
--> $DIR/ternary_operator.rs:5: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:12:19
10+
--> $DIR/ternary_operator.rs:21: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:22:19
18+
--> $DIR/ternary_operator.rs:37:19
1919
|
2020
LL | let x = 5 > 2 ? f32::MAX : f32::MIN;
2121
| ^^^^^^^^^^^^^^^^^^^^^^
2222
|
2323
= help: use an `if-else` expression instead
2424

2525
error: expected one of `.`, `;`, `?`, `else`, or an operator, found `:`
26-
--> $DIR/ternary_operator.rs:32:37
26+
--> $DIR/ternary_operator.rs:53:37
2727
|
2828
LL | let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false };
2929
| ^ expected one of `.`, `;`, `?`, `else`, or an operator
30+
|
31+
= note: type ascription syntax has been removed, see issue #101728 <https://github.com/rust-lang/rust/issues/101728>
3032

3133
error: Rust has no ternary operator
32-
--> $DIR/ternary_operator.rs:32:19
34+
--> $DIR/ternary_operator.rs:53:19
3335
|
3436
LL | let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false };
3537
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3638
|
3739
= help: use an `if-else` expression instead
3840

3941
error[E0277]: the `?` operator can only be applied to values that implement `Try`
40-
--> $DIR/ternary_operator.rs:2:17
42+
--> $DIR/ternary_operator.rs:5:17
4143
|
4244
LL | let x = 5 > 2 ? true : false;
4345
| ^^^ the `?` operator cannot be applied to type `{integer}`
4446
|
4547
= help: the trait `Try` is not implemented for `{integer}`
4648

4749
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
48-
--> $DIR/ternary_operator.rs:2:19
50+
--> $DIR/ternary_operator.rs:5:19
4951
|
5052
LL | fn a() {
5153
| ------ this function should return `Result` or `Option` to accept `?`
@@ -55,15 +57,15 @@ LL | let x = 5 > 2 ? true : false;
5557
= help: the trait `FromResidual<_>` is not implemented for `()`
5658

5759
error[E0277]: the `?` operator can only be applied to values that implement `Try`
58-
--> $DIR/ternary_operator.rs:12:17
60+
--> $DIR/ternary_operator.rs:21:17
5961
|
6062
LL | let x = 5 > 2 ? { true } : { false };
6163
| ^^^ the `?` operator cannot be applied to type `{integer}`
6264
|
6365
= help: the trait `Try` is not implemented for `{integer}`
6466

6567
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
66-
--> $DIR/ternary_operator.rs:12:19
68+
--> $DIR/ternary_operator.rs:21:19
6769
|
6870
LL | fn b() {
6971
| ------ this function should return `Result` or `Option` to accept `?`
@@ -73,15 +75,15 @@ LL | let x = 5 > 2 ? { true } : { false };
7375
= help: the trait `FromResidual<_>` is not implemented for `()`
7476

7577
error[E0277]: the `?` operator can only be applied to values that implement `Try`
76-
--> $DIR/ternary_operator.rs:22:17
78+
--> $DIR/ternary_operator.rs:37:17
7779
|
7880
LL | let x = 5 > 2 ? f32::MAX : f32::MIN;
7981
| ^^^ the `?` operator cannot be applied to type `{integer}`
8082
|
8183
= help: the trait `Try` is not implemented for `{integer}`
8284

8385
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
84-
--> $DIR/ternary_operator.rs:22:19
86+
--> $DIR/ternary_operator.rs:37:19
8587
|
8688
LL | fn c() {
8789
| ------ this function should return `Result` or `Option` to accept `?`
@@ -91,15 +93,15 @@ LL | let x = 5 > 2 ? f32::MAX : f32::MIN;
9193
= help: the trait `FromResidual<_>` is not implemented for `()`
9294

9395
error[E0277]: the `?` operator can only be applied to values that implement `Try`
94-
--> $DIR/ternary_operator.rs:32:17
96+
--> $DIR/ternary_operator.rs:53:17
9597
|
9698
LL | let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false };
9799
| ^^^ the `?` operator cannot be applied to type `{integer}`
98100
|
99101
= help: the trait `Try` is not implemented for `{integer}`
100102

101103
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
102-
--> $DIR/ternary_operator.rs:32:19
104+
--> $DIR/ternary_operator.rs:53:19
103105
|
104106
LL | fn main() {
105107
| --------- this function should return `Result` or `Option` to accept `?`

0 commit comments

Comments
 (0)