authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-10-17 19:10:49-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-10-17 19:10:49-07:00
log40cbf525f7d7b17b0728f835e6f68efe3c2eabf6
tree4533eaccd46b97525f0f9c744216d9353cc2a475
parente5dac0a0b391f227605e496a09f32b453ac3280d

stage2: implement coercion from null to C pointer


4 files changed, 33 insertions(+), 25 deletions(-)

src/Sema.zig+18-11
......@@ -4731,7 +4731,7 @@ fn zirFunc(
47314731 body_inst,
47324732 ret_ty_body,
47334733 cc,
4734 Value.initTag(.null_value),
4734 Value.@"null",
47354735 false,
47364736 inferred_error_set,
47374737 false,
......@@ -8105,7 +8105,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
81058105 // return_type: ?type,
81068106 field_values[4] = try Value.Tag.ty.create(sema.arena, ty.fnReturnType());
81078107 // args: []const FnArg,
8108 field_values[5] = Value.initTag(.null_value); // TODO
8108 field_values[5] = Value.@"null"; // TODO
81098109
81108110 return sema.addConstant(
81118111 type_info_ty,
......@@ -8163,7 +8163,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
81638163 // is_allowzero: bool,
81648164 field_values[5] = if (info.@"allowzero") Value.initTag(.bool_true) else Value.initTag(.bool_false);
81658165 // sentinel: anytype,
8166 field_values[6] = if (info.sentinel) |some| try Value.Tag.opt_payload.create(sema.arena, some) else Value.initTag(.null_value);
8166 field_values[6] = if (info.sentinel) |some| try Value.Tag.opt_payload.create(sema.arena, some) else Value.@"null";
81678167
81688168 return sema.addConstant(
81698169 type_info_ty,
......@@ -8181,7 +8181,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
81818181 // child: type,
81828182 field_values[1] = try Value.Tag.ty.create(sema.arena, info.elem_type);
81838183 // sentinel: anytype,
8184 field_values[2] = if (info.sentinel) |some| try Value.Tag.opt_payload.create(sema.arena, some) else Value.initTag(.null_value);
8184 field_values[2] = if (info.sentinel) |some| try Value.Tag.opt_payload.create(sema.arena, some) else Value.@"null";
81858185
81868186 return sema.addConstant(
81878187 type_info_ty,
......@@ -9753,7 +9753,7 @@ fn zirCmpxchg(
97539753
97549754 // special case zero bit types
97559755 if ((try sema.typeHasOnePossibleValue(block, elem_ty_src, elem_ty)) != null) {
9756 return sema.addConstant(result_ty, Value.initTag(.null_value));
9756 return sema.addConstant(result_ty, Value.@"null");
97579757 }
97589758
97599759 const runtime_src = if (try sema.resolveDefinedValue(block, ptr_src, ptr)) |ptr_val| rs: {
......@@ -9767,7 +9767,7 @@ fn zirCmpxchg(
97679767 const stored_val = (try ptr_val.pointerDeref(sema.arena)) orelse break :rs ptr_src;
97689768 const result_val = if (stored_val.eql(expected_val, elem_ty)) blk: {
97699769 try sema.storePtr(block, src, ptr, new_value);
9770 break :blk Value.initTag(.null_value);
9770 break :blk Value.@"null";
97719771 } else try Value.Tag.opt_payload.create(sema.arena, stored_val);
97729772
97739773 return sema.addConstant(result_ty, result_val);
......@@ -10225,7 +10225,7 @@ fn zirVarExtended(
1022510225 // extra_index += 1;
1022610226 // const align_tv = try sema.resolveInstConst(block, align_src, align_ref);
1022710227 // break :blk align_tv.val;
10228 //} else Value.initTag(.null_value);
10228 //} else Value.@"null";
1022910229
1023010230 const uncasted_init: Air.Inst.Ref = if (small.has_init) blk: {
1023110231 const init_ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_index]);
......@@ -10307,7 +10307,7 @@ fn zirFuncExtended(
1030710307 extra_index += 1;
1030810308 const align_tv = try sema.resolveInstConst(block, align_src, align_ref);
1030910309 break :blk align_tv.val;
10310 } else Value.initTag(.null_value);
10310 } else Value.@"null";
1031110311
1031210312 const ret_ty_body = sema.code.extra[extra_index..][0..extra.data.ret_body_len];
1031310313 extra_index += ret_ty_body.len;
......@@ -10592,7 +10592,7 @@ fn panicWithMsg(
1059210592 });
1059310593 const null_stack_trace = try sema.addConstant(
1059410594 try Type.optional(arena, ptr_stack_trace_ty),
10595 Value.initTag(.null_value),
10595 Value.@"null",
1059610596 );
1059710597 const args = try arena.create([2]Air.Inst.Ref);
1059810598 args.* = .{ msg_inst, null_stack_trace };
......@@ -11544,7 +11544,7 @@ fn coerce(
1154411544 .Optional => {
1154511545 // null to ?T
1154611546 if (inst_ty.zigTypeTag() == .Null) {
11547 return sema.addConstant(dest_ty, Value.initTag(.null_value));
11547 return sema.addConstant(dest_ty, Value.@"null");
1154811548 }
1154911549
1155011550 // T to ?T
......@@ -11605,6 +11605,13 @@ fn coerce(
1160511605 .One => {},
1160611606 }
1160711607 }
11608
11609 // coercion to C pointer
11610 if (dest_ty.ptrSize() == .C) {
11611 if (inst_ty.zigTypeTag() == .Null) {
11612 return sema.addConstant(dest_ty, Value.@"null");
11613 }
11614 }
1160811615 },
1160911616 .Int => {
1161011617 // integer widening
......@@ -13806,7 +13813,7 @@ fn typeHasOnePossibleValue(
1380613813 .empty_struct, .empty_struct_literal => return Value.initTag(.empty_struct_value),
1380713814 .void => return Value.void,
1380813815 .noreturn => return Value.initTag(.unreachable_value),
13809 .@"null" => return Value.initTag(.null_value),
13816 .@"null" => return Value.@"null",
1381013817 .@"undefined" => return Value.initTag(.undef),
1381113818
1381213819 .int_unsigned, .int_signed => {
src/value.zig+1
......@@ -2824,6 +2824,7 @@ pub const Value = extern union {
28242824 pub const negative_one: Value = .{ .ptr_otherwise = &negative_one_payload.base };
28252825 pub const undef = initTag(.undef);
28262826 pub const @"void" = initTag(.void_value);
2827 pub const @"null" = initTag(.null_value);
28272828};
28282829
28292830var negative_one_payload: Value.Payload.I64 = .{
test/behavior/pointers.zig+14
......@@ -44,3 +44,17 @@ test "double pointer parsing" {
4444fn PtrOf(comptime T: type) type {
4545 return *T;
4646}
47
48test "implicit cast single item pointer to C pointer and back" {
49 var y: u8 = 11;
50 var x: [*c]u8 = &y;
51 var z: *u8 = x;
52 z.* += 1;
53 try expect(y == 12);
54}
55
56test "initialize const optional C pointer to null" {
57 const a: ?[*c]i32 = null;
58 try expect(a == null);
59 comptime try expect(a == null);
60}
test/behavior/pointers_stage1.zig-14
......@@ -29,14 +29,6 @@ test "assigning integer to C pointer" {
2929 }
3030}
3131
32test "implicit cast single item pointer to C pointer and back" {
33 var y: u8 = 11;
34 var x: [*c]u8 = &y;
35 var z: *u8 = x;
36 z.* += 1;
37 try expect(y == 12);
38}
39
4032test "C pointer comparison and arithmetic" {
4133 const S = struct {
4234 fn doTheTest() !void {
......@@ -103,12 +95,6 @@ test "implicit cast error unions with non-optional to optional pointer" {
10395 comptime try S.doTheTest();
10496}
10597
106test "initialize const optional C pointer to null" {
107 const a: ?[*c]i32 = null;
108 try expect(a == null);
109 comptime try expect(a == null);
110}
111
11298test "compare equality of optional and non-optional pointer" {
11399 const a = @intToPtr(*const usize, 0x12345678);
114100 const b = @intToPtr(?*usize, 0x12345678);