Commit e915b905e0

Jacob Young <jacobly0@users.noreply.github.com>
2022-09-26 12:20:54
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.
1 parent 38a50f8
Changed files (2)
lib
compiler_rt
std
lib/compiler_rt/emutls.zig
@@ -296,7 +296,7 @@ const emutls_control = extern struct {
             .size = @sizeOf(T),
             .alignment = @alignOf(T),
             .object = .{ .index = 0 },
-            .default_value = @ptrCast(?*anyopaque, default_value),
+            .default_value = @ptrCast(?*const anyopaque, default_value),
         };
     }
 
lib/std/enums.zig
@@ -16,7 +16,7 @@ pub fn EnumFieldStruct(comptime E: type, comptime Data: type, comptime field_def
         fields = fields ++ &[_]StructField{.{
             .name = field.name,
             .field_type = Data,
-            .default_value = if (field_default) |d| &d else null,
+            .default_value = if (field_default) |d| @ptrCast(?*const anyopaque, &d) else null,
             .is_comptime = false,
             .alignment = if (@sizeOf(Data) > 0) @alignOf(Data) else 0,
         }};
@@ -160,6 +160,20 @@ test "std.enums.directEnumArrayDefault" {
     try testing.expectEqual(false, array[2]);
 }
 
+test "std.enums.directEnumArrayDefault slice" {
+    const E = enum(i4) { a = 4, b = 6, c = 2 };
+    var runtime_b = "b";
+    const array = directEnumArrayDefault(E, []const u8, "default", 4, .{
+        .a = "a",
+        .b = runtime_b,
+    });
+
+    try testing.expectEqual([7][]const u8, @TypeOf(array));
+    try testing.expectEqualSlices(u8, "a", array[4]);
+    try testing.expectEqualSlices(u8, "b", array[6]);
+    try testing.expectEqualSlices(u8, "default", array[2]);
+}
+
 /// Cast an enum literal, value, or string to the enum value of type E
 /// with the same name.
 pub fn nameCast(comptime E: type, comptime value: anytype) E {