stage2: *[N]T to [*]T (and vice versa)

This commit is contained in:
Mitchell Hashimoto
2022-02-25 17:17:47 -08:00
parent e442f88b76
commit 943ee59bb1
2 changed files with 41 additions and 0 deletions

View File

@@ -17109,6 +17109,35 @@ fn resolvePeerTypes(
}
}
// *[N]T to [*]T
if (candidate_ty.ptrSize() == .Many and
chosen_ty_tag == .Pointer and
chosen_ty.ptrSize() == .One and
chosen_ty.childType().zigTypeTag() == .Array)
{
chosen = candidate;
chosen_i = candidate_i + 1;
convert_to_slice = false;
if (chosen_ty.childType().isConstPtr() and !candidate_ty.childType().isConstPtr())
make_the_slice_const = true;
continue;
}
// *[N]T to [*]T (prev is many pointer)
if (candidate_ty.ptrSize() == .One and
candidate_ty.childType().zigTypeTag() == .Array and
chosen_ty_tag == .Pointer and
chosen_ty.ptrSize() == .Many)
{
if (candidate_ty.childType().isConstPtr() and !chosen_ty.childType().isConstPtr())
make_the_slice_const = true;
continue;
}
// *[N]T to []T (prev is slice)
// *[N]T to E![]T
if ((chosen_ty.isSlice() or (chosen_ty_tag == .ErrorUnion and chosen_ty.errorUnionPayload().isSlice())) and

View File

@@ -643,6 +643,18 @@ test "peer cast *[0]T to []const T" {
try expect(mem.eql(u8, "abcde", y));
}
test "peer cast *[N]T to [*]T" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
var array = [4:99]i32{ 1, 2, 3, 4 };
var dest: [*]i32 = undefined;
try expect(@TypeOf(&array, dest) == [*]i32);
try expect(@TypeOf(dest, &array) == [*]i32);
}
test "peer resolution of string literals" {
if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO