authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-06-25 18:50:58-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-26 01:30:00-07:00
logc036f83fa0f029e8e03e599f5b9e95cf98224a1c
treee187b0edca116f386992449434456193d083e671
parentcc2daae47e4fbb7d68e00211d509dd934d2e14b2

Value: fix incorrect types returned from readFromMemory


3 files changed, 28 insertions(+), 4 deletions(-)

src/Sema.zig+1-1
......@@ -30579,7 +30579,7 @@ fn analyzeLoad(
3057930579
3058030580 if (try sema.resolveDefinedValue(block, ptr_src, ptr)) |ptr_val| {
3058130581 if (try sema.pointerDeref(block, src, ptr_val, ptr_ty)) |elem_val| {
30582 return sema.addConstant(try mod.getCoerced(elem_val, elem_ty));
30582 return sema.addConstant(elem_val);
3058330583 }
3058430584 }
3058530585
src/value.zig+15-3
......@@ -946,12 +946,24 @@ pub const Value = struct {
946946 },
947947 .Pointer => {
948948 assert(!ty.isSlice(mod)); // No well defined layout.
949 return readFromMemory(Type.usize, mod, buffer, arena);
949 const int_val = try readFromMemory(Type.usize, mod, buffer, arena);
950 return (try mod.intern(.{ .ptr = .{
951 .ty = ty.toIntern(),
952 .addr = .{ .int = int_val.toIntern() },
953 } })).toValue();
950954 },
951955 .Optional => {
952956 assert(ty.isPtrLikeOptional(mod));
953 const child = ty.optionalChild(mod);
954 return readFromMemory(child, mod, buffer, arena);
957 const child_ty = ty.optionalChild(mod);
958 const child_val = try readFromMemory(child_ty, mod, buffer, arena);
959 return (try mod.intern(.{ .opt = .{
960 .ty = ty.toIntern(),
961 .val = switch (child_val.orderAgainstZero(mod)) {
962 .lt => unreachable,
963 .eq => .none,
964 .gt => child_val.toIntern(),
965 },
966 } })).toValue();
955967 },
956968 else => @panic("TODO implement readFromMemory for more types"),
957969 }
test/behavior/comptime_memory.zig+12
......@@ -438,3 +438,15 @@ test "type pun extern struct" {
438438 @as(*u8, @ptrCast(&s)).* = 72;
439439 try testing.expectEqual(@as(u8, 72), s.f);
440440}
441
442test "type pun @ptrFromInt" {
443 const p: *u8 = @ptrFromInt(42);
444 // note that expectEqual hides the bug
445 try testing.expect(@as(*const [*]u8, @ptrCast(&p)).* == @as([*]u8, @ptrFromInt(42)));
446}
447
448test "type pun null pointer-like optional" {
449 const p: ?*u8 = null;
450 // note that expectEqual hides the bug
451 try testing.expect(@as(*const ?*i8, @ptrCast(&p)).* == null);
452}