Skip to content

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

Closed
@VivekYadav7272

Description

@VivekYadav7272

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).

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions