-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Gracefully handle ternary operator #114028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// A good chunk of these errors aren't shown to the user, but are still | ||
// required in the test for it to pass. | ||
|
||
fn a() { //~ NOTE this function should return `Result` or `Option` to accept `?` | ||
let x = 5 > 2 ? true : false; | ||
//~^ ERROR Rust has no ternary operator | ||
//~| HELP use an `if-else` expression instead | ||
//~| ERROR the `?` operator can only be applied to values that implement `Try` [E0277] | ||
//~| HELP the trait `Try` is not implemented for `{integer}` | ||
//~| ERROR the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) [E0277] | ||
//~| HELP the trait `FromResidual<_>` is not implemented for `()` | ||
//~| NOTE in this expansion of desugaring of operator `?` | ||
//~| NOTE the `?` operator cannot be applied to type `{integer}` | ||
//~| NOTE in this expansion of desugaring of operator `?` | ||
//~| NOTE in this expansion of desugaring of operator `?` | ||
//~| NOTE cannot use the `?` operator in a function that returns `()` | ||
//~| NOTE in this expansion of desugaring of operator `?` | ||
} | ||
|
||
fn b() { //~ NOTE this function should return `Result` or `Option` to accept `?` | ||
let x = 5 > 2 ? { true } : { false }; | ||
//~^ ERROR Rust has no ternary operator | ||
//~| HELP use an `if-else` expression instead | ||
//~| ERROR the `?` operator can only be applied to values that implement `Try` [E0277] | ||
//~| HELP the trait `Try` is not implemented for `{integer}` | ||
//~| ERROR the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) [E0277] | ||
//~| HELP the trait `FromResidual<_>` is not implemented for `()` | ||
//~| NOTE in this expansion of desugaring of operator `?` | ||
//~| NOTE the `?` operator cannot be applied to type `{integer}` | ||
//~| NOTE in this expansion of desugaring of operator `?` | ||
//~| NOTE in this expansion of desugaring of operator `?` | ||
//~| NOTE cannot use the `?` operator in a function that returns `()` | ||
//~| NOTE in this expansion of desugaring of operator `?` | ||
} | ||
|
||
fn c() { //~ NOTE this function should return `Result` or `Option` to accept `?` | ||
let x = 5 > 2 ? f32::MAX : f32::MIN; | ||
//~^ ERROR Rust has no ternary operator | ||
//~| HELP use an `if-else` expression instead | ||
//~| ERROR the `?` operator can only be applied to values that implement `Try` [E0277] | ||
//~| HELP the trait `Try` is not implemented for `{integer}` | ||
//~| ERROR the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) [E0277] | ||
//~| HELP the trait `FromResidual<_>` is not implemented for `()` | ||
//~| NOTE in this expansion of desugaring of operator `?` | ||
//~| NOTE the `?` operator cannot be applied to type `{integer}` | ||
//~| NOTE in this expansion of desugaring of operator `?` | ||
//~| NOTE in this expansion of desugaring of operator `?` | ||
//~| NOTE cannot use the `?` operator in a function that returns `()` | ||
//~| NOTE in this expansion of desugaring of operator `?` | ||
} | ||
|
||
fn main() { //~ NOTE this function should return `Result` or `Option` to accept `?` | ||
let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false }; | ||
//~^ ERROR Rust has no ternary operator | ||
//~| HELP use an `if-else` expression instead | ||
//~| ERROR expected one of `.`, `;`, `?`, `else`, or an operator, found `:` | ||
//~| NOTE expected one of `.`, `;`, `?`, `else`, or an operator | ||
//~| ERROR the `?` operator can only be applied to values that implement `Try` [E0277] | ||
//~| HELP the trait `Try` is not implemented for `{integer}` | ||
//~| ERROR the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`) [E0277] | ||
//~| HELP the trait `FromResidual<_>` is not implemented for `()` | ||
//~| NOTE type ascription syntax has been removed, see issue #101728 <https://github.com/rust-lang/rust/issues/101728> | ||
//~| NOTE in this expansion of desugaring of operator `?` | ||
//~| NOTE the `?` operator cannot be applied to type `{integer}` | ||
//~| NOTE in this expansion of desugaring of operator `?` | ||
//~| NOTE in this expansion of desugaring of operator `?` | ||
//~| NOTE cannot use the `?` operator in a function that returns `()` | ||
//~| NOTE in this expansion of desugaring of operator `?` | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
error: Rust has no ternary operator | ||
--> $DIR/ternary_operator.rs:5:19 | ||
| | ||
LL | let x = 5 > 2 ? true : false; | ||
| ^^^^^^^^^^^^^^^ | ||
| | ||
= help: use an `if-else` expression instead | ||
|
||
error: Rust has no ternary operator | ||
--> $DIR/ternary_operator.rs:21:19 | ||
| | ||
LL | let x = 5 > 2 ? { true } : { false }; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: use an `if-else` expression instead | ||
|
||
error: Rust has no ternary operator | ||
--> $DIR/ternary_operator.rs:37:19 | ||
| | ||
LL | let x = 5 > 2 ? f32::MAX : f32::MIN; | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: use an `if-else` expression instead | ||
|
||
error: expected one of `.`, `;`, `?`, `else`, or an operator, found `:` | ||
--> $DIR/ternary_operator.rs:53:37 | ||
| | ||
LL | let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false }; | ||
| ^ expected one of `.`, `;`, `?`, `else`, or an operator | ||
| | ||
= note: type ascription syntax has been removed, see issue #101728 <https://github.com/rust-lang/rust/issues/101728> | ||
|
||
error: Rust has no ternary operator | ||
--> $DIR/ternary_operator.rs:53:19 | ||
| | ||
LL | let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false }; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: use an `if-else` expression instead | ||
|
||
error[E0277]: the `?` operator can only be applied to values that implement `Try` | ||
--> $DIR/ternary_operator.rs:5:17 | ||
| | ||
LL | let x = 5 > 2 ? true : false; | ||
| ^^^ the `?` operator cannot be applied to type `{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`) | ||
--> $DIR/ternary_operator.rs:5:19 | ||
| | ||
LL | fn a() { | ||
| ------ this function should return `Result` or `Option` to accept `?` | ||
LL | let x = 5 > 2 ? true : false; | ||
| ^ cannot use the `?` operator in a function that returns `()` | ||
| | ||
= help: the trait `FromResidual<_>` is not implemented for `()` | ||
|
||
error[E0277]: the `?` operator can only be applied to values that implement `Try` | ||
--> $DIR/ternary_operator.rs:21:17 | ||
| | ||
LL | let x = 5 > 2 ? { true } : { false }; | ||
| ^^^ the `?` operator cannot be applied to type `{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`) | ||
--> $DIR/ternary_operator.rs:21:19 | ||
| | ||
LL | fn b() { | ||
| ------ this function should return `Result` or `Option` to accept `?` | ||
LL | let x = 5 > 2 ? { true } : { false }; | ||
| ^ cannot use the `?` operator in a function that returns `()` | ||
| | ||
= help: the trait `FromResidual<_>` is not implemented for `()` | ||
|
||
error[E0277]: the `?` operator can only be applied to values that implement `Try` | ||
--> $DIR/ternary_operator.rs:37:17 | ||
| | ||
LL | let x = 5 > 2 ? f32::MAX : f32::MIN; | ||
| ^^^ the `?` operator cannot be applied to type `{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`) | ||
--> $DIR/ternary_operator.rs:37:19 | ||
| | ||
LL | fn c() { | ||
| ------ this function should return `Result` or `Option` to accept `?` | ||
LL | let x = 5 > 2 ? f32::MAX : f32::MIN; | ||
| ^ cannot use the `?` operator in a function that returns `()` | ||
| | ||
= help: the trait `FromResidual<_>` is not implemented for `()` | ||
|
||
error[E0277]: the `?` operator can only be applied to values that implement `Try` | ||
--> $DIR/ternary_operator.rs:53:17 | ||
| | ||
LL | let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false }; | ||
| ^^^ the `?` operator cannot be applied to type `{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`) | ||
--> $DIR/ternary_operator.rs:53:19 | ||
| | ||
LL | fn main() { | ||
| --------- this function should return `Result` or `Option` to accept `?` | ||
LL | let x = 5 > 2 ? { let x = vec![]: Vec<u16>; x } : { false }; | ||
| ^ cannot use the `?` operator in a function that returns `()` | ||
| | ||
= help: the trait `FromResidual<_>` is not implemented for `()` | ||
|
||
error: aborting due to 13 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.