Skip to content

Commit

Permalink
Rollup merge of rust-lang#24072 - ebfull:explain_closure_type_err, r=…
Browse files Browse the repository at this point in the history
…pnkfelix

Also fixed bug calling .note() instead of .help()

See rust-lang#24036
  • Loading branch information
Manishearth committed Apr 12, 2015
2 parents a1e3c25 + 3308c06 commit 4ebc16c
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/librustc/middle/infer/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,9 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
fn report_and_explain_type_error(&self,
trace: TypeTrace<'tcx>,
terr: &ty::type_err<'tcx>) {
let span = trace.origin.span();
self.report_type_error(trace, terr);
ty::note_and_explain_type_err(self.tcx, terr);
ty::note_and_explain_type_err(self.tcx, terr, span);
}

/// Returns a string of the form "expected `{}`, found `{}`", or None if this is a derived
Expand Down Expand Up @@ -812,7 +813,7 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
}
self.give_suggestion(same_regions);
for &(ref trace, terr) in trace_origins {
self.report_type_error(trace.clone(), &terr);
self.report_and_explain_type_error(trace.clone(), &terr);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
error_str));

if let Some(err) = err {
ty::note_and_explain_type_err(self.tcx, err)
ty::note_and_explain_type_err(self.tcx, err, sp)
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5103,7 +5103,7 @@ pub fn type_err_to_str<'tcx>(cx: &ctxt<'tcx>, err: &type_err<'tcx>) -> String {
}
}

pub fn note_and_explain_type_err(cx: &ctxt, err: &type_err) {
pub fn note_and_explain_type_err<'tcx>(cx: &ctxt<'tcx>, err: &type_err<'tcx>, sp: Span) {
match *err {
terr_regions_does_not_outlive(subregion, superregion) => {
note_and_explain_region(cx, "", subregion, "...");
Expand Down Expand Up @@ -5134,6 +5134,16 @@ pub fn note_and_explain_type_err(cx: &ctxt, err: &type_err) {
"expected concrete lifetime is ",
conc_region, "");
}
terr_sorts(values) => {
let expected_str = ty_sort_string(cx, values.expected);
let found_str = ty_sort_string(cx, values.found);
if expected_str == found_str && expected_str == "closure" {
cx.sess.span_note(sp, &format!("no two closures, even if identical, have the same \
type"));
cx.sess.span_help(sp, &format!("consider boxing your closure and/or \
using it as a trait object"));
}
}
_ => {}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl Session {
self.diagnostic().handler().note(msg)
}
pub fn help(&self, msg: &str) {
self.diagnostic().handler().note(msg)
self.diagnostic().handler().help(msg)
}
pub fn opt_span_bug(&self, opt_sp: Option<Span>, msg: &str) -> ! {
match opt_sp {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3575,7 +3575,7 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
.ty_to_string(
actual_structure_type),
type_error_description);
ty::note_and_explain_type_err(tcx, &type_error);
ty::note_and_explain_type_err(tcx, &type_error, path.span);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ fn require_same_types<'a, 'tcx, M>(tcx: &ty::ctxt<'tcx>,
msg(),
ty::type_err_to_str(tcx,
terr));
ty::note_and_explain_type_err(tcx, terr);
ty::note_and_explain_type_err(tcx, terr, span);
false
}
}
Expand Down
30 changes: 30 additions & 0 deletions src/test/compile-fail/issue-24036.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn closure_to_loc() {
let mut x = |c| c + 1;
x = |c| c + 1;
//~^ ERROR mismatched types
//~| NOTE no two closures, even if identical, have the same type
//~| HELP consider boxing your closure and/or using it as a trait object
}

fn closure_from_match() {
let x = match 1usize {
1 => |c| c + 1,
2 => |c| c - 1,
_ => |c| c - 1
};
//~^^^^^ ERROR match arms have incompatible types
//~| NOTE no two closures, even if identical, have the same type
//~| HELP consider boxing your closure and/or using it as a trait object
}

fn main() { }

0 comments on commit 4ebc16c

Please sign in to comment.