Skip to content

Commit

Permalink
Rollup merge of rust-lang#81995 - 0yoyoyo:fix-issue-81650-explicit-li…
Browse files Browse the repository at this point in the history
…fetime-error, r=estebank

Fix suggestion to introduce explicit lifetime

Addresses rust-lang#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 |_| {
   |           ^^^^^
```
  • Loading branch information
JohnTitor authored Feb 12, 2021
2 parents 863af9d + 788e4bb commit 2a83fdf
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
7 changes: 6 additions & 1 deletion compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2248,13 +2248,18 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
"...",
);
if let Some(infer::RelateParamBound(_, t)) = origin {
let return_impl_trait = self
.in_progress_typeck_results
.map(|typeck_results| typeck_results.borrow().hir_owner)
.and_then(|owner| self.tcx.return_type_impl_trait(owner))
.is_some();
let t = self.resolve_vars_if_possible(t);
match t.kind() {
// We've got:
// fn get_later<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
// suggest:
// fn get_later<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
ty::Closure(_, _substs) | ty::Opaque(_, _substs) => {
ty::Closure(_, _substs) | ty::Opaque(_, _substs) if return_impl_trait => {
new_binding_suggestion(&mut err, type_param_span, bound_kind);
}
_ => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Regression test for #81650

struct Foo<'a> {
x: &'a mut &'a i32,
}

impl<'a> Foo<'a> {
fn bar<F, T>(&self, f: F)
where
F: FnOnce(&Foo<'a>) -> T,
F: 'a,
{}
}

trait Test {
fn test(&self);
}

fn func<T: Test>(foo: &Foo, t: T) {
foo.bar(move |_| {
//~^ ERROR the parameter type `T` may not live long enough
t.test();
});
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0311]: the parameter type `T` may not live long enough
--> $DIR/missing-lifetimes-in-signature-2.rs:20:9
|
LL | fn func<T: Test>(foo: &Foo, t: T) {
| -- help: consider adding an explicit lifetime bound...: `T: 'a +`
LL | foo.bar(move |_| {
| ^^^
|
note: the parameter type `T` must be valid for the anonymous lifetime #2 defined on the function body at 19:1...
--> $DIR/missing-lifetimes-in-signature-2.rs:19:1
|
LL | fn func<T: Test>(foo: &Foo, t: T) {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...so that the type `[closure@$DIR/missing-lifetimes-in-signature-2.rs:20:13: 23:6]` will meet its required lifetime bounds
--> $DIR/missing-lifetimes-in-signature-2.rs:20:9
|
LL | foo.bar(move |_| {
| ^^^

error: aborting due to previous error

0 comments on commit 2a83fdf

Please sign in to comment.