From 258236ec1bbfa72555189d87db42e57e1f74be3c Mon Sep 17 00:00:00 2001 From: mlugg Date: Sun, 15 Sep 2024 13:25:18 +0100 Subject: [PATCH] Sema: don't emit instruction when casting @min/@max result to OPV type Resolves: #21408 --- src/Sema.zig | 4 ++++ test/behavior/maximum_minimum.zig | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/Sema.zig b/src/Sema.zig index 433429f92a..7a4b9b3210 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -26201,6 +26201,10 @@ fn analyzeMinMax( .child = refined_scalar_ty.toIntern(), }) else refined_scalar_ty; + if (try sema.typeHasOnePossibleValue(refined_ty)) |opv| { + return Air.internedToRef(opv.toIntern()); + } + if (!refined_ty.eql(unrefined_ty, zcu)) { // We've reduced the type - cast the result down return block.addTyOp(.intcast, refined_ty, cur_minmax.?); diff --git a/test/behavior/maximum_minimum.zig b/test/behavior/maximum_minimum.zig index ab1803c5b1..8994d9d182 100644 --- a/test/behavior/maximum_minimum.zig +++ b/test/behavior/maximum_minimum.zig @@ -336,3 +336,17 @@ test "@min/@max of signed and unsigned runtime integers" { try expectEqual(x, @min(x, y)); try expectEqual(y, @max(x, y)); } + +test "@min resulting in u0" { + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO + + const S = struct { + fn min(a: u0, b: u8) u8 { + return @min(a, b); + } + }; + const x = S.min(0, 1); + try expect(x == 0); +}