diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-07-30 16:05:46 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-07-30 16:17:59 -0700 |
| commit | 507dc1f2e7fac212e79f152e557cbec98a3c30e9 (patch) | |
| tree | 34b057caf8ebadef2d199af39b1b44fca48c1a23 /src/RangeSet.zig | |
| parent | 84039a57e4684e8df10e657bb76c6acb3fb89238 (diff) | |
| download | zig-507dc1f2e7fac212e79f152e557cbec98a3c30e9.tar.gz zig-507dc1f2e7fac212e79f152e557cbec98a3c30e9.zip | |
stage2: fix hashing and comparison design flaw with Value
* `Value.toType` accepts a buffer parameter instead of an allocator
parameter and can no longer fail.
* Module: remove the unused `mod: *Module` parameter from various
functions.
* `Value.compare` now accepts a `Type` parameter which indicates the
type of both operands. There is also a `Value.compareHetero` which
accepts only Value parameters and supports comparing mixed types.
Likewise, `Value.eql` requires a `Type` parameter.
* `Value.hash` is removed; instead the hash map context structs now
have a `ty: Type` field, and the hash function lives there, where it
has access to a Value's Type when it computes a hash.
- This allowed the hash function to be greatly simplified and sound
in the sense that the same Values, even with different
representations, always hash to the same thing.
* Sema: Fix source location of zirCmp when an operand is runtime known
but needs to be comptime known.
* Remove unused target parameter from `Value.floatCast`.
Diffstat (limited to 'src/RangeSet.zig')
| -rw-r--r-- | src/RangeSet.zig | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/src/RangeSet.zig b/src/RangeSet.zig index fd258d55b0..2a8a55a077 100644 --- a/src/RangeSet.zig +++ b/src/RangeSet.zig @@ -1,5 +1,6 @@ const std = @import("std"); const Order = std.math.Order; +const Type = @import("type.zig").Type; const Value = @import("value.zig").Value; const RangeSet = @This(); const SwitchProngSrc = @import("Module.zig").SwitchProngSrc; @@ -22,9 +23,15 @@ pub fn deinit(self: *RangeSet) void { self.ranges.deinit(); } -pub fn add(self: *RangeSet, first: Value, last: Value, src: SwitchProngSrc) !?SwitchProngSrc { +pub fn add( + self: *RangeSet, + first: Value, + last: Value, + ty: Type, + src: SwitchProngSrc, +) !?SwitchProngSrc { for (self.ranges.items) |range| { - if (last.compare(.gte, range.first) and first.compare(.lte, range.last)) { + if (last.compare(.gte, range.first, ty) and first.compare(.lte, range.last, ty)) { return range.src; // They overlap. } } @@ -37,18 +44,18 @@ pub fn add(self: *RangeSet, first: Value, last: Value, src: SwitchProngSrc) !?Sw } /// Assumes a and b do not overlap -fn lessThan(_: void, a: Range, b: Range) bool { - return a.first.compare(.lt, b.first); +fn lessThan(ty: Type, a: Range, b: Range) bool { + return a.first.compare(.lt, b.first, ty); } -pub fn spans(self: *RangeSet, first: Value, last: Value) !bool { +pub fn spans(self: *RangeSet, first: Value, last: Value, ty: Type) !bool { if (self.ranges.items.len == 0) return false; - std.sort.sort(Range, self.ranges.items, {}, lessThan); + std.sort.sort(Range, self.ranges.items, ty, lessThan); - if (!self.ranges.items[0].first.eql(first) or - !self.ranges.items[self.ranges.items.len - 1].last.eql(last)) + if (!self.ranges.items[0].first.eql(first, ty) or + !self.ranges.items[self.ranges.items.len - 1].last.eql(last, ty)) { return false; } |
