Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 22318f1

Browse files
committed
Account for blocks in arguments
When giving an error about an obligation introduced by a function call that an argument doesn't fulfill, and that argument is a block, add a span_label pointing at the innermost tail expression.
1 parent 569a842 commit 22318f1

File tree

1 file changed

+22
-2
lines changed
  • compiler/rustc_trait_selection/src/traits/error_reporting

1 file changed

+22
-2
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2296,19 +2296,39 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
22962296
});
22972297
}
22982298
ObligationCauseCode::FunctionArgumentObligation {
2299-
arg_hir_id: _,
2299+
arg_hir_id,
23002300
call_hir_id,
23012301
ref parent_code,
23022302
} => {
23032303
let hir = self.tcx.hir();
2304+
if let Some(Node::Expr(expr @ hir::Expr { kind: hir::ExprKind::Block(..), .. })) =
2305+
hir.find(arg_hir_id)
2306+
{
2307+
let in_progress_typeck_results =
2308+
self.in_progress_typeck_results.map(|t| t.borrow());
2309+
let parent_id = hir.local_def_id(hir.get_parent_item(arg_hir_id));
2310+
let typeck_results: &TypeckResults<'tcx> = match &in_progress_typeck_results {
2311+
Some(t) if t.hir_owner == parent_id => t,
2312+
_ => self.tcx.typeck(parent_id),
2313+
};
2314+
let ty = typeck_results.expr_ty_adjusted(expr);
2315+
err.span_label(
2316+
expr.peel_blocks().span,
2317+
&if ty.references_error() {
2318+
String::new()
2319+
} else {
2320+
format!("this tail expression is of type `{:?}`", ty)
2321+
},
2322+
);
2323+
}
23042324
if let Some(Node::Expr(hir::Expr {
23052325
kind:
23062326
hir::ExprKind::Call(hir::Expr { span, .. }, _)
23072327
| hir::ExprKind::MethodCall(_, span, ..),
23082328
..
23092329
})) = hir.find(call_hir_id)
23102330
{
2311-
err.span_label(*span, "required by a bound in this call");
2331+
err.span_label(*span, "required by a bound introduced by this call");
23122332
}
23132333
ensure_sufficient_stack(|| {
23142334
self.note_obligation_cause_code(

0 commit comments

Comments
 (0)