hibra
February 8, 2026, 9:16pm
1
Is there a reason for something like this to not be allowed?
const Value = union(enum) {
int: i32,
float: f32,
};
const v: Value = .{ .int = 10 };
const v_ptr = &v;
switch (v_ptr) {
.int => |i| std.debug.print("int {d}", .{i}),
.float => |f| std.debug.print("float: {d:.3}", .{f}),
else => unreachable,
}
1 Like
jmc
February 8, 2026, 9:22pm
2
Because switch (v_ptr.*) { ... } achieves the same thing?
7 Likes
vulpesx
February 8, 2026, 11:07pm
3
Because switches act on the direct values, if given a pointer you will be comparing the address (yes, you can do this), not the dereferenced value.
1 Like
hibra
February 8, 2026, 11:55pm
4
ty! switching on direct addresses seems kinda cursed lol but still cool
vulpesx
February 8, 2026, 11:57pm
5
It’s more type safe to allow it than forcing to convert to an integer and switching on that, you’d lose the child type and constness doing that.
Unfortunately switching on pointers has been broken for quite a while on the LLVM backend (and I think also on some other backends):
opened 05:09AM - 09 Dec 20 UTC
bug
frontend
The following example segfaults during compilation on both 0.7.0 and master.
``… `zig
pub fn main() !void {
var foo = "a";
switch (foo) {
"a" => {},
"b" => {},
else => {},
}
}
```
Regardless whether this should be valid Zig, it shouldn't cause a segfault without any error report.
Do note that this example compiles and works if one of the string branches is removed. What is the official stance on slice matching with switch?
and in light of #23509 it’s probably going to be removed anyway.
6 Likes
alexrp
February 9, 2026, 5:47am
7
Additionally, LLVM 22 has started rejecting the bitcode we produce for switching on pointers. We could try to fix that (in addition to the ways in which it’s already broken), but it’s far more likely we’ll just go ahead and axe the feature with the LLVM 22 upgrade.
6 Likes