authorgravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-03-22 23:32:57-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-23 13:44:31-07:00
log5374e245c50fde8bbb133bbff2db15efa3604721
treef97abf82092e557f0e73a119fe7ac36506b9e8f4
parent1c223819098e5621e2e5ad526879cd33852144e1

stage2: Remove premature elem_val index check

We were enforcing bounds on the index of an elem_ptr in pointerDeref, but we want to support out-of-bounds accesses by reinterpreting memory. This removes that check, so that the deref falls back to bitcasting, as usual. This was masked by another bug that was forcing bitcasts incorrectly, which is why this wasn't noticed earlier.

1 files changed, 7 insertions(+), 9 deletions(-)

src/Sema.zig+7-9
......@@ -18752,6 +18752,11 @@ fn beginComptimePtrLoad(
1875218752 const elem_ty = elem_ptr.elem_ty;
1875318753 var deref = try beginComptimePtrLoad(sema, block, src, elem_ptr.array_ptr, null);
1875418754
18755 // This code assumes that elem_ptrs have been "flattened" in order for direct dereference
18756 // to succeed, meaning that elem ptrs of the same elem_ty are coalesced. Here we check that
18757 // our parent is not an elem_ptr with the same elem_ty, since that would be "unflattened"
18758 if (elem_ptr.array_ptr.castTag(.elem_ptr)) |parent_elem_ptr| assert(!(parent_elem_ptr.data.elem_ty.eql(elem_ty, target)));
18759
1875518760 if (elem_ptr.index != 0) {
1875618761 if (elem_ty.hasWellDefinedLayout()) {
1875718762 if (deref.parent) |*parent| {
......@@ -18780,13 +18785,6 @@ fn beginComptimePtrLoad(
1878018785
1878118786 var array_tv = deref.pointee.?;
1878218787 const check_len = array_tv.ty.arrayLenIncludingSentinel();
18783 if (elem_ptr.index >= check_len) {
18784 // TODO have the deref include the decl so we can say "declared here"
18785 return sema.fail(block, src, "comptime load of index {d} out of bounds of array length {d}", .{
18786 elem_ptr.index, check_len,
18787 });
18788 }
18789
1879018788 if (maybe_array_ty) |load_ty| {
1879118789 // It's possible that we're loading a [N]T, in which case we'd like to slice
1879218790 // the pointee array directly from our parent array.
......@@ -18800,10 +18798,10 @@ fn beginComptimePtrLoad(
1880018798 }
1880118799 }
1880218800
18803 deref.pointee = .{
18801 deref.pointee = if (elem_ptr.index < check_len) TypedValue{
1880418802 .ty = elem_ty,
1880518803 .val = try array_tv.val.elemValue(sema.arena, elem_ptr.index),
18806 };
18804 } else null;
1880718805 break :blk deref;
1880818806 },
1880918807