authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-06-26 17:47:32-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-26 18:46:25-07:00
log9343c31c3879514c890c9bfa2a1cd1a1f222ed6d
tree371a2fd2694adde2be6579563448e28e8facbfa5
parent6bd54793060191ffce2c7492a90a40a30f9e2a1d

Sema: fix `@min`/`@max` type resolution with all runtime args

Closes #16229

2 files changed, 18 insertions(+), 0 deletions(-)

src/Sema.zig+4
...@@ -23301,6 +23301,7 @@ fn analyzeMinMax(...@@ -23301,6 +23301,7 @@ fn analyzeMinMax(
2330123301
23302 if (cur_minmax == null) {23302 if (cur_minmax == null) {
23303 // No comptime operands - use the first operand as the starting value23303 // No comptime operands - use the first operand as the starting value
23304 assert(bounds_status == .unknown);
23304 assert(runtime_idx == 0);23305 assert(runtime_idx == 0);
23305 cur_minmax = operands[0];23306 cur_minmax = operands[0];
23306 cur_minmax_src = runtime_src;23307 cur_minmax_src = runtime_src;
...@@ -23309,6 +23310,9 @@ fn analyzeMinMax(...@@ -23309,6 +23310,9 @@ fn analyzeMinMax(
23309 if (scalar_ty.isInt(mod)) {23310 if (scalar_ty.isInt(mod)) {
23310 cur_min_scalar = try scalar_ty.minInt(mod, scalar_ty);23311 cur_min_scalar = try scalar_ty.minInt(mod, scalar_ty);
23311 cur_max_scalar = try scalar_ty.maxInt(mod, scalar_ty);23312 cur_max_scalar = try scalar_ty.maxInt(mod, scalar_ty);
23313 bounds_status = .defined;
23314 } else {
23315 bounds_status = .non_integral;
23312 }23316 }
23313 }23317 }
2331423318
test/behavior/maximum_minimum.zig+14
...@@ -295,3 +295,17 @@ test "@min/@max notices bounds from vector types when element of comptime-known...@@ -295,3 +295,17 @@ test "@min/@max notices bounds from vector types when element of comptime-known
295 try expectEqual(@as(u32, 1_000_000), max[0]);295 try expectEqual(@as(u32, 1_000_000), max[0]);
296 // Cannot assert values at index 1 as one was undefined296 // Cannot assert values at index 1 as one was undefined
297}297}
298
299test "@min/@max of signed and unsigned runtime integers" {
300 var x: i32 = -1;
301 var y: u31 = 1;
302
303 const min = @min(x, y);
304 const max = @max(x, y);
305
306 comptime assert(@TypeOf(min) == i32);
307 comptime assert(@TypeOf(max) == u31);
308
309 try expectEqual(x, @min(x, y));
310 try expectEqual(y, @max(x, y));
311}