From e2db6c70fc975f501b1fc8544aab5f00156a60a5 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 10 Oct 2025 15:38:35 +0300 Subject: [PATCH] time,hash,rand: tag more functions with an explicit @[ignore_overflow] or with explicit casts (where needed) --- vlib/hash/wyhash.v | 4 ++-- vlib/rand/mt19937/mt19937.v | 3 ++- vlib/rand/musl/musl_rng.v | 1 + vlib/rand/pcg32/pcg32.v | 3 ++- vlib/rand/rand.c.v | 1 + vlib/rand/seed/seed.v | 2 +- vlib/rand/splitmix64/splitmix64.v | 2 +- vlib/rand/wyrand/wyrand.v | 2 +- vlib/rand/xoroshiro128pp/xoros128pp.v | 2 +- vlib/time/operator_test.v | 2 +- vlib/time/time.v | 8 ++++---- vlib/time/time_test.v | 2 +- 12 files changed, 18 insertions(+), 14 deletions(-) diff --git a/vlib/hash/wyhash.v b/vlib/hash/wyhash.v index a28aad8be..75387fee7 100644 --- a/vlib/hash/wyhash.v +++ b/vlib/hash/wyhash.v @@ -21,13 +21,13 @@ const wyp2 = u64(0x8ebc6af09c88c6e3) const wyp3 = u64(0x589965cc75374cc3) const wyp4 = u64(0x1d8e4e27c47d124f) -@[inline] +@[ignore_overflow; inline] fn wyrotr(v u64, k u32) u64 { return (v >> k) | (v << (64 - k)) } // wymum returns a hash by performing multiply and mix on `a` and `b`. -@[inline] +@[ignore_overflow; inline] pub fn wymum(a u64, b u64) u64 { /* mut r := u128(a) diff --git a/vlib/rand/mt19937/mt19937.v b/vlib/rand/mt19937/mt19937.v index 622ed04bc..90c47c5f0 100644 --- a/vlib/rand/mt19937/mt19937.v +++ b/vlib/rand/mt19937/mt19937.v @@ -72,6 +72,7 @@ fn get_first_state(seed_data []u32) []u64 { } // calculate_state returns a random state array calculated from the `seed_data`. +@[ignore_overflow] fn calculate_state(seed_data []u32, mut state []u64) []u64 { lo := u64(seed_data[0]) hi := u64(seed_data[1]) @@ -145,7 +146,7 @@ pub fn (mut rng MT19937RNG) u32() u32 { const mag01 = [u64(0), u64(matrix_a)] // u64 returns a pseudorandom 64bit int in range `[0, 2⁶⁴)`. -@[direct_array_access; inline] +@[direct_array_access; ignore_overflow; inline] pub fn (mut rng MT19937RNG) u64() u64 { mut x := u64(0) mut i := int(0) diff --git a/vlib/rand/musl/musl_rng.v b/vlib/rand/musl/musl_rng.v index 7d4e0f15b..4c19f3529 100644 --- a/vlib/rand/musl/musl_rng.v +++ b/vlib/rand/musl/musl_rng.v @@ -70,6 +70,7 @@ fn temper(prev u32) u32 { } // u32 returns a pseudorandom 32-bit unsigned integer (`u32`). +@[ignore_overflow] pub fn (mut rng MuslRNG) u32() u32 { rng.state = rng.state * 1103515245 + 12345 // We are not dividing by 2 (or shifting right by 1) diff --git a/vlib/rand/pcg32/pcg32.v b/vlib/rand/pcg32/pcg32.v index 62b294cd3..d96433e4c 100644 --- a/vlib/rand/pcg32/pcg32.v +++ b/vlib/rand/pcg32/pcg32.v @@ -21,6 +21,7 @@ mut: // seed seeds the PCG32RNG with 4 `u32` values. // The first 2 represent the 64-bit initial state as `[lower 32 bits, higher 32 bits]` // The last 2 represent the 64-bit stream/step of the PRNG. +@[ignore_overflow] pub fn (mut rng PCG32RNG) seed(seed_data []u32) { if seed_data.len != 4 { eprintln('PCG32RNG needs 4 u32s to be seeded. First two the initial state and the last two the stream/step. Both in little endian format: [lower, higher].') @@ -69,7 +70,7 @@ pub fn (mut rng PCG32RNG) u16() u16 { } // u32 returns a pseudorandom unsigned `u32`. -@[inline] +@[ignore_overflow; inline] pub fn (mut rng PCG32RNG) u32() u32 { oldstate := rng.state rng.state = oldstate * (6364136223846793005) + rng.inc diff --git a/vlib/rand/rand.c.v b/vlib/rand/rand.c.v index 98733e2bf..9ff6d4331 100644 --- a/vlib/rand/rand.c.v +++ b/vlib/rand/rand.c.v @@ -82,6 +82,7 @@ pub fn new_uuid_v7_session() UUIDSession { } // next get a new uuid_v7 from current session. +@[ignore_overflow] pub fn (mut u UUIDSession) next() string { timestamp := u64(time.now().unix_nano()) // make place for holding 4 bits `version` diff --git a/vlib/rand/seed/seed.v b/vlib/rand/seed/seed.v index bec761033..a60e973d9 100644 --- a/vlib/rand/seed/seed.v +++ b/vlib/rand/seed/seed.v @@ -6,7 +6,7 @@ module seed import time // nr_next returns a next value based on the previous value `prev`. -@[inline] +@[ignore_overflow; inline] fn nr_next(prev u32) u32 { return prev * 1664525 + 1013904223 } diff --git a/vlib/rand/splitmix64/splitmix64.v b/vlib/rand/splitmix64/splitmix64.v index ca349738e..540d35ba5 100644 --- a/vlib/rand/splitmix64/splitmix64.v +++ b/vlib/rand/splitmix64/splitmix64.v @@ -75,7 +75,7 @@ pub fn (mut rng SplitMix64RNG) u32() u32 { } // u64 returns a pseudorandom 64bit int in range `[0, 2⁶⁴)`. -@[inline] +@[ignore_overflow; inline] pub fn (mut rng SplitMix64RNG) u64() u64 { rng.state += (0x9e3779b97f4a7c15) mut z := rng.state diff --git a/vlib/rand/wyrand/wyrand.v b/vlib/rand/wyrand/wyrand.v index 4d9401f15..2c668973a 100644 --- a/vlib/rand/wyrand/wyrand.v +++ b/vlib/rand/wyrand/wyrand.v @@ -82,7 +82,7 @@ pub fn (mut rng WyRandRNG) u32() u32 { } // u64 returns a pseudorandom 64bit int in range `[0, 2⁶⁴)`. -@[inline] +@[ignore_overflow; inline] pub fn (mut rng WyRandRNG) u64() u64 { unsafe { mut seed1 := rng.state diff --git a/vlib/rand/xoroshiro128pp/xoros128pp.v b/vlib/rand/xoroshiro128pp/xoros128pp.v index d11c6cebd..50960e0b5 100644 --- a/vlib/rand/xoroshiro128pp/xoros128pp.v +++ b/vlib/rand/xoroshiro128pp/xoros128pp.v @@ -83,7 +83,7 @@ pub fn (mut rng XOROS128PPRNG) u32() u32 { } // u64 returns a pseudorandom 64-bit unsigned `u64`. -@[inline] +@[ignore_overflow; inline] pub fn (mut rng XOROS128PPRNG) u64() u64 { oldstate0 := rng.state0 mut oldstate1 := rng.state1 diff --git a/vlib/time/operator_test.v b/vlib/time/operator_test.v index 9ad6adb6d..ccab5489b 100644 --- a/vlib/time/operator_test.v +++ b/vlib/time/operator_test.v @@ -371,7 +371,7 @@ fn test_time2_copied_from_time1_should_be_equal() { fn test_subtract() { d_seconds := 3 d_nanoseconds := 13 - duration := d_seconds * second + d_nanoseconds * nanosecond + duration := i64(d_seconds) * second + d_nanoseconds * nanosecond t1 := new(Time{ year: 2000 month: 5 diff --git a/vlib/time/time.v b/vlib/time/time.v index c9b1059a4..03b26a932 100644 --- a/vlib/time/time.v +++ b/vlib/time/time.v @@ -165,12 +165,12 @@ pub fn (t Time) add(duration_in_nanosecond Duration) Time { // add_seconds returns a new time struct with an added number of seconds. pub fn (t Time) add_seconds(seconds int) Time { - return time_with_unix(t).add(seconds * second) + return time_with_unix(t).add(i64(seconds) * second) } // add_days returns a new time struct with an added number of days. pub fn (t Time) add_days(days int) Time { - return time_with_unix(t).add(days * 24 * hour) + return time_with_unix(t).add(i64(days) * 24 * hour) } // since returns the time duration elapsed since a given time. @@ -401,7 +401,7 @@ pub fn (t Time) local_to_utc() Time { return t } return Time{ - ...t.add(-offset() * second) + ...t.add(i64(-offset()) * second) is_local: false } } @@ -413,7 +413,7 @@ pub fn (u Time) utc_to_local() Time { return u } return Time{ - ...u.add(offset() * second) + ...u.add(i64(offset()) * second) is_local: true } } diff --git a/vlib/time/time_test.v b/vlib/time/time_test.v index e33b0e5c2..97803e531 100644 --- a/vlib/time/time_test.v +++ b/vlib/time/time_test.v @@ -250,7 +250,7 @@ fn test_weekday_str() { fn test_add() { d_seconds := 3 d_nanoseconds := 13 - duration := time.Duration(d_seconds * time.second + d_nanoseconds * time.nanosecond) + duration := time.Duration(i64(d_seconds) * time.second + d_nanoseconds * time.nanosecond) // dump(duration.debug()) t1 := local_time_to_test // dump(t1.debug()) -- 2.39.5