authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-09-26 06:20:54-04:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-09-27 13:21:01+03:00
loge915b905e0d5e397cc298abee1c34cda357bb573
tree1f5ba7f99416aaa0958231c6f428a109f9fe5836
parent38a50f819c8cfb4a8d08e76934bb90f223bc6d7c

use @ptrCast to assigned generic type to default_value

If the type happens to be a pointer, then the double pointer will not coerce implicitly.

2 files changed, 16 insertions(+), 2 deletions(-)

lib/compiler_rt/emutls.zig+1-1
...@@ -296,7 +296,7 @@ const emutls_control = extern struct {...@@ -296,7 +296,7 @@ const emutls_control = extern struct {
296 .size = @sizeOf(T),296 .size = @sizeOf(T),
297 .alignment = @alignOf(T),297 .alignment = @alignOf(T),
298 .object = .{ .index = 0 },298 .object = .{ .index = 0 },
299 .default_value = @ptrCast(?*anyopaque, default_value),299 .default_value = @ptrCast(?*const anyopaque, default_value),
300 };300 };
301 }301 }
302302
lib/std/enums.zig+15-1
...@@ -16,7 +16,7 @@ pub fn EnumFieldStruct(comptime E: type, comptime Data: type, comptime field_def...@@ -16,7 +16,7 @@ pub fn EnumFieldStruct(comptime E: type, comptime Data: type, comptime field_def
16 fields = fields ++ &[_]StructField{.{16 fields = fields ++ &[_]StructField{.{
17 .name = field.name,17 .name = field.name,
18 .field_type = Data,18 .field_type = Data,
19 .default_value = if (field_default) |d| &d else null,19 .default_value = if (field_default) |d| @ptrCast(?*const anyopaque, &d) else null,
20 .is_comptime = false,20 .is_comptime = false,
21 .alignment = if (@sizeOf(Data) > 0) @alignOf(Data) else 0,21 .alignment = if (@sizeOf(Data) > 0) @alignOf(Data) else 0,
22 }};22 }};
...@@ -160,6 +160,20 @@ test "std.enums.directEnumArrayDefault" {...@@ -160,6 +160,20 @@ test "std.enums.directEnumArrayDefault" {
160 try testing.expectEqual(false, array[2]);160 try testing.expectEqual(false, array[2]);
161}161}
162162
163test "std.enums.directEnumArrayDefault slice" {
164 const E = enum(i4) { a = 4, b = 6, c = 2 };
165 var runtime_b = "b";
166 const array = directEnumArrayDefault(E, []const u8, "default", 4, .{
167 .a = "a",
168 .b = runtime_b,
169 });
170
171 try testing.expectEqual([7][]const u8, @TypeOf(array));
172 try testing.expectEqualSlices(u8, "a", array[4]);
173 try testing.expectEqualSlices(u8, "b", array[6]);
174 try testing.expectEqualSlices(u8, "default", array[2]);
175}
176
163/// Cast an enum literal, value, or string to the enum value of type E177/// Cast an enum literal, value, or string to the enum value of type E
164/// with the same name.178/// with the same name.
165pub fn nameCast(comptime E: type, comptime value: anytype) E {179pub fn nameCast(comptime E: type, comptime value: anytype) E {