forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#137245 - estebank:from-residual-note-2, r=oli-obk 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:7: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` | | | this has type `Result<_, E>` | note: `E` needs to implement `std::error::Error` --> $DIR/bad-question-mark-on-trait-object.rs:1:1 | LL | struct 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`. Fix rust-lang#137238. ----- CC rust-lang#137232, which was a smaller step related to this.
- Loading branch information
Showing
11 changed files
with
175 additions
and
71 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,29 @@ | ||
struct E; | ||
//~^ NOTE `E` needs to implement `std::error::Error` | ||
//~| NOTE alternatively, `E` needs to implement `Into<X>` | ||
struct X; //~ NOTE `X` needs to implement `From<E>` | ||
|
||
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 this has type `Result<_, 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() {} |
43 changes: 43 additions & 0 deletions
43
tests/ui/try-trait/bad-question-mark-on-trait-object.stderr
This file contains 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,43 @@ | ||
error[E0277]: `?` couldn't convert the error: `E: std::error::Error` is not satisfied | ||
--> $DIR/bad-question-mark-on-trait-object.rs:7: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` | ||
| | | ||
| this has type `Result<_, E>` | ||
| | ||
note: `E` needs to implement `std::error::Error` | ||
--> $DIR/bad-question-mark-on-trait-object.rs:1:1 | ||
| | ||
LL | struct 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:18: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: `X` needs to implement `From<E>` | ||
--> $DIR/bad-question-mark-on-trait-object.rs:4:1 | ||
| | ||
LL | struct X; | ||
| ^^^^^^^^ | ||
note: alternatively, `E` needs to implement `Into<X>` | ||
--> $DIR/bad-question-mark-on-trait-object.rs:1:1 | ||
| | ||
LL | struct 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`. |
Oops, something went wrong.