-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
std.meta.stringToEnum
erroneous compile error with std.meta.FieldEnum
#17842
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Seems like it would be due to this line: Line 565 in b197c4d
which will set But this actually passes: test {
const E = enum(u0) { a };
try std.testing.expectEqual(@as(?E, E.a), std.meta.stringToEnum(E, "a"));
try std.testing.expectEqual(@as(?E, null), std.meta.stringToEnum(E, "b"));
} and a
|
@squeek502's alternate version above with using an enum directly does work, however, constructing the same enum type using const std = @import("std");
test {
//const E = enum(u0) { a };
const E = @Type(.{ .Enum = .{
.tag_type = u0,
.fields = &.{.{ .name = "a", .value = 0 }},
.decls = &.{},
.is_exhaustive = true,
} });
try std.testing.expectEqual(@as(?E, E.a), std.meta.stringToEnum(E, "a"));
try std.testing.expectEqual(@as(?E, null), std.meta.stringToEnum(E, "b"));
} Output:
The following example gives more insight into what's going on here by removing test {
//const E = enum(u0) { a };
const E = @Type(.{ .Enum = .{
.tag_type = u0,
.fields = &.{.{ .name = "a", .value = 0 }},
.decls = &.{},
.is_exhaustive = true,
} });
const e: E = @enumFromInt(0);
@compileLog(@TypeOf(e));
const T = struct { E };
const t: T = .{@enumFromInt(0)};
@compileLog(@typeInfo(T).Struct.fields);
@compileLog(@TypeOf(t[0]));
} Compile log output:
Compare with the compile log output when the non-
So, somehow, when By the way, changing |
Just retested this, and it still reproduces on the latest master branch. However, the signature changed:
|
This looks to be fixed as of 0.14.0-dev.2267+5f3a70ed5 (maybe a result of #21817?). The original test passes, and my test {
//const E = enum(u0) { a };
const E = @Type(.{ .@"enum" = .{
.tag_type = u0,
.fields = &.{.{ .name = "a", .value = 0 }},
.decls = &.{},
.is_exhaustive = true,
} });
const e: E = @enumFromInt(0);
@compileLog(@TypeOf(e));
const T = struct { E };
const t: T = .{@enumFromInt(0)};
@compileLog(@typeInfo(T).@"struct".fields);
@compileLog(@TypeOf(t[0]));
} Output (same regardless of which version of
|
Zig Version
0.12.0-dev.1369+a09ba455c
Steps to Reproduce and Observed Behavior
When
std.meta.FieldEnum
is used on a struct with a single field, then there is a compile error produced when attempting to convert a string to that enum. i.e.:The error:
I added a
@compileLog
instd.meta.stringToEnum
logging thekvs_array
which shows the value is undefined when it was expected to beE.a
:Oddly, this is not the case for a enum created through the normal mechanism:
Expected Behavior
That first test should compile and pass.
The text was updated successfully, but these errors were encountered: