Skip to content

Commit 8b64988

Browse files
Fix error message with non-tupled bare fn trait
1 parent 4033686 commit 8b64988

File tree

10 files changed

+101
-38
lines changed

10 files changed

+101
-38
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1750,6 +1750,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
17501750
if let Some(ValuePairs::PolyTraitRefs(exp_found)) = values
17511751
&& let ty::Closure(def_id, _) = exp_found.expected.skip_binder().self_ty().kind()
17521752
&& let Some(def_id) = def_id.as_local()
1753+
&& terr.involves_regions()
17531754
{
17541755
let span = self.tcx.def_span(def_id);
17551756
diag.span_note(span, "this closure does not fulfill the lifetime requirements");

compiler/rustc_infer/src/infer/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1935,6 +1935,18 @@ impl<'tcx> TypeTrace<'tcx> {
19351935
}
19361936
}
19371937

1938+
pub fn poly_trait_refs(
1939+
cause: &ObligationCause<'tcx>,
1940+
a_is_expected: bool,
1941+
a: ty::PolyTraitRef<'tcx>,
1942+
b: ty::PolyTraitRef<'tcx>,
1943+
) -> TypeTrace<'tcx> {
1944+
TypeTrace {
1945+
cause: cause.clone(),
1946+
values: PolyTraitRefs(ExpectedFound::new(a_is_expected, a.into(), b.into())),
1947+
}
1948+
}
1949+
19381950
pub fn consts(
19391951
cause: &ObligationCause<'tcx>,
19401952
a_is_expected: bool,

compiler/rustc_middle/src/ty/error.rs

+12
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ pub enum TypeError<'tcx> {
7474
TargetFeatureCast(DefId),
7575
}
7676

77+
impl TypeError<'_> {
78+
pub fn involves_regions(self) -> bool {
79+
match self {
80+
TypeError::RegionsDoesNotOutlive(_, _)
81+
| TypeError::RegionsInsufficientlyPolymorphic(_, _)
82+
| TypeError::RegionsOverlyPolymorphic(_, _)
83+
| TypeError::RegionsPlaceholderMismatch => true,
84+
_ => false,
85+
}
86+
}
87+
}
88+
7789
/// Explains the source of a type err in a short, human readable way. This is meant to be placed
7890
/// in parentheses after some larger message. You should also invoke `note_and_explain_type_err()`
7991
/// afterwards to present additional details, particularly when it comes to lifetime-related

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

+27-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use rustc_hir::intravisit::Visitor;
2222
use rustc_hir::GenericParam;
2323
use rustc_hir::Item;
2424
use rustc_hir::Node;
25+
use rustc_infer::infer::TypeTrace;
2526
use rustc_infer::traits::TraitEngine;
2627
use rustc_middle::traits::select::OverflowError;
2728
use rustc_middle::ty::abstract_const::NotConstEvaluatable;
@@ -941,20 +942,43 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
941942

942943
self.reported_closure_mismatch.borrow_mut().insert((span, found_span));
943944

945+
let mut not_tupled = false;
946+
944947
let found = match found_trait_ref.skip_binder().substs.type_at(1).kind() {
945948
ty::Tuple(ref tys) => vec![ArgKind::empty(); tys.len()],
946-
_ => vec![ArgKind::empty()],
949+
_ => {
950+
not_tupled = true;
951+
vec![ArgKind::empty()]
952+
}
947953
};
948954

949955
let expected_ty = expected_trait_ref.skip_binder().substs.type_at(1);
950956
let expected = match expected_ty.kind() {
951957
ty::Tuple(ref tys) => {
952958
tys.iter().map(|t| ArgKind::from_expected_ty(t, Some(span))).collect()
953959
}
954-
_ => vec![ArgKind::Arg("_".to_owned(), expected_ty.to_string())],
960+
_ => {
961+
not_tupled = true;
962+
vec![ArgKind::Arg("_".to_owned(), expected_ty.to_string())]
963+
}
955964
};
956965

957-
if found.len() == expected.len() {
966+
// If this is a `Fn` family trait and either the expected or found
967+
// is not tupled, then fall back to just a regular mismatch error.
968+
// This shouldn't be common unless manually implementing one of the
969+
// traits manually, but don't make it more confusing when it does
970+
// happen.
971+
if Some(expected_trait_ref.def_id()) != tcx.lang_items().gen_trait() && not_tupled {
972+
self.report_and_explain_type_error(
973+
TypeTrace::poly_trait_refs(
974+
&obligation.cause,
975+
true,
976+
expected_trait_ref,
977+
found_trait_ref,
978+
),
979+
ty::error::TypeError::Mismatch,
980+
)
981+
} else if found.len() == expected.len() {
958982
self.report_closure_arg_mismatch(
959983
span,
960984
found_span,

src/test/ui/mismatched_types/E0631.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn bar<F: Fn<usize>>(_: F) {}
55
fn main() {
66
fn f(_: u64) {}
77
foo(|_: isize| {}); //~ ERROR type mismatch
8-
bar(|_: isize| {}); //~ ERROR type mismatch
8+
bar(|_: isize| {}); //~ ERROR mismatched types
99
foo(f); //~ ERROR type mismatch
10-
bar(f); //~ ERROR type mismatch
10+
bar(f); //~ ERROR mismatched types
1111
}

src/test/ui/mismatched_types/E0631.stderr

+10-14
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@ note: required by a bound in `foo`
1414
LL | fn foo<F: Fn(usize)>(_: F) {}
1515
| ^^^^^^^^^ required by this bound in `foo`
1616

17-
error[E0631]: type mismatch in closure arguments
17+
error[E0308]: mismatched types
1818
--> $DIR/E0631.rs:8:5
1919
|
2020
LL | bar(|_: isize| {});
21-
| ^^^ ---------- found signature defined here
22-
| |
23-
| expected due to this
21+
| ^^^ types differ
2422
|
25-
= note: expected closure signature `fn(usize) -> _`
26-
found closure signature `fn(isize) -> _`
23+
= note: expected trait `Fn<usize>`
24+
found trait `Fn<(isize,)>`
2725
note: required by a bound in `bar`
2826
--> $DIR/E0631.rs:4:11
2927
|
@@ -49,19 +47,16 @@ note: required by a bound in `foo`
4947
LL | fn foo<F: Fn(usize)>(_: F) {}
5048
| ^^^^^^^^^ required by this bound in `foo`
5149

52-
error[E0631]: type mismatch in function arguments
50+
error[E0308]: mismatched types
5351
--> $DIR/E0631.rs:10:9
5452
|
55-
LL | fn f(_: u64) {}
56-
| ------------ found signature defined here
57-
...
5853
LL | bar(f);
59-
| --- ^ expected due to this
54+
| --- ^ types differ
6055
| |
6156
| required by a bound introduced by this call
6257
|
63-
= note: expected function signature `fn(usize) -> _`
64-
found function signature `fn(u64) -> _`
58+
= note: expected trait `Fn<usize>`
59+
found trait `Fn<(u64,)>`
6560
note: required by a bound in `bar`
6661
--> $DIR/E0631.rs:4:11
6762
|
@@ -70,4 +65,5 @@ LL | fn bar<F: Fn<usize>>(_: F) {}
7065

7166
error: aborting due to 4 previous errors
7267

73-
For more information about this error, try `rustc --explain E0631`.
68+
Some errors have detailed explanations: E0308, E0631.
69+
For more information about an error, try `rustc --explain E0308`.

src/test/ui/mismatched_types/closure-arg-count.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ fn main() {
1111
[1, 2, 3].sort_by(|(tuple, tuple2): (usize, _)| panic!());
1212
//~^ ERROR closure is expected to take
1313
f(|| panic!());
14-
//~^ ERROR closure is expected to take
14+
//~^ ERROR mismatched types
1515
f( move || panic!());
16-
//~^ ERROR closure is expected to take
16+
//~^ ERROR mismatched types
1717

1818
let _it = vec![1, 2, 3].into_iter().enumerate().map(|i, x| i);
1919
//~^ ERROR closure is expected to take

src/test/ui/mismatched_types/closure-arg-count.stderr

+10-17
Original file line numberDiff line numberDiff line change
@@ -45,41 +45,33 @@ help: change the closure to take multiple arguments instead of a single tuple
4545
LL | [1, 2, 3].sort_by(|tuple, tuple2| panic!());
4646
| ~~~~~~~~~~~~~~~
4747

48-
error[E0593]: closure is expected to take 1 argument, but it takes 0 arguments
48+
error[E0308]: mismatched types
4949
--> $DIR/closure-arg-count.rs:13:5
5050
|
5151
LL | f(|| panic!());
52-
| ^ -- takes 0 arguments
53-
| |
54-
| expected closure that takes 1 argument
52+
| ^ types differ
5553
|
54+
= note: expected trait `Fn<usize>`
55+
found trait `Fn<()>`
5656
note: required by a bound in `f`
5757
--> $DIR/closure-arg-count.rs:3:9
5858
|
5959
LL | fn f<F: Fn<usize>>(_: F) {}
6060
| ^^^^^^^^^ required by this bound in `f`
61-
help: consider changing the closure to take and ignore the expected argument
62-
|
63-
LL | f(|_| panic!());
64-
| ~~~
6561

66-
error[E0593]: closure is expected to take 1 argument, but it takes 0 arguments
62+
error[E0308]: mismatched types
6763
--> $DIR/closure-arg-count.rs:15:5
6864
|
6965
LL | f( move || panic!());
70-
| ^ ---------- takes 0 arguments
71-
| |
72-
| expected closure that takes 1 argument
66+
| ^ types differ
7367
|
68+
= note: expected trait `Fn<usize>`
69+
found trait `Fn<()>`
7470
note: required by a bound in `f`
7571
--> $DIR/closure-arg-count.rs:3:9
7672
|
7773
LL | fn f<F: Fn<usize>>(_: F) {}
7874
| ^^^^^^^^^ required by this bound in `f`
79-
help: consider changing the closure to take and ignore the expected argument
80-
|
81-
LL | f( move |_| panic!());
82-
| ~~~
8375

8476
error[E0593]: closure is expected to take a single 2-tuple as argument, but it takes 2 distinct arguments
8577
--> $DIR/closure-arg-count.rs:18:53
@@ -198,4 +190,5 @@ LL | fn call<F, R>(_: F) where F: FnOnce() -> R {}
198190

199191
error: aborting due to 14 previous errors
200192

201-
For more information about this error, try `rustc --explain E0593`.
193+
Some errors have detailed explanations: E0308, E0593.
194+
For more information about an error, try `rustc --explain E0308`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![feature(unboxed_closures)]
2+
3+
fn a<F: Fn<usize>>(f: F) {}
4+
5+
fn main() {
6+
a(|_: usize| {});
7+
//~^ ERROR mismatched types
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/non-tupled-arg-mismatch.rs:6:5
3+
|
4+
LL | a(|_: usize| {});
5+
| ^ types differ
6+
|
7+
= note: expected trait `Fn<usize>`
8+
found trait `Fn<(usize,)>`
9+
note: required by a bound in `a`
10+
--> $DIR/non-tupled-arg-mismatch.rs:3:9
11+
|
12+
LL | fn a<F: Fn<usize>>(f: F) {}
13+
| ^^^^^^^^^ required by this bound in `a`
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)