compiler crash writing pointer to comptime var
to reinterpreted memory
#21216
Labels
bug
Observed behavior contradicts documented or intended behavior
Zig Version
0.14.0-dev.617+208baa37c
Steps to Reproduce and Observed Behavior
(For context, I was basically playing around with a userland implementation of a safety feature to simulate the pinned struct/type feature proposed in #7769.)
minimized repro:
zig test .zig
outputs a brief progress display, then crashes (exits with non-0 exit code).Expected Behavior
Certainly the compiler shouldn't crash.
The same code is valid at runtime (if you change the
comptime
block to atest
block), so ideally I'd like it to work atcomptime
.I agree that it's tricky though, the compiler would have to implement some serialization / bit representation of
comptime
-only pointers (if that doesn't exist yet).To be fully deterministic it should also assert that none of the bits used here escape to program runtime,
i.e. the memory should not be allowed into runtime.
(Though as a further improvement, if all affected bits have been overwritten, it should then again be allowed into runtime.)
Because of that complexity I expect the most likely short-term resolution to be a compile error that blocks this code from executing at
comptime
.A workaround would have to check
@inComptime()
and replace the affected logic with manual bookkeeping of some sort.Note that equivalent reinterpreting code using
packed union
leads to the other union fields being treated as a runtime value,and only crashes if trying to read these bits at runtime:
The text was updated successfully, but these errors were encountered: