diff options
| author | mlugg <mlugg@mlugg.co.uk> | 2023-03-16 17:51:31 +0000 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2023-03-17 01:56:36 -0400 |
| commit | 4ec299007a6a183edddfcb0505a9c1501da7ad0c (patch) | |
| tree | 147976e4d469e4e494244bb86111b73aaeb7bd11 /src/Sema.zig | |
| parent | 68c7261e1daf7787dcbf0cd6f9b01e5678d20a93 (diff) | |
| download | zig-4ec299007a6a183edddfcb0505a9c1501da7ad0c.tar.gz zig-4ec299007a6a183edddfcb0505a9c1501da7ad0c.zip | |
Sema: allow dereferencing ill-defined pointers to zero-bit types at comptime
It doesn't matter if a pointer to a zero-bit (i.e. OPV) type is
undefined or runtime-known; we still know the result of the dereference
at comptime. Code may use this, for instance, when allocating zero-bit
types: `@as(*void, undefined)` is entirely reasonable to use at runtime,
since we know the pointer will never be accessed, thus it should be
valid at comptime too.
Diffstat (limited to 'src/Sema.zig')
| -rw-r--r-- | src/Sema.zig | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index 8b476d4542..cdbd8f8b41 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -4712,6 +4712,11 @@ fn zirValidateDeref(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr .Slice => return sema.fail(block, src, "index syntax required for slice type '{}'", .{operand_ty.fmt(sema.mod)}), } + if ((try sema.typeHasOnePossibleValue(operand_ty.childType())) != null) { + // No need to validate the actual pointer value, we don't need it! + return; + } + const elem_ty = operand_ty.elemType2(); if (try sema.resolveMaybeUndefVal(operand)) |val| { if (val.isUndef()) { |
