aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-01-25 14:53:41 -0700
committerAndrew Kelley <andrew@ziglang.org>2022-01-25 14:53:41 -0700
commitef7eff393912cae322fbc755536bc060c0166b94 (patch)
treea9c6d4ca180644cb85a048673c7583708d6e4b69
parentf2835c6a286c9e6bb033cbf04a2ed3463e206bf3 (diff)
downloadzig-ef7eff393912cae322fbc755536bc060c0166b94.tar.gz
zig-ef7eff393912cae322fbc755536bc060c0166b94.zip
Sema: coercion of pointers to C pointers
-rw-r--r--src/Sema.zig17
-rw-r--r--test/behavior/cast.zig13
2 files changed, 29 insertions, 1 deletions
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"));
+}