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.
This commit is contained in:
Jacob Young 2022-09-26 06:20:54 -04:00
parent 6d7b0690a0
commit f14cc753d8
2 changed files with 16 additions and 2 deletions

View file

@ -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),
};
}

View file

@ -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 {