authorgravatar for mitchell.hashimoto@gmail.comMitchell Hashimoto <mitchell.hashimoto@gmail.com> 2022-03-03 17:01:31-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-03 21:33:18-07:00
log26be5bb8b1e1c05ceab4b7620efa2a058a174886
treef229afae27f08c469cb00e9c2b8d5645bfeeeaca
parentc9ee3c1e474a7b10fb806b60ef108057395a3cca

stage2: peer resolve *T to [*c]T


2 files changed, 35 insertions(+), 3 deletions(-)

src/Sema.zig+20
...@@ -18280,6 +18280,17 @@ fn resolvePeerTypes(...@@ -18280,6 +18280,17 @@ fn resolvePeerTypes(
18280 },18280 },
18281 .Pointer => {18281 .Pointer => {
18282 if (candidate_ty.ptrSize() == .C) {18282 if (candidate_ty.ptrSize() == .C) {
18283 // *T to [*c]T
18284 if (chosen_ty_tag == .Pointer) {
18285 const chosen_elem_ty = chosen_ty.childType();
18286 const candidate_elem_ty = candidate_ty.childType();
18287 if ((try sema.coerceInMemoryAllowed(block, chosen_elem_ty, candidate_elem_ty, false, target, src, src)) == .ok) {
18288 chosen = candidate;
18289 chosen_i = candidate_i + 1;
18290 continue;
18291 }
18292 }
18293
18283 if (chosen_ty_tag == .Int or chosen_ty_tag == .ComptimeInt) {18294 if (chosen_ty_tag == .Int or chosen_ty_tag == .ComptimeInt) {
18284 chosen = candidate;18295 chosen = candidate;
18285 chosen_i = candidate_i + 1;18296 chosen_i = candidate_i + 1;
...@@ -18290,6 +18301,15 @@ fn resolvePeerTypes(...@@ -18290,6 +18301,15 @@ fn resolvePeerTypes(
18290 }18301 }
18291 }18302 }
1829218303
18304 // [*c]T and *T
18305 if (chosen_ty_tag == .Pointer and chosen_ty.ptrSize() == .C) {
18306 const chosen_elem_ty = chosen_ty.childType();
18307 const candidate_elem_ty = candidate_ty.childType();
18308 if ((try sema.coerceInMemoryAllowed(block, chosen_elem_ty, candidate_elem_ty, false, target, src, src)) == .ok) {
18309 continue;
18310 }
18311 }
18312
18293 // *[N]T to [*]T18313 // *[N]T to [*]T
18294 if (candidate_ty.ptrSize() == .Many and18314 if (candidate_ty.ptrSize() == .Many and
18295 chosen_ty_tag == .Pointer and18315 chosen_ty_tag == .Pointer and
test/behavior/pointers.zig+15-3
...@@ -128,7 +128,11 @@ fn testDerefPtrOneVal() !void {...@@ -128,7 +128,11 @@ fn testDerefPtrOneVal() !void {
128}128}
129129
130test "peer type resolution with C pointers" {130test "peer type resolution with C pointers" {
131 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO131 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
132 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
133 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
134 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
135 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
132136
133 var ptr_one: *u8 = undefined;137 var ptr_one: *u8 = undefined;
134 var ptr_many: [*]u8 = undefined;138 var ptr_many: [*]u8 = undefined;
...@@ -159,7 +163,11 @@ test "implicit casting between C pointer and optional non-C pointer" {...@@ -159,7 +163,11 @@ test "implicit casting between C pointer and optional non-C pointer" {
159}163}
160164
161test "implicit cast error unions with non-optional to optional pointer" {165test "implicit cast error unions with non-optional to optional pointer" {
162 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO166 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
167 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
168 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
169 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
170 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
163171
164 const S = struct {172 const S = struct {
165 fn doTheTest() !void {173 fn doTheTest() !void {
...@@ -376,7 +384,11 @@ test "pointer arithmetic affects the alignment" {...@@ -376,7 +384,11 @@ test "pointer arithmetic affects the alignment" {
376}384}
377385
378test "@ptrToInt on null optional at comptime" {386test "@ptrToInt on null optional at comptime" {
379 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO387 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
388 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
389 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
390 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
391 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
380392
381 {393 {
382 const pointer = @intToPtr(?*u8, 0x000);394 const pointer = @intToPtr(?*u8, 0x000);