Skip to content

Commit

Permalink
behavior: add tests for ziglang#18816
Browse files Browse the repository at this point in the history
  • Loading branch information
mlugg committed Mar 6, 2024
1 parent a7cac5f commit 20403ee
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
21 changes: 21 additions & 0 deletions test/behavior/enum.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1242,3 +1242,24 @@ test "Non-exhaustive enum backed by comptime_int" {
e = @as(E, @enumFromInt(378089457309184723749));
try expect(@intFromEnum(e) == 378089457309184723749);
}

test "matching captures causes enum equivalence" {
const S = struct {
fn Nonexhaustive(comptime I: type) type {
const UTag = @Type(.{ .Int = .{
.signedness = .unsigned,
.bits = @typeInfo(I).Int.bits,
} });
return enum(UTag) { _ };
}
};

comptime assert(S.Nonexhaustive(u8) == S.Nonexhaustive(i8));
comptime assert(S.Nonexhaustive(u16) == S.Nonexhaustive(i16));
comptime assert(S.Nonexhaustive(u8) != S.Nonexhaustive(u16));

const a: S.Nonexhaustive(u8) = @enumFromInt(123);
const b: S.Nonexhaustive(i8) = @enumFromInt(123);
comptime assert(@TypeOf(a) == @TypeOf(b));
try expect(@intFromEnum(a) == @intFromEnum(b));
}
23 changes: 23 additions & 0 deletions test/behavior/struct.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2127,3 +2127,26 @@ test "struct containing optional pointer to array of @This()" {
_ = &s;
try expect(s.x.?[0].x == null);
}

test "matching captures causes struct equivalence" {
const S = struct {
fn UnsignedWrapper(comptime I: type) type {
const bits = @typeInfo(I).Int.bits;
return struct {
x: @Type(.{ .Int = .{
.signedness = .unsigned,
.bits = bits,
} }),
};
}
};

comptime assert(S.UnsignedWrapper(u8) == S.UnsignedWrapper(i8));
comptime assert(S.UnsignedWrapper(u16) == S.UnsignedWrapper(i16));
comptime assert(S.UnsignedWrapper(u8) != S.UnsignedWrapper(u16));

const a: S.UnsignedWrapper(u8) = .{ .x = 10 };
const b: S.UnsignedWrapper(i8) = .{ .x = 10 };
comptime assert(@TypeOf(a) == @TypeOf(b));
try expect(a.x == b.x);
}
26 changes: 26 additions & 0 deletions test/behavior/type.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const std = @import("std");
const builtin = @import("builtin");
const Type = std.builtin.Type;
const testing = std.testing;
const assert = std.debug.assert;

fn testTypes(comptime types: []const type) !void {
inline for (types) |testType| {
Expand Down Expand Up @@ -734,3 +735,28 @@ test "struct field names sliced at comptime from larger string" {
try testing.expectEqualStrings("f3", gen_fields[2].name);
}
}

test "matching captures causes opaque equivalence" {
const S = struct {
fn UnsignedId(comptime I: type) type {
const U = @Type(.{ .Int = .{
.signedness = .unsigned,
.bits = @typeInfo(I).Int.bits,
} });
return opaque {
fn id(x: U) U {
return x;
}
};
}
};

comptime assert(S.UnsignedId(u8) == S.UnsignedId(i8));
comptime assert(S.UnsignedId(u16) == S.UnsignedId(i16));
comptime assert(S.UnsignedId(u8) != S.UnsignedId(u16));

const a = S.UnsignedId(u8).id(123);
const b = S.UnsignedId(i8).id(123);
comptime assert(@TypeOf(a) == @TypeOf(b));
try testing.expect(a == b);
}
27 changes: 27 additions & 0 deletions test/behavior/union.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2273,3 +2273,30 @@ test "create union(enum) from other union(enum)" {
else => {},
}
}

test "matching captures causes union equivalence" {
const S = struct {
fn SignedUnsigned(comptime I: type) type {
const bits = @typeInfo(I).Int.bits;
return union {
u: @Type(.{ .Int = .{
.signedness = .unsigned,
.bits = bits,
} }),
i: @Type(.{ .Int = .{
.signedness = .signed,
.bits = bits,
} }),
};
}
};

comptime assert(S.SignedUnsigned(u8) == S.SignedUnsigned(i8));
comptime assert(S.SignedUnsigned(u16) == S.SignedUnsigned(i16));
comptime assert(S.SignedUnsigned(u8) != S.SignedUnsigned(u16));

const a: S.SignedUnsigned(u8) = .{ .u = 10 };
const b: S.SignedUnsigned(i8) = .{ .u = 10 };
comptime assert(@TypeOf(a) == @TypeOf(b));
try expect(a.u == b.u);
}

0 comments on commit 20403ee

Please sign in to comment.