| ... | @@ -249,26 +249,38 @@ fn testUnion() !void { | ... | @@ -249,26 +249,38 @@ fn testUnion() !void { |
| 249 | } | 249 | } |
| 250 | | 250 | |
| 251 | test "type info: struct info" { | 251 | test "type info: struct info" { |
| 252 | if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO | | |
| 253 | | | |
| 254 | try testStruct(); | 252 | try testStruct(); |
| 255 | comptime try testStruct(); | 253 | comptime try testStruct(); |
| 256 | } | 254 | } |
| 257 | | 255 | |
| 258 | fn testStruct() !void { | 256 | fn testStruct() !void { |
| 259 | const unpacked_struct_info = @typeInfo(TestUnpackedStruct); | 257 | const unpacked_struct_info = @typeInfo(TestStruct); |
| 260 | try expect(unpacked_struct_info.Struct.is_tuple == false); | 258 | try expect(unpacked_struct_info.Struct.is_tuple == false); |
| 261 | try expect(unpacked_struct_info.Struct.fields[0].alignment == @alignOf(u32)); | 259 | try expect(unpacked_struct_info.Struct.fields[0].alignment == @alignOf(u32)); |
| 262 | try expect(@ptrCast(*const u32, unpacked_struct_info.Struct.fields[0].default_value.?).* == 4); | 260 | try expect(@ptrCast(*const u32, unpacked_struct_info.Struct.fields[0].default_value.?).* == 4); |
| 263 | try expectEqualStrings("foobar", @ptrCast(*const *const [6:0]u8, unpacked_struct_info.Struct.fields[1].default_value.?).*); | 261 | try expect(mem.eql(u8, "foobar", @ptrCast(*const *const [6:0]u8, unpacked_struct_info.Struct.fields[1].default_value.?).*)); |
| | 262 | } |
| | 263 | |
| | 264 | const TestStruct = struct { |
| | 265 | fieldA: u32 = 4, |
| | 266 | fieldB: *const [6:0]u8 = "foobar", |
| | 267 | }; |
| 264 | | 268 | |
| 265 | const struct_info = @typeInfo(TestStruct); | 269 | test "type info: packed struct info" { |
| | 270 | if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO |
| | 271 | |
| | 272 | try testPackedStruct(); |
| | 273 | comptime try testPackedStruct(); |
| | 274 | } |
| | 275 | |
| | 276 | fn testPackedStruct() !void { |
| | 277 | const struct_info = @typeInfo(TestPackedStruct); |
| 266 | try expect(struct_info == .Struct); | 278 | try expect(struct_info == .Struct); |
| 267 | try expect(struct_info.Struct.is_tuple == false); | 279 | try expect(struct_info.Struct.is_tuple == false); |
| 268 | try expect(struct_info.Struct.layout == .Packed); | 280 | try expect(struct_info.Struct.layout == .Packed); |
| 269 | try expect(struct_info.Struct.fields.len == 4); | 281 | try expect(struct_info.Struct.fields.len == 4); |
| 270 | try expect(struct_info.Struct.fields[0].alignment == 2 * @alignOf(usize)); | 282 | try expect(struct_info.Struct.fields[0].alignment == 2 * @alignOf(usize)); |
| 271 | try expect(struct_info.Struct.fields[2].field_type == *TestStruct); | 283 | try expect(struct_info.Struct.fields[2].field_type == *TestPackedStruct); |
| 272 | try expect(struct_info.Struct.fields[2].default_value == null); | 284 | try expect(struct_info.Struct.fields[2].default_value == null); |
| 273 | try expect(@ptrCast(*const u32, struct_info.Struct.fields[3].default_value.?).* == 4); | 285 | try expect(@ptrCast(*const u32, struct_info.Struct.fields[3].default_value.?).* == 4); |
| 274 | try expect(struct_info.Struct.fields[3].alignment == 1); | 286 | try expect(struct_info.Struct.fields[3].alignment == 1); |
| ... | @@ -276,12 +288,7 @@ fn testStruct() !void { | ... | @@ -276,12 +288,7 @@ fn testStruct() !void { |
| 276 | try expect(struct_info.Struct.decls[0].is_pub); | 288 | try expect(struct_info.Struct.decls[0].is_pub); |
| 277 | } | 289 | } |
| 278 | | 290 | |
| 279 | const TestUnpackedStruct = struct { | 291 | const TestPackedStruct = packed struct { |
| 280 | fieldA: u32 = 4, | | |
| 281 | fieldB: *const [6:0]u8 = "foobar", | | |
| 282 | }; | | |
| 283 | | | |
| 284 | const TestStruct = packed struct { | | |
| 285 | fieldA: usize align(2 * @alignOf(usize)), | 292 | fieldA: usize align(2 * @alignOf(usize)), |
| 286 | fieldB: void, | 293 | fieldB: void, |
| 287 | fieldC: *Self, | 294 | fieldC: *Self, |
| ... | @@ -329,18 +336,16 @@ fn testFunction() !void { | ... | @@ -329,18 +336,16 @@ fn testFunction() !void { |
| 329 | const fn_aligned_info = @typeInfo(@TypeOf(fooAligned)); | 336 | const fn_aligned_info = @typeInfo(@TypeOf(fooAligned)); |
| 330 | try expect(fn_aligned_info.Fn.alignment == 4); | 337 | try expect(fn_aligned_info.Fn.alignment == 4); |
| 331 | | 338 | |
| 332 | const test_instance: TestStruct = undefined; | 339 | const test_instance: TestPackedStruct = undefined; |
| 333 | const bound_fn_info = @typeInfo(@TypeOf(test_instance.foo)); | 340 | const bound_fn_info = @typeInfo(@TypeOf(test_instance.foo)); |
| 334 | try expect(bound_fn_info == .BoundFn); | 341 | try expect(bound_fn_info == .BoundFn); |
| 335 | try expect(bound_fn_info.BoundFn.args[0].arg_type.? == *const TestStruct); | 342 | try expect(bound_fn_info.BoundFn.args[0].arg_type.? == *const TestPackedStruct); |
| 336 | } | 343 | } |
| 337 | | 344 | |
| 338 | extern fn foo(a: usize, b: bool, ...) callconv(.C) usize; | 345 | extern fn foo(a: usize, b: bool, ...) callconv(.C) usize; |
| 339 | extern fn fooAligned(a: usize, b: bool, ...) align(4) callconv(.C) usize; | 346 | extern fn fooAligned(a: usize, b: bool, ...) align(4) callconv(.C) usize; |
| 340 | | 347 | |
| 341 | test "typeInfo with comptime parameter in struct fn def" { | 348 | test "typeInfo with comptime parameter in struct fn def" { |
| 342 | if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO | | |
| 343 | | | |
| 344 | const S = struct { | 349 | const S = struct { |
| 345 | pub fn func(comptime x: f32) void { | 350 | pub fn func(comptime x: f32) void { |
| 346 | _ = x; | 351 | _ = x; |
| ... | @@ -408,8 +413,6 @@ test "sentinel of opaque pointer type" { | ... | @@ -408,8 +413,6 @@ test "sentinel of opaque pointer type" { |
| 408 | } | 413 | } |
| 409 | | 414 | |
| 410 | test "@typeInfo does not force declarations into existence" { | 415 | test "@typeInfo does not force declarations into existence" { |
| 411 | if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO | | |
| 412 | | | |
| 413 | const S = struct { | 416 | const S = struct { |
| 414 | x: i32, | 417 | x: i32, |
| 415 | | 418 | |
| ... | @@ -436,8 +439,6 @@ test "type info for async frames" { | ... | @@ -436,8 +439,6 @@ test "type info for async frames" { |
| 436 | } | 439 | } |
| 437 | | 440 | |
| 438 | test "Declarations are returned in declaration order" { | 441 | test "Declarations are returned in declaration order" { |
| 439 | if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO | | |
| 440 | | | |
| 441 | const S = struct { | 442 | const S = struct { |
| 442 | const a = 1; | 443 | const a = 1; |
| 443 | const b = 2; | 444 | const b = 2; |
| ... | @@ -461,16 +462,12 @@ test "Struct.is_tuple" { | ... | @@ -461,16 +462,12 @@ test "Struct.is_tuple" { |
| 461 | } | 462 | } |
| 462 | | 463 | |
| 463 | test "StructField.is_comptime" { | 464 | test "StructField.is_comptime" { |
| 464 | if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO | | |
| 465 | | | |
| 466 | const info = @typeInfo(struct { x: u8 = 3, comptime y: u32 = 5 }).Struct; | 465 | const info = @typeInfo(struct { x: u8 = 3, comptime y: u32 = 5 }).Struct; |
| 467 | try expect(!info.fields[0].is_comptime); | 466 | try expect(!info.fields[0].is_comptime); |
| 468 | try expect(info.fields[1].is_comptime); | 467 | try expect(info.fields[1].is_comptime); |
| 469 | } | 468 | } |
| 470 | | 469 | |
| 471 | test "typeInfo resolves usingnamespace declarations" { | 470 | test "typeInfo resolves usingnamespace declarations" { |
| 472 | if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO | | |
| 473 | | | |
| 474 | const A = struct { | 471 | const A = struct { |
| 475 | pub const f1 = 42; | 472 | pub const f1 = 42; |
| 476 | }; | 473 | }; |