authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-09-14 21:20:41+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-09-14 23:21:26+03:00
log29fd0c6d61d1569c1691fc5489d51311207b1759
tree8af5c37da1cec24b70f932d4a023d81a3ed56a50
parentd073836894df8b9fb56f24f577a51dd09a85a932
signaturelock-open Commit is signed but in an unrecognized format.

fix meta.cast behavior; add exhaustive tests


1 files changed, 24 insertions(+), 12 deletions(-)

lib/std/meta.zig+24-12
......@@ -717,7 +717,7 @@ pub fn cast(comptime DestType: type, target: anytype) DestType {
717717 },
718718 .Optional => |opt| {
719719 if (@typeInfo(opt.child) == .Pointer) {
720 return @ptrCast(DestType, @alignCast(dest_ptr, target));
720 return @ptrCast(DestType, @alignCast(dest_ptr.alignment, target));
721721 }
722722 },
723723 else => {},
......@@ -725,23 +725,24 @@ pub fn cast(comptime DestType: type, target: anytype) DestType {
725725 },
726726 .Optional => |dest_opt| {
727727 if (@typeInfo(dest_opt.child) == .Pointer) {
728 const dest_ptr = @typeInfo(dest_opt.child).Pointer;
728729 switch (@typeInfo(TargetType)) {
729730 .Int, .ComptimeInt => {
730731 return @intToPtr(DestType, target);
731732 },
732733 .Pointer => {
733 return @ptrCast(DestType, @alignCast(@alignOf(dest_opt.child.Child), target));
734 return @ptrCast(DestType, @alignCast(dest_ptr.alignment, target));
734735 },
735736 .Optional => |target_opt| {
736737 if (@typeInfo(target_opt.child) == .Pointer) {
737 return @ptrCast(DestType, @alignCast(@alignOf(dest_opt.child.Child), target));
738 return @ptrCast(DestType, @alignCast(dest_ptr.alignment, target));
738739 }
739740 },
740741 else => {},
741742 }
742743 }
743744 },
744 .Enum, .EnumLiteral => {
745 .Enum => {
745746 if (@typeInfo(TargetType) == .Int or @typeInfo(TargetType) == .ComptimeInt) {
746747 return @intToEnum(DestType, target);
747748 }
......@@ -749,15 +750,18 @@ pub fn cast(comptime DestType: type, target: anytype) DestType {
749750 .Int, .ComptimeInt => {
750751 switch (@typeInfo(TargetType)) {
751752 .Pointer => {
752 return @as(DestType, @ptrToInt(target));
753 return @intCast(DestType, @ptrToInt(target));
753754 },
754755 .Optional => |opt| {
755756 if (@typeInfo(opt.child) == .Pointer) {
756 return @as(DestType, @ptrToInt(target));
757 return @intCast(DestType, @ptrToInt(target));
757758 }
758759 },
759 .Enum, .EnumLiteral => {
760 return @as(DestType, @enumToInt(target));
760 .Enum => {
761 return @intCast(DestType, @enumToInt(target));
762 },
763 .Int, .ComptimeInt => {
764 return @intCast(DestType, target);
761765 },
762766 else => {},
763767 }
......@@ -776,10 +780,18 @@ test "std.meta.cast" {
776780
777781 var i = @as(i64, 10);
778782
779 testing.expect(cast(?*c_void, 0) == @intToPtr(?*c_void, 0));
780783 testing.expect(cast(*u8, 16) == @intToPtr(*u8, 16));
781 testing.expect(cast(u64, @as(u32, 10)) == @as(u64, 10));
782 testing.expect(cast(E, 1) == .One);
783 testing.expect(cast(u8, E.Two) == 2);
784784 testing.expect(cast(*u64, &i).* == @as(u64, 10));
785 testing.expect(cast(*i64, @as(?*align(1) i64, &i)) == &i);
786
787 testing.expect(cast(?*u8, 2) == @intToPtr(*u8, 2));
788 testing.expect(cast(?*i64, @as(*align(1) i64, &i)) == &i);
789 testing.expect(cast(?*i64, @as(?*align(1) i64, &i)) == &i);
790
791 testing.expect(cast(E, 1) == .One);
792
793 testing.expectEqual(@as(u32, 4), cast(u32, @intToPtr(*u32, 4)));
794 testing.expectEqual(@as(u32, 4), cast(u32, @intToPtr(?*u32, 4)));
795 testing.expectEqual(@as(u32, 10), cast(u32, @as(u64, 10)));
796 testing.expectEqual(@as(u8, 2), cast(u8, E.Two));
785797}