aboutsummaryrefslogtreecommitdiff
path: root/src/value.zig
diff options
context:
space:
mode:
authorVeikka Tuominen <git@vexu.eu>2022-02-25 16:20:57 +0200
committerAndrew Kelley <andrew@ziglang.org>2022-02-26 12:52:06 -0700
commitbff7714a7c681ec41abf45a6cfc74a32ff8655dd (patch)
treef4fd12151470deb8a988e2d3dc3c5b17f363adc5 /src/value.zig
parentee149aaa03e586e48c32cce09bf488ae0e88d053 (diff)
downloadzig-bff7714a7c681ec41abf45a6cfc74a32ff8655dd.tar.gz
zig-bff7714a7c681ec41abf45a6cfc74a32ff8655dd.zip
stage2: fix toAllocatedBytes on slices
Diffstat (limited to 'src/value.zig')
-rw-r--r--src/value.zig23
1 files changed, 14 insertions, 9 deletions
diff --git a/src/value.zig b/src/value.zig
index 4df36f79a1..7c28398b73 100644
--- a/src/value.zig
+++ b/src/value.zig
@@ -791,19 +791,24 @@ pub const Value = extern union {
return decl_val.toAllocatedBytes(decl.ty, allocator);
},
.the_only_possible_value => return &[_]u8{},
- .slice => return toAllocatedBytes(val.castTag(.slice).?.data.ptr, ty, allocator),
- else => {
- const result = try allocator.alloc(u8, @intCast(usize, ty.arrayLen()));
- var elem_value_buf: ElemValueBuffer = undefined;
- for (result) |*elem, i| {
- const elem_val = val.elemValueBuffer(i, &elem_value_buf);
- elem.* = @intCast(u8, elem_val.toUnsignedInt());
- }
- return result;
+ .slice => {
+ const slice = val.castTag(.slice).?.data;
+ return arrayToAllocatedBytes(slice.ptr, slice.len.toUnsignedInt(), allocator);
},
+ else => return arrayToAllocatedBytes(val, ty.arrayLen(), allocator),
}
}
+ fn arrayToAllocatedBytes(val: Value, len: u64, allocator: Allocator) ![]u8 {
+ const result = try allocator.alloc(u8, @intCast(usize, len));
+ var elem_value_buf: ElemValueBuffer = undefined;
+ for (result) |*elem, i| {
+ const elem_val = val.elemValueBuffer(i, &elem_value_buf);
+ elem.* = @intCast(u8, elem_val.toUnsignedInt());
+ }
+ return result;
+ }
+
pub const ToTypeBuffer = Type.Payload.Bits;
/// Asserts that the value is representable as a type.