authorgravatar for timonkruiper@gmail.comTimon Kruiper <timonkruiper@gmail.com> 2020-02-10 23:42:07+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-10 19:43:58-05:00
log26183660558c43133d862912c602e316f43698c7
tree7f3b8aea43d6f68f42592d7a9b7c612a316e7a03
parent3237528a592f2dc22659f77f5fbfb061dc14566a

Add cast between [*c]T and ?[*:0]T on fn parameter

Fixes #4176

2 files changed, 16 insertions(+), 1 deletions(-)

src/ir.cpp+2-1
...@@ -11592,7 +11592,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -11592,7 +11592,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
11592 wanted_ptr_type->data.pointer.sentinel == nullptr ||11592 wanted_ptr_type->data.pointer.sentinel == nullptr ||
11593 (actual_ptr_type->data.pointer.sentinel != nullptr &&11593 (actual_ptr_type->data.pointer.sentinel != nullptr &&
11594 const_values_equal(ira->codegen, wanted_ptr_type->data.pointer.sentinel,11594 const_values_equal(ira->codegen, wanted_ptr_type->data.pointer.sentinel,
11595 actual_ptr_type->data.pointer.sentinel));11595 actual_ptr_type->data.pointer.sentinel)) ||
11596 actual_ptr_type->data.pointer.ptr_len == PtrLenC;
11596 if (!ok_null_term_ptrs) {11597 if (!ok_null_term_ptrs) {
11597 result.id = ConstCastResultIdPtrSentinel;11598 result.id = ConstCastResultIdPtrSentinel;
11598 result.data.bad_ptr_sentinel = allocate_nonzero<ConstCastPtrSentinel>(1);11599 result.data.bad_ptr_sentinel = allocate_nonzero<ConstCastPtrSentinel>(1);
test/stage1/behavior/cast.zig+14
...@@ -768,3 +768,17 @@ test "variable initialization uses result locations properly with regards to the...@@ -768,3 +768,17 @@ test "variable initialization uses result locations properly with regards to the
768 const x: i32 = if (b) 1 else 2;768 const x: i32 = if (b) 1 else 2;
769 expect(x == 1);769 expect(x == 1);
770}770}
771
772test "cast between [*c]T and ?[*:0]T on fn parameter" {
773 const S = struct {
774 const Handler = ?extern fn ([*c]const u8) void;
775 fn addCallback(handler: Handler) void {}
776
777 fn myCallback(cstr: ?[*:0]const u8) callconv(.C) void {}
778
779 fn doTheTest() void {
780 addCallback(myCallback);
781 }
782 };
783 S.doTheTest();
784}