Skip to content

Commit 65b7319

Browse files
committed
Explain that ? converts the error type using From
1 parent 1962ade commit 65b7319

File tree

4 files changed

+9
-3
lines changed

4 files changed

+9
-3
lines changed

src/librustc/traits/error_reporting.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -643,13 +643,16 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
643643
.map(|s| &s == "?")
644644
.unwrap_or(false);
645645
let is_from = format!("{}", trait_ref).starts_with("std::convert::From<");
646-
let message = if is_try && is_from {
647-
Some(format!(
646+
let (message, note) = if is_try && is_from {
647+
(Some(format!(
648648
"`?` couldn't convert the error to `{}`",
649649
trait_ref.self_ty(),
650+
)), Some(
651+
"the question mark operation (`?`) implicitly performs a \
652+
conversion on the error value using the `From` trait".to_owned()
650653
))
651654
} else {
652-
message
655+
(message, note)
653656
};
654657

655658
let mut err = struct_span_err!(

src/test/ui/issues/issue-32709.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error[E0277]: `?` couldn't convert the error to `()`
44
LL | Err(5)?;
55
| ^ the trait `std::convert::From<{integer}>` is not implemented for `()`
66
|
7+
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
78
= note: required by `std::convert::From::from`
89

910
error: aborting due to previous error

src/test/ui/try-block/try-block-bad-type.stderr

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

src/test/ui/try-on-option.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error[E0277]: `?` couldn't convert the error to `()`
44
LL | x?;
55
| ^ the trait `std::convert::From<std::option::NoneError>` is not implemented for `()`
66
|
7+
= note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
78
= note: required by `std::convert::From::from`
89

910
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)

0 commit comments

Comments
 (0)