value: use int tag type when querying for tag value

Closes #13757
This commit is contained in:
Veikka Tuominen 2022-12-19 15:07:11 +02:00
parent 2926d95e6a
commit 22d46e1d77
2 changed files with 31 additions and 4 deletions

View File

@ -1072,11 +1072,13 @@ pub const Value = extern union {
.enum_simple => Module.EnumFull.ValueMap{},
else => unreachable,
};
break :field_index if (values.entries.len == 0)
if (values.entries.len == 0) {
// auto-numbered enum
@intCast(u32, val.toUnsignedInt(mod.getTarget()))
else
@intCast(u32, values.getIndexContext(val, .{ .ty = ty, .mod = mod }).?);
break :field_index @intCast(u32, val.toUnsignedInt(mod.getTarget()));
}
var buffer: Type.Payload.Bits = undefined;
const int_tag_ty = ty.intTagType(&buffer);
break :field_index @intCast(u32, values.getIndexContext(val, .{ .ty = int_tag_ty, .mod = mod }).?);
},
};

View File

@ -344,3 +344,28 @@ test "inline call doesn't re-evaluate non generic struct" {
try @call(.always_inline, S.foo, ArgTuple{.{ .a = 123, .b = 45 }});
comptime try @call(.always_inline, S.foo, ArgTuple{.{ .a = 123, .b = 45 }});
}
test "Enum constructed by @Type passed as generic argument" {
const S = struct {
const E = std.meta.FieldEnum(struct {
prev_pos: bool,
pos: bool,
vel: bool,
damp_vel: bool,
acc: bool,
rgba: bool,
prev_scale: bool,
scale: bool,
prev_rotation: bool,
rotation: bool,
angular_vel: bool,
alive: bool,
});
fn foo(comptime a: E, b: u32) !void {
try expect(@enumToInt(a) == b);
}
};
inline for (@typeInfo(S.E).Enum.fields) |_, i| {
try S.foo(@intToEnum(S.E, i), i);
}
}