Commit eb1a4970da

Jackson Wambolt <jtwambolt@gmail.com>
2025-07-30 22:48:38
Sema: check min/max operand types
1 parent 389ccf6
Changed files (2)
src
test
cases
src/Sema.zig
@@ -24609,6 +24609,7 @@ fn analyzeMinMax(
         } else {
             for (operands[1..], operand_srcs[1..]) |operand, operand_src| {
                 const operand_ty = sema.typeOf(operand);
+                try sema.checkNumericType(block, operand_src, operand_ty);
                 if (operand_ty.zigTypeTag(zcu) == .vector) {
                     return sema.failWithOwnedErrorMsg(block, msg: {
                         const msg = try sema.errMsg(operand_srcs[0], "expected vector, found '{f}'", .{first_operand_ty.fmt(pt)});
test/cases/compile_errors/minmax_nonnumeric_operand.zig
@@ -0,0 +1,41 @@
+// zig fmt: off
+comptime { _ = @min(0, u32); } // type
+comptime { _ = @max(0, {}); } // void
+comptime { _ = @min(0, false); } // boolean
+comptime { _ = @min(0, &@as(u8, 0)); } // pointer
+comptime { _ = @max(0, [0]u8{}); } // array
+comptime { _ = @min(0, Struct{}); } // struct
+comptime { _ = @max(0, null); } // null
+comptime { _ = @min(0, @as(?u8, 0)); } // nullable
+comptime { _ = @max(0, @as(error{}!u8, 0)); } // error union
+comptime { _ = @min(0, error.Foo); } // error set
+comptime { _ = @max(0, Enum.foo); } // enum
+comptime { _ = @min(0, Union{ .foo = {} }); } // union
+comptime { _ = @max(0, struct { fn func() u8 { return 42; }}.func); }
+comptime { _ = @max(0, .foo); } // enum literal
+
+const Struct = struct {};
+const Enum = enum { foo };
+const Union = union { foo: void };
+
+// error
+// backend=stage2
+// target=native
+//
+// :2:24: error: expected number, found 'type'
+// :3:24: error: expected number, found 'void'
+// :4:24: error: expected number, found 'bool'
+// :5:24: error: expected number, found '*const u8'
+// :6:29: error: expected number, found '[0]u8'
+// :7:30: error: expected number, found 'tmp.Struct'
+// :17:16: note: struct declared here
+// :8:24: error: expected number, found '@TypeOf(null)'
+// :9:24: error: expected number, found '?u8'
+// :10:24: error: expected number, found 'error{}!u8'
+// :11:24: error: expected number, found 'error{Foo}'
+// :12:28: error: expected number, found 'tmp.Enum'
+// :18:14: note: enum declared here
+// :13:29: error: expected number, found 'tmp.Union'
+// :19:15: note: union declared here
+// :14:61: error: expected number, found 'fn () u8'
+// :15:25: error: expected number, found '@Type(.enum_literal)'