-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
comptime
keyword used with runtime-known identifier fails to produce a compile error
#2471
Comments
Here's what's happening in each of your examples: if (comptime hasField()) {
if (comptime hasField() and x == 123) { Perhaps related to #114, this is parsed as if (x == 123 and comptime hasField()) {
const std = @import("std");
test "aoeu" {
var x: u32 = 123; // runtime value
if (comptime x == 123) {
std.debug.warn("why am i allowed to do this? what does it mean?\n", .{});
}
} This is a bug. If the expression is used in another context where a comptime value is required, you get a compilation error as expected: const std = @import("std");
test "aoeu" {
var x: u32 = 123; // runtime value
var y: [@boolToInt(x == 123)]u8 = undefined;
}
This compile error is correct; the other case is incorrectly missing the compile error. |
this affects stage2 as well |
The only issue here is this: const std = @import("std");
test "comptime keyword used on runtime-known variable" {
var x: u32 = 123; // runtime value
if (comptime x == 123) { // should be a compile error because `x` is runtime-known
return error.TestFailed;
}
}
|
comptime
expression prefix is ignored in some casescomptime
keyword used with runtime-known identifier fails to produce a compile error
Output:
Disclaimer: I don't actually know how this
comptime
thing works. I don't think it's even documented? Is it an official feature? (I find it useful.)In the above test, I'm not sure what I expect to happen. In the first condition,
comptime hasField() and x == 123
, I don't whethercomptime
is supposed to apply to the first term or to the whole expression.But in any case, I think there should be a compile error if a
comptime
is not going to be applied.The text was updated successfully, but these errors were encountered: