Skip to content

Commit

Permalink
Sema: don't emit instruction when casting @min/@max result to OPV type
Browse files Browse the repository at this point in the history
Resolves: #21408
  • Loading branch information
mlugg committed Sep 15, 2024
1 parent 19924ca commit 258236e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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.?);
Expand Down
14 changes: 14 additions & 0 deletions test/behavior/maximum_minimum.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 258236e

Please sign in to comment.