| author | |
| committer | |
| log | e2837fd2245c213c842617d6d98e311057893bb0 |
| tree | e42c8accf30a118fdb63f0c5302dd89d9f905f8d |
| parent | 46e724ab28ba5934471d8b2dec60acdd7885d64b |
Resolves: #157762 files changed, 26 insertions(+), 1 deletions(-)
src/Sema.zig+9-1| ... | ... | @@ -21940,10 +21940,18 @@ fn analyzeMinMax( |
| 21940 | 21940 | } |
| 21941 | 21941 | } |
| 21942 | 21942 | |
| 21943 | const opt_runtime_idx = runtime_known.findFirstSet(); | |
| 21944 | ||
| 21943 | 21945 | const comptime_refined_ty: ?Type = if (cur_minmax) |ct_minmax_ref| refined: { |
| 21944 | 21946 | // Refine the comptime-known result type based on the operation |
| 21945 | 21947 | const val = (try sema.resolveMaybeUndefVal(ct_minmax_ref)).?; |
| 21946 | 21948 | const orig_ty = sema.typeOf(ct_minmax_ref); |
| 21949 | ||
| 21950 | if (opt_runtime_idx == null and orig_ty.eql(Type.comptime_int, mod)) { | |
| 21951 | // If all arguments were `comptime_int`, and there are no runtime args, we'll preserve that type | |
| 21952 | break :refined orig_ty; | |
| 21953 | } | |
| 21954 | ||
| 21947 | 21955 | const refined_ty = if (orig_ty.zigTypeTag() == .Vector) blk: { |
| 21948 | 21956 | const elem_ty = orig_ty.childType(); |
| 21949 | 21957 | const len = orig_ty.vectorLen(); |
| ... | ... | @@ -21982,7 +21990,7 @@ fn analyzeMinMax( |
| 21982 | 21990 | break :refined refined_ty; |
| 21983 | 21991 | } else null; |
| 21984 | 21992 | |
| 21985 | const runtime_idx = runtime_known.findFirstSet() orelse return cur_minmax.?; | |
| 21993 | const runtime_idx = opt_runtime_idx orelse return cur_minmax.?; | |
| 21986 | 21994 | const runtime_src = operand_srcs[runtime_idx]; |
| 21987 | 21995 | try sema.requireRuntimeBlock(block, src, runtime_src); |
| 21988 | 21996 |
test/behavior/maximum_minimum.zig+17| ... | ... | @@ -194,3 +194,20 @@ test "@min/@max notices vector bounds" { |
| 194 | 194 | try expectEqual(@Vector(2, u32){ 140, 300 }, max); |
| 195 | 195 | try expectEqual(@Vector(2, u32), @TypeOf(max)); |
| 196 | 196 | } |
| 197 | ||
| 198 | test "@min/@max on comptime_int" { | |
| 199 | if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO | |
| 200 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO | |
| 201 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO | |
| 202 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO | |
| 203 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO | |
| 204 | if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; // TODO | |
| 205 | ||
| 206 | const min = @min(1, 2, -2, -1); | |
| 207 | const max = @max(1, 2, -2, -1); | |
| 208 | ||
| 209 | try expectEqual(comptime_int, @TypeOf(min)); | |
| 210 | try expectEqual(comptime_int, @TypeOf(max)); | |
| 211 | try expectEqual(-2, min); | |
| 212 | try expectEqual(2, max); | |
| 213 | } |