diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-02-17 20:47:51 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2022-02-17 20:48:09 -0700 |
| commit | 9ea253b9c4085b9f577c4d7634da69b2d40d6cce (patch) | |
| tree | 9c3b50e9ff50432b6d7b465e5e43803f4cc3c296 /test | |
| parent | 23567cdd984322c7428d43c8cbf8ecd40a2b7ec6 (diff) | |
| download | zig-9ea253b9c4085b9f577c4d7634da69b2d40d6cce.tar.gz zig-9ea253b9c4085b9f577c4d7634da69b2d40d6cce.zip | |
Sema: implement type info for structs
Diffstat (limited to 'test')
| -rw-r--r-- | test/behavior/type_info.zig | 45 |
1 files changed, 21 insertions, 24 deletions
diff --git a/test/behavior/type_info.zig b/test/behavior/type_info.zig index fc5b268832..1aafd4f82b 100644 --- a/test/behavior/type_info.zig +++ b/test/behavior/type_info.zig @@ -249,26 +249,38 @@ fn testUnion() !void { } test "type info: struct info" { - if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO - try testStruct(); comptime try testStruct(); } fn testStruct() !void { - const unpacked_struct_info = @typeInfo(TestUnpackedStruct); + const unpacked_struct_info = @typeInfo(TestStruct); try expect(unpacked_struct_info.Struct.is_tuple == false); try expect(unpacked_struct_info.Struct.fields[0].alignment == @alignOf(u32)); try expect(@ptrCast(*const u32, unpacked_struct_info.Struct.fields[0].default_value.?).* == 4); - try expectEqualStrings("foobar", @ptrCast(*const *const [6:0]u8, unpacked_struct_info.Struct.fields[1].default_value.?).*); + try expect(mem.eql(u8, "foobar", @ptrCast(*const *const [6:0]u8, unpacked_struct_info.Struct.fields[1].default_value.?).*)); +} + +const TestStruct = struct { + fieldA: u32 = 4, + fieldB: *const [6:0]u8 = "foobar", +}; - const struct_info = @typeInfo(TestStruct); +test "type info: packed struct info" { + if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO + + try testPackedStruct(); + comptime try testPackedStruct(); +} + +fn testPackedStruct() !void { + const struct_info = @typeInfo(TestPackedStruct); try expect(struct_info == .Struct); try expect(struct_info.Struct.is_tuple == false); try expect(struct_info.Struct.layout == .Packed); try expect(struct_info.Struct.fields.len == 4); try expect(struct_info.Struct.fields[0].alignment == 2 * @alignOf(usize)); - try expect(struct_info.Struct.fields[2].field_type == *TestStruct); + try expect(struct_info.Struct.fields[2].field_type == *TestPackedStruct); try expect(struct_info.Struct.fields[2].default_value == null); try expect(@ptrCast(*const u32, struct_info.Struct.fields[3].default_value.?).* == 4); try expect(struct_info.Struct.fields[3].alignment == 1); @@ -276,12 +288,7 @@ fn testStruct() !void { try expect(struct_info.Struct.decls[0].is_pub); } -const TestUnpackedStruct = struct { - fieldA: u32 = 4, - fieldB: *const [6:0]u8 = "foobar", -}; - -const TestStruct = packed struct { +const TestPackedStruct = packed struct { fieldA: usize align(2 * @alignOf(usize)), fieldB: void, fieldC: *Self, @@ -329,18 +336,16 @@ fn testFunction() !void { const fn_aligned_info = @typeInfo(@TypeOf(fooAligned)); try expect(fn_aligned_info.Fn.alignment == 4); - const test_instance: TestStruct = undefined; + const test_instance: TestPackedStruct = undefined; const bound_fn_info = @typeInfo(@TypeOf(test_instance.foo)); try expect(bound_fn_info == .BoundFn); - try expect(bound_fn_info.BoundFn.args[0].arg_type.? == *const TestStruct); + try expect(bound_fn_info.BoundFn.args[0].arg_type.? == *const TestPackedStruct); } extern fn foo(a: usize, b: bool, ...) callconv(.C) usize; extern fn fooAligned(a: usize, b: bool, ...) align(4) callconv(.C) usize; test "typeInfo with comptime parameter in struct fn def" { - if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO - const S = struct { pub fn func(comptime x: f32) void { _ = x; @@ -408,8 +413,6 @@ test "sentinel of opaque pointer type" { } test "@typeInfo does not force declarations into existence" { - if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO - const S = struct { x: i32, @@ -436,8 +439,6 @@ test "type info for async frames" { } test "Declarations are returned in declaration order" { - if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO - const S = struct { const a = 1; const b = 2; @@ -461,16 +462,12 @@ test "Struct.is_tuple" { } test "StructField.is_comptime" { - if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO - const info = @typeInfo(struct { x: u8 = 3, comptime y: u32 = 5 }).Struct; try expect(!info.fields[0].is_comptime); try expect(info.fields[1].is_comptime); } test "typeInfo resolves usingnamespace declarations" { - if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO - const A = struct { pub const f1 = 42; }; |
