rust-random / rand

A Rust library for random number generation.
https://crates.io/crates/rand
Other
1.6k stars 423 forks source link

Add #[track_caller] to methods which panic #1442

Closed josephlr closed 2 months ago

josephlr commented 2 months ago

Fixes #1437

This makes it easier to diagnose problems when the preconditions of these methods are not met.

I think I found most of the cases where we can panic based on bad user input. Let me know if I missed any.

We don't use #[track_caller] for the panics originating from failing to initialized thread_rng as this is more of a "global" problem than a "local" problem.

dhardy commented 2 months ago

Also see:

josephlr commented 2 months ago

The reference uses std::panic::Location::caller. Is it worth doing this too for better error messages?

I think that's mostly if we wanted to print out the location, or use the location for something else. If we are just panicking anyway, I think just using #[track_caller] is fine, that's what libstd does for things like .unwrap().

dhardy commented 2 months ago

BTW, I was hoping you'd give #1424 a review — not sure if you missed the comment there?

josephlr commented 2 months ago

BTW, I was hoping you'd give #1424 a review — not sure if you missed the comment there?

Whoops, I did miss that. Ill try to take a look sometime this week.