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

Unhelpful error message not pointing to the issue #119665

Closed
xNxExOx opened this issue Jan 6, 2024 · 1 comment · Fixed by #121657
Closed

Unhelpful error message not pointing to the issue #119665

xNxExOx opened this issue Jan 6, 2024 · 1 comment · Fixed by #121657
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The parsing of Rust source code to an AST D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@xNxExOx
Copy link

xNxExOx commented Jan 6, 2024

Code

struct MyStruct {
    foo: String
}

fn ok() -> MyStruct {
    MyStruct {
        foo: std::env::temp_dir().join("something").to_str().unwrap().to_string()
    }
}

fn bad_refactor() -> MyStruct {
    let foo: std::env::temp_dir().join("something").to_str().unwrap().to_string();
    MyStruct {
        foo
    }
}

Current output

error: expected one of `!`, `+`, `->`, `::`, `;`, or `=`, found `.`
  --> src/main.rs:14:34
   |
12 |     let foo: std::env::temp_dir().join("something").to_str().unwrap().to_string();
   |                                  ^ expected one of `!`, `+`, `->`, `::`, `;`, or `=`

Desired output

error: did you mean `=` instead of `:`?
  --> src/main.rs:14:34
   |
12 |     let foo: std::env::temp_dir().join("something").to_str().unwrap().to_string();
   |            ^ did you mean `=` instead of `:`?

OR
error: expected type, found function `expresion`
  --> src/main.rs:14:34
   |
12 |     let foo: std::env::temp_dir().join("something").to_str().unwrap().to_string();
   |            --^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not a type
   |            |
   |            help: use `=` if you meant to assign

Rationale and extra context

I was just been asked about this in not too much complicated code, and we was both thinking, why join does not work, if there needs to be some use ... to bring it to the scope.

When I tried to write the simplified example I got much better error. (help: use `=` if you meant to assign)

Other cases

fn ok2() -> MyStruct {
    MyStruct {
        foo: some_fn()
    }
}

fn bad_refactor2() -> MyStruct {
    let foo: some_fn();
    MyStruct {
        foo
    }
}

fn some_fn() -> String {
    "".into()
}

// produces:
error[E0573]: expected type, found function `some_fn`
  --> src/main.rs:25:14
   |
25 |     let foo: some_fn();
   |            --^^^^^^^^^ not a type
   |            |
   |            help: use `=` if you meant to assign

Anything else?

No response

@xNxExOx xNxExOx 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 6, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 6, 2024
@jieyouxu
Copy link
Member

@rustbot label +A-parser +D-newcomer-roadblock -needs-triage

@rustbot rustbot added A-parser Area: The parsing of Rust source code to an AST D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 14, 2024
estebank added a commit to estebank/rust that referenced this issue Feb 27, 2024
When a `Local` is fully parsed, but not followed by a `;`, keep the `:` span
arround and mention it. If the type could continue being parsed as an
expression, suggest replacing the `:` with a `=`.

```
error: expected one of `!`, `+`, `->`, `::`, `;`, or `=`, found `.`
 --> file.rs:2:32
  |
2 |     let _: std::env::temp_dir().join("foo");
  |          -                     ^ expected one of `!`, `+`, `->`, `::`, `;`, or `=`
  |          |
  |          while parsing the type for `_`
  |          help: use `=` if you meant to assign
```

Fix rust-lang#119665.
bors added a commit to rust-lang-ci/rust that referenced this issue Mar 1, 2024
Detect more cases of `=` to `:` typo

When a `Local` is fully parsed, but not followed by a `;`, keep the `:` span arround and mention it. If the type could continue being parsed as an expression, suggest replacing the `:` with a `=`.

```
error: expected one of `!`, `+`, `->`, `::`, `;`, or `=`, found `.`
 --> file.rs:2:32
  |
2 |     let _: std::env::temp_dir().join("foo");
  |          -                     ^ expected one of `!`, `+`, `->`, `::`, `;`, or `=`
  |          |
  |          while parsing the type for `_`
  |          help: use `=` if you meant to assign
```

Fix rust-lang#119665.
bors added a commit to rust-lang-ci/rust that referenced this issue Mar 2, 2024
Detect more cases of `=` to `:` typo

When a `Local` is fully parsed, but not followed by a `;`, keep the `:` span arround and mention it. If the type could continue being parsed as an expression, suggest replacing the `:` with a `=`.

```
error: expected one of `!`, `+`, `->`, `::`, `;`, or `=`, found `.`
 --> file.rs:2:32
  |
2 |     let _: std::env::temp_dir().join("foo");
  |          -                     ^ expected one of `!`, `+`, `->`, `::`, `;`, or `=`
  |          |
  |          while parsing the type for `_`
  |          help: use `=` if you meant to assign
```

Fix rust-lang#119665.
@bors bors closed this as completed in bde2dfb Mar 2, 2024
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 A-parser Area: The parsing of Rust source code to an AST D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. 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.

3 participants