Skip to content

Commit 258236e

Browse files
committed
Sema: don't emit instruction when casting @min/@max result to OPV type
Resolves: #21408
1 parent 19924ca commit 258236e

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/Sema.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26201,6 +26201,10 @@ fn analyzeMinMax(
2620126201
.child = refined_scalar_ty.toIntern(),
2620226202
}) else refined_scalar_ty;
2620326203

26204+
if (try sema.typeHasOnePossibleValue(refined_ty)) |opv| {
26205+
return Air.internedToRef(opv.toIntern());
26206+
}
26207+
2620426208
if (!refined_ty.eql(unrefined_ty, zcu)) {
2620526209
// We've reduced the type - cast the result down
2620626210
return block.addTyOp(.intcast, refined_ty, cur_minmax.?);

test/behavior/maximum_minimum.zig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,3 +336,17 @@ test "@min/@max of signed and unsigned runtime integers" {
336336
try expectEqual(x, @min(x, y));
337337
try expectEqual(y, @max(x, y));
338338
}
339+
340+
test "@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+
}

0 commit comments

Comments
 (0)