Sema: coercion of pointers to C pointers

This commit is contained in:
Andrew Kelley 2022-01-25 14:53:41 -07:00
parent f2835c6a28
commit ef7eff3939
2 changed files with 29 additions and 1 deletions

View File

@ -14014,6 +14014,23 @@ fn coerce(
const addr = try sema.coerce(block, ptr_size_ty, inst, inst_src);
return sema.coerceCompatiblePtrs(block, dest_ty, addr, inst_src);
},
.Pointer => p: {
const inst_info = inst_ty.ptrInfo().data;
if (inst_info.size == .Slice) break :p;
switch (try sema.coerceInMemoryAllowed(
block,
dest_info.pointee_type,
inst_info.pointee_type,
dest_info.mutable,
target,
dest_ty_src,
inst_src,
)) {
.ok => {},
.no_match => break :p,
}
return sema.coerceCompatiblePtrs(block, dest_ty, inst, inst_src);
},
else => {},
}
}

View File

@ -316,7 +316,8 @@ test "cast from ?[*]T to ??[*]T" {
}
test "peer type unsigned int to signed" {
if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
var w: u31 = 5;
var x: u8 = 7;
@ -325,3 +326,13 @@ test "peer type unsigned int to signed" {
comptime try expect(@TypeOf(a) == i32);
try expect(a == 7);
}
test "expected [*c]const u8, found [*:0]const u8" {
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
var a: [*:0]const u8 = "hello";
var b: [*c]const u8 = a;
var c: [*:0]const u8 = b;
try expect(std.mem.eql(u8, c[0..5], "hello"));
}