authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-02-11 15:48:35+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-02-11 17:03:11+01:00
log6c05f0949aa09ac41268097161bacf8dc047e195
tree53dbd5a50a589cd0c93db62e19a4a36f619f3401
parentb81c5be451b18597b38f606dc9a9f3255401190f

ir: Fix erroneous error message for ptr casts

Don't blindly throw an error if two integer types are checked for compatibility. Bug reported in #4430

2 files changed, 22 insertions(+), 4 deletions(-)

src/ir.cpp+9-4
...@@ -11873,10 +11873,15 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -11873,10 +11873,15 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
11873 }11873 }
1187411874
11875 if (wanted_type->id == ZigTypeIdInt && actual_type->id == ZigTypeIdInt) {11875 if (wanted_type->id == ZigTypeIdInt && actual_type->id == ZigTypeIdInt) {
11876 result.id = ConstCastResultIdIntShorten;11876 if (wanted_type->data.integral.is_signed != actual_type->data.integral.is_signed ||
11877 result.data.int_shorten = heap::c_allocator.allocate_nonzero<ConstCastIntShorten>(1);11877 wanted_type->data.integral.bit_count != actual_type->data.integral.bit_count)
11878 result.data.int_shorten->wanted_type = wanted_type;11878 {
11879 result.data.int_shorten->actual_type = actual_type;11879 result.id = ConstCastResultIdIntShorten;
11880 result.data.int_shorten = heap::c_allocator.allocate_nonzero<ConstCastIntShorten>(1);
11881 result.data.int_shorten->wanted_type = wanted_type;
11882 result.data.int_shorten->actual_type = actual_type;
11883 return result;
11884 }
11880 return result;11885 return result;
11881 }11886 }
1188211887
test/stage1/behavior/cast.zig+13
...@@ -782,3 +782,16 @@ test "cast between [*c]T and ?[*:0]T on fn parameter" {...@@ -782,3 +782,16 @@ test "cast between [*c]T and ?[*:0]T on fn parameter" {
782 };782 };
783 S.doTheTest();783 S.doTheTest();
784}784}
785
786test "cast between C pointer with different but compatible types" {
787 const S = struct {
788 fn foo(arg: [*]c_ushort) u16 {
789 return arg[0];
790 }
791 fn doTheTest() void {
792 var x = [_]u16{ 4, 2, 1, 3 };
793 expect(foo(@ptrCast([*]u16, &x)) == 4);
794 }
795 };
796 S.doTheTest();
797}