authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-24 18:08:10-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-24 18:09:45-07:00
log7cfa97aa4ee9eb49d97d7d3b88488387bf6d175f
tree58ec04014b5b46f15238237acc558ec2c56374ac
parent5c68afef94b0b80823e033bd6965fcda74e19ebe

std.meta.trait: remove assumption about vector ABI size

The unit test for hasUniqueRepresentation asserted that a vector of length 3 would not have a unique representation. This would be true if it were lowered to ABI size 8 instead of 6. However lowering it to ABI size 6 is perfectly valid depending on the target. This commit also simplifies the logic for hasUniqueRepresentation of integers.

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

lib/std/meta/trait.zig+24-26
......@@ -18,7 +18,7 @@ pub fn multiTrait(comptime traits: anytype) TraitFn {
1818 return Closure.trait;
1919}
2020
21test "std.meta.trait.multiTrait" {
21test "multiTrait" {
2222 const Vector2 = struct {
2323 const MyType = @This();
2424
......@@ -54,7 +54,7 @@ pub fn hasFn(comptime name: []const u8) TraitFn {
5454 return Closure.trait;
5555}
5656
57test "std.meta.trait.hasFn" {
57test "hasFn" {
5858 const TestStruct = struct {
5959 pub fn useless() void {}
6060 };
......@@ -84,7 +84,7 @@ pub fn hasField(comptime name: []const u8) TraitFn {
8484 return Closure.trait;
8585}
8686
87test "std.meta.trait.hasField" {
87test "hasField" {
8888 const TestStruct = struct {
8989 value: u32,
9090 };
......@@ -105,7 +105,7 @@ pub fn is(comptime id: std.builtin.TypeId) TraitFn {
105105 return Closure.trait;
106106}
107107
108test "std.meta.trait.is" {
108test "is" {
109109 try testing.expect(is(.Int)(u8));
110110 try testing.expect(!is(.Int)(f32));
111111 try testing.expect(is(.Pointer)(*u8));
......@@ -123,7 +123,7 @@ pub fn isPtrTo(comptime id: std.builtin.TypeId) TraitFn {
123123 return Closure.trait;
124124}
125125
126test "std.meta.trait.isPtrTo" {
126test "isPtrTo" {
127127 try testing.expect(!isPtrTo(.Struct)(struct {}));
128128 try testing.expect(isPtrTo(.Struct)(*struct {}));
129129 try testing.expect(!isPtrTo(.Struct)(**struct {}));
......@@ -139,7 +139,7 @@ pub fn isSliceOf(comptime id: std.builtin.TypeId) TraitFn {
139139 return Closure.trait;
140140}
141141
142test "std.meta.trait.isSliceOf" {
142test "isSliceOf" {
143143 try testing.expect(!isSliceOf(.Struct)(struct {}));
144144 try testing.expect(isSliceOf(.Struct)([]struct {}));
145145 try testing.expect(!isSliceOf(.Struct)([][]struct {}));
......@@ -159,7 +159,7 @@ pub fn isExtern(comptime T: type) bool {
159159 };
160160}
161161
162test "std.meta.trait.isExtern" {
162test "isExtern" {
163163 const TestExStruct = extern struct {};
164164 const TestStruct = struct {};
165165
......@@ -177,7 +177,7 @@ pub fn isPacked(comptime T: type) bool {
177177 };
178178}
179179
180test "std.meta.trait.isPacked" {
180test "isPacked" {
181181 const TestPStruct = packed struct {};
182182 const TestStruct = struct {};
183183
......@@ -222,7 +222,7 @@ pub fn isSingleItemPtr(comptime T: type) bool {
222222 return false;
223223}
224224
225test "std.meta.trait.isSingleItemPtr" {
225test "isSingleItemPtr" {
226226 const array = [_]u8{0} ** 10;
227227 comptime try testing.expect(isSingleItemPtr(@TypeOf(&array[0])));
228228 comptime try testing.expect(!isSingleItemPtr(@TypeOf(array)));
......@@ -237,7 +237,7 @@ pub fn isManyItemPtr(comptime T: type) bool {
237237 return false;
238238}
239239
240test "std.meta.trait.isManyItemPtr" {
240test "isManyItemPtr" {
241241 const array = [_]u8{0} ** 10;
242242 const mip = @ptrCast([*]const u8, &array[0]);
243243 try testing.expect(isManyItemPtr(@TypeOf(mip)));
......@@ -252,7 +252,7 @@ pub fn isSlice(comptime T: type) bool {
252252 return false;
253253}
254254
255test "std.meta.trait.isSlice" {
255test "isSlice" {
256256 const array = [_]u8{0} ** 10;
257257 var runtime_zero: usize = 0;
258258 try testing.expect(isSlice(@TypeOf(array[runtime_zero..])));
......@@ -270,7 +270,7 @@ pub fn isIndexable(comptime T: type) bool {
270270 return comptime is(.Array)(T) or is(.Vector)(T) or isTuple(T);
271271}
272272
273test "std.meta.trait.isIndexable" {
273test "isIndexable" {
274274 const array = [_]u8{0} ** 10;
275275 const slice = @as([]const u8, &array);
276276 const vector: meta.Vector(2, u32) = [_]u32{0} ** 2;
......@@ -291,7 +291,7 @@ pub fn isNumber(comptime T: type) bool {
291291 };
292292}
293293
294test "std.meta.trait.isNumber" {
294test "isNumber" {
295295 const NotANumber = struct {
296296 number: u8,
297297 };
......@@ -342,7 +342,7 @@ pub fn isConstPtr(comptime T: type) bool {
342342 return @typeInfo(T).Pointer.is_const;
343343}
344344
345test "std.meta.trait.isConstPtr" {
345test "isConstPtr" {
346346 var t = @as(u8, 0);
347347 const c = @as(u8, 0);
348348 try testing.expect(isConstPtr(*const @TypeOf(t)));
......@@ -358,7 +358,7 @@ pub fn isContainer(comptime T: type) bool {
358358 };
359359}
360360
361test "std.meta.trait.isContainer" {
361test "isContainer" {
362362 const TestStruct = struct {};
363363 const TestUnion = union {
364364 a: void,
......@@ -380,7 +380,7 @@ pub fn isTuple(comptime T: type) bool {
380380 return is(.Struct)(T) and @typeInfo(T).Struct.is_tuple;
381381}
382382
383test "std.meta.trait.isTuple" {
383test "isTuple" {
384384 const t1 = struct {};
385385 const t2 = .{ .a = 0 };
386386 const t3 = .{ 1, 2, 3 };
......@@ -429,7 +429,7 @@ pub fn isZigString(comptime T: type) bool {
429429 }
430430}
431431
432test "std.meta.trait.isZigString" {
432test "isZigString" {
433433 try testing.expect(isZigString([]const u8));
434434 try testing.expect(isZigString([]u8));
435435 try testing.expect(isZigString([:0]const u8));
......@@ -475,7 +475,7 @@ pub fn hasDecls(comptime T: type, comptime names: anytype) bool {
475475 return true;
476476}
477477
478test "std.meta.trait.hasDecls" {
478test "hasDecls" {
479479 const TestStruct1 = struct {};
480480 const TestStruct2 = struct {
481481 pub var a: u32 = undefined;
......@@ -501,7 +501,7 @@ pub fn hasFields(comptime T: type, comptime names: anytype) bool {
501501 return true;
502502}
503503
504test "std.meta.trait.hasFields" {
504test "hasFields" {
505505 const TestStruct1 = struct {};
506506 const TestStruct2 = struct {
507507 a: u32,
......@@ -527,7 +527,7 @@ pub fn hasFunctions(comptime T: type, comptime names: anytype) bool {
527527 return true;
528528}
529529
530test "std.meta.trait.hasFunctions" {
530test "hasFunctions" {
531531 const TestStruct1 = struct {};
532532 const TestStruct2 = struct {
533533 pub fn a() void {}
......@@ -557,9 +557,7 @@ pub fn hasUniqueRepresentation(comptime T: type) bool {
557557
558558 .Bool => return false,
559559
560 // The padding bits are undefined.
561 .Int => |info| return (info.bits % 8) == 0 and
562 (info.bits == 0 or std.math.isPowerOfTwo(info.bits)),
560 .Int => |info| return @sizeOf(T) * 8 == info.bits,
563561
564562 .Pointer => |info| return info.size != .Slice,
565563
......@@ -577,11 +575,12 @@ pub fn hasUniqueRepresentation(comptime T: type) bool {
577575 return @sizeOf(T) == sum_size;
578576 },
579577
580 .Vector => |info| return comptime hasUniqueRepresentation(info.child) and @sizeOf(T) == @sizeOf(info.child) * info.len,
578 .Vector => |info| return comptime hasUniqueRepresentation(info.child) and
579 @sizeOf(T) == @sizeOf(info.child) * info.len,
581580 }
582581}
583582
584test "std.meta.trait.hasUniqueRepresentation" {
583test "hasUniqueRepresentation" {
585584 const TestStruct1 = struct {
586585 a: u32,
587586 b: u32,
......@@ -650,5 +649,4 @@ test "std.meta.trait.hasUniqueRepresentation" {
650649 try testing.expect(!hasUniqueRepresentation([]const u8));
651650
652651 try testing.expect(hasUniqueRepresentation(@Vector(4, u16)));
653 try testing.expect(!hasUniqueRepresentation(@Vector(3, u16)));
654652}