Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
LilyIsTrans opened this issue Jan 7, 2024 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-typesystem Area: The type system D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@LilyIsTrans
Copy link

LilyIsTrans commented Jan 7, 2024

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

@LilyIsTrans LilyIsTrans added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 7, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 7, 2024
@jieyouxu
Copy link
Member

@rustbot label +D-confusing +A-typesystem +T-types -needs-triage

@rustbot rustbot added A-typesystem Area: The type system D-confusing Diagnostics: Confusing error or lint that should be reworked. T-types Relevant to the types team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-typesystem Area: The type system D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants