-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Switch expression returns incorrect value. #8640
Comments
I'm also having a similar problem where the result of a switch is different if the variable is const std = @import("std");
pub fn main() void {
const x: u32 = 1;
var y = x;
std.log.info("{s}", .{switch (x) {
0 => @as([]const u8, "a"),
1 => @as([]const u8, "b"),
2 => @as([]const u8, "c"),
else => @as([]const u8, "wow"),
}});
std.log.info("{s}", .{switch (y) {
0 => @as([]const u8, "a"),
1 => @as([]const u8, "b"),
2 => @as([]const u8, "c"),
else => @as([]const u8, "wow"),
}});
} The above program prints
instead of the expected
|
const std = @import("std");
pub fn main() void {
var y: u32 = 1;
var answer = switch (y) {
1 => @as([]const u8, "b"),
else => @as([]const u8, "wow"),
};
std.log.info("{d},{s}", .{ y, answer });
} This works. So I think this may not be a problem of switch expression? |
const std = @import("std");
pub fn main() void {
var y: u32 = 1;
std.log.info("{s}", .{switch (y) {
1 => @as([]const u8, "b"),
else => unreachable,
}});
} Also this works. |
@Sirius902's case is a duplicate of #7097 |
idk I tested this behavior on the nightly build. so if this problem still exists, you can keep this issue. |
unless the initial code snippet is also a duplicate, it needs to be added to https://github.com/ziglang/zig/tree/master/test/cases/ to catch/prevent regressions in order for the issue to be closed |
ok so |
Hello, I encountered the following bug when trying to use switch expressions.
When running the code below, I would expect that the variable
state
to change to the valueStates.B
afterchar
changes to the value'.'
.Instead the value of
state
stays the same.When examining the assembly it seems that the value is correctly changed at first, but shortly after replaced with the value of
States.A
https://godbolt.org/z/d9aaTj6z8
Output
I'm using zig version 0.8.0-dev.2008+6cd71931c.
The text was updated successfully, but these errors were encountered: