Skip to content

Commit 37cb1d4

Browse files
committed
Auto merge of #24072 - ebfull:explain_closure_type_err, r=pnkfelix
Also fixed bug calling .note() instead of .help() See #24036
2 parents a1e3c25 + 3308c06 commit 37cb1d4

File tree

7 files changed

+48
-7
lines changed

7 files changed

+48
-7
lines changed

src/librustc/middle/infer/error_reporting.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,9 @@ impl<'a, 'tcx> ErrorReporting<'tcx> for InferCtxt<'a, 'tcx> {
373373
fn report_and_explain_type_error(&self,
374374
trace: TypeTrace<'tcx>,
375375
terr: &ty::type_err<'tcx>) {
376+
let span = trace.origin.span();
376377
self.report_type_error(trace, terr);
377-
ty::note_and_explain_type_err(self.tcx, terr);
378+
ty::note_and_explain_type_err(self.tcx, terr, span);
378379
}
379380

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

src/librustc/middle/infer/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
987987
error_str));
988988

989989
if let Some(err) = err {
990-
ty::note_and_explain_type_err(self.tcx, err)
990+
ty::note_and_explain_type_err(self.tcx, err, sp)
991991
}
992992
}
993993
}

src/librustc/middle/ty.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5103,7 +5103,7 @@ pub fn type_err_to_str<'tcx>(cx: &ctxt<'tcx>, err: &type_err<'tcx>) -> String {
51035103
}
51045104
}
51055105

5106-
pub fn note_and_explain_type_err(cx: &ctxt, err: &type_err) {
5106+
pub fn note_and_explain_type_err<'tcx>(cx: &ctxt<'tcx>, err: &type_err<'tcx>, sp: Span) {
51075107
match *err {
51085108
terr_regions_does_not_outlive(subregion, superregion) => {
51095109
note_and_explain_region(cx, "", subregion, "...");
@@ -5134,6 +5134,16 @@ pub fn note_and_explain_type_err(cx: &ctxt, err: &type_err) {
51345134
"expected concrete lifetime is ",
51355135
conc_region, "");
51365136
}
5137+
terr_sorts(values) => {
5138+
let expected_str = ty_sort_string(cx, values.expected);
5139+
let found_str = ty_sort_string(cx, values.found);
5140+
if expected_str == found_str && expected_str == "closure" {
5141+
cx.sess.span_note(sp, &format!("no two closures, even if identical, have the same \
5142+
type"));
5143+
cx.sess.span_help(sp, &format!("consider boxing your closure and/or \
5144+
using it as a trait object"));
5145+
}
5146+
}
51375147
_ => {}
51385148
}
51395149
}

src/librustc/session/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ impl Session {
163163
self.diagnostic().handler().note(msg)
164164
}
165165
pub fn help(&self, msg: &str) {
166-
self.diagnostic().handler().note(msg)
166+
self.diagnostic().handler().help(msg)
167167
}
168168
pub fn opt_span_bug(&self, opt_sp: Option<Span>, msg: &str) -> ! {
169169
match opt_sp {

src/librustc_typeck/check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3575,7 +3575,7 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
35753575
.ty_to_string(
35763576
actual_structure_type),
35773577
type_error_description);
3578-
ty::note_and_explain_type_err(tcx, &type_error);
3578+
ty::note_and_explain_type_err(tcx, &type_error, path.span);
35793579
}
35803580
}
35813581
}

src/librustc_typeck/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ fn require_same_types<'a, 'tcx, M>(tcx: &ty::ctxt<'tcx>,
200200
msg(),
201201
ty::type_err_to_str(tcx,
202202
terr));
203-
ty::note_and_explain_type_err(tcx, terr);
203+
ty::note_and_explain_type_err(tcx, terr, span);
204204
false
205205
}
206206
}

src/test/compile-fail/issue-24036.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn closure_to_loc() {
12+
let mut x = |c| c + 1;
13+
x = |c| c + 1;
14+
//~^ ERROR mismatched types
15+
//~| NOTE no two closures, even if identical, have the same type
16+
//~| HELP consider boxing your closure and/or using it as a trait object
17+
}
18+
19+
fn closure_from_match() {
20+
let x = match 1usize {
21+
1 => |c| c + 1,
22+
2 => |c| c - 1,
23+
_ => |c| c - 1
24+
};
25+
//~^^^^^ ERROR match arms have incompatible types
26+
//~| NOTE no two closures, even if identical, have the same type
27+
//~| HELP consider boxing your closure and/or using it as a trait object
28+
}
29+
30+
fn main() { }

0 commit comments

Comments
 (0)