Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
bradzacher opened this issue Jan 22, 2024 · 1 comment · Fixed by #120929
Closed

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

bradzacher opened this issue Jan 22, 2024 · 1 comment · Fixed by #120929
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@bradzacher
Copy link

bradzacher commented Jan 22, 2024

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

@bradzacher bradzacher added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 22, 2024
@long-long-float
Copy link
Contributor

@rustbot claim

@bors bors closed this as completed in 80f2b91 Apr 23, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Apr 23, 2024
Rollup merge of rust-lang#120929 - long-long-float:wrap-dyn-in-suggestion, r=fmease

Wrap dyn type with parentheses in suggestion

Close rust-lang#120223

Fix wrong suggestion that is grammatically incorrect.
Specifically, I added parentheses to dyn types that need lifetime bound.

```
help: consider adding an explicit lifetime bound
  |
4 |     executor: impl FnOnce(T) -> (dyn Future<Output = ()>) + 'static,
  |                                 +                       +++++++++++
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants