mirror of
https://github.com/ziglang/zig.git
synced 2024-11-30 00:52:52 +00:00
Value: fix incorrect types returned from readFromMemory
This commit is contained in:
parent
cc2daae47e
commit
c036f83fa0
@ -30579,7 +30579,7 @@ fn analyzeLoad(
|
||||
|
||||
if (try sema.resolveDefinedValue(block, ptr_src, ptr)) |ptr_val| {
|
||||
if (try sema.pointerDeref(block, src, ptr_val, ptr_ty)) |elem_val| {
|
||||
return sema.addConstant(try mod.getCoerced(elem_val, elem_ty));
|
||||
return sema.addConstant(elem_val);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -946,12 +946,24 @@ pub const Value = struct {
|
||||
},
|
||||
.Pointer => {
|
||||
assert(!ty.isSlice(mod)); // No well defined layout.
|
||||
return readFromMemory(Type.usize, mod, buffer, arena);
|
||||
const int_val = try readFromMemory(Type.usize, mod, buffer, arena);
|
||||
return (try mod.intern(.{ .ptr = .{
|
||||
.ty = ty.toIntern(),
|
||||
.addr = .{ .int = int_val.toIntern() },
|
||||
} })).toValue();
|
||||
},
|
||||
.Optional => {
|
||||
assert(ty.isPtrLikeOptional(mod));
|
||||
const child = ty.optionalChild(mod);
|
||||
return readFromMemory(child, mod, buffer, arena);
|
||||
const child_ty = ty.optionalChild(mod);
|
||||
const child_val = try readFromMemory(child_ty, mod, buffer, arena);
|
||||
return (try mod.intern(.{ .opt = .{
|
||||
.ty = ty.toIntern(),
|
||||
.val = switch (child_val.orderAgainstZero(mod)) {
|
||||
.lt => unreachable,
|
||||
.eq => .none,
|
||||
.gt => child_val.toIntern(),
|
||||
},
|
||||
} })).toValue();
|
||||
},
|
||||
else => @panic("TODO implement readFromMemory for more types"),
|
||||
}
|
||||
|
@ -438,3 +438,15 @@ test "type pun extern struct" {
|
||||
@as(*u8, @ptrCast(&s)).* = 72;
|
||||
try testing.expectEqual(@as(u8, 72), s.f);
|
||||
}
|
||||
|
||||
test "type pun @ptrFromInt" {
|
||||
const p: *u8 = @ptrFromInt(42);
|
||||
// note that expectEqual hides the bug
|
||||
try testing.expect(@as(*const [*]u8, @ptrCast(&p)).* == @as([*]u8, @ptrFromInt(42)));
|
||||
}
|
||||
|
||||
test "type pun null pointer-like optional" {
|
||||
const p: ?*u8 = null;
|
||||
// note that expectEqual hides the bug
|
||||
try testing.expect(@as(*const ?*i8, @ptrCast(&p)).* == null);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user