Sema: implement array coercion

This commit is contained in:
Andrew Kelley
2021-12-27 22:04:21 -07:00
parent 042b770d62
commit 85d4c8620f
6 changed files with 138 additions and 107 deletions

View File

@@ -164,3 +164,53 @@ test "read/write through global variable array of struct fields initialized via
};
try S.doTheTest();
}
test "single-item pointer to array indexing and slicing" {
try testSingleItemPtrArrayIndexSlice();
comptime try testSingleItemPtrArrayIndexSlice();
}
fn testSingleItemPtrArrayIndexSlice() !void {
{
var array: [4]u8 = "aaaa".*;
doSomeMangling(&array);
try expect(mem.eql(u8, "azya", &array));
}
{
var array = "aaaa".*;
doSomeMangling(&array);
try expect(mem.eql(u8, "azya", &array));
}
}
fn doSomeMangling(array: *[4]u8) void {
array[1] = 'z';
array[2..3][0] = 'y';
}
test "implicit cast zero sized array ptr to slice" {
{
var b = "".*;
const c: []const u8 = &b;
try expect(c.len == 0);
}
{
var b: [0]u8 = "".*;
const c: []const u8 = &b;
try expect(c.len == 0);
}
}
test "anonymous list literal syntax" {
const S = struct {
fn doTheTest() !void {
var array: [4]u8 = .{ 1, 2, 3, 4 };
try expect(array[0] == 1);
try expect(array[1] == 2);
try expect(array[2] == 3);
try expect(array[3] == 4);
}
};
try S.doTheTest();
comptime try S.doTheTest();
}