Same enum values result in unequal

Hi, I encountered a wild issue, here is my enum:

#[derive(Clone, PartialEq)]
pub(crate) enum TransactionStatus {
    Active,
    Aborted,
    Committed,
}

When I do the comparison, it may failed even the values are the same (here the status is &TransactionStatus::Committed:

if *status == TransactionStatus::Committed {
    return true;
} else {
    return false;
}

or:

if status.Clone() == TransactionStatus::Committed {
    return true;
} else {
    return false;
}

And this is my debug screenshot, you can see it ran to the else branch:

We'll need an SSCCE that we can run, in order to help. There's nothing we can investigate with a screenshot.

1 Like

Ok, I will make an SSCCE later, thanks for your feedback!

Don't believe any IDE-integrated tooling. They are notoriously wrong all the time.

Quite obviously, the Rust toolchain (including the compiler and the built-in derive macros) that have been around for 10+ years aren't so full of bugs that a trivial equality comparison on an enum wouldn't work correctly. It cannot be reproduced.

Either your IDE/debugger is misleading you, or you have undefined behavior in some unsafe code that causes weird remote action at a distance like this.

1 Like

I am wondering whether you are debugging release mode, or at least with some optimizations enabled. Generally debugger is far more unreliable in this case, though this usually manifests as inability to do some actions (inspect variables, step on exact lines, etc) and not false data.

1 Like

Have you tried printing the status and the return value for sanity? If so, what does it output, and is it consistent?

2 Likes

this should be writable as return *status == TransactionStatus::Committed btw.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.