authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-03-05 07:37:31+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-03-06 21:26:38+00:00
log20403ee41d67b02faedbee97a94fc8b9fd3781e0
treecb51145f74df43e41b70a8a8c76e9e05251bcb3b
parenta7cac5fc8ed2d49badc6a07ee2e4e77f4ac6e6ae
signaturelock-open Commit is signed but in an unrecognized format.

behavior: add tests for #18816


4 files changed, 97 insertions(+), 0 deletions(-)

test/behavior/enum.zig+21
...@@ -1242,3 +1242,24 @@ test "Non-exhaustive enum backed by comptime_int" {...@@ -1242,3 +1242,24 @@ test "Non-exhaustive enum backed by comptime_int" {
1242 e = @as(E, @enumFromInt(378089457309184723749));1242 e = @as(E, @enumFromInt(378089457309184723749));
1243 try expect(@intFromEnum(e) == 378089457309184723749);1243 try expect(@intFromEnum(e) == 378089457309184723749);
1244}1244}
1245
1246test "matching captures causes enum equivalence" {
1247 const S = struct {
1248 fn Nonexhaustive(comptime I: type) type {
1249 const UTag = @Type(.{ .Int = .{
1250 .signedness = .unsigned,
1251 .bits = @typeInfo(I).Int.bits,
1252 } });
1253 return enum(UTag) { _ };
1254 }
1255 };
1256
1257 comptime assert(S.Nonexhaustive(u8) == S.Nonexhaustive(i8));
1258 comptime assert(S.Nonexhaustive(u16) == S.Nonexhaustive(i16));
1259 comptime assert(S.Nonexhaustive(u8) != S.Nonexhaustive(u16));
1260
1261 const a: S.Nonexhaustive(u8) = @enumFromInt(123);
1262 const b: S.Nonexhaustive(i8) = @enumFromInt(123);
1263 comptime assert(@TypeOf(a) == @TypeOf(b));
1264 try expect(@intFromEnum(a) == @intFromEnum(b));
1265}
test/behavior/struct.zig+23
...@@ -2127,3 +2127,26 @@ test "struct containing optional pointer to array of @This()" {...@@ -2127,3 +2127,26 @@ test "struct containing optional pointer to array of @This()" {
2127 _ = &s;2127 _ = &s;
2128 try expect(s.x.?[0].x == null);2128 try expect(s.x.?[0].x == null);
2129}2129}
2130
2131test "matching captures causes struct equivalence" {
2132 const S = struct {
2133 fn UnsignedWrapper(comptime I: type) type {
2134 const bits = @typeInfo(I).Int.bits;
2135 return struct {
2136 x: @Type(.{ .Int = .{
2137 .signedness = .unsigned,
2138 .bits = bits,
2139 } }),
2140 };
2141 }
2142 };
2143
2144 comptime assert(S.UnsignedWrapper(u8) == S.UnsignedWrapper(i8));
2145 comptime assert(S.UnsignedWrapper(u16) == S.UnsignedWrapper(i16));
2146 comptime assert(S.UnsignedWrapper(u8) != S.UnsignedWrapper(u16));
2147
2148 const a: S.UnsignedWrapper(u8) = .{ .x = 10 };
2149 const b: S.UnsignedWrapper(i8) = .{ .x = 10 };
2150 comptime assert(@TypeOf(a) == @TypeOf(b));
2151 try expect(a.x == b.x);
2152}
test/behavior/type.zig+26
...@@ -2,6 +2,7 @@ const std = @import("std");...@@ -2,6 +2,7 @@ const std = @import("std");
2const builtin = @import("builtin");2const builtin = @import("builtin");
3const Type = std.builtin.Type;3const Type = std.builtin.Type;
4const testing = std.testing;4const testing = std.testing;
5const assert = std.debug.assert;
56
6fn testTypes(comptime types: []const type) !void {7fn testTypes(comptime types: []const type) !void {
7 inline for (types) |testType| {8 inline for (types) |testType| {
...@@ -734,3 +735,28 @@ test "struct field names sliced at comptime from larger string" {...@@ -734,3 +735,28 @@ test "struct field names sliced at comptime from larger string" {
734 try testing.expectEqualStrings("f3", gen_fields[2].name);735 try testing.expectEqualStrings("f3", gen_fields[2].name);
735 }736 }
736}737}
738
739test "matching captures causes opaque equivalence" {
740 const S = struct {
741 fn UnsignedId(comptime I: type) type {
742 const U = @Type(.{ .Int = .{
743 .signedness = .unsigned,
744 .bits = @typeInfo(I).Int.bits,
745 } });
746 return opaque {
747 fn id(x: U) U {
748 return x;
749 }
750 };
751 }
752 };
753
754 comptime assert(S.UnsignedId(u8) == S.UnsignedId(i8));
755 comptime assert(S.UnsignedId(u16) == S.UnsignedId(i16));
756 comptime assert(S.UnsignedId(u8) != S.UnsignedId(u16));
757
758 const a = S.UnsignedId(u8).id(123);
759 const b = S.UnsignedId(i8).id(123);
760 comptime assert(@TypeOf(a) == @TypeOf(b));
761 try testing.expect(a == b);
762}
test/behavior/union.zig+27
...@@ -2273,3 +2273,30 @@ test "create union(enum) from other union(enum)" {...@@ -2273,3 +2273,30 @@ test "create union(enum) from other union(enum)" {
2273 else => {},2273 else => {},
2274 }2274 }
2275}2275}
2276
2277test "matching captures causes union equivalence" {
2278 const S = struct {
2279 fn SignedUnsigned(comptime I: type) type {
2280 const bits = @typeInfo(I).Int.bits;
2281 return union {
2282 u: @Type(.{ .Int = .{
2283 .signedness = .unsigned,
2284 .bits = bits,
2285 } }),
2286 i: @Type(.{ .Int = .{
2287 .signedness = .signed,
2288 .bits = bits,
2289 } }),
2290 };
2291 }
2292 };
2293
2294 comptime assert(S.SignedUnsigned(u8) == S.SignedUnsigned(i8));
2295 comptime assert(S.SignedUnsigned(u16) == S.SignedUnsigned(i16));
2296 comptime assert(S.SignedUnsigned(u8) != S.SignedUnsigned(u16));
2297
2298 const a: S.SignedUnsigned(u8) = .{ .u = 10 };
2299 const b: S.SignedUnsigned(i8) = .{ .u = 10 };
2300 comptime assert(@TypeOf(a) == @TypeOf(b));
2301 try expect(a.u == b.u);
2302}