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#116496 - estebank:question-method-chain-context, r=compiler-errors Provide context when `?` can't be called because of `Result<_, E>` When a method chain ending in `?` causes an E0277 because the expression's `Result::Err` variant doesn't have a type that can be converted to the `Result<_, E>` type parameter in the return type, provide additional context of which parts of the chain can and can't support the `?` operator. ``` error[E0277]: `?` couldn't convert the error to `String` --> $DIR/question-mark-result-err-mismatch.rs:27:25 | LL | fn bar() -> Result<(), String> { | ------------------ expected `String` because of this LL | let x = foo(); | ----- this has type `Result<_, String>` ... LL | .map_err(|_| ())?; | ---------------^ the trait `From<()>` is not implemented for `String` | | | this can't be annotated with `?` because it has type `Result<_, ()>` | = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait = help: the following other types implement trait `From<T>`: <String as From<char>> <String as From<Box<str>>> <String as From<Cow<'a, str>>> <String as From<&str>> <String as From<&mut str>> <String as From<&String>> = note: required for `Result<(), String>` to implement `FromResidual<Result<Infallible, ()>>` ``` Fix rust-lang#72124.
- Loading branch information
Showing
7 changed files
with
364 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
fn foo() -> Result<String, String> { //~ NOTE expected `String` because of this | ||
let test = String::from("one,two"); | ||
let x = test | ||
.split_whitespace() | ||
.next() | ||
.ok_or_else(|| { | ||
"Couldn't split the test string" | ||
}); | ||
let one = x | ||
.map(|s| ()) | ||
.map_err(|e| { //~ NOTE this can't be annotated with `?` because it has type `Result<_, ()>` | ||
e; //~ HELP remove this semicolon | ||
}) | ||
.map(|()| "")?; //~ ERROR `?` couldn't convert the error to `String` | ||
//~^ NOTE in this expansion of desugaring of operator `?` | ||
//~| NOTE in this expansion of desugaring of operator `?` | ||
//~| NOTE in this expansion of desugaring of operator `?` | ||
//~| NOTE the trait `From<()>` is not implemented for `String` | ||
//~| NOTE the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait | ||
//~| NOTE required for `Result<String, String>` to implement `FromResidual<Result<Infallible, ()>>` | ||
Ok(one.to_string()) | ||
} | ||
|
||
fn bar() -> Result<(), String> { //~ NOTE expected `String` because of this | ||
let x = foo(); //~ NOTE this has type `Result<_, String>` | ||
let one = x | ||
.map(|s| ()) | ||
.map_err(|_| ())?; //~ ERROR `?` couldn't convert the error to `String` | ||
//~^ NOTE in this expansion of desugaring of operator `?` | ||
//~| NOTE in this expansion of desugaring of operator `?` | ||
//~| NOTE in this expansion of desugaring of operator `?` | ||
//~| NOTE in this expansion of desugaring of operator `?` | ||
//~| NOTE this can't be annotated with `?` because it has type `Result<_, ()>` | ||
//~| NOTE the trait `From<()>` is not implemented for `String` | ||
//~| NOTE the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait | ||
//~| NOTE required for `Result<(), String>` to implement `FromResidual<Result<Infallible, ()>>` | ||
//~| HELP the following other types implement trait `From<T>`: | ||
Ok(one) | ||
} | ||
|
||
fn baz() -> Result<String, String> { //~ NOTE expected `String` because of this | ||
let test = String::from("one,two"); | ||
let one = test | ||
.split_whitespace() | ||
.next() | ||
.ok_or_else(|| { //~ NOTE this can't be annotated with `?` because it has type `Result<_, ()>` | ||
"Couldn't split the test string"; //~ HELP remove this semicolon | ||
})?; | ||
//~^ ERROR `?` couldn't convert the error to `String` | ||
//~| NOTE in this expansion of desugaring of operator `?` | ||
//~| NOTE in this expansion of desugaring of operator `?` | ||
//~| NOTE in this expansion of desugaring of operator `?` | ||
//~| NOTE the trait `From<()>` is not implemented for `String` | ||
//~| NOTE the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait | ||
//~| NOTE required for `Result<String, String>` to implement `FromResidual<Result<Infallible, ()>>` | ||
Ok(one.to_string()) | ||
} | ||
|
||
fn main() {} |
Oops, something went wrong.