-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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:28:25 | LL | fn bar() -> Result<(), String> { | ------------------ expected `String` because of this LL | let x = foo(); | ----- this can be annotated with `?` because it has type `Result<String, String>` LL | let one = x LL | .map(|s| ()) | ----------- this can be annotated with `?` because it 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 #72124.
- Loading branch information
Showing
7 changed files
with
302 additions
and
12 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(|| { //~ NOTE this can be annotated with `?` because it has type `Result<&str, &str>` | ||
"Couldn't split the test string" | ||
}); | ||
let one = x | ||
.map(|s| ()) //~ NOTE this can be annotated with `?` because it has type `Result<(), &str>` | ||
.map_err(|_| ()) //~ NOTE this can't be annotated with `?` because it has type `Result<(), ()>` | ||
.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 in this expansion of desugaring of operator `?` | ||
//~| NOTE this can't be annotated with `?` because it has type `Result<&str, ()>` | ||
//~| 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 can be annotated with `?` because it has type `Result<String, String>` | ||
let one = x | ||
.map(|s| ()) //~ NOTE this can be annotated with `?` because it has type `Result<(), String>` | ||
.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, ()>>` | ||
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<&str, ()>` | ||
"Couldn't split the test string"; | ||
})?; | ||
//~^ 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 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() {} |
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,83 @@ | ||
error[E0277]: `?` couldn't convert the error to `String` | ||
--> $DIR/question-mark-result-err-mismatch.rs:12:22 | ||
| | ||
LL | fn foo() -> Result<String, String> { | ||
| ---------------------- expected `String` because of this | ||
... | ||
LL | .ok_or_else(|| { | ||
| __________- | ||
LL | | "Couldn't split the test string" | ||
LL | | }); | ||
| |__________- this can be annotated with `?` because it has type `Result<&str, &str>` | ||
LL | let one = x | ||
LL | .map(|s| ()) | ||
| ----------- this can be annotated with `?` because it has type `Result<(), &str>` | ||
LL | .map_err(|_| ()) | ||
| --------------- this can't be annotated with `?` because it has type `Result<(), ()>` | ||
LL | .map(|()| "")?; | ||
| ------------^ the trait `From<()>` is not implemented for `String` | ||
| | | ||
| this can't be annotated with `?` because it has type `Result<&str, ()>` | ||
| | ||
= 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, String>` to implement `FromResidual<Result<Infallible, ()>>` | ||
|
||
error[E0277]: `?` couldn't convert the error to `String` | ||
--> $DIR/question-mark-result-err-mismatch.rs:28:25 | ||
| | ||
LL | fn bar() -> Result<(), String> { | ||
| ------------------ expected `String` because of this | ||
LL | let x = foo(); | ||
| ----- this can be annotated with `?` because it has type `Result<String, String>` | ||
LL | let one = x | ||
LL | .map(|s| ()) | ||
| ----------- this can be annotated with `?` because it 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, ()>>` | ||
|
||
error[E0277]: `?` couldn't convert the error to `String` | ||
--> $DIR/question-mark-result-err-mismatch.rs:47:11 | ||
| | ||
LL | fn baz() -> Result<String, String> { | ||
| ---------------------- expected `String` because of this | ||
... | ||
LL | .ok_or_else(|| { | ||
| __________- | ||
LL | | "Couldn't split the test string"; | ||
LL | | })?; | ||
| | -^ the trait `From<()>` is not implemented for `String` | ||
| |__________| | ||
| this can't be annotated with `?` because it has type `Result<&str, ()>` | ||
| | ||
= 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, String>` to implement `FromResidual<Result<Infallible, ()>>` | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
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