Give custom error for E0277 on ? error case

This commit is contained in:
Esteban Küber 2019-04-17 19:50:50 -07:00
parent 007b40be01
commit 1e99b2ec9d
6 changed files with 18 additions and 6 deletions

View File

@ -638,6 +638,18 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
let OnUnimplementedNote { message, label, note }
= self.on_unimplemented_note(trait_ref, obligation);
let have_alt_message = message.is_some() || label.is_some();
let is_try = self.tcx.sess.source_map().span_to_snippet(span)
.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!(
"`?` couldn't convert the error to `{}`",
trait_ref.self_ty(),
))
} else {
message
};
let mut err = struct_span_err!(
self.tcx.sess,

View File

@ -1,4 +1,4 @@
error[E0277]: the trait bound `(): std::convert::From<{integer}>` is not satisfied
error[E0277]: `?` couldn't convert the error to `()`
--> $DIR/issue-32709.rs:4:11
|
LL | Err(5)?;

View File

@ -4,7 +4,7 @@
pub fn main() {
let res: Result<u32, i32> = try {
Err("")?; //~ ERROR the trait bound `i32: std::convert::From<&str>` is not satisfied
Err("")?; //~ ERROR `?` couldn't convert the error
5
};

View File

@ -1,4 +1,4 @@
error[E0277]: the trait bound `i32: std::convert::From<&str>` is not satisfied
error[E0277]: `?` couldn't convert the error to `i32`
--> $DIR/try-block-bad-type.rs:7:16
|
LL | Err("")?;

View File

@ -4,12 +4,12 @@ fn main() {}
fn foo() -> Result<u32, ()> {
let x: Option<u32> = None;
x?; //~ the trait bound
x?; //~ ERROR `?` couldn't convert the error
Ok(22)
}
fn bar() -> u32 {
let x: Option<u32> = None;
x?; //~ the `?` operator
x?; //~ ERROR the `?` operator
22
}

View File

@ -1,4 +1,4 @@
error[E0277]: the trait bound `(): std::convert::From<std::option::NoneError>` is not satisfied
error[E0277]: `?` couldn't convert the error to `()`
--> $DIR/try-on-option.rs:7:6
|
LL | x?;