From c4cc796695dddbfba749514823ed29bda37b7bcd Mon Sep 17 00:00:00 2001 From: mlugg Date: Wed, 14 Jun 2023 00:51:31 +0100 Subject: Sema: consider type bounds when refining result type of `@min`/`@max` I achieved this through a major refactor of the logic of analyzeMinMax. This change should be compatible with vectors of comptime_int, which Andrew said are supposed to work (but which currently do not). --- src/value.zig | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/value.zig') diff --git a/src/value.zig b/src/value.zig index d3f15121b8..85204e2b10 100644 --- a/src/value.zig +++ b/src/value.zig @@ -4146,6 +4146,20 @@ pub const Value = struct { return val.toIntern() == .generic_poison; } + /// For an integer (comptime or fixed-width) `val`, returns the comptime-known bounds of the value. + /// If `val` is not undef, the bounds are both `val`. + /// If `val` is undef and has a fixed-width type, the bounds are the bounds of the type. + /// If `val` is undef and is a `comptime_int`, returns null. + pub fn intValueBounds(val: Value, mod: *Module) !?[2]Value { + if (!val.isUndef(mod)) return .{ val, val }; + const ty = mod.intern_pool.typeOf(val.toIntern()); + if (ty == .comptime_int_type) return null; + return .{ + try ty.toType().minInt(mod, ty.toType()), + try ty.toType().maxInt(mod, ty.toType()), + }; + } + /// This type is not copyable since it may contain pointers to its inner data. pub const Payload = struct { tag: Tag, -- cgit v1.2.3