From ef7eff393912cae322fbc755536bc060c0166b94 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 25 Jan 2022 14:53:41 -0700 Subject: [PATCH] Sema: coercion of pointers to C pointers --- src/Sema.zig | 17 +++++++++++++++++ test/behavior/cast.zig | 13 ++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Sema.zig b/src/Sema.zig index a761623b2e..6fc4f85174 100644 --- a/src/Sema.zig +++ b/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 => {}, } } diff --git a/test/behavior/cast.zig b/test/behavior/cast.zig index 05894be55e..54b7df4fc3 100644 --- a/test/behavior/cast.zig +++ b/test/behavior/cast.zig @@ -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")); +}