| ... | @@ -900,6 +900,15 @@ pub inline fn hasUniqueRepresentation(comptime T: type) bool { | ... | @@ -900,6 +900,15 @@ pub inline fn hasUniqueRepresentation(comptime T: type) bool { |
| 900 | return @sizeOf(T) == sum_size; | 900 | return @sizeOf(T) == sum_size; |
| 901 | }, | 901 | }, |
| 902 | | 902 | |
| | 903 | .@"union" => |info| { |
| | 904 | if (info.layout == .@"packed") return @sizeOf(T) * 8 == @bitSizeOf(T); |
| | 905 | inline for (info.field_types) |field_type| { |
| | 906 | if (@sizeOf(field_type) != @sizeOf(T)) return false; |
| | 907 | if (!hasUniqueRepresentation(field_type)) return false; |
| | 908 | } |
| | 909 | return true; |
| | 910 | }, |
| | 911 | |
| 903 | .vector => |info| hasUniqueRepresentation(info.child) and | 912 | .vector => |info| hasUniqueRepresentation(info.child) and |
| 904 | @sizeOf(T) == @sizeOf(info.child) * info.len, | 913 | @sizeOf(T) == @sizeOf(info.child) * info.len, |
| 905 | }; | 914 | }; |
| ... | @@ -969,6 +978,27 @@ test hasUniqueRepresentation { | ... | @@ -969,6 +978,27 @@ test hasUniqueRepresentation { |
| 969 | | 978 | |
| 970 | try testing.expect(!hasUniqueRepresentation(TestUnion4)); | 979 | try testing.expect(!hasUniqueRepresentation(TestUnion4)); |
| 971 | | 980 | |
| | 981 | const TestUnion5 = extern union { |
| | 982 | a: u32, |
| | 983 | b: i32, |
| | 984 | }; |
| | 985 | |
| | 986 | try testing.expect(hasUniqueRepresentation(TestUnion5)); |
| | 987 | |
| | 988 | const TestUnion6 = packed union(u7) { |
| | 989 | a: u7, |
| | 990 | b: i7, |
| | 991 | }; |
| | 992 | |
| | 993 | try testing.expect(!hasUniqueRepresentation(TestUnion6)); |
| | 994 | |
| | 995 | const TestUnion7 = packed union(u8) { |
| | 996 | a: u8, |
| | 997 | b: i8, |
| | 998 | }; |
| | 999 | |
| | 1000 | try testing.expect(hasUniqueRepresentation(TestUnion7)); |
| | 1001 | |
| 972 | inline for ([_]type{ u8, i16, u32, i64 }) |T| { | 1002 | inline for ([_]type{ u8, i16, u32, i64 }) |T| { |
| 973 | try testing.expect(hasUniqueRepresentation(T)); | 1003 | try testing.expect(hasUniqueRepresentation(T)); |
| 974 | try testing.expect(hasUniqueRepresentation(enum(T) { _ })); | 1004 | try testing.expect(hasUniqueRepresentation(enum(T) { _ })); |