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.
2023-10-06 19:07:05 +00:00
|
|
|
fn foo() -> Result<String, String> { //~ NOTE expected `String` because of this
|
|
|
|
let test = String::from("one,two");
|
|
|
|
let x = test
|
|
|
|
.split_whitespace()
|
|
|
|
.next()
|
2023-10-07 03:58:45 +00:00
|
|
|
.ok_or_else(|| {
|
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.
2023-10-06 19:07:05 +00:00
|
|
|
"Couldn't split the test string"
|
|
|
|
});
|
|
|
|
let one = x
|
2023-10-07 01:14:43 +00:00
|
|
|
.map(|s| ())
|
2023-10-07 03:47:02 +00:00
|
|
|
.map_err(|e| { //~ NOTE this can't be annotated with `?` because it has type `Result<_, ()>`
|
|
|
|
e; //~ HELP remove this semicolon
|
|
|
|
})
|
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.
2023-10-06 19:07:05 +00:00
|
|
|
.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
|
2023-10-07 01:14:43 +00:00
|
|
|
let x = foo(); //~ NOTE this has type `Result<_, String>`
|
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.
2023-10-06 19:07:05 +00:00
|
|
|
let one = x
|
2023-10-07 01:14:43 +00:00
|
|
|
.map(|s| ())
|
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.
2023-10-06 19:07:05 +00:00
|
|
|
.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 `?`
|
2023-10-07 01:14:43 +00:00
|
|
|
//~| NOTE this can't be annotated with `?` because it has type `Result<_, ()>`
|
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.
2023-10-06 19:07:05 +00:00
|
|
|
//~| 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, ()>>`
|
2023-10-07 03:47:02 +00:00
|
|
|
//~| HELP the following other types implement trait `From<T>`:
|
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.
2023-10-06 19:07:05 +00:00
|
|
|
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()
|
2023-10-07 01:14:43 +00:00
|
|
|
.ok_or_else(|| { //~ NOTE this can't be annotated with `?` because it has type `Result<_, ()>`
|
2023-10-07 03:47:02 +00:00
|
|
|
"Couldn't split the test string"; //~ HELP remove this semicolon
|
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.
2023-10-06 19:07:05 +00:00
|
|
|
})?;
|
|
|
|
//~^ 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() {}
|