diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-01-14 23:05:33 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2022-01-14 23:08:11 -0700 |
| commit | 41f3799bf0cfc8241f458094781ba45967e2576e (patch) | |
| tree | b26beee8643ba7ebac7774898366f6add4667e3b /test | |
| parent | ba0f72363accc19edbfc5a7ae42d5a8970f56f64 (diff) | |
| download | zig-41f3799bf0cfc8241f458094781ba45967e2576e.tar.gz zig-41f3799bf0cfc8241f458094781ba45967e2576e.zip | |
Sema: fix array_init with runtime element
Previously it emitted an invalid AIR encoding.
Diffstat (limited to 'test')
| -rw-r--r-- | test/behavior/array_llvm.zig | 42 | ||||
| -rw-r--r-- | test/behavior/array_stage1.zig | 40 |
2 files changed, 42 insertions, 40 deletions
diff --git a/test/behavior/array_llvm.zig b/test/behavior/array_llvm.zig index 3fca269034..a828954b73 100644 --- a/test/behavior/array_llvm.zig +++ b/test/behavior/array_llvm.zig @@ -45,3 +45,45 @@ fn testImplicitCastSingleItemPtr() !void { slice[0] += 1; try expect(byte == 101); } + +fn testArrayByValAtComptime(b: [2]u8) u8 { + return b[0]; +} + +test "comptime evaluating function that takes array by value" { + const arr = [_]u8{ 1, 2 }; + const x = comptime testArrayByValAtComptime(arr); + const y = comptime testArrayByValAtComptime(arr); + try expect(x == 1); + try expect(y == 1); +} + +test "runtime initialize array elem and then implicit cast to slice" { + var two: i32 = 2; + const x: []const i32 = &[_]i32{two}; + try expect(x[0] == 2); +} + +test "array literal as argument to function" { + const S = struct { + fn entry(two: i32) !void { + try foo(&[_]i32{ 1, 2, 3 }); + try foo(&[_]i32{ 1, two, 3 }); + try foo2(true, &[_]i32{ 1, 2, 3 }); + try foo2(true, &[_]i32{ 1, two, 3 }); + } + fn foo(x: []const i32) !void { + try expect(x[0] == 1); + try expect(x[1] == 2); + try expect(x[2] == 3); + } + fn foo2(trash: bool, x: []const i32) !void { + try expect(trash); + try expect(x[0] == 1); + try expect(x[1] == 2); + try expect(x[2] == 3); + } + }; + try S.entry(2); + comptime try S.entry(2); +} diff --git a/test/behavior/array_stage1.zig b/test/behavior/array_stage1.zig index d4b9221fe0..daef20ffb5 100644 --- a/test/behavior/array_stage1.zig +++ b/test/behavior/array_stage1.zig @@ -4,46 +4,6 @@ const mem = std.mem; const expect = testing.expect; const expectEqual = testing.expectEqual; -fn testArrayByValAtComptime(b: [2]u8) u8 { - return b[0]; -} - -test "comptime evaluating function that takes array by value" { - const arr = [_]u8{ 0, 1 }; - _ = comptime testArrayByValAtComptime(arr); - _ = comptime testArrayByValAtComptime(arr); -} - -test "runtime initialize array elem and then implicit cast to slice" { - var two: i32 = 2; - const x: []const i32 = &[_]i32{two}; - try expect(x[0] == 2); -} - -test "array literal as argument to function" { - const S = struct { - fn entry(two: i32) !void { - try foo(&[_]i32{ 1, 2, 3 }); - try foo(&[_]i32{ 1, two, 3 }); - try foo2(true, &[_]i32{ 1, 2, 3 }); - try foo2(true, &[_]i32{ 1, two, 3 }); - } - fn foo(x: []const i32) !void { - try expect(x[0] == 1); - try expect(x[1] == 2); - try expect(x[2] == 3); - } - fn foo2(trash: bool, x: []const i32) !void { - try expect(trash); - try expect(x[0] == 1); - try expect(x[1] == 2); - try expect(x[2] == 3); - } - }; - 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 { |
