Tweak E0277 when predicate comes indirectly from ?
When a `?` operation requires an `Into` conversion with additional bounds (like having a concrete error but wanting to convert to a trait object), we handle it speficically and provide the same kind of information we give other `?` related errors.
```
error[E0277]: `?` couldn't convert the error: `E: std::error::Error` is not satisfied
--> $DIR/bad-question-mark-on-trait-object.rs:5:13
|
LL | fn foo() -> Result<(), Box<dyn std::error::Error>> {
| -------------------------------------- required `E: std::error::Error` because of this
LL | Ok(bar()?)
| ^ the trait `std::error::Error` is not implemented for `E`
|
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
= note: required for `Box<dyn std::error::Error>` to implement `From<E>`
```
Avoid talking about `FromResidual` when other more relevant information is being given, particularly from `rust_on_unimplemented`.
This commit is contained in:
parent
28b83ee596
commit
e565eeed78
11 changed files with 103 additions and 65 deletions
|
|
@ -17,8 +17,6 @@ LL | | test()?;
|
|||
... |
|
||||
LL | | }
|
||||
| |_- this function should return `Result` or `Option` to accept `?`
|
||||
|
|
||||
= help: the trait `FromResidual<_>` is not implemented for `()`
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
|
|
|
|||
|
|
@ -6,8 +6,6 @@ LL | async {
|
|||
LL | let x: Option<u32> = None;
|
||||
LL | x?;
|
||||
| ^ cannot use the `?` operator in an async block that returns `{integer}`
|
||||
|
|
||||
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `{integer}`
|
||||
|
||||
error[E0277]: the `?` operator can only be used in an async closure that returns `Result` or `Option` (or another type that implements `FromResidual`)
|
||||
--> $DIR/try-on-option-in-async.rs:16:10
|
||||
|
|
@ -20,8 +18,6 @@ LL | | x?;
|
|||
LL | | 22_u32
|
||||
LL | | };
|
||||
| |_____- this function should return `Result` or `Option` to accept `?`
|
||||
|
|
||||
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `u32`
|
||||
|
||||
error[E0277]: the `?` operator can only be used in an async function that returns `Result` or `Option` (or another type that implements `FromResidual`)
|
||||
--> $DIR/try-on-option-in-async.rs:25:6
|
||||
|
|
@ -34,8 +30,6 @@ LL | | x?;
|
|||
LL | | 22
|
||||
LL | | }
|
||||
| |_- this function should return `Result` or `Option` to accept `?`
|
||||
|
|
||||
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `u32`
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ LL | fn test1() {
|
|||
LL | let mut _file = File::create("foo.txt")?;
|
||||
| ^ cannot use the `?` operator in a function that returns `()`
|
||||
|
|
||||
= help: the trait `FromResidual<Result<Infallible, std::io::Error>>` is not implemented for `()`
|
||||
help: consider adding return type
|
||||
|
|
||||
LL ~ fn test1() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
|
@ -23,7 +22,6 @@ LL | fn test2() {
|
|||
LL | let mut _file = File::create("foo.txt")?;
|
||||
| ^ cannot use the `?` operator in a function that returns `()`
|
||||
|
|
||||
= help: the trait `FromResidual<Result<Infallible, std::io::Error>>` is not implemented for `()`
|
||||
help: consider adding return type
|
||||
|
|
||||
LL ~ fn test2() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
|
@ -41,7 +39,6 @@ LL | fn test4(&self) {
|
|||
LL | let mut _file = File::create("foo.txt")?;
|
||||
| ^ cannot use the `?` operator in a method that returns `()`
|
||||
|
|
||||
= help: the trait `FromResidual<Result<Infallible, std::io::Error>>` is not implemented for `()`
|
||||
help: consider adding return type
|
||||
|
|
||||
LL ~ fn test4(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
|
@ -59,7 +56,6 @@ LL | fn test5(&self) {
|
|||
LL | let mut _file = File::create("foo.txt")?;
|
||||
| ^ cannot use the `?` operator in a method that returns `()`
|
||||
|
|
||||
= help: the trait `FromResidual<Result<Infallible, std::io::Error>>` is not implemented for `()`
|
||||
help: consider adding return type
|
||||
|
|
||||
LL ~ fn test5(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
|
@ -78,7 +74,6 @@ LL | fn main() {
|
|||
LL | let mut _file = File::create("foo.txt")?;
|
||||
| ^ cannot use the `?` operator in a function that returns `()`
|
||||
|
|
||||
= help: the trait `FromResidual<Result<Infallible, std::io::Error>>` is not implemented for `()`
|
||||
help: consider adding return type
|
||||
|
|
||||
LL ~ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
|
@ -99,7 +94,6 @@ LL | let mut _file = File::create("foo.txt")?;
|
|||
LL | mac!();
|
||||
| ------ in this macro invocation
|
||||
|
|
||||
= help: the trait `FromResidual<Result<Infallible, std::io::Error>>` is not implemented for `()`
|
||||
= note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
help: consider adding return type
|
||||
|
|
||||
|
|
|
|||
|
|
@ -20,9 +20,6 @@ LL | fn option_to_result() -> Result<u64, String> {
|
|||
| -------------------------------------------- this function returns a `Result`
|
||||
LL | Some(3)?;
|
||||
| ^ use `.ok_or(...)?` to provide an error compatible with `Result<u64, String>`
|
||||
|
|
||||
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `Result<u64, String>`
|
||||
= help: the trait `FromResidual<Result<Infallible, E>>` is implemented for `Result<T, F>`
|
||||
|
||||
error[E0277]: the `?` operator can only be used on `Result`s in a function that returns `Result`
|
||||
--> $DIR/bad-interconversion.rs:15:31
|
||||
|
|
@ -31,9 +28,6 @@ LL | fn control_flow_to_result() -> Result<u64, String> {
|
|||
| -------------------------------------------------- this function returns a `Result`
|
||||
LL | Ok(ControlFlow::Break(123)?)
|
||||
| ^ this `?` produces `ControlFlow<{integer}, Infallible>`, which is incompatible with `Result<u64, String>`
|
||||
|
|
||||
= help: the trait `FromResidual<ControlFlow<{integer}, Infallible>>` is not implemented for `Result<u64, String>`
|
||||
= help: the trait `FromResidual<Result<Infallible, E>>` is implemented for `Result<T, F>`
|
||||
|
||||
error[E0277]: the `?` operator can only be used on `Option`s, not `Result`s, in a function that returns `Option`
|
||||
--> $DIR/bad-interconversion.rs:20:22
|
||||
|
|
@ -42,9 +36,6 @@ LL | fn result_to_option() -> Option<u16> {
|
|||
| ------------------------------------ this function returns an `Option`
|
||||
LL | Some(Err("hello")?)
|
||||
| ^ use `.ok()?` if you want to discard the `Result<Infallible, &str>` error information
|
||||
|
|
||||
= help: the trait `FromResidual<Result<Infallible, &str>>` is not implemented for `Option<u16>`
|
||||
= help: the trait `FromResidual<Option<Infallible>>` is implemented for `Option<T>`
|
||||
|
||||
error[E0277]: the `?` operator can only be used on `Option`s in a function that returns `Option`
|
||||
--> $DIR/bad-interconversion.rs:25:33
|
||||
|
|
@ -53,9 +44,6 @@ LL | fn control_flow_to_option() -> Option<u64> {
|
|||
| ------------------------------------------ this function returns an `Option`
|
||||
LL | Some(ControlFlow::Break(123)?)
|
||||
| ^ this `?` produces `ControlFlow<{integer}, Infallible>`, which is incompatible with `Option<u64>`
|
||||
|
|
||||
= help: the trait `FromResidual<ControlFlow<{integer}, Infallible>>` is not implemented for `Option<u64>`
|
||||
= help: the trait `FromResidual<Option<Infallible>>` is implemented for `Option<T>`
|
||||
|
||||
error[E0277]: the `?` operator can only be used on `ControlFlow`s in a function that returns `ControlFlow`
|
||||
--> $DIR/bad-interconversion.rs:30:39
|
||||
|
|
|
|||
26
tests/ui/try-trait/bad-question-mark-on-trait-object.rs
Normal file
26
tests/ui/try-trait/bad-question-mark-on-trait-object.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
struct E;
|
||||
struct X;
|
||||
|
||||
fn foo() -> Result<(), Box<dyn std::error::Error>> { //~ NOTE required `E: std::error::Error` because of this
|
||||
Ok(bar()?)
|
||||
//~^ ERROR `?` couldn't convert the error: `E: std::error::Error` is not satisfied
|
||||
//~| NOTE the trait `std::error::Error` is not implemented for `E`
|
||||
//~| NOTE the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
|
||||
//~| NOTE required for `Box<dyn std::error::Error>` to implement `From<E>`
|
||||
//~| NOTE in this expansion
|
||||
//~| NOTE in this expansion
|
||||
//~| NOTE in this expansion
|
||||
}
|
||||
fn bat() -> Result<(), X> { //~ NOTE expected `X` because of this
|
||||
Ok(bar()?)
|
||||
//~^ ERROR `?` couldn't convert the error to `X`
|
||||
//~| NOTE the trait `From<E>` is not implemented for `X`
|
||||
//~| NOTE this can't be annotated with `?` because it has type `Result<_, E>`
|
||||
//~| NOTE the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
|
||||
//~| NOTE in this expansion
|
||||
//~| NOTE in this expansion
|
||||
}
|
||||
fn bar() -> Result<(), E> {
|
||||
Err(E)
|
||||
}
|
||||
fn main() {}
|
||||
26
tests/ui/try-trait/bad-question-mark-on-trait-object.stderr
Normal file
26
tests/ui/try-trait/bad-question-mark-on-trait-object.stderr
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
error[E0277]: `?` couldn't convert the error: `E: std::error::Error` is not satisfied
|
||||
--> $DIR/bad-question-mark-on-trait-object.rs:5:13
|
||||
|
|
||||
LL | fn foo() -> Result<(), Box<dyn std::error::Error>> {
|
||||
| -------------------------------------- required `E: std::error::Error` because of this
|
||||
LL | Ok(bar()?)
|
||||
| ^ the trait `std::error::Error` is not implemented for `E`
|
||||
|
|
||||
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
|
||||
= note: required for `Box<dyn std::error::Error>` to implement `From<E>`
|
||||
|
||||
error[E0277]: `?` couldn't convert the error to `X`
|
||||
--> $DIR/bad-question-mark-on-trait-object.rs:15:13
|
||||
|
|
||||
LL | fn bat() -> Result<(), X> {
|
||||
| ------------- expected `X` because of this
|
||||
LL | Ok(bar()?)
|
||||
| -----^ the trait `From<E>` is not implemented for `X`
|
||||
| |
|
||||
| this can't be annotated with `?` because it has type `Result<_, E>`
|
||||
|
|
||||
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0277`.
|
||||
|
|
@ -6,9 +6,6 @@ LL | fn test_result() -> Result<(),()> {
|
|||
LL | let a:Option<()> = Some(());
|
||||
LL | a?;
|
||||
| ^ use `.ok_or(...)?` to provide an error compatible with `Result<(), ()>`
|
||||
|
|
||||
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `Result<(), ()>`
|
||||
= help: the trait `FromResidual<Result<Infallible, E>>` is implemented for `Result<T, F>`
|
||||
|
||||
error[E0277]: the `?` operator can only be used on `Option`s, not `Result`s, in a function that returns `Option`
|
||||
--> $DIR/option-to-result.rs:11:6
|
||||
|
|
@ -18,9 +15,6 @@ LL | fn test_option() -> Option<i32>{
|
|||
LL | let a:Result<i32, i32> = Ok(5);
|
||||
LL | a?;
|
||||
| ^ use `.ok()?` if you want to discard the `Result<Infallible, i32>` error information
|
||||
|
|
||||
= help: the trait `FromResidual<Result<Infallible, i32>>` is not implemented for `Option<i32>`
|
||||
= help: the trait `FromResidual<Option<Infallible>>` is implemented for `Option<T>`
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
|
|
|
|||
|
|
@ -6,8 +6,6 @@ LL | fn a_function() -> u32 {
|
|||
LL | let x: Option<u32> = None;
|
||||
LL | x?;
|
||||
| ^ cannot use the `?` operator in a function that returns `u32`
|
||||
|
|
||||
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `u32`
|
||||
|
||||
error[E0277]: the `?` operator can only be used in a closure that returns `Result` or `Option` (or another type that implements `FromResidual`)
|
||||
--> $DIR/try-on-option-diagnostics.rs:14:10
|
||||
|
|
@ -17,8 +15,6 @@ LL | let a_closure = || {
|
|||
LL | let x: Option<u32> = None;
|
||||
LL | x?;
|
||||
| ^ cannot use the `?` operator in a closure that returns `{integer}`
|
||||
|
|
||||
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `{integer}`
|
||||
|
||||
error[E0277]: the `?` operator can only be used in a method that returns `Result` or `Option` (or another type that implements `FromResidual`)
|
||||
--> $DIR/try-on-option-diagnostics.rs:26:14
|
||||
|
|
@ -28,8 +24,6 @@ LL | fn a_method() {
|
|||
LL | let x: Option<u32> = None;
|
||||
LL | x?;
|
||||
| ^ cannot use the `?` operator in a method that returns `()`
|
||||
|
|
||||
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `()`
|
||||
|
||||
error[E0277]: the `?` operator can only be used in a trait method that returns `Result` or `Option` (or another type that implements `FromResidual`)
|
||||
--> $DIR/try-on-option-diagnostics.rs:39:14
|
||||
|
|
@ -39,8 +33,6 @@ LL | fn a_trait_method() {
|
|||
LL | let x: Option<u32> = None;
|
||||
LL | x?;
|
||||
| ^ cannot use the `?` operator in a trait method that returns `()`
|
||||
|
|
||||
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `()`
|
||||
|
||||
error: aborting due to 4 previous errors
|
||||
|
||||
|
|
|
|||
|
|
@ -6,9 +6,6 @@ LL | fn foo() -> Result<u32, ()> {
|
|||
LL | let x: Option<u32> = None;
|
||||
LL | x?;
|
||||
| ^ use `.ok_or(...)?` to provide an error compatible with `Result<u32, ()>`
|
||||
|
|
||||
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `Result<u32, ()>`
|
||||
= help: the trait `FromResidual<Result<Infallible, E>>` is implemented for `Result<T, F>`
|
||||
|
||||
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `FromResidual`)
|
||||
--> $DIR/try-on-option.rs:11:6
|
||||
|
|
@ -18,8 +15,6 @@ LL | fn bar() -> u32 {
|
|||
LL | let x: Option<u32> = None;
|
||||
LL | x?;
|
||||
| ^ cannot use the `?` operator in a function that returns `u32`
|
||||
|
|
||||
= help: the trait `FromResidual<Option<Infallible>>` is not implemented for `u32`
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ LL | // error for a `Try` type on a non-`Try` fn
|
|||
LL | std::fs::File::open("foo")?;
|
||||
| ^ cannot use the `?` operator in a function that returns `()`
|
||||
|
|
||||
= help: the trait `FromResidual<Result<Infallible, std::io::Error>>` is not implemented for `()`
|
||||
help: consider adding return type
|
||||
|
|
||||
LL ~ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
|
@ -33,8 +32,6 @@ LL | fn main() {
|
|||
...
|
||||
LL | ()?;
|
||||
| ^ cannot use the `?` operator in a function that returns `()`
|
||||
|
|
||||
= help: the trait `FromResidual<_>` is not implemented for `()`
|
||||
|
||||
error[E0277]: the trait bound `(): Try` is not satisfied
|
||||
--> $DIR/try-operator-on-main.rs:14:25
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue