Skip to content

Commit 14b217c

Browse files
authored
Rollup merge of #81995 - 0yoyoyo:fix-issue-81650-explicit-lifetime-error, r=estebank
Fix suggestion to introduce explicit lifetime Addresses #81650 Error message after fix: ``` error[E0311]: the parameter type `T` may not live long enough --> src/main.rs:25:11 | 24 | fn play_with<T: Animal + Send>(scope: &Scope, animal: T) { | -- help: consider adding an explicit lifetime bound...: `T: 'a +` 25 | scope.spawn(move |_| { | ^^^^^ | note: the parameter type `T` must be valid for the anonymous lifetime #2 defined on the function body at 24:1... --> src/main.rs:24:1 | 24 | fn play_with<T: Animal + Send>(scope: &Scope, animal: T) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: ...so that the type `[closure@src/main.rs:25:17: 27:6]` will meet its required lifetime bounds --> src/main.rs:25:11 | 25 | scope.spawn(move |_| { | ^^^^^ ```
2 parents a390206 + fcce998 commit 14b217c

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-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,17 @@
1+
error[E0311]: the parameter type `T` may not live long enough
2+
--> $DIR/missing-lifetimes-in-signature-2.rs:20:5
3+
|
4+
LL | / foo.bar(move |_| {
5+
LL | |
6+
LL | | t.test();
7+
LL | | });
8+
| |______^
9+
|
10+
note: the parameter type `T` must be valid for the anonymous lifetime #2 defined on the function body at 19:1...
11+
--> $DIR/missing-lifetimes-in-signature-2.rs:19:1
12+
|
13+
LL | fn func<T: Test>(foo: &Foo, t: T) {
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
16+
error: aborting due to previous error
17+
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)