-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
design flaw: semantics of loading values with undefined bits #19634
Copy link
Copy link
Open
Labels
researchThis proposal requires a considerable amount of investigation before it can be considered.This proposal requires a considerable amount of investigation before it can be considered.use caseDescribes a real use case that is difficult or impossible, but does not propose a solution.Describes a real use case that is difficult or impossible, but does not propose a solution.
Milestone
Metadata
Metadata
Assignees
Labels
researchThis proposal requires a considerable amount of investigation before it can be considered.This proposal requires a considerable amount of investigation before it can be considered.use caseDescribes a real use case that is difficult or impossible, but does not propose a solution.Describes a real use case that is difficult or impossible, but does not propose a solution.
Whilst working on #19630, it was suggested by @andrewrk to introduce the semantic rule that when loading a value from a pointer, if any bits are
undefined, the entire value is considered to beundefined. This rule simplifies the language considerably, because it avoids the need to deal with the awkward concept of a comptime-known value with some undefined bits. However, it causes breakages in some code.The main example I've noticed is
std.PackedIntArray, which uses a backing buffer to store a sequence of integer values bit-packed in memory. The problem is that if we initialize the buffer toundefined, any load which crosses a boundary onto an adjacent element -- even if the element being loaded was set previously -- is consideredundefined. Thus, the only way to obey the new semantics is to always@memsetthe buffer on creation, which is potentially wasteful.This issue serves to track this design complexity and potential solutions to it. The example I brought up above could potentially be solved with runtime bit-pointers (related: #16271), but I can't say for sure whether that would solve all potential problems.
For now, this rule is never enforced at runtime, only at comptime. Workarounds have been placed in the standard library where necessary behind
@inComptime()checks (such checks link to this issue).