Misleading "`match` arms have incompatible types" when match expression used in unimplemented operation
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
Code
pub struct Instant(chrono::NaiveDateTime);
pub fn example() {
use std::ops::Bound;
use std::ops::RangeBounds;
let hours = Instant(chrono::NaiveDateTime::MIN)..=Instant(chrono::NaiveDateTime::MAX);
let total_hours = match hours.end_bound() {
Bound::Included(a) => a,
_ => unreachable!(),
} - match hours.start_bound() {
Bound::Included(a) => a,
_ => unreachable!(),
};
}
Current output
error[E0308]: `match` arms have incompatible types
--> src/lib.rs:22:14
|
20 | } - match hours.start_bound() {
| _________-
21 | | Bound::Included(a) => a,
| | - this is found to be of type `&Instant`
22 | | _ => unreachable!(),
| | ^^^^^^^^^^^^^^ expected `&Instant`, found `!`
23 | | };
| |_____- `match` arms have incompatible types
|
= note: expected reference `&Instant`
found type `!`
= note: this error originates in the macro `$crate::panic::unreachable_2021` which comes from the expansion of the macro `unreachable` (in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try `rustc --explain E0308`.
Desired output
error[E0369]: cannot subtract `&Instant` from `&Instant`
--> src/lib.rs:20:7
|
20 | } - match hours.start_bound() {
| ^ no implementation for `&Instant - &Instant`
|
note: an implementation of `Sub` might be missing for `Instant`
--> src/lib.rs:9:1
|
9 | pub struct Instant(chrono::NaiveDateTime);
| ^^^^^^^^^^^^^^^^^^ must implement `Sub`
note: the trait `Sub` must be implemented
--> /home/lily/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/arith.rs:184:1
|
184 | pub trait Sub<Rhs = Self> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^
For more information about this error, try `rustc --explain E0369`.
Rationale and extra context
The error goes away if std::ops::Sub is implemented for &Instant
Other cases
Output is identical on latest stable, beta, and nightly toolchains
Anything else?
I think this might be related to this issue (actually I initially thought it was the same, but that issue appears to have been fixed in the latest beta and nightly releases, while this has not).
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.
Research direction
Start by reproducing the example in src/lib.rs on stable, beta, and nightly, then trace the compiler's handling of match expression typing and subtraction diagnostics. Compare the result with the Sub trait definition referenced in core/src/ops/arith.rs; done means the missing-operation diagnostic is reported instead of the misleading incompatible-match-arms error.
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
- 42/100