Skip to content

Commit 788e4bb

Browse files
committed
Fix suggestion to introduce explicit lifetime
1 parent 07194ff commit 788e4bb

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -2248,13 +2248,18 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
22482248
"...",
22492249
);
22502250
if let Some(infer::RelateParamBound(_, t)) = origin {
2251+
let return_impl_trait = self
2252+
.in_progress_typeck_results
2253+
.map(|typeck_results| typeck_results.borrow().hir_owner)
2254+
.and_then(|owner| self.tcx.return_type_impl_trait(owner))
2255+
.is_some();
22512256
let t = self.resolve_vars_if_possible(t);
22522257
match t.kind() {
22532258
// We've got:
22542259
// fn get_later<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
22552260
// suggest:
22562261
// fn get_later<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
2257-
ty::Closure(_, _substs) | ty::Opaque(_, _substs) => {
2262+
ty::Closure(_, _substs) | ty::Opaque(_, _substs) if return_impl_trait => {
22582263
new_binding_suggestion(&mut err, type_param_span, bound_kind);
22592264
}
22602265
_ => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Regression test for #81650
2+
3+
struct Foo<'a> {
4+
x: &'a mut &'a i32,
5+
}
6+
7+
impl<'a> Foo<'a> {
8+
fn bar<F, T>(&self, f: F)
9+
where
10+
F: FnOnce(&Foo<'a>) -> T,
11+
F: 'a,
12+
{}
13+
}
14+
15+
trait Test {
16+
fn test(&self);
17+
}
18+
19+
fn func<T: Test>(foo: &Foo, t: T) {
20+
foo.bar(move |_| {
21+
//~^ ERROR the parameter type `T` may not live long enough
22+
t.test();
23+
});
24+
}
25+
26+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0311]: the parameter type `T` may not live long enough
2+
--> $DIR/missing-lifetimes-in-signature-2.rs:20:9
3+
|
4+
LL | fn func<T: Test>(foo: &Foo, t: T) {
5+
| -- help: consider adding an explicit lifetime bound...: `T: 'a +`
6+
LL | foo.bar(move |_| {
7+
| ^^^
8+
|
9+
note: the parameter type `T` must be valid for the anonymous lifetime #2 defined on the function body at 19:1...
10+
--> $DIR/missing-lifetimes-in-signature-2.rs:19:1
11+
|
12+
LL | fn func<T: Test>(foo: &Foo, t: T) {
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
note: ...so that the type `[closure@$DIR/missing-lifetimes-in-signature-2.rs:20:13: 23:6]` will meet its required lifetime bounds
15+
--> $DIR/missing-lifetimes-in-signature-2.rs:20:9
16+
|
17+
LL | foo.bar(move |_| {
18+
| ^^^
19+
20+
error: aborting due to previous error
21+

0 commit comments

Comments
 (0)