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

Slightly misleading error message when passing a negative float into Duration::from_secs_f32 #89913

Closed
VivekYadav7272 opened this issue Oct 15, 2021 · 1 comment · Fixed by #89944
Assignees
Labels
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.

Comments

@VivekYadav7272
Copy link

VivekYadav7272 commented Oct 15, 2021

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f974e7cf48544a6625e0ff9e11af9a76

use std::time::Duration;

fn main() {
    let _ = Duration::from_secs_f32(-12.3_f32);
}

The current output is:

thread 'main' panicked at 'underflow when converting float to duration', /rustc/c8dfcfe046a7680554bf4eb612bad840e7631c4b/library/core/src/time.rs:754:23
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Ideally the output should look like:

thread 'main' panicked at 'negative floats cannot be used as a duration', /rustc/c8dfcfe046a7680554bf4eb612bad840e7631c4b/library/core/src/time.rs:754:23
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

while the current error message might still give a hint as to what could be going on, if you have genuinely large numbers passed to the function, you may go in a completely wrong path while debugging (testing the input to be within limits, rather than checking if it is negative or not).

@VivekYadav7272 VivekYadav7272 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 Oct 15, 2021
@mbartlett21
Copy link
Contributor

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 20, 2021
Change `Duration::[try_]from_secs_{f32, f64}` underflow error

The error message now says that it was a negative value.

Fixes rust-lang#89913.
JohnTitor added a commit to JohnTitor/rust that referenced this issue Oct 21, 2021
Change `Duration::[try_]from_secs_{f32, f64}` underflow error

The error message now says that it was a negative value.

Fixes rust-lang#89913.
JohnTitor added a commit to JohnTitor/rust that referenced this issue Oct 21, 2021
Change `Duration::[try_]from_secs_{f32, f64}` underflow error

The error message now says that it was a negative value.

Fixes rust-lang#89913.
JohnTitor added a commit to JohnTitor/rust that referenced this issue Oct 22, 2021
Change `Duration::[try_]from_secs_{f32, f64}` underflow error

The error message now says that it was a negative value.

Fixes rust-lang#89913.
@bors bors closed this as completed in 8b7adf6 Oct 22, 2021
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 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants