Skip to content

Commit 1e99b2e

Browse files
committed
Give custom error for E0277 on ? error case
1 parent 007b40b commit 1e99b2e

File tree

6 files changed

+18
-6
lines changed

6 files changed

+18
-6
lines changed

src/librustc/traits/error_reporting.rs

+12
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,18 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
638638
let OnUnimplementedNote { message, label, note }
639639
= self.on_unimplemented_note(trait_ref, obligation);
640640
let have_alt_message = message.is_some() || label.is_some();
641+
let is_try = self.tcx.sess.source_map().span_to_snippet(span)
642+
.map(|s| &s == "?")
643+
.unwrap_or(false);
644+
let is_from = format!("{}", trait_ref).starts_with("std::convert::From<");
645+
let message = if is_try && is_from {
646+
Some(format!(
647+
"`?` couldn't convert the error to `{}`",
648+
trait_ref.self_ty(),
649+
))
650+
} else {
651+
message
652+
};
641653

642654
let mut err = struct_span_err!(
643655
self.tcx.sess,

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

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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

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

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

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

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ fn main() {}
44

55
fn foo() -> Result<u32, ()> {
66
let x: Option<u32> = None;
7-
x?; //~ the trait bound
7+
x?; //~ ERROR `?` couldn't convert the error
88
Ok(22)
99
}
1010

1111
fn bar() -> u32 {
1212
let x: Option<u32> = None;
13-
x?; //~ the `?` operator
13+
x?; //~ ERROR the `?` operator
1414
22
1515
}

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

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

0 commit comments

Comments
 (0)