Skip to content

Commit 620b0f1

Browse files
committed
improve type mismatch error for functions
This also fixes the argument names in `report_closure_arg_mismatch` (confusing expected/found)
1 parent 96b9bb4 commit 620b0f1

File tree

1 file changed

+20
-13
lines changed
  • compiler/rustc_trait_selection/src/traits/error_reporting

1 file changed

+20
-13
lines changed

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use rustc_hir::def_id::DefId;
2020
use rustc_hir::intravisit::Visitor;
2121
use rustc_hir::lang_items::LangItem;
2222
use rustc_hir::{AsyncGeneratorKind, GeneratorKind, Node};
23+
use rustc_infer::infer::TyCtxtInferExt;
2324
use rustc_middle::hir::map;
2425
use rustc_middle::ty::{
2526
self, suggest_arbitrary_trait_bound, suggest_constraining_type_param, AdtKind, DefIdTree,
@@ -253,8 +254,8 @@ pub trait InferCtxtExt<'tcx> {
253254
&self,
254255
span: Span,
255256
found_span: Option<Span>,
256-
expected_ref: ty::PolyTraitRef<'tcx>,
257257
found: ty::PolyTraitRef<'tcx>,
258+
expected: ty::PolyTraitRef<'tcx>,
258259
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed>;
259260

260261
fn suggest_fully_qualified_path(
@@ -1529,13 +1530,13 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
15291530
&self,
15301531
span: Span,
15311532
found_span: Option<Span>,
1532-
expected_ref: ty::PolyTraitRef<'tcx>,
15331533
found: ty::PolyTraitRef<'tcx>,
1534+
expected: ty::PolyTraitRef<'tcx>,
15341535
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
1535-
pub(crate) fn build_fn_sig_string<'tcx>(
1536+
pub(crate) fn build_fn_sig_ty<'tcx>(
15361537
tcx: TyCtxt<'tcx>,
15371538
trait_ref: ty::PolyTraitRef<'tcx>,
1538-
) -> String {
1539+
) -> Ty<'tcx> {
15391540
let inputs = trait_ref.skip_binder().substs.type_at(1);
15401541
let sig = match inputs.kind() {
15411542
ty::Tuple(inputs)
@@ -1557,10 +1558,11 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
15571558
abi::Abi::Rust,
15581559
),
15591560
};
1560-
trait_ref.rebind(sig).to_string()
1561+
1562+
tcx.mk_fn_ptr(trait_ref.rebind(sig))
15611563
}
15621564

1563-
let argument_kind = match expected_ref.skip_binder().self_ty().kind() {
1565+
let argument_kind = match expected.skip_binder().self_ty().kind() {
15641566
ty::Closure(..) => "closure",
15651567
ty::Generator(..) => "generator",
15661568
_ => "function",
@@ -1569,17 +1571,22 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
15691571
self.tcx.sess,
15701572
span,
15711573
E0631,
1572-
"type mismatch in {} arguments",
1573-
argument_kind
1574+
"type mismatch in {argument_kind} arguments",
15741575
);
15751576

1576-
let found_str = format!("expected signature of `{}`", build_fn_sig_string(self.tcx, found));
1577-
err.span_label(span, found_str);
1577+
err.span_label(span, "expected due to this");
15781578

15791579
let found_span = found_span.unwrap_or(span);
1580-
let expected_str =
1581-
format!("found signature of `{}`", build_fn_sig_string(self.tcx, expected_ref));
1582-
err.span_label(found_span, expected_str);
1580+
err.span_label(found_span, "found signature defined here");
1581+
1582+
let expected = build_fn_sig_ty(self.tcx, expected);
1583+
let found = build_fn_sig_ty(self.tcx, found);
1584+
1585+
let (expected_str, found_str) =
1586+
self.tcx.infer_ctxt().enter(|infcx| infcx.cmp(expected, found));
1587+
1588+
let signature_kind = format!("{argument_kind} signature");
1589+
err.note_expected_found(&signature_kind, expected_str, &signature_kind, found_str);
15831590

15841591
err
15851592
}

0 commit comments

Comments
 (0)