Skip to content

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

Open
@LilyIsTrans

Description

@LilyIsTrans

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-type-systemArea: Type systemD-confusingDiagnostics: Confusing error or lint that should be reworked.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions