Skip to content

E0310 - Invalid suggestion when type is a function with a dyn return type #120223

Closed
@bradzacher

Description

@bradzacher

Code

use std::{future::Future};

pub fn foo<T>(
    executor: impl FnOnce(T) -> dyn Future<Output = ()>,
) -> Box<dyn FnOnce(T) -> dyn Future<Output = ()>> {
    Box::new(executor)
}

Current output

error[E0310]: the parameter type `impl FnOnce(T) -> dyn Future<Output = ()>` may not live long enough
 --> src/lib.rs:6:5
  |
6 |     Box::new(executor)
  |     ^^^^^^^^^^^^^^^^^^
  |     |
  |     the parameter type `impl FnOnce(T) -> dyn Future<Output = ()>` must be valid for the static lifetime...
  |     ...so that the type `impl FnOnce(T) -> dyn Future<Output = ()>` will meet its required lifetime bounds
  |
help: consider adding an explicit lifetime bound
  |
4 |     executor: impl FnOnce(T) -> dyn Future<Output = ()> + 'static,
  |                                                         +++++++++

Desired output

error[E0310]: the parameter type `impl FnOnce(T) -> dyn Future<Output = ()>` may not live long enough
 --> src/lib.rs:6:5
  |
6 |     Box::new(executor)
  |     ^^^^^^^^^^^^^^^^^^
  |     |
  |     the parameter type `impl FnOnce(T) -> dyn Future<Output = ()>` must be valid for the static lifetime...
  |     ...so that the type `impl FnOnce(T) -> dyn Future<Output = ()>` will meet its required lifetime bounds
  |
help: consider adding an explicit lifetime bound
  |
4 |     executor: impl FnOnce(T) -> (dyn Future<Output = ()>) + 'static,
  |                                 +                       +++++++++++

Rationale and extra context

The issue is that the diagnostic suggests adding + 'static
However performing this modification exactly as specified results in a syntax error:

error: ambiguous `+` in a type
 --> src/lib.rs:4:33
  |
4 |     executor: impl FnOnce(T) -> dyn Future<Output = ()> + 'static,
  |                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use parentheses to disambiguate: `(dyn Future<Output = ()> + 'static)`                                                     ^^^^^^^^^^^^ expected a path

And then if you follow this syntax error's suggestion, you get back to the original state:

error[E0310]: the parameter type `impl FnOnce(T) -> (dyn Future<Output = ()> + 'static)` may not live long enough
 --> src/lib.rs:6:5
  |
6 |     Box::new(executor)
  |     ^^^^^^^^^^^^^^^^^^
  |     |
  |     the parameter type `impl FnOnce(T) -> (dyn Future<Output = ()> + 'static)` must be valid for the static lifetime...
  |     ...so that the type `impl FnOnce(T) -> (dyn Future<Output = ()> + 'static)` will meet its required lifetime bounds
  |
help: consider adding an explicit lifetime bound
  |
4 |     executor: impl FnOnce(T) -> (dyn Future<Output = ()> + 'static) + 'static,
  |                                                                     +++++++++

For more information about this error, try `rustc --explain E0310`.

However if instead it suggested wrapping the return value in () and then adding + static - you'd get to the right state immediately (I think? I'm still a rust newbie)

Other cases

No response

Rust Version

Stable: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=233f12d3bc7a1958232137e4b27e2921

Anything else?

No response

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions