diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-01-15 00:17:25 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2022-01-15 00:17:25 -0700 |
| commit | a5c7742ba6fc793608b8bb7ba058e33eccd9cfec (patch) | |
| tree | 29bf43a0bb915e35244d457514885baca2a5916b /test | |
| parent | 41f3799bf0cfc8241f458094781ba45967e2576e (diff) | |
| download | zig-a5c7742ba6fc793608b8bb7ba058e33eccd9cfec.tar.gz zig-a5c7742ba6fc793608b8bb7ba058e33eccd9cfec.zip | |
stage2: fix Decl garbage collection not marking enough
It is the job of codegen backends to mark Decls that are referenced as
alive so that the frontend does not sweep them with the garbage. This
commit unifies the code between the backends with an added method on
Decl.
The implementation is more complete than before, switching on the Decl
val tag and recursing into sub-values.
As a result, two more array tests are passing.
Diffstat (limited to 'test')
| -rw-r--r-- | test/behavior/array_llvm.zig | 79 | ||||
| -rw-r--r-- | test/behavior/array_stage1.zig | 90 |
2 files changed, 85 insertions, 84 deletions
diff --git a/test/behavior/array_llvm.zig b/test/behavior/array_llvm.zig index a828954b73..f1a57d9c29 100644 --- a/test/behavior/array_llvm.zig +++ b/test/behavior/array_llvm.zig @@ -1,6 +1,7 @@ const std = @import("std"); const testing = std.testing; const expect = testing.expect; +const mem = std.mem; var s_array: [8]Sub = undefined; const Sub = struct { b: u8 }; @@ -87,3 +88,81 @@ test "array literal as argument to function" { try S.entry(2); comptime try S.entry(2); } + +test "double nested array to const slice cast in array literal" { + const S = struct { + fn entry(two: i32) !void { + const cases = [_][]const []const i32{ + &[_][]const i32{&[_]i32{1}}, + &[_][]const i32{&[_]i32{ 2, 3 }}, + &[_][]const i32{ + &[_]i32{4}, + &[_]i32{ 5, 6, 7 }, + }, + }; + try check(&cases); + + const cases2 = [_][]const i32{ + &[_]i32{1}, + &[_]i32{ two, 3 }, + }; + try expect(cases2.len == 2); + try expect(cases2[0].len == 1); + try expect(cases2[0][0] == 1); + try expect(cases2[1].len == 2); + try expect(cases2[1][0] == 2); + try expect(cases2[1][1] == 3); + + const cases3 = [_][]const []const i32{ + &[_][]const i32{&[_]i32{1}}, + &[_][]const i32{&[_]i32{ two, 3 }}, + &[_][]const i32{ + &[_]i32{4}, + &[_]i32{ 5, 6, 7 }, + }, + }; + try check(&cases3); + } + + fn check(cases: []const []const []const i32) !void { + try expect(cases.len == 3); + try expect(cases[0].len == 1); + try expect(cases[0][0].len == 1); + try expect(cases[0][0][0] == 1); + try expect(cases[1].len == 1); + try expect(cases[1][0].len == 2); + try expect(cases[1][0][0] == 2); + try expect(cases[1][0][1] == 3); + try expect(cases[2].len == 2); + try expect(cases[2][0].len == 1); + try expect(cases[2][0][0] == 4); + try expect(cases[2][1].len == 3); + try expect(cases[2][1][0] == 5); + try expect(cases[2][1][1] == 6); + try expect(cases[2][1][2] == 7); + } + }; + try S.entry(2); + comptime try S.entry(2); +} + +test "anonymous literal in array" { + const S = struct { + const Foo = struct { + a: usize = 2, + b: usize = 4, + }; + fn doTheTest() !void { + var array: [2]Foo = .{ + .{ .a = 3 }, + .{ .b = 3 }, + }; + try expect(array[0].a == 3); + try expect(array[0].b == 4); + try expect(array[1].a == 2); + try expect(array[1].b == 3); + } + }; + try S.doTheTest(); + comptime try S.doTheTest(); +} diff --git a/test/behavior/array_stage1.zig b/test/behavior/array_stage1.zig index daef20ffb5..5c37f34de6 100644 --- a/test/behavior/array_stage1.zig +++ b/test/behavior/array_stage1.zig @@ -4,84 +4,6 @@ const mem = std.mem; const expect = testing.expect; const expectEqual = testing.expectEqual; -test "double nested array to const slice cast in array literal" { - const S = struct { - fn entry(two: i32) !void { - const cases = [_][]const []const i32{ - &[_][]const i32{&[_]i32{1}}, - &[_][]const i32{&[_]i32{ 2, 3 }}, - &[_][]const i32{ - &[_]i32{4}, - &[_]i32{ 5, 6, 7 }, - }, - }; - try check(&cases); - - const cases2 = [_][]const i32{ - &[_]i32{1}, - &[_]i32{ two, 3 }, - }; - try expect(cases2.len == 2); - try expect(cases2[0].len == 1); - try expect(cases2[0][0] == 1); - try expect(cases2[1].len == 2); - try expect(cases2[1][0] == 2); - try expect(cases2[1][1] == 3); - - const cases3 = [_][]const []const i32{ - &[_][]const i32{&[_]i32{1}}, - &[_][]const i32{&[_]i32{ two, 3 }}, - &[_][]const i32{ - &[_]i32{4}, - &[_]i32{ 5, 6, 7 }, - }, - }; - try check(&cases3); - } - - fn check(cases: []const []const []const i32) !void { - try expect(cases.len == 3); - try expect(cases[0].len == 1); - try expect(cases[0][0].len == 1); - try expect(cases[0][0][0] == 1); - try expect(cases[1].len == 1); - try expect(cases[1][0].len == 2); - try expect(cases[1][0][0] == 2); - try expect(cases[1][0][1] == 3); - try expect(cases[2].len == 2); - try expect(cases[2][0].len == 1); - try expect(cases[2][0][0] == 4); - try expect(cases[2][1].len == 3); - try expect(cases[2][1][0] == 5); - try expect(cases[2][1][1] == 6); - try expect(cases[2][1][2] == 7); - } - }; - try S.entry(2); - comptime try S.entry(2); -} - -test "anonymous literal in array" { - const S = struct { - const Foo = struct { - a: usize = 2, - b: usize = 4, - }; - fn doTheTest() !void { - var array: [2]Foo = .{ - .{ .a = 3 }, - .{ .b = 3 }, - }; - try expect(array[0].a == 3); - try expect(array[0].b == 4); - try expect(array[1].a == 2); - try expect(array[1].b == 3); - } - }; - try S.doTheTest(); - comptime try S.doTheTest(); -} - test "access the null element of a null terminated array" { const S = struct { fn doTheTest() !void { @@ -100,9 +22,9 @@ test "type deduction for array subscript expression" { fn doTheTest() !void { var array = [_]u8{ 0x55, 0xAA }; var v0 = true; - try expectEqual(@as(u8, 0xAA), array[if (v0) 1 else 0]); + try expect(@as(u8, 0xAA) == array[if (v0) 1 else 0]); var v1 = false; - try expectEqual(@as(u8, 0x55), array[if (v1) 1 else 0]); + try expect(@as(u8, 0x55) == array[if (v1) 1 else 0]); } }; try S.doTheTest(); @@ -119,9 +41,9 @@ test "sentinel element count towards the ABI size calculation" { }; var x = T{}; var as_slice = mem.asBytes(&x); - try expectEqual(@as(usize, 3), as_slice.len); - try expectEqual(@as(u8, 0x55), as_slice[0]); - try expectEqual(@as(u8, 0xAA), as_slice[2]); + try expect(@as(usize, 3) == as_slice.len); + try expect(@as(u8, 0x55) == as_slice[0]); + try expect(@as(u8, 0xAA) == as_slice[2]); } }; @@ -144,7 +66,7 @@ test "zero-sized array with recursive type definition" { }; var t: S = .{ .list = .{ .s = undefined } }; - try expectEqual(@as(usize, 0), t.list.x); + try expect(@as(usize, 0) == t.list.x); } test "type coercion of anon struct literal to array" { |
