mirror of
https://github.com/ziglang/zig.git
synced 2025-02-02 20:21:35 +00:00
Sema: coercion of pointers to C pointers
This commit is contained in:
parent
f2835c6a28
commit
ef7eff3939
17
src/Sema.zig
17
src/Sema.zig
@ -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 => {},
|
||||
}
|
||||
}
|
||||
|
@ -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"));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user