Commit ef7eff3939

Andrew Kelley <andrew@ziglang.org>
2022-01-25 22:53:41
Sema: coercion of pointers to C pointers
1 parent f2835c6
Changed files (2)
src
test
behavior
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 => {},
                 }
             }
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"));
+}