Skip to content

Commit eb1a497

Browse files
authored
Sema: check min/max operand types
1 parent 389ccf6 commit eb1a497

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/Sema.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24609,6 +24609,7 @@ fn analyzeMinMax(
2460924609
} else {
2461024610
for (operands[1..], operand_srcs[1..]) |operand, operand_src| {
2461124611
const operand_ty = sema.typeOf(operand);
24612+
try sema.checkNumericType(block, operand_src, operand_ty);
2461224613
if (operand_ty.zigTypeTag(zcu) == .vector) {
2461324614
return sema.failWithOwnedErrorMsg(block, msg: {
2461424615
const msg = try sema.errMsg(operand_srcs[0], "expected vector, found '{f}'", .{first_operand_ty.fmt(pt)});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// zig fmt: off
2+
comptime { _ = @min(0, u32); } // type
3+
comptime { _ = @max(0, {}); } // void
4+
comptime { _ = @min(0, false); } // boolean
5+
comptime { _ = @min(0, &@as(u8, 0)); } // pointer
6+
comptime { _ = @max(0, [0]u8{}); } // array
7+
comptime { _ = @min(0, Struct{}); } // struct
8+
comptime { _ = @max(0, null); } // null
9+
comptime { _ = @min(0, @as(?u8, 0)); } // nullable
10+
comptime { _ = @max(0, @as(error{}!u8, 0)); } // error union
11+
comptime { _ = @min(0, error.Foo); } // error set
12+
comptime { _ = @max(0, Enum.foo); } // enum
13+
comptime { _ = @min(0, Union{ .foo = {} }); } // union
14+
comptime { _ = @max(0, struct { fn func() u8 { return 42; }}.func); }
15+
comptime { _ = @max(0, .foo); } // enum literal
16+
17+
const Struct = struct {};
18+
const Enum = enum { foo };
19+
const Union = union { foo: void };
20+
21+
// error
22+
// backend=stage2
23+
// target=native
24+
//
25+
// :2:24: error: expected number, found 'type'
26+
// :3:24: error: expected number, found 'void'
27+
// :4:24: error: expected number, found 'bool'
28+
// :5:24: error: expected number, found '*const u8'
29+
// :6:29: error: expected number, found '[0]u8'
30+
// :7:30: error: expected number, found 'tmp.Struct'
31+
// :17:16: note: struct declared here
32+
// :8:24: error: expected number, found '@TypeOf(null)'
33+
// :9:24: error: expected number, found '?u8'
34+
// :10:24: error: expected number, found 'error{}!u8'
35+
// :11:24: error: expected number, found 'error{Foo}'
36+
// :12:28: error: expected number, found 'tmp.Enum'
37+
// :18:14: note: enum declared here
38+
// :13:29: error: expected number, found 'tmp.Union'
39+
// :19:15: note: union declared here
40+
// :14:61: error: expected number, found 'fn () u8'
41+
// :15:25: error: expected number, found '@Type(.enum_literal)'

0 commit comments

Comments
 (0)