Commit 069fc1a269

Andrew Kelley <andrew@ziglang.org>
2019-02-12 01:21:59
peer type resolution with C pointers
See #1059
1 parent 57a7ab0
Changed files (2)
src
test
stage1
behavior
src/ir.cpp
@@ -9275,6 +9275,24 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
             continue;
         }
 
+        if (prev_type->id == ZigTypeIdPointer && cur_type->id == ZigTypeIdPointer) {
+            if (prev_type->data.pointer.ptr_len == PtrLenC &&
+                types_match_const_cast_only(ira, prev_type->data.pointer.child_type,
+                    cur_type->data.pointer.child_type, source_node,
+                    !prev_type->data.pointer.is_const).id == ConstCastResultIdOk)
+            {
+                continue;
+            }
+            if (cur_type->data.pointer.ptr_len == PtrLenC &&
+                types_match_const_cast_only(ira, cur_type->data.pointer.child_type,
+                    prev_type->data.pointer.child_type, source_node,
+                    !cur_type->data.pointer.is_const).id == ConstCastResultIdOk)
+            {
+                prev_inst = cur_inst;
+                continue;
+            }
+        }
+
         if (types_match_const_cast_only(ira, prev_type, cur_type, source_node, false).id == ConstCastResultIdOk) {
             continue;
         }
test/stage1/behavior/pointers.zig
@@ -82,3 +82,18 @@ test "C pointer comparison and arithmetic" {
     S.doTheTest();
     comptime S.doTheTest();
 }
+
+test "peer type resolution with C pointers" {
+    var ptr_one: *u8 = undefined;
+    var ptr_many: [*]u8 = undefined;
+    var ptr_c: [*c]u8 = undefined;
+    var t = true;
+    var x1 = if (t) ptr_one else ptr_c;
+    var x2 = if (t) ptr_many else ptr_c;
+    var x3 = if (t) ptr_c else ptr_one;
+    var x4 = if (t) ptr_c else ptr_many;
+    expect(@typeOf(x1) == [*c]u8);
+    expect(@typeOf(x2) == [*c]u8);
+    expect(@typeOf(x3) == [*c]u8);
+    expect(@typeOf(x4) == [*c]u8);
+}