Sema: don't assume slice value is interned when loading from comptime pointer

Resolves: #16030
This commit is contained in:
mlugg 2023-06-15 01:04:22 +01:00
parent b975701a4d
commit 45e9617720
No known key found for this signature in database
GPG Key ID: 58978E823BDE3EF9
2 changed files with 18 additions and 1 deletions

View File

@ -29265,7 +29265,7 @@ fn beginComptimePtrLoad(
},
Value.slice_len_index => TypedValue{
.ty = Type.usize,
.val = mod.intern_pool.indexToKey(tv.val.toIntern()).ptr.len.toValue(),
.val = mod.intern_pool.indexToKey(try tv.val.intern(tv.ty, mod)).ptr.len.toValue(),
},
else => unreachable,
};

View File

@ -869,3 +869,20 @@ test "write through pointer to optional slice arg" {
try S.bar(&foo);
try expectEqualStrings(foo.?, "ok");
}
test "modify slice length at comptime" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const arr: [2]u8 = .{ 10, 20 };
comptime var s: []const u8 = arr[0..0];
s.len += 1;
const a = s;
s.len += 1;
const b = s;
try expectEqualSlices(u8, &.{10}, a);
try expectEqualSlices(u8, &.{ 10, 20 }, b);
}