authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-10-24 05:16:49+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-10-24 14:28:34+01:00
logf2814caaf04fc041a028d5ede4fb4db5ee2f19ae
tree50dfaa41d3118225cf1d3da6b31d637e657792bf
parent67caf685056bcdbbe4e696cada927615b0dd6fe9
signaturelock-open Commit is signed but in an unrecognized format.

Sema: don't allow undef values through resolveDefinedValue in typeof block

This logic is not correct in most cases. If any instruction needs to operate with different semantics within `@TypeOf`, it should be made to do so explicitly. This broke a line in `std.mem`: I have opted to fix this in std for now, since as far as I know it's not yet been discussed which operations (if any) should be special-cased like this within `@TypeOf`.

2 files changed, 7 insertions(+), 2 deletions(-)

lib/std/mem.zig+7-1
...@@ -3666,7 +3666,13 @@ fn ReverseIterator(comptime T: type) type {...@@ -3666,7 +3666,13 @@ fn ReverseIterator(comptime T: type) type {
3666 @compileError("expected slice or pointer to array, found '" ++ @typeName(T) ++ "'");3666 @compileError("expected slice or pointer to array, found '" ++ @typeName(T) ++ "'");
3667 };3667 };
3668 const Element = std.meta.Elem(Pointer);3668 const Element = std.meta.Elem(Pointer);
3669 const ElementPointer = @TypeOf(&@as(Pointer, undefined)[0]);3669 const ElementPointer = @Type(.{ .Pointer = ptr: {
3670 var ptr = @typeInfo(Pointer).Pointer;
3671 ptr.size = .One;
3672 ptr.child = Element;
3673 ptr.sentinel = null;
3674 break :ptr ptr;
3675 } });
3670 return struct {3676 return struct {
3671 ptr: Pointer,3677 ptr: Pointer,
3672 index: usize,3678 index: usize,
src/Sema.zig-1
...@@ -2104,7 +2104,6 @@ fn resolveDefinedValue(...@@ -2104,7 +2104,6 @@ fn resolveDefinedValue(
2104 const mod = sema.mod;2104 const mod = sema.mod;
2105 const val = try sema.resolveValue(air_ref) orelse return null;2105 const val = try sema.resolveValue(air_ref) orelse return null;
2106 if (val.isUndef(mod)) {2106 if (val.isUndef(mod)) {
2107 if (block.is_typeof) return null;
2108 return sema.failWithUseOfUndef(block, src);2107 return sema.failWithUseOfUndef(block, src);
2109 }2108 }
2110 return val;2109 return val;