Skip to content

Commit 7b2c698

Browse files
committed
better suggestion based on hir
Signed-off-by: Alex Chi <[email protected]>
1 parent 50a8097 commit 7b2c698

File tree

6 files changed

+200
-45
lines changed

6 files changed

+200
-45
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1883,7 +1883,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
18831883
{
18841884
let span = self.tcx.def_span(def_id);
18851885
diag.span_note(span, "this closure does not fulfill the lifetime requirements");
1886-
self.suggest_for_all_lifetime_closure(span, &exp_found, diag);
1886+
self.suggest_for_all_lifetime_closure(span, self.tcx.hir().get_by_def_id(def_id), &exp_found, diag);
18871887
}
18881888

18891889
// It reads better to have the error origin as the final

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

+48-40
Original file line numberDiff line numberDiff line change
@@ -559,54 +559,62 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
559559
pub(super) fn suggest_for_all_lifetime_closure(
560560
&self,
561561
span: Span,
562+
hir: hir::Node<'_>,
562563
exp_found: &ty::error::ExpectedFound<ty::PolyTraitRef<'tcx>>,
563564
diag: &mut Diagnostic,
564565
) {
566+
// 0. Extract fn_decl from hir
567+
let hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(hir::Closure { fn_decl, .. }), .. }) = hir else { return; };
568+
565569
// 1. Get the substs of the closure.
566570
// 2. Assume exp_found is FnOnce / FnMut / Fn, we can extract function parameters from [1].
567-
let expected = exp_found.expected.map_bound(|x| x.substs.get(1).cloned()).transpose();
568-
let found = exp_found.found.map_bound(|x| x.substs.get(1).cloned()).transpose();
569-
571+
let Some(expected) = exp_found.expected.skip_binder().substs.get(1) else { return; };
572+
let Some(found) = exp_found.found.skip_binder().substs.get(1) else { return; };
573+
let expected = expected.unpack();
574+
let found = found.unpack();
570575
// 3. Extract the tuple type from Fn trait and suggest the change.
571-
if let (Some(expected), Some(found)) = (expected, found) {
572-
let expected = expected.skip_binder().unpack();
573-
let found = found.skip_binder().unpack();
574-
if let (GenericArgKind::Type(expected), GenericArgKind::Type(found)) = (expected, found)
575-
&& let (ty::Tuple(expected), ty::Tuple(found)) = (expected.kind(), found.kind())
576-
&& expected.len() == found.len() {
577-
let mut suggestion = "|".to_string();
578-
let mut is_first = true;
579-
let mut has_suggestion = false;
580-
581-
for (expected, found) in expected.iter().zip(found.iter()) {
582-
if is_first {
583-
is_first = true;
584-
} else {
585-
suggestion += ", ";
586-
}
587-
588-
if let (ty::Ref(expected_region, _, _), ty::Ref(found_region, _, _)) = (expected.kind(), found.kind())
589-
&& expected_region.is_late_bound() && !found_region.is_late_bound() {
590-
// If the expected region is late bound, and the found region is not, we can suggest adding `: &_`.
591-
// FIXME: use the actual type + variable name provided by user instead of `_`.
592-
suggestion += "_: &_";
593-
has_suggestion = true;
594-
} else {
595-
// Otherwise, keep it as-is.
596-
suggestion += "_";
597-
}
598-
}
599-
suggestion += "|";
576+
if let GenericArgKind::Type(expected) = expected &&
577+
let GenericArgKind::Type(found) = found &&
578+
let ty::Tuple(expected) = expected.kind() &&
579+
let ty::Tuple(found)= found.kind() &&
580+
expected.len() == found.len() {
581+
let mut suggestion = "|".to_string();
582+
let mut is_first = true;
583+
let mut has_suggestion = false;
584+
585+
for ((expected, found), arg_hir) in expected.iter().zip(found.iter()).zip(fn_decl.inputs.iter()) {
586+
if is_first {
587+
is_first = false;
588+
} else {
589+
suggestion += ", ";
590+
}
600591

601-
if has_suggestion {
602-
diag.span_suggestion_verbose(
603-
span,
604-
"consider changing the type of the closure parameters",
605-
suggestion,
606-
Applicability::MaybeIncorrect,
607-
);
608-
}
592+
if let ty::Ref(expected_region, _, _) = expected.kind() &&
593+
let ty::Ref(found_region, _, _) = found.kind() &&
594+
expected_region.is_late_bound() &&
595+
!found_region.is_late_bound() &&
596+
let hir::TyKind::Infer = arg_hir.kind {
597+
// If the expected region is late bound, the found region is not, and users are asking compiler
598+
// to infer the type, we can suggest adding `: &_`.
599+
let Ok(arg) = self.tcx.sess.source_map().span_to_snippet(arg_hir.span) else { return; };
600+
suggestion += &format!("{}: &_", arg);
601+
has_suggestion = true;
602+
} else {
603+
let Ok(arg) = self.tcx.sess.source_map().span_to_snippet(arg_hir.span) else { return; };
604+
// Otherwise, keep it as-is.
605+
suggestion += &arg;
609606
}
607+
}
608+
suggestion += "|";
609+
610+
if has_suggestion {
611+
diag.span_suggestion_verbose(
612+
span,
613+
"consider specifying the type of the closure parameters",
614+
suggestion,
615+
Applicability::MaybeIncorrect,
616+
);
617+
}
610618
}
611619
}
612620
}

tests/ui/lifetimes/issue-105675.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
fn thing(x: impl FnOnce(&u32, &u32)) {}
2+
3+
fn main() {
4+
let f = |_, _| ();
5+
thing(f);
6+
//~^ ERROR mismatched types
7+
//~^^ ERROR mismatched types
8+
//~^^^ ERROR implementation of `FnOnce` is not general enough
9+
//~^^^^ ERROR implementation of `FnOnce` is not general enough
10+
let f = |x, y| ();
11+
thing(f);
12+
//~^ ERROR mismatched types
13+
//~^^ ERROR mismatched types
14+
//~^^^ ERROR implementation of `FnOnce` is not general enough
15+
//~^^^^ ERROR implementation of `FnOnce` is not general enough
16+
}
+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-105675.rs:5:5
3+
|
4+
LL | thing(f);
5+
| ^^^^^^^^ one type is more general than the other
6+
|
7+
= note: expected trait `for<'a, 'b> FnOnce<(&'a u32, &'b u32)>`
8+
found trait `FnOnce<(&u32, &u32)>`
9+
note: this closure does not fulfill the lifetime requirements
10+
--> $DIR/issue-105675.rs:4:13
11+
|
12+
LL | let f = |_, _| ();
13+
| ^^^^^^
14+
note: the lifetime requirement is introduced here
15+
--> $DIR/issue-105675.rs:1:18
16+
|
17+
LL | fn thing(x: impl FnOnce(&u32, &u32)) {}
18+
| ^^^^^^^^^^^^^^^^^^
19+
help: consider specifying the type of the closure parameters
20+
|
21+
LL | let f = |_: &_, _: &_| ();
22+
| ~~~~~~~~~~~~~~
23+
24+
error[E0308]: mismatched types
25+
--> $DIR/issue-105675.rs:5:5
26+
|
27+
LL | thing(f);
28+
| ^^^^^^^^ one type is more general than the other
29+
|
30+
= note: expected trait `for<'a, 'b> FnOnce<(&'a u32, &'b u32)>`
31+
found trait `FnOnce<(&u32, &u32)>`
32+
note: this closure does not fulfill the lifetime requirements
33+
--> $DIR/issue-105675.rs:4:13
34+
|
35+
LL | let f = |_, _| ();
36+
| ^^^^^^
37+
note: the lifetime requirement is introduced here
38+
--> $DIR/issue-105675.rs:1:18
39+
|
40+
LL | fn thing(x: impl FnOnce(&u32, &u32)) {}
41+
| ^^^^^^^^^^^^^^^^^^
42+
help: consider specifying the type of the closure parameters
43+
|
44+
LL | let f = |_: &_, _: &_| ();
45+
| ~~~~~~~~~~~~~~
46+
47+
error: implementation of `FnOnce` is not general enough
48+
--> $DIR/issue-105675.rs:5:5
49+
|
50+
LL | thing(f);
51+
| ^^^^^^^^ implementation of `FnOnce` is not general enough
52+
|
53+
= note: closure with signature `fn(&'2 u32, &u32)` must implement `FnOnce<(&'1 u32, &u32)>`, for any lifetime `'1`...
54+
= note: ...but it actually implements `FnOnce<(&'2 u32, &u32)>`, for some specific lifetime `'2`
55+
56+
error: implementation of `FnOnce` is not general enough
57+
--> $DIR/issue-105675.rs:5:5
58+
|
59+
LL | thing(f);
60+
| ^^^^^^^^ implementation of `FnOnce` is not general enough
61+
|
62+
= note: closure with signature `fn(&u32, &'2 u32)` must implement `FnOnce<(&u32, &'1 u32)>`, for any lifetime `'1`...
63+
= note: ...but it actually implements `FnOnce<(&u32, &'2 u32)>`, for some specific lifetime `'2`
64+
65+
error[E0308]: mismatched types
66+
--> $DIR/issue-105675.rs:11:5
67+
|
68+
LL | thing(f);
69+
| ^^^^^^^^ one type is more general than the other
70+
|
71+
= note: expected trait `for<'a, 'b> FnOnce<(&'a u32, &'b u32)>`
72+
found trait `FnOnce<(&u32, &u32)>`
73+
note: this closure does not fulfill the lifetime requirements
74+
--> $DIR/issue-105675.rs:10:13
75+
|
76+
LL | let f = |x, y| ();
77+
| ^^^^^^
78+
note: the lifetime requirement is introduced here
79+
--> $DIR/issue-105675.rs:1:18
80+
|
81+
LL | fn thing(x: impl FnOnce(&u32, &u32)) {}
82+
| ^^^^^^^^^^^^^^^^^^
83+
help: consider specifying the type of the closure parameters
84+
|
85+
LL | let f = |x: &_, y: &_| ();
86+
| ~~~~~~~~~~~~~~
87+
88+
error[E0308]: mismatched types
89+
--> $DIR/issue-105675.rs:11:5
90+
|
91+
LL | thing(f);
92+
| ^^^^^^^^ one type is more general than the other
93+
|
94+
= note: expected trait `for<'a, 'b> FnOnce<(&'a u32, &'b u32)>`
95+
found trait `FnOnce<(&u32, &u32)>`
96+
note: this closure does not fulfill the lifetime requirements
97+
--> $DIR/issue-105675.rs:10:13
98+
|
99+
LL | let f = |x, y| ();
100+
| ^^^^^^
101+
note: the lifetime requirement is introduced here
102+
--> $DIR/issue-105675.rs:1:18
103+
|
104+
LL | fn thing(x: impl FnOnce(&u32, &u32)) {}
105+
| ^^^^^^^^^^^^^^^^^^
106+
help: consider specifying the type of the closure parameters
107+
|
108+
LL | let f = |x: &_, y: &_| ();
109+
| ~~~~~~~~~~~~~~
110+
111+
error: implementation of `FnOnce` is not general enough
112+
--> $DIR/issue-105675.rs:11:5
113+
|
114+
LL | thing(f);
115+
| ^^^^^^^^ implementation of `FnOnce` is not general enough
116+
|
117+
= note: closure with signature `fn(&'2 u32, &u32)` must implement `FnOnce<(&'1 u32, &u32)>`, for any lifetime `'1`...
118+
= note: ...but it actually implements `FnOnce<(&'2 u32, &u32)>`, for some specific lifetime `'2`
119+
120+
error: implementation of `FnOnce` is not general enough
121+
--> $DIR/issue-105675.rs:11:5
122+
|
123+
LL | thing(f);
124+
| ^^^^^^^^ implementation of `FnOnce` is not general enough
125+
|
126+
= note: closure with signature `fn(&u32, &'2 u32)` must implement `FnOnce<(&u32, &'1 u32)>`, for any lifetime `'1`...
127+
= note: ...but it actually implements `FnOnce<(&u32, &'2 u32)>`, for some specific lifetime `'2`
128+
129+
error: aborting due to 8 previous errors
130+
131+
For more information about this error, try `rustc --explain E0308`.

tests/ui/lifetimes/issue-79187.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ note: the lifetime requirement is introduced here
1616
|
1717
LL | fn thing(x: impl FnOnce(&u32)) {}
1818
| ^^^^^^^^^^^^
19-
help: consider changing the type of the closure parameters
19+
help: consider specifying the type of the closure parameters
2020
|
2121
LL | let f = |_: &_| ();
2222
| ~~~~~~~

tests/ui/mismatched_types/closure-mismatch.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ note: the lifetime requirement is introduced here
2525
|
2626
LL | fn baz<T: Foo>(_: T) {}
2727
| ^^^
28-
help: consider changing the type of the closure parameters
28+
help: consider specifying the type of the closure parameters
2929
|
3030
LL | baz(|_: &_| ());
3131
| ~~~~~~~
@@ -57,9 +57,9 @@ note: the lifetime requirement is introduced here
5757
|
5858
LL | fn baz<T: Foo>(_: T) {}
5959
| ^^^
60-
help: consider changing the type of the closure parameters
60+
help: consider specifying the type of the closure parameters
6161
|
62-
LL | baz(|_: &_| ());
62+
LL | baz(|x: &_| ());
6363
| ~~~~~~~
6464

6565
error: aborting due to 4 previous errors

0 commit comments

Comments
 (0)