rust-lang / rust-lang/rust

E0308 Suggestion gets added to unrelated code and error span is too big

Open
#134,445 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-macros D-imprecise-spans T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
#[tokio::main]
async fn main() {
    if true { 
        return Ok(());
    }
    let something_else = 1;
}
Current output
error[E0308]: mismatched types
 --> src/main.rs:2:17
  |
2 |   async fn main() {
  |  _________________^
3 | |     if true { 
4 | |         return Ok(());
5 | |     }
6 | |     let something_else = 1;
7 | | }
  | |_^ expected `Result<(), _>`, found `()`
  |
  = note:   expected enum `Result<(), _>`
          found unit type `()`

error[E0308]: mismatched types
 --> src/main.rs:3:5
  |
2 |   async fn main() {
  |                  - expected `()` because of default return type
3 | /     if true { 
4 | |         return Ok(());
5 | |     }
6 | |     let something_else = 1;
  | |___________________________^ expected `()`, found `Result<(), _>`
  |
  = note: expected unit type `()`
                  found enum `Result<(), _>`
help: consider using `Result::expect` to unwrap the `Result<(), _>` value, panicking if the value is a `Result::Err`
  |
6 |     let something_else = 1;.expect("REASON")
  |                            +++++++++++++++++
Desired output
error[E0308]: mismatched types
 --> src/main.rs:3:5
  |
2 |   async fn main() {
  |                  - expected `()` because of default return type
3 |       if true { 
4 |           return Ok(());
  |           ^^^^^^^^^^^^^^ expected `()`, found `Result<(), _>`
  |
  = note: expected unit type `()`
                  found enum `Result<(), _>`
help: consider using `Result::expect` to unwrap the `Result<(), _>` value, panicking if the value is a `Result::Err`
  |
4 |     return Ok(()).expect("REASON");
  |                  +++++++++++++++++
Rationale and extra context

This is probably just caused by the #[tokio::main] proc-macro. It's confusing that absolutely everything after (and inside of) the bad return statement scope is seen as a bad statement, and that the help section suggests adding .expect("REASON") after a semicolon at the very end.

Found this because copy-pasted code contained return Ok(());, which caused the scope and everything after it to be highlighted as an error with no further explanation.

Other cases

Rust Version
rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-unknown-linux-gnu
release: 1.83.0
LLVM version: 19.1.1
Anything else?

Return statements without a scope and at the end get highlighted semi-correctly:

#[tokio::main]
async fn main() {
    return Ok(());
}

Produces

error[E0308]: mismatched types
 --> src/main.rs:3:5
  |
2 | async fn main() {
  |                - expected `()` because of default return type
3 |     return Ok(());
  |     ^^^^^^^^^^^^^^ expected `()`, found `Result<(), _>`
  |
  = note: expected unit type `()`
                  found enum `Result<(), _>`
help: consider using `Result::expect` to unwrap the `Result<(), _>` value, panicking if the value is a `Result::Err`
  |
3 |     return Ok(());.expect("REASON")
  |                   +++++++++++++++++

The error span is fine, but the help isn't

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the E0308 diagnostic with the provided #[tokio::main] example using rustc 1.83.0, then compare the current and desired output. Investigate how the proc-macro-expanded return statement determines the error span and help suggestion. Done means the diagnostic highlights only return Ok(()) and places the suggested .expect("REASON") correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.