Skip to content

Commit

Permalink
Rollup merge of rust-lang#60064 - estebank:issue-59980, r=varkor
Browse files Browse the repository at this point in the history
Point at try `?` on errors affecting the err match arm of the desugared code

Fix rust-lang#59980.
  • Loading branch information
Centril authored Apr 18, 2019
2 parents c56aafa + 1e99b2e commit 73307ed
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 19 deletions.
22 changes: 15 additions & 7 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4685,6 +4685,14 @@ impl<'a> LoweringContext<'a> {
Symbol::intern("try_trait")
].into()),
);
let try_span = self.sess.source_map().end_point(e.span);
let try_span = self.mark_span_with_reason(
CompilerDesugaringKind::QuestionMark,
try_span,
Some(vec![
Symbol::intern("try_trait")
].into()),
);

// `Try::into_result(<expr>)`
let discr = {
Expand Down Expand Up @@ -4729,14 +4737,14 @@ impl<'a> LoweringContext<'a> {
// return Try::from_error(From::from(err)),`
let err_arm = {
let err_ident = self.str_to_ident("err");
let (err_local, err_local_nid) = self.pat_ident(e.span, err_ident);
let (err_local, err_local_nid) = self.pat_ident(try_span, err_ident);
let from_expr = {
let path = &["convert", "From", "from"];
let from = P(self.expr_std_path(
e.span, path, None, ThinVec::new()));
let err_expr = self.expr_ident(e.span, err_ident, err_local_nid);
try_span, path, None, ThinVec::new()));
let err_expr = self.expr_ident(try_span, err_ident, err_local_nid);

self.expr_call(e.span, from, hir_vec![err_expr])
self.expr_call(try_span, from, hir_vec![err_expr])
};
let from_err_expr =
self.wrap_in_try_constructor("from_error", from_expr, unstable_span);
Expand All @@ -4745,7 +4753,7 @@ impl<'a> LoweringContext<'a> {
let ret_expr = if let Some(catch_node) = catch_scope {
let target_id = Ok(self.lower_node_id(catch_node).hir_id);
P(self.expr(
e.span,
try_span,
hir::ExprKind::Break(
hir::Destination {
label: None,
Expand All @@ -4756,10 +4764,10 @@ impl<'a> LoweringContext<'a> {
thin_attrs,
))
} else {
P(self.expr(e.span, hir::ExprKind::Ret(Some(from_err_expr)), thin_attrs))
P(self.expr(try_span, hir::ExprKind::Ret(Some(from_err_expr)), thin_attrs))
};

let err_pat = self.pat_err(e.span, err_local);
let err_pat = self.pat_err(try_span, err_local);
self.arm(hir_vec![err_pat], ret_expr)
};

Expand Down
12 changes: 12 additions & 0 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/issues/issue-32709.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: the trait bound `(): std::convert::From<{integer}>` is not satisfied
--> $DIR/issue-32709.rs:4:5
error[E0277]: `?` couldn't convert the error to `()`
--> $DIR/issue-32709.rs:4:11
|
LL | Err(5)?;
| ^^^^^^^ the trait `std::convert::From<{integer}>` is not implemented for `()`
| ^ the trait `std::convert::From<{integer}>` is not implemented for `()`
|
= note: required by `std::convert::From::from`

Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/try-block/try-block-bad-type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/try-block/try-block-bad-type.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: the trait bound `i32: std::convert::From<&str>` is not satisfied
--> $DIR/try-block-bad-type.rs:7:9
error[E0277]: `?` couldn't convert the error to `i32`
--> $DIR/try-block-bad-type.rs:7:16
|
LL | Err("")?;
| ^^^^^^^^ the trait `std::convert::From<&str>` is not implemented for `i32`
| ^ the trait `std::convert::From<&str>` is not implemented for `i32`
|
= help: the following implementations were found:
<i32 as std::convert::From<bool>>
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/try-on-option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions src/test/ui/try-on-option.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error[E0277]: the trait bound `(): std::convert::From<std::option::NoneError>` is not satisfied
--> $DIR/try-on-option.rs:7:5
error[E0277]: `?` couldn't convert the error to `()`
--> $DIR/try-on-option.rs:7:6
|
LL | x?;
| ^^ the trait `std::convert::From<std::option::NoneError>` is not implemented for `()`
| ^ the trait `std::convert::From<std::option::NoneError>` is not implemented for `()`
|
= note: required by `std::convert::From::from`

Expand Down

0 comments on commit 73307ed

Please sign in to comment.