diff options
| author | mlugg <mlugg@mlugg.co.uk> | 2023-06-14 00:51:31 +0100 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2023-06-16 13:33:31 -0700 |
| commit | c4cc796695dddbfba749514823ed29bda37b7bcd (patch) | |
| tree | 8cf201509609767dbe5a8301e4e1e58824576275 /src/value.zig | |
| parent | 5d9e8f27d0dc131e0b4154c5f65376f2fb9f3500 (diff) | |
| download | zig-c4cc796695dddbfba749514823ed29bda37b7bcd.tar.gz zig-c4cc796695dddbfba749514823ed29bda37b7bcd.zip | |
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).
Diffstat (limited to 'src/value.zig')
| -rw-r--r-- | src/value.zig | 14 |
1 files changed, 14 insertions, 0 deletions
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, |
