rust-lang / rust-lang/rust

clamp on std integer types should probably have a better error message and ideally track caller

Open
#142,309 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics C-bug T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

when running this code

fn main() {
    println!("{}", 0u8.clamp(7,3))
}

i get the following output

thread 'main' panicked at /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/cmp.rs:1078:9:
assertion failed: min <= max

this is very unhelpful in any non-trivial codebase that uses this method.
this is a pretty niche error but it can happen.

something like

thread 'main' panicked at src/main.rs:3:20:
invalid clamp range : there are no numbers smaller than 7 and greater than 3

would be ideal, but any improvement would be good.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in library/core/src/cmp.rs around the reported assertion at line 1078 and inspect how integer clamp validates its bounds. Determine the expected panic wording and whether the failure can report the caller location, then verify the behavior with the provided 0u8.clamp(7, 3) example and the relevant existing standard-library tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.