-
-
Notifications
You must be signed in to change notification settings - Fork 41
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
Span
: the Eq
/PartialEq
trait implementation can lead to benign-looking code that is actually buggy.
#32
Comments
(Note: I think what you might want is When doing the API design, I did think about this case and wondered what the right trade off is. In the way things are setup now, it's not that the This is a tough one because the With all that said, the fact that |
I'm calling this a bug in the API because I think we probably need to do something about this unfortunately. |
Span
: the Eq
/PartialEq
trait implementation can lead to benign-looking code that is actually buggy.
I wonder if it would make sense to remove PartialEq from Span and then add types (available via methods or as newtype wrappers or both) that implement PartialEq in different ways. E.G., |
I plan to at least remove the I'm not sure if adapter types make sense yet. The interesting thing is that neither |
I ended up implementing this. There's a new I think this is probably overall the right thing to do here. The trap demonstrated in this issue is extremely easy to fall into. With that said, I do find it annoying to deal with types that don't implement So I'm not quite sure what the right trade-off here is, but I think it makes sense to move to the more conservative stance in Also, this changes was very annoying to do in Jiff because of all the doc tests doing So in summary, I am willing to revisit this issue. |
Pretty much what it says on the tin. The idea here is that two different `Span` values in memory, which compare not equal, might still be the same duration. Obviously I knew this when I added the trait impl originally, and realized it could be a footgun, but I thought its convenience outweighed the footgun. However, I think #32 pretty convincingly argues that it's the wrong default. If this ends up being the wrong decision, we can always add the trait impl back. In particular, I am worried about this making `Span` a very annoying-to-use type. Not implementing basic equality is just super annoying because it's common to want it in tests and other various things where the footgun isn't really relevant. But at least this way, we can fix the mistake in the future. Ref #32
Pretty much what it says on the tin. The idea here is that two different `Span` values in memory, which compare not equal, might still be the same duration. Obviously I knew this when I added the trait impl originally, and realized it could be a footgun, but I thought its convenience outweighed the footgun. However, I think #32 pretty convincingly argues that it's the wrong default. If this ends up being the wrong decision, we can always add the trait impl back. In particular, I am worried about this making `Span` a very annoying-to-use type. Not implementing basic equality is just super annoying because it's common to want it in tests and other various things where the footgun isn't really relevant. But at least this way, we can fix the mistake in the future. Ref #32
I'm doing a experimental migration of my project from chrono to jiff, and I've run into subtle errors involving Span comparisons. This usually involves subtracting two DateTimes and checking that its TimeDelta/Span is some value.
The documentation says:
A Span implements the PartialEq and Eq traits, but not the PartialOrd or Ord traits. In particular, its Eq trait implementation compares for field-wise equality only. This means two spans can represent identical durations while comparing inequal
Perhaps Span should not implement PartialEq and Eq at all. I would rather the compiler give an error if I'm doing the wrong thing rather than have a subtle bug go through. This behavior breaks the principle of least surprise. If a person unfamiliar with jiff sees
&dt1 - &dt2 == some_span
in code review, they probably would not think that it's a serious bug.The text was updated successfully, but these errors were encountered: