| author | |
| committer | |
| log | c036f83fa0f029e8e03e599f5b9e95cf98224a1c |
| tree | e187b0edca116f386992449434456193d083e671 |
| parent | cc2daae47e4fbb7d68e00211d509dd934d2e14b2 |
3 files changed, 28 insertions(+), 4 deletions(-)
src/Sema.zig+1-1| ... | ... | @@ -30579,7 +30579,7 @@ fn analyzeLoad( |
| 30579 | 30579 | |
| 30580 | 30580 | if (try sema.resolveDefinedValue(block, ptr_src, ptr)) |ptr_val| { |
| 30581 | 30581 | 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); | |
| 30583 | 30583 | } |
| 30584 | 30584 | } |
| 30585 | 30585 |
src/value.zig+15-3| ... | ... | @@ -946,12 +946,24 @@ pub const Value = struct { |
| 946 | 946 | }, |
| 947 | 947 | .Pointer => { |
| 948 | 948 | 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(); | |
| 950 | 954 | }, |
| 951 | 955 | .Optional => { |
| 952 | 956 | 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(); | |
| 955 | 967 | }, |
| 956 | 968 | else => @panic("TODO implement readFromMemory for more types"), |
| 957 | 969 | } |
test/behavior/comptime_memory.zig+12| ... | ... | @@ -438,3 +438,15 @@ test "type pun extern struct" { |
| 438 | 438 | @as(*u8, @ptrCast(&s)).* = 72; |
| 439 | 439 | try testing.expectEqual(@as(u8, 72), s.f); |
| 440 | 440 | } |
| 441 | ||
| 442 | test "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 | ||
| 448 | test "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 | } |