authorgravatar for pancelor@fastmail.compancelor <pancelor@fastmail.com> 2026-06-01 02:59:53-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-06-07 06:11:08+02:00
log474017b43d861ebf80188cdd013a8027c464e2d6
tree61781b64ebec28d1a97144587768d11f515174cc
parentabd649a50c500706c37e8e8859bb4bd6fec2006a

std.zon.parse: fix large int parse

this fixes the compile error that was happening when T=u128, because value is always a f128, and maxInt(u128) is too large to coerce and compare ("error: type 'f128' cannot represent integer value")

1 files changed, 8 insertions(+), 1 deletions(-)

lib/std/zon/parse.zig+8-1
...@@ -1173,7 +1173,9 @@ const Parser = struct {...@@ -1173,7 +1173,9 @@ const Parser = struct {
1173};1173};
11741174
1175fn intFromFloatExact(T: type, value: anytype) ?T {1175fn intFromFloatExact(T: type, value: anytype) ?T {
1176 if (value > std.math.maxInt(T) or value < std.math.minInt(T)) {1176 const max: @TypeOf(value) = @floatFromInt(std.math.maxInt(T));
1177 const min: @TypeOf(value) = @floatFromInt(std.math.minInt(T));
1178 if (value > max or value < min) {
1177 return null;1179 return null;
1178 }1180 }
11791181
...@@ -2448,6 +2450,7 @@ test "std.zon intFromFloatExact" {...@@ -2448,6 +2450,7 @@ test "std.zon intFromFloatExact" {
2448 try std.testing.expectEqual(@as(u8, 10), intFromFloatExact(u8, @as(f32, 10.0)).?);2450 try std.testing.expectEqual(@as(u8, 10), intFromFloatExact(u8, @as(f32, 10.0)).?);
2449 try std.testing.expectEqual(@as(i8, -123), intFromFloatExact(i8, @as(f64, @as(f64, -123.0))).?);2451 try std.testing.expectEqual(@as(i8, -123), intFromFloatExact(i8, @as(f64, @as(f64, -123.0))).?);
2450 try std.testing.expectEqual(@as(i16, 45), intFromFloatExact(i16, @as(f128, @as(f128, 45.0))).?);2452 try std.testing.expectEqual(@as(i16, 45), intFromFloatExact(i16, @as(f128, @as(f128, 45.0))).?);
2453 try std.testing.expectEqual(@as(u128, 67), intFromFloatExact(u128, @as(f128, @as(f128, 67.0))).?);
24512454
2452 // Out of range2455 // Out of range
2453 try std.testing.expectEqual(@as(?u4, null), intFromFloatExact(u4, @as(f32, 16.0)));2456 try std.testing.expectEqual(@as(?u4, null), intFromFloatExact(u4, @as(f32, 16.0)));
...@@ -2490,6 +2493,10 @@ test "std.zon parse int" {...@@ -2490,6 +2493,10 @@ test "std.zon parse int" {
2490 @as(u65, 36893488147419103231),2493 @as(u65, 36893488147419103231),
2491 try fromSlice(u65, gpa, "368934_881_474191032_31", null, .{}),2494 try fromSlice(u65, gpa, "368934_881_474191032_31", null, .{}),
2492 );2495 );
2496 try std.testing.expectEqual(
2497 @as(u128, 340282366920938463463374607431768211455),
2498 try fromSlice(u128, gpa, "340282366920938463463374607431768211455", null, .{}),
2499 );
24932500
2494 // Test big integer limits2501 // Test big integer limits
2495 try std.testing.expectEqual(2502 try std.testing.expectEqual(