From 4d50bc3f8dd2b9fd5b7dde6f63f104aaba6dbf5a Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 23 Mar 2019 18:46:20 -0400 Subject: [PATCH] add peer type resolution for `*const T` and `?*T` closes #1298 --- doc/langref.html.in | 7 +++++++ src/ir.cpp | 17 +++++++++++++++++ test/stage1/behavior/pointers.zig | 7 +++++++ 3 files changed, 31 insertions(+) diff --git a/doc/langref.html.in b/doc/langref.html.in index 0de6650c56..a9281b4918 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -4695,6 +4695,13 @@ fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 { return slice[0..1]; } + +test "peer type resolution: *const T and ?*T" { + const a = @intToPtr(*const usize, 0x123456789); + const b = @intToPtr(?*usize, 0x123456789); + assert(a == b); + assert(b == a); +} {#code_end#} {#header_close#} {#header_close#} diff --git a/src/ir.cpp b/src/ir.cpp index f8301f0116..54b567efe5 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -9658,6 +9658,23 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT continue; } + if (prev_type->id == ZigTypeIdOptional && + types_match_const_cast_only(ira, cur_type, prev_type->data.maybe.child_type, + source_node, false).id == ConstCastResultIdOk) + { + prev_inst = cur_inst; + any_are_null = true; + continue; + } + + if (cur_type->id == ZigTypeIdOptional && + types_match_const_cast_only(ira, prev_type, cur_type->data.maybe.child_type, + source_node, false).id == ConstCastResultIdOk) + { + any_are_null = true; + continue; + } + if (cur_type->id == ZigTypeIdUndefined) { continue; } diff --git a/test/stage1/behavior/pointers.zig b/test/stage1/behavior/pointers.zig index 1bb23866b4..5b73cd98df 100644 --- a/test/stage1/behavior/pointers.zig +++ b/test/stage1/behavior/pointers.zig @@ -130,3 +130,10 @@ test "initialize const optional C pointer to null" { expect(a == null); comptime expect(a == null); } + +test "compare equality of optional and non-optional pointer" { + const a = @intToPtr(*const usize, 0x123456789); + const b = @intToPtr(?*usize, 0x123456789); + expect(a == b); + expect(b == a); +}