Skip to content

Error diagnostic suggests syntactically invalid code #64388

Closed
@cdisselkoen

Description

@cdisselkoen

For the following code (Playground link):

fn foo<'a>(u: &'a usize) -> impl IntoIterator<Item = usize> + 'a {
    std::thread::spawn(move || {
        println!("{}", u);
    });
    vec![]
}

rustc provides the following error and suggestion:

error: cannot infer an appropriate lifetime
 --> src/lib.rs:2:24
  |
2 |       std::thread::spawn(move || {
  |  _____------------------_^
  | |     |
  | |     this return type evaluates to the `'static` lifetime...
3 | |         println!("{}", u);
4 | |     });
  | |_____^ ...but this borrow...
  |
note: ...can't outlive the lifetime 'a as defined on the function body at 1:8
 --> src/lib.rs:1:8
  |
1 | fn foo<'a>(u: &'a usize) -> impl IntoIterator<Item = usize> + 'a {
  |        ^^
help: you can add a constraint to the return type to make it last less than `'static` and match the lifetime 'a as defined on the function body at 1:8
  |
2 |     std::thread::spawn + 'a(move || {
  |     ^^^^^^^^^^^^^^^^^^^^^^^

But applying this suggestion results in a syntax error:

error: expected `:`, found `(`
 --> src/lib.rs:2:28
  |
2 |     std::thread::spawn + 'a(move || {
  |                            ^ expected `:`

Using the Playground link, I reproduced this invalid suggestion on Rust 1.37.0 stable, 1.38.0-beta.2, and 1.39.0-nightly 2019-08-23.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-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