-
-
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
compile error for modification of comptime data inside runtime block #1470
Comments
The specific decision here is that comptime vars should become immutable when inside a runtime branch. So for example: fn foo() {
comptime var x = 0; // x is mutable
if (runtime_value) {
comptime var y = 0;
// y is mutable, x is immutable
} else {
// x is immutable
}
for (some_slice) |item, i| {
// runtime branch, x is immutable
}
while (some_condition) {
// runtime branch, x is immutable
}
switch (runtime_value) {
else => {}, // runtime branch, x is immutable
}
if (comptime_value) {
// comptime branch, x is mutable
} else {
// comptime branch, x is mutable
}
inline for (some_array_or_tuple) |item, i| {
// not a runtime branch, x is mutable
}
inline while (some_comptime_condition) {
// not a runtime branch, x is mutable
}
switch (comptime_value) {
else => {}, // not a runtime branch, x is mutable
}
} This is similar to how comptime vars become immutable when they go out of scope. |
I don't think we've defined this before in the language: what is a "runtime branch"? |
A scope which is compiled but may or may not be executed, or may be executed multiple times, depending on values which are not comptime known. |
this is implemented now; just need to verify that we have test coverage before closing |
https://github.com/ziglang/zig/blob/master/test/cases/x86_64-linux/comptime_var.0.zig though the first file of these iterations should likely be pulled out of the target folder |
|
FYI if anybody runs into "error: store to comptime variable depends on runtime condition", but your runtime condition can be calculated at comptime, then wrap that section in a comptime block. // BEFORE
inline for (type_info.Struct.fields) |field| {
const FieldType = field.type;
if (isSliceType(FieldType)) count += 1; // isSliceType is runtime condition
}
// AFTER
comptime {
for (type_info.Struct.fields) |field| {
const FieldType = field.type;
if (isSliceType(FieldType)) count += 1; // now comptime
}
} In this example, it would be been simpler to |
you can also use inline for (type_info.Struct.fields) |field| {
const FieldType = field.type;
- if (isSliceType(FieldType)) count += 1; // isSliceType is runtime condition
+ if (comptime isSliceType(FieldType)) count += 1; // isSliceType is runtime condition
} |
Inspired by #906
count += 1
is modification of comptime data inside of a runtime block. It can have surprising results, as demonstrated by this test case. With this proposal, Zig would detect thatcount
was declared outside the runtime block scope, and therefore this is illegal modification of comptime data.The text was updated successfully, but these errors were encountered: