authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-09-15 13:25:18+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-09-15 17:52:35+01:00
log258236ec1bbfa72555189d87db42e57e1f74be3c
tree7090f69e565fdd0a43af39b0c6c5be1b9184239f
parent19924ca2890964b411362c423dd9f4b10596a18f

Sema: don't emit instruction when casting @min/@max result to OPV type

Resolves: #21408

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

src/Sema.zig+4
...@@ -26201,6 +26201,10 @@ fn analyzeMinMax(...@@ -26201,6 +26201,10 @@ fn analyzeMinMax(
26201 .child = refined_scalar_ty.toIntern(),26201 .child = refined_scalar_ty.toIntern(),
26202 }) else refined_scalar_ty;26202 }) else refined_scalar_ty;
2620326203
26204 if (try sema.typeHasOnePossibleValue(refined_ty)) |opv| {
26205 return Air.internedToRef(opv.toIntern());
26206 }
26207
26204 if (!refined_ty.eql(unrefined_ty, zcu)) {26208 if (!refined_ty.eql(unrefined_ty, zcu)) {
26205 // We've reduced the type - cast the result down26209 // We've reduced the type - cast the result down
26206 return block.addTyOp(.intcast, refined_ty, cur_minmax.?);26210 return block.addTyOp(.intcast, refined_ty, cur_minmax.?);
test/behavior/maximum_minimum.zig+14
...@@ -336,3 +336,17 @@ test "@min/@max of signed and unsigned runtime integers" {...@@ -336,3 +336,17 @@ test "@min/@max of signed and unsigned runtime integers" {
336 try expectEqual(x, @min(x, y));336 try expectEqual(x, @min(x, y));
337 try expectEqual(y, @max(x, y));337 try expectEqual(y, @max(x, y));
338}338}
339
340test "@min resulting in u0" {
341 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
342 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
343 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
344
345 const S = struct {
346 fn min(a: u0, b: u8) u8 {
347 return @min(a, b);
348 }
349 };
350 const x = S.min(0, 1);
351 try expect(x == 0);
352}