Skip to content

Commit d9a947c

Browse files
committed
use new note_expected_found API
This API pulls the "expected type foo, found type bar" out after the main snippet. There are some other places where it makes sense, but this is a start.
1 parent 11dc974 commit d9a947c

File tree

6 files changed

+46
-49
lines changed

6 files changed

+46
-49
lines changed

src/librustc/infer/error_reporting.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,12 @@ pub trait ErrorReporting<'tcx> {
249249
terr: &TypeError<'tcx>)
250250
-> DiagnosticBuilder<'tcx>;
251251

252-
fn values_str(&self, values: &ValuePairs<'tcx>) -> Option<String>;
252+
fn values_str(&self, values: &ValuePairs<'tcx>) -> Option<(String, String)>;
253253

254254
fn expected_found_str<T: fmt::Display + Resolvable<'tcx> + TypeFoldable<'tcx>>(
255255
&self,
256256
exp_found: &ty::error::ExpectedFound<T>)
257-
-> Option<String>;
257+
-> Option<(String, String)>;
258258

259259
fn report_concrete_failure(&self,
260260
origin: SubregionOrigin<'tcx>,
@@ -535,7 +535,7 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
535535
trace: TypeTrace<'tcx>,
536536
terr: &TypeError<'tcx>)
537537
-> DiagnosticBuilder<'tcx> {
538-
let expected_found_str = match self.values_str(&trace.values) {
538+
let (expected, found) = match self.values_str(&trace.values) {
539539
Some(v) => v,
540540
None => {
541541
return self.tcx.sess.diagnostic().struct_dummy(); /* derived error */
@@ -548,18 +548,17 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
548548
false
549549
};
550550

551-
let expected_found_str = if is_simple_error {
552-
expected_found_str
553-
} else {
554-
format!("{} ({})", expected_found_str, terr)
555-
};
556-
557551
let mut err = struct_span_err!(self.tcx.sess,
558552
trace.origin.span(),
559553
E0308,
560-
"{}: {}",
561-
trace.origin,
562-
expected_found_str);
554+
"{}",
555+
trace.origin);
556+
557+
if !is_simple_error {
558+
err = err.note_expected_found(&"type", &expected, &found);
559+
}
560+
561+
err = err.span_label(trace.origin.span(), &terr);
563562

564563
self.check_and_note_conflicting_crates(&mut err, terr, trace.origin.span());
565564

@@ -574,6 +573,7 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
574573
},
575574
_ => ()
576575
}
576+
577577
err
578578
}
579579

@@ -631,7 +631,7 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
631631

632632
/// Returns a string of the form "expected `{}`, found `{}`", or None if this is a derived
633633
/// error.
634-
fn values_str(&self, values: &ValuePairs<'tcx>) -> Option<String> {
634+
fn values_str(&self, values: &ValuePairs<'tcx>) -> Option<(String, String)> {
635635
match *values {
636636
infer::Types(ref exp_found) => self.expected_found_str(exp_found),
637637
infer::TraitRefs(ref exp_found) => self.expected_found_str(exp_found),
@@ -642,7 +642,7 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
642642
fn expected_found_str<T: fmt::Display + Resolvable<'tcx> + TypeFoldable<'tcx>>(
643643
&self,
644644
exp_found: &ty::error::ExpectedFound<T>)
645-
-> Option<String>
645+
-> Option<(String, String)>
646646
{
647647
let expected = exp_found.expected.resolve(self);
648648
if expected.references_error() {
@@ -654,9 +654,7 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
654654
return None;
655655
}
656656

657-
Some(format!("expected `{}`, found `{}`",
658-
expected,
659-
found))
657+
Some((format!("{}", expected), format!("{}", found)))
660658
}
661659

662660
fn report_generic_bound_failure(&self,
@@ -1751,11 +1749,11 @@ impl<'a, 'tcx> ErrorReportingHelpers<'tcx> for InferCtxt<'a, 'tcx> {
17511749
};
17521750

17531751
match self.values_str(&trace.values) {
1754-
Some(values_str) => {
1752+
Some((expected, found)) => {
17551753
err.span_note(
17561754
trace.origin.span(),
1757-
&format!("...so that {} ({})",
1758-
desc, values_str));
1755+
&format!("...so that {} (expected {}, found {})",
1756+
desc, expected, found));
17591757
}
17601758
None => {
17611759
// Really should avoid printing this error at

src/librustc_typeck/check/_match.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ pub fn check_pat<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
116116

117117
// Check that the types of the end-points can be unified.
118118
let types_unify = require_same_types(
119-
tcx, Some(fcx.infcx()), false, pat.span, rhs_ty, lhs_ty,
120-
|| "mismatched types in range".to_string()
119+
tcx, Some(fcx.infcx()), false, pat.span, rhs_ty, lhs_ty,
120+
"mismatched types in range",
121121
);
122122

123123
// It's ok to return without a message as `require_same_types` prints an error.

src/librustc_typeck/check/intrinsic.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ fn equate_intrinsic_type<'a, 'tcx>(tcx: &TyCtxt<'tcx>, it: &hir::ForeignItem,
6161
it.span,
6262
i_ty.ty,
6363
fty,
64-
|| {
65-
format!("intrinsic has wrong type: expected `{}`",
66-
fty)
67-
});
64+
"intrinsic has wrong type");
6865
}
6966
}
7067

src/librustc_typeck/check/wfcheck.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,7 @@ impl<'ccx, 'tcx> CheckTypeWellFormedVisitor<'ccx, 'tcx> {
418418
let _ = ::require_same_types(
419419
fcx.tcx(), Some(fcx.infcx()), false, span,
420420
sig.inputs[0], rcvr_ty,
421-
|| "mismatched method receiver".to_owned()
422-
);
421+
"mismatched method receiver");
423422
}
424423

425424
fn check_variances_for_type_defn(&self,

src/librustc_typeck/collect.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,8 +1044,9 @@ fn convert_enum_def<'tcx>(tcx: &TyCtxt<'tcx>,
10441044
-> ty::AdtDefMaster<'tcx>
10451045
{
10461046
fn print_err(tcx: &TyCtxt, span: Span, ty: ty::Ty, cv: ConstVal) {
1047-
span_err!(tcx.sess, span, E0079, "mismatched types: expected `{}` got `{}`",
1048-
ty, cv.description());
1047+
struct_span_err!(tcx.sess, span, E0079, "mismatched types")
1048+
.note_expected_found(&"type", &ty, &format!("{}", cv.description()))
1049+
.emit();
10491050
}
10501051
fn evaluate_disr_expr<'tcx>(tcx: &TyCtxt<'tcx>,
10511052
repr_ty: attr::IntType,

src/librustc_typeck/lib.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,14 @@ fn require_c_abi_if_variadic(tcx: &TyCtxt,
185185
}
186186
}
187187

188-
fn require_same_types<'a, 'tcx, M>(tcx: &TyCtxt<'tcx>,
189-
maybe_infcx: Option<&infer::InferCtxt<'a, 'tcx>>,
190-
t1_is_expected: bool,
191-
span: Span,
192-
t1: Ty<'tcx>,
193-
t2: Ty<'tcx>,
194-
msg: M)
195-
-> bool where
196-
M: FnOnce() -> String,
188+
fn require_same_types<'a, 'tcx>(tcx: &TyCtxt<'tcx>,
189+
maybe_infcx: Option<&infer::InferCtxt<'a, 'tcx>>,
190+
t1_is_expected: bool,
191+
span: Span,
192+
t1: Ty<'tcx>,
193+
t2: Ty<'tcx>,
194+
msg: &str)
195+
-> bool
197196
{
198197
let result = match maybe_infcx {
199198
None => {
@@ -208,7 +207,17 @@ fn require_same_types<'a, 'tcx, M>(tcx: &TyCtxt<'tcx>,
208207
match result {
209208
Ok(_) => true,
210209
Err(ref terr) => {
211-
let mut err = struct_span_err!(tcx.sess, span, E0211, "{}: {}", msg(), terr);
210+
let mut err = struct_span_err!(tcx.sess, span, E0211, "{}", msg);
211+
err = err.span_label(span, &terr);
212+
let (mut expected_ty, mut found_ty) =
213+
if t1_is_expected {(t1, t2)} else {(t2, t1)};
214+
if let Some(infcx) = maybe_infcx {
215+
expected_ty = infcx.resolve_type_vars_if_possible(&expected_ty);
216+
found_ty = infcx.resolve_type_vars_if_possible(&found_ty);
217+
}
218+
err = err.note_expected_found(&"type",
219+
&expected_ty,
220+
&found_ty);
212221
tcx.note_and_explain_type_err(&mut err, terr, span);
213222
err.emit();
214223
false
@@ -250,10 +259,7 @@ fn check_main_fn_ty(ccx: &CrateCtxt,
250259
});
251260

252261
require_same_types(tcx, None, false, main_span, main_t, se_ty,
253-
|| {
254-
format!("main function expects type: `{}`",
255-
se_ty)
256-
});
262+
"main function has wrong type");
257263
}
258264
_ => {
259265
span_bug!(main_span,
@@ -301,11 +307,7 @@ fn check_start_fn_ty(ccx: &CrateCtxt,
301307
});
302308

303309
require_same_types(tcx, None, false, start_span, start_t, se_ty,
304-
|| {
305-
format!("start function expects type: `{}`",
306-
se_ty)
307-
});
308-
310+
"start function has wrong type");
309311
}
310312
_ => {
311313
span_bug!(start_span,

0 commit comments

Comments
 (0)