From 65b731908abee2bb8b0af50d18567eb4abaa4ac2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 17 May 2019 12:18:56 -0700 Subject: [PATCH] Explain that ? converts the error type using From --- src/librustc/traits/error_reporting.rs | 9 ++++++--- src/test/ui/issues/issue-32709.stderr | 1 + src/test/ui/try-block/try-block-bad-type.stderr | 1 + src/test/ui/try-on-option.stderr | 1 + 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/librustc/traits/error_reporting.rs b/src/librustc/traits/error_reporting.rs index df26883971022..9019c4a0575d4 100644 --- a/src/librustc/traits/error_reporting.rs +++ b/src/librustc/traits/error_reporting.rs @@ -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!( diff --git a/src/test/ui/issues/issue-32709.stderr b/src/test/ui/issues/issue-32709.stderr index 84cca5b20af47..04b8c3aa35396 100644 --- a/src/test/ui/issues/issue-32709.stderr +++ b/src/test/ui/issues/issue-32709.stderr @@ -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 diff --git a/src/test/ui/try-block/try-block-bad-type.stderr b/src/test/ui/try-block/try-block-bad-type.stderr index 7c7366df1dc3e..e1c2c6b675e9b 100644 --- a/src/test/ui/try-block/try-block-bad-type.stderr +++ b/src/test/ui/try-block/try-block-bad-type.stderr @@ -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: > > diff --git a/src/test/ui/try-on-option.stderr b/src/test/ui/try-on-option.stderr index 4465fbe14b75c..db5046f8c151a 100644 --- a/src/test/ui/try-on-option.stderr +++ b/src/test/ui/try-on-option.stderr @@ -4,6 +4,7 @@ error[E0277]: `?` couldn't convert the error to `()` LL | x?; | ^ the trait `std::convert::From` 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`)