-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Crash with switch inside inline for #3136
Comments
I think it shouldn't be possible to switch on a float, because internally a float is always an approximation. Also the C standard prohibits the use of a float inside a switch expression. Thus I think this should add a compile error when a float is used inside a switch expression. |
This should be a compiler error for floats, but the issue is reproduced even with an pub fn float() i32 {
return 2;
}
pub fn foo(comptime args: var) void {
inline for (args) |a| {
switch (float()) {
0...1 => {},
else => {},
}
}
}
test "foo" {
foo([_]u32{ 20, 30, 57 });
}
|
Switching on floats came up in an issue regarding switch range syntax #359. I don't think any consensus was reached. |
This program also segfaults the compiler:
|
Your problem is similar to #4074, some custom kind of lowering must be implemented during the codegen phase as LLVM can only switch on integer types. |
Since llvm doesn't work with switching on non-ints (from scanning issues) would it make sense to have zig just convert switch on floats to an if / else if / else chain? |
To address the tolerance issue, maybe switching on a float should always require a range? If no range is given, compile throw a error. Or the lazy me would want to add a tolerance to the switch, eg |
Switching on floats has been disabled. |
Segfaults during LLVM codegen.
Reduced repro:
This worked some time ago, does on 0.4.0 and still does with the trunk version available on godbolt.
The text was updated successfully, but these errors were encountered: