authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-23 18:46:20-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-23 18:48:12-04:00
log4d50bc3f8dd2b9fd5b7dde6f63f104aaba6dbf5a
tree57890fd7b757fd39103327ffd05d1f4352d21128
parent89953ec83d8afe4fed0fc9e3cdded09c7522bf86
signaturelock-open Commit is signed but in an unrecognized format.

add peer type resolution for `*const T` and `?*T`

closes #1298

3 files changed, 31 insertions(+), 0 deletions(-)

doc/langref.html.in+7
......@@ -4695,6 +4695,13 @@ fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 {
46954695
46964696 return slice[0..1];
46974697}
4698
4699test "peer type resolution: *const T and ?*T" {
4700 const a = @intToPtr(*const usize, 0x123456789);
4701 const b = @intToPtr(?*usize, 0x123456789);
4702 assert(a == b);
4703 assert(b == a);
4704}
46984705 {#code_end#}
46994706 {#header_close#}
47004707 {#header_close#}
src/ir.cpp+17
......@@ -9658,6 +9658,23 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
96589658 continue;
96599659 }
96609660
9661 if (prev_type->id == ZigTypeIdOptional &&
9662 types_match_const_cast_only(ira, cur_type, prev_type->data.maybe.child_type,
9663 source_node, false).id == ConstCastResultIdOk)
9664 {
9665 prev_inst = cur_inst;
9666 any_are_null = true;
9667 continue;
9668 }
9669
9670 if (cur_type->id == ZigTypeIdOptional &&
9671 types_match_const_cast_only(ira, prev_type, cur_type->data.maybe.child_type,
9672 source_node, false).id == ConstCastResultIdOk)
9673 {
9674 any_are_null = true;
9675 continue;
9676 }
9677
96619678 if (cur_type->id == ZigTypeIdUndefined) {
96629679 continue;
96639680 }
test/stage1/behavior/pointers.zig+7
......@@ -130,3 +130,10 @@ test "initialize const optional C pointer to null" {
130130 expect(a == null);
131131 comptime expect(a == null);
132132}
133
134test "compare equality of optional and non-optional pointer" {
135 const a = @intToPtr(*const usize, 0x123456789);
136 const b = @intToPtr(?*usize, 0x123456789);
137 expect(a == b);
138 expect(b == a);
139}