Skip to content

Commit a2375dc

Browse files
authored
Add #[track_caller] to methods which panic (#1442)
* Add #[track_caller] to methods which panic This makes it easier to diagnose problems when the preconidtions of these methods are not met. Signed-off-by: Joe Richey <[email protected]>
1 parent 4f8257a commit a2375dc

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
1313
- Bump the MSRV to 1.61.0
1414
- Rename `Rng::gen` to `Rng::random` to avoid conflict with the new `gen` keyword in Rust 2024 (#1435)
1515
- Move all benchmarks to new `benches` crate (#1439)
16+
- Annotate panicking methods with `#[track_caller]` (#1442)
1617

1718
## [0.9.0-alpha.1] - 2024-03-18
1819
- Add the `Slice::num_choices` method to the Slice distribution (#1402)

rand_core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ pub trait SeedableRng: Sized {
385385
/// [`getrandom`]: https://docs.rs/getrandom
386386
#[cfg(feature = "getrandom")]
387387
#[cfg_attr(doc_cfg, doc(cfg(feature = "getrandom")))]
388+
#[track_caller]
388389
fn from_entropy() -> Self {
389390
let mut seed = Self::Seed::default();
390391
if let Err(err) = getrandom::getrandom(seed.as_mut()) {

src/rng.rs

+19-8
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010
//! [`Rng`] trait
1111
12-
use rand_core::{Error, RngCore};
1312
use crate::distributions::uniform::{SampleRange, SampleUniform};
1413
use crate::distributions::{self, Distribution, Standard};
1514
use core::num::Wrapping;
1615
use core::{mem, slice};
16+
use rand_core::{Error, RngCore};
1717

1818
/// An automatically-implemented extension trait on [`RngCore`] providing high-level
1919
/// generic methods for sampling values and other convenience methods.
@@ -124,10 +124,11 @@ pub trait Rng: RngCore {
124124
/// ```
125125
///
126126
/// [`Uniform`]: distributions::uniform::Uniform
127+
#[track_caller]
127128
fn gen_range<T, R>(&mut self, range: R) -> T
128129
where
129130
T: SampleUniform,
130-
R: SampleRange<T>
131+
R: SampleRange<T>,
131132
{
132133
assert!(!range.is_empty(), "cannot sample empty range");
133134
range.sample_single(self).unwrap()
@@ -236,8 +237,9 @@ pub trait Rng: RngCore {
236237
///
237238
/// [`fill_bytes`]: RngCore::fill_bytes
238239
/// [`try_fill`]: Rng::try_fill
240+
#[track_caller]
239241
fn fill<T: Fill + ?Sized>(&mut self, dest: &mut T) {
240-
dest.try_fill(self).unwrap_or_else(|_| panic!("Rng::fill failed"))
242+
dest.try_fill(self).expect("Rng::fill failed")
241243
}
242244

243245
/// Fill any type implementing [`Fill`] with random data
@@ -288,9 +290,12 @@ pub trait Rng: RngCore {
288290
///
289291
/// [`Bernoulli`]: distributions::Bernoulli
290292
#[inline]
293+
#[track_caller]
291294
fn gen_bool(&mut self, p: f64) -> bool {
292-
let d = distributions::Bernoulli::new(p).unwrap();
293-
self.sample(d)
295+
match distributions::Bernoulli::new(p) {
296+
Ok(d) => self.sample(d),
297+
Err(_) => panic!("p={:?} is outside range [0.0, 1.0]", p),
298+
}
294299
}
295300

296301
/// Return a bool with a probability of `numerator/denominator` of being
@@ -317,9 +322,15 @@ pub trait Rng: RngCore {
317322
///
318323
/// [`Bernoulli`]: distributions::Bernoulli
319324
#[inline]
325+
#[track_caller]
320326
fn gen_ratio(&mut self, numerator: u32, denominator: u32) -> bool {
321-
let d = distributions::Bernoulli::from_ratio(numerator, denominator).unwrap();
322-
self.sample(d)
327+
match distributions::Bernoulli::from_ratio(numerator, denominator) {
328+
Ok(d) => self.sample(d),
329+
Err(_) => panic!(
330+
"p={}/{} is outside range [0.0, 1.0]",
331+
numerator, denominator
332+
),
333+
}
323334
}
324335

325336
/// Alias for [`Rng::random`].
@@ -432,8 +443,8 @@ where [T]: Fill
432443
#[cfg(test)]
433444
mod test {
434445
use super::*;
435-
use crate::test::rng;
436446
use crate::rngs::mock::StepRng;
447+
use crate::test::rng;
437448
#[cfg(feature = "alloc")] use alloc::boxed::Box;
438449

439450
#[test]

0 commit comments

Comments
 (0)