Skip to content

Commit aa7eb65

Browse files
authored
Rollup merge of rust-lang#39905 - estebank:useless-error, r=arielb1
Properly display note/expected details Given a file ```rust fn takes_cb(f: fn(i8)) {} fn main() { fn callback(x: i32) {} takes_cb(callback) } ``` output ```rust error[E0308]: mismatched types --> file2.rs:5:22 | 5 | takes_cb(callback) | ^^^^^^^^ expected i8, found i32 | = note: expected type `fn(i8)` found type `fn(i32) {main::callback}` ``` Fix rust-lang#39343.
2 parents a692417 + 038a166 commit aa7eb65

File tree

6 files changed

+37
-30
lines changed

6 files changed

+37
-30
lines changed

src/librustc/infer/error_reporting.rs

+27-26
Original file line numberDiff line numberDiff line change
@@ -379,40 +379,41 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
379379
values: Option<ValuePairs<'tcx>>,
380380
terr: &TypeError<'tcx>)
381381
{
382-
let expected_found = match values {
383-
None => None,
384-
Some(values) => match self.values_str(&values) {
385-
Some((expected, found)) => Some((expected, found)),
386-
None => {
387-
// Derived error. Cancel the emitter.
388-
self.tcx.sess.diagnostic().cancel(diag);
389-
return
390-
}
382+
let (expected_found, is_simple_error) = match values {
383+
None => (None, false),
384+
Some(values) => {
385+
let is_simple_error = match values {
386+
ValuePairs::Types(exp_found) => {
387+
exp_found.expected.is_primitive() && exp_found.found.is_primitive()
388+
}
389+
_ => false,
390+
};
391+
let vals = match self.values_str(&values) {
392+
Some((expected, found)) => Some((expected, found)),
393+
None => {
394+
// Derived error. Cancel the emitter.
395+
self.tcx.sess.diagnostic().cancel(diag);
396+
return
397+
}
398+
};
399+
(vals, is_simple_error)
391400
}
392401
};
393402

394403
let span = cause.span;
395404

396405
if let Some((expected, found)) = expected_found {
397-
let is_simple_error = if let &TypeError::Sorts(ref values) = terr {
398-
values.expected.is_primitive() && values.found.is_primitive()
399-
} else {
400-
false
401-
};
402-
403-
if !is_simple_error {
404-
if expected == found {
405-
if let &TypeError::Sorts(ref values) = terr {
406-
diag.note_expected_found_extra(
407-
&"type", &expected, &found,
408-
&format!(" ({})", values.expected.sort_string(self.tcx)),
409-
&format!(" ({})", values.found.sort_string(self.tcx)));
410-
} else {
411-
diag.note_expected_found(&"type", &expected, &found);
412-
}
413-
} else {
406+
match (terr, is_simple_error, expected == found) {
407+
(&TypeError::Sorts(ref values), false, true) => {
408+
diag.note_expected_found_extra(
409+
&"type", &expected, &found,
410+
&format!(" ({})", values.expected.sort_string(self.tcx)),
411+
&format!(" ({})", values.found.sort_string(self.tcx)));
412+
}
413+
(_, false, _) => {
414414
diag.note_expected_found(&"type", &expected, &found);
415415
}
416+
_ => (),
416417
}
417418
}
418419

src/test/compile-fail/default_ty_param_conflict.rs

-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ fn main() {
2323
// Here, F is instantiated with $0=uint
2424
let x = foo();
2525
//~^ ERROR: mismatched types
26-
//~| expected type `usize`
27-
//~| found type `isize`
2826
//~| NOTE: conflicting type parameter defaults `usize` and `isize`
2927
//~| NOTE: conflicting type parameter defaults `usize` and `isize`
3028
//~| NOTE: ...that was applied to an unconstrained type variable here

src/test/compile-fail/default_ty_param_conflict_cross_crate.rs

-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,4 @@ fn main() {
2929
//~| NOTE: conflicting type parameter defaults `bool` and `char`
3030
//~| a second default is defined on `default_param_test::bleh`
3131
//~| NOTE: ...that was applied to an unconstrained type variable here
32-
//~| expected type `bool`
33-
//~| found type `char`
3432
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,19 @@ impl Foo for Bar {
2323
fn foo(_: fn(u16) -> ()) {}
2424
//~^ ERROR method `foo` has an incompatible type for trait
2525
//~| NOTE expected u8
26+
//~| NOTE expected type `fn(fn(u8))`
2627
fn bar(_: Option<u16>) {}
2728
//~^ ERROR method `bar` has an incompatible type for trait
2829
//~| NOTE expected u8
30+
//~| NOTE expected type `fn(std::option::Option<u8>)`
2931
fn baz(_: (u16, u16)) {}
3032
//~^ ERROR method `baz` has an incompatible type for trait
3133
//~| NOTE expected u8
34+
//~| NOTE expected type `fn((u8, u16))`
3235
fn qux() -> u16 { 5u16 }
3336
//~^ ERROR method `qux` has an incompatible type for trait
3437
//~| NOTE expected u8
38+
//~| NOTE expected type `fn() -> u8`
3539
}
3640

3741
fn main() {}

src/test/ui/mismatched_types/E0053.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ error[E0053]: method `foo` has an incompatible type for trait
66
...
77
19 | fn foo(x: i16) { }
88
| ^^^ expected u16, found i16
9+
|
10+
= note: expected type `fn(u16)`
11+
found type `fn(i16)`
912

1013
error[E0053]: method `bar` has an incompatible type for trait
1114
--> $DIR/E0053.rs:22:12

src/test/ui/mismatched_types/trait-impl-fn-incompatibility.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ error[E0053]: method `foo` has an incompatible type for trait
66
...
77
21 | fn foo(x: i16) { }
88
| ^^^ expected u16, found i16
9+
|
10+
= note: expected type `fn(u16)`
11+
found type `fn(i16)`
912

1013
error[E0053]: method `bar` has an incompatible type for trait
1114
--> $DIR/trait-impl-fn-incompatibility.rs:22:28

0 commit comments

Comments
 (0)