Skip to content

Commit 069fc1a

Browse files
committed
peer type resolution with C pointers
See #1059
1 parent 57a7ab0 commit 069fc1a

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/ir.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -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
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,18 @@ test "C pointer comparison and arithmetic" {
8282
S.doTheTest();
8383
comptime S.doTheTest();
8484
}
85+
86+
test "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+
}

0 commit comments

Comments
 (0)