rust-lang / rust-lang/rust

Misleading "`match` arms have incompatible types" when match expression used in unimplemented operation

Open
#119,683 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-type-system D-confusing T-compiler T-types
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

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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.