authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-11 19:21:59-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-11 19:21:59-05:00
log069fc1a26990b3946cf788b4ebe5edefca5a3bfd
treebc9c44ef542d629ad3d2cb15c085ea7c4827a830
parent57a7ab0d330416f15c0288004b67101c1c3e9629
signaturelock-open Commit is signed but in an unrecognized format.

peer type resolution with C pointers

See #1059

2 files changed, 33 insertions(+), 0 deletions(-)

src/ir.cpp+18
......@@ -9275,6 +9275,24 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
92759275 continue;
92769276 }
92779277
9278 if (prev_type->id == ZigTypeIdPointer && cur_type->id == ZigTypeIdPointer) {
9279 if (prev_type->data.pointer.ptr_len == PtrLenC &&
9280 types_match_const_cast_only(ira, prev_type->data.pointer.child_type,
9281 cur_type->data.pointer.child_type, source_node,
9282 !prev_type->data.pointer.is_const).id == ConstCastResultIdOk)
9283 {
9284 continue;
9285 }
9286 if (cur_type->data.pointer.ptr_len == PtrLenC &&
9287 types_match_const_cast_only(ira, cur_type->data.pointer.child_type,
9288 prev_type->data.pointer.child_type, source_node,
9289 !cur_type->data.pointer.is_const).id == ConstCastResultIdOk)
9290 {
9291 prev_inst = cur_inst;
9292 continue;
9293 }
9294 }
9295
92789296 if (types_match_const_cast_only(ira, prev_type, cur_type, source_node, false).id == ConstCastResultIdOk) {
92799297 continue;
92809298 }
test/stage1/behavior/pointers.zig+15
......@@ -82,3 +82,18 @@ test "C pointer comparison and arithmetic" {
8282 S.doTheTest();
8383 comptime S.doTheTest();
8484}
85
86test "peer type resolution with C pointers" {
87 var ptr_one: *u8 = undefined;
88 var ptr_many: [*]u8 = undefined;
89 var ptr_c: [*c]u8 = undefined;
90 var t = true;
91 var x1 = if (t) ptr_one else ptr_c;
92 var x2 = if (t) ptr_many else ptr_c;
93 var x3 = if (t) ptr_c else ptr_one;
94 var x4 = if (t) ptr_c else ptr_many;
95 expect(@typeOf(x1) == [*c]u8);
96 expect(@typeOf(x2) == [*c]u8);
97 expect(@typeOf(x3) == [*c]u8);
98 expect(@typeOf(x4) == [*c]u8);
99}