diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2023-08-28 08:13:57 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-08-28 08:13:57 +0200 |
| commit | d2644d9fe97d4ffae93af836975903f5800c7fb0 (patch) | |
| tree | c86df372df8682cb0671791c6a552ef4e66c5901 /compiler/rustc_mir_transform/src/coverage/debug.rs | |
| parent | fb98f7adc3f74e331c21e5ec9bf2577b707b1523 (diff) | |
| parent | f1d4e48c9c6409aff557b0117d48f2d7d1e05280 (diff) | |
| download | rust-d2644d9fe97d4ffae93af836975903f5800c7fb0.tar.gz rust-d2644d9fe97d4ffae93af836975903f5800c7fb0.zip | |
Rollup merge of #114238 - jhpratt:fix-duration-div, r=thomcc
Fix implementation of `Duration::checked_div`
I ran across this while running some sanity checks on `time`. Quickcheck immediately found a bug, and as I'd modified the code from `std` I knew there was a bug here as well.
tl;dr this code fails ([playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=1189a3efcdfc192c27d6d87815359353))
```rust
use std::time::Duration;
fn main() {
assert_eq!(
Duration::new(1, 1).checked_div(7),
Some(Duration::new(0, 142_857_143)),
);
}
```
The existing code determines that 1/7 = 0 (seconds), 1/7 = 0 (nanoseconds), 1 billion / 7 = 142,857,142 (extra nanoseconds). The billion comes from multiplying the remainder of the seconds (1) by the number of nanoseconds in a second. However, **this wrongly ignores any remaining nanoseconds**. This PR takes that into consideration, adds a test, and also changes the roundabout way of calculating the remainder into directly computing it.
Note: This is _not_ a rounding error. This result divides evenly.
`@rustbot` label +A-time +C-bug +S-waiting-on-reviewer +T-libs
Diffstat (limited to 'compiler/rustc_mir_transform/src/coverage/debug.rs')
0 files changed, 0 insertions, 0 deletions
