The compilation error prompt points to the wrong location
Open
@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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.