-
-
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
pointers to "comptime mutable" data should be const pointers outside the comptime scope #1487
Comments
Here's a further reduced test case: const std = @import("std");
const assert = std.debug.assert;
var a = &init().field;
const Foo = struct {
field: i32,
};
fn init() Foo {
return Foo{ .field = 1234 };
}
test "oaeu" {
assert(a.* == 1234);
a.* += 1;
assert(a.* == 1235);
} |
Ah. What's going on is that the "hidden" global data is valid, but constant. But when we get a pointer to the data, it's a mutable pointer, so Zig lets us attempt to mutate the data. This causes a segfault because the data is in the read only section. So it's a missing compile error. E.g. |
Even simpler: var a = blk: {
var x: i32 = undefined;
break :blk &x;
};
export fn entry() void {
@compileLog(@typeOf(a));
} This outputs |
Postponing to 0.4.0 since there's a reasonable workaround, and the fix is rather involved. |
What if we access this data multiple times in the code? We'd need to instantiate it once for every handover to runtime. Inefficient, and not obvious. I don't think it should be allowed at all. See #5874. |
This is not true if the data is const at runtime. It exists in one place in the globals section and nothing can change it. |
Consider the following code: comptime var comptime_data: u32 = 3;
const comppoint = &comptime_data;
var runpoint = comppoint;
var runtime_data = runpoint.*;
comppoint.* = 4;
var runtime_data2 = runpoint.*;
std.debug.print("{}, {}", .{runtime_data, runtime_data2}); What will be the output of |
Promoting to a stage2 issue. Here's a self-contained test case that is expected to pass: const std = @import("std");
var a = blk: {
var x: i32 = undefined;
break :blk &x;
};
test "mutable comptime memory should become const once it leaves scope" {
try std.testing.expectEqual(*const i32, @TypeOf(a));
} |
Given the changes made in #19414, and specifically this test case: zig/test/cases/compile_errors/comptime_var_referenced_by_decl.zig Lines 27 to 31 in d02c2c7
|
The following crashes on Windows.
replacing the
allocator
line with:succeeds.
I'm guessing this is because DirectAllocator needs to maintain state on Windows because of the heap handle, and something about the failing test case's construction causes the outer struct to become invalid.
The text was updated successfully, but these errors were encountered: