Skip to content

Negating Duration::MIN panics with negating overflow in 0.3.0-alpha-2 #340

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

Closed
timvisee opened this issue Jul 29, 2021 · 2 comments
Closed
Labels
C-question Category: seeking clarification on a topic

Comments

@timvisee
Copy link

timvisee commented Jul 29, 2021

Negating Duration::MIN currently overflows in v0.3.0-alpha-2.

-Duration::MIN
thread 'main' panicked at 'attempt to negate with overflow', /home/timvisee/.cargo/registry/src/github.com-1ecc6299db9ec823/time-0.3.0-alpha-2/src/duration.rs:810:29
stack backtrace:
   0: rust_begin_unwind
             at /rustc/53cb7b09b00cbea8754ffb78e7e3cb521cb8af4b/library/std/src/panicking.rs:493:5
   1: core::panicking::panic_fmt
             at /rustc/53cb7b09b00cbea8754ffb78e7e3cb521cb8af4b/library/core/src/panicking.rs:92:14
   2: core::panicking::panic
             at /rustc/53cb7b09b00cbea8754ffb78e7e3cb521cb8af4b/library/core/src/panicking.rs:50:5
   3: <time::duration::Duration as core::ops::arith::Neg>::neg
             at /home/timvisee/.cargo/registry/src/github.com-1ecc6299db9ec823/time-0.3.0-alpha-2/src/duration.rs:810:29
   4: testproject::main
             at ./src/main.rs:4:13
   5: core::ops::function::FnOnce::call_once
             at /home/timvisee/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Is this expected/desired behavior? It doesn't seem to be explicitly stated in the documentation.

cc #248

@jhpratt
Copy link
Member

jhpratt commented Jul 29, 2021

What other behavior would there be? It does overflow as stated. If you want to handle that case specifically, foo.checked_mul(-1) will work.

@timvisee
Copy link
Author

timvisee commented Jul 29, 2021

Thanks for responding so quickly.

That's a good point. I was wondering (without looking into the internals) whether an overflow was desired in this case, as this didn't happen before. But I guess there's no real way to properly represent the negated value now.

Thanks for suggesting foo.checked_mul(-1)!

@jhpratt jhpratt added the C-question Category: seeking clarification on a topic label Jul 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-question Category: seeking clarification on a topic
Projects
None yet
Development

No branches or pull requests

2 participants