peer type resolution with C pointers

See #1059
This commit is contained in:
Andrew Kelley 2019-02-11 19:21:59 -05:00
parent 57a7ab0d33
commit 069fc1a269
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
2 changed files with 33 additions and 0 deletions

View File

@ -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;
}

View File

@ -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);
}