rust-lang / rust-lang/rust

The compilation error prompt points to the wrong location

Open
#146,190 3 comments 0 reactions 1 assignee View on GitHub

@sgasho is already working on this.

Since Sep 14, 2025.

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

Description

I tried this code:

fn main() {
    let oa = Some(1);
    let oa2 = Some(1);
    let v = if let Some(a) = oa {
        Some(&a)
    } else if let Some(a) = oa2 {
        &Some(a)
    } else {
        None
    };
    println!("{v:?}");
}

I expected to see this happen:

&Some(a)
^^^^^^^^ expected `Option<&{integer}>`, found `&Option<{integer}>`

Instead, this happened:

None
^^^^ expected `&Option<{integer}>`, found `Option<_>`
Meta

rustc --version --verbose:

rustc 1.91.0-nightly (a1208bf76 2025-09-03)
binary: rustc
commit-hash: a1208bf765ba783ee4ebdc4c29ab0a0c215806ef
commit-date: 2025-09-03
host: x86_64-unknown-linux-gnu
release: 1.91.0-nightly
LLVM version: 21.1.0
Backtrace

$> RUST_BACKTRACE=1 cargo build
   Compiling test_proj v0.1.0 (/home/fawdlstty/workspace/test_proj)
error[E0308]: `if` and `else` have incompatible types
  --> src/main.rs:9:9
   |
 6 |       } else if let Some(a) = oa2 {
   |  ____________-
 7 | |         &Some(a)
   | |         -------- expected because of this
 8 | |     } else {
 9 | |         None
   | |         ^^^^ expected `&Option<{integer}>`, found `Option<_>`
10 | |     };
   | |_____- `if` and `else` have incompatible types
   |
   = note: expected reference `&Option<{integer}>`
                   found enum `Option<_>`
help: consider using `Option::expect` to unwrap the `Option<_>` value, panicking if the value is an `Option::None`
   |
10 |     }.expect("REASON");
   |      +++++++++++++++++

For more information about this error, try `rustc --explain E0308`.
error: could not compile `test_proj` (bin "test_proj") due to 1 previous error
$>

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.