Rollup merge of #60924 - estebank:try-msg, r=petrochenkov

Explain that ? converts the error type using From

Fix #60917.
This commit is contained in:
Mazdak Farrokhzad 2019-05-19 02:31:39 +02:00 committed by GitHub
commit 80d372f7cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 3 deletions

View File

@ -643,13 +643,16 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
.map(|s| &s == "?")
.unwrap_or(false);
let is_from = format!("{}", trait_ref).starts_with("std::convert::From<");
let message = if is_try && is_from {
Some(format!(
let (message, note) = if is_try && is_from {
(Some(format!(
"`?` couldn't convert the error to `{}`",
trait_ref.self_ty(),
)), Some(
"the question mark operation (`?`) implicitly performs a \
conversion on the error value using the `From` trait".to_owned()
))
} else {
message
(message, note)
};
let mut err = struct_span_err!(

View File

@ -4,6 +4,7 @@ error[E0277]: `?` couldn't convert the error to `()`
LL | Err(5)?;
| ^ the trait `std::convert::From<{integer}>` is not implemented for `()`
|
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
= note: required by `std::convert::From::from`
error: aborting due to previous error

View File

@ -4,6 +4,7 @@ error[E0277]: `?` couldn't convert the error to `i32`
LL | Err("")?;
| ^ the trait `std::convert::From<&str>` is not implemented for `i32`
|
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
= help: the following implementations were found:
<i32 as std::convert::From<bool>>
<i32 as std::convert::From<i16>>

View File

@ -4,6 +4,7 @@ error[E0277]: `?` couldn't convert the error to `()`
LL | x?;
| ^ the trait `std::convert::From<std::option::NoneError>` is not implemented for `()`
|
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
= note: required by `std::convert::From::from`
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)