authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-27 00:48:56+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-27 01:31:18+03:00
logb937a045607deae158ccb6a00f5defaf36510e61
tree962f15f44d455e77dd8a3369bd4125b740679853
parentf3a3fb3d880528e8b6404648c605ed092ef1412c

Sema: check `coerceInMemoryAllowed` earlier in `resolvePeerTypes`

Closes #13310

2 files changed, 16 insertions(+), 12 deletions(-)

src/Sema.zig+9-12
...@@ -28340,8 +28340,16 @@ fn resolvePeerTypes(...@@ -28340,8 +28340,16 @@ fn resolvePeerTypes(
28340 const candidate_ty_tag = try candidate_ty.zigTypeTagOrPoison();28340 const candidate_ty_tag = try candidate_ty.zigTypeTagOrPoison();
28341 const chosen_ty_tag = try chosen_ty.zigTypeTagOrPoison();28341 const chosen_ty_tag = try chosen_ty.zigTypeTagOrPoison();
2834228342
28343 if (candidate_ty.eql(chosen_ty, sema.mod))28343 // If the candidate can coerce into our chosen type, we're done.
28344 // If the chosen type can coerce into the candidate, use that.
28345 if ((try sema.coerceInMemoryAllowed(block, chosen_ty, candidate_ty, false, target, src, src)) == .ok) {
28346 continue;
28347 }
28348 if ((try sema.coerceInMemoryAllowed(block, candidate_ty, chosen_ty, false, target, src, src)) == .ok) {
28349 chosen = candidate;
28350 chosen_i = candidate_i + 1;
28344 continue;28351 continue;
28352 }
2834528353
28346 switch (candidate_ty_tag) {28354 switch (candidate_ty_tag) {
28347 .NoReturn, .Undefined => continue,28355 .NoReturn, .Undefined => continue,
...@@ -28741,17 +28749,6 @@ fn resolvePeerTypes(...@@ -28741,17 +28749,6 @@ fn resolvePeerTypes(
28741 else => {},28749 else => {},
28742 }28750 }
2874328751
28744 // If the candidate can coerce into our chosen type, we're done.
28745 // If the chosen type can coerce into the candidate, use that.
28746 if ((try sema.coerceInMemoryAllowed(block, chosen_ty, candidate_ty, false, target, src, src)) == .ok) {
28747 continue;
28748 }
28749 if ((try sema.coerceInMemoryAllowed(block, candidate_ty, chosen_ty, false, target, src, src)) == .ok) {
28750 chosen = candidate;
28751 chosen_i = candidate_i + 1;
28752 continue;
28753 }
28754
28755 // At this point, we hit a compile error. We need to recover28752 // At this point, we hit a compile error. We need to recover
28756 // the source locations.28753 // the source locations.
28757 const chosen_src = candidate_srcs.resolve(28754 const chosen_src = candidate_srcs.resolve(
test/behavior/cast.zig+7
...@@ -1444,3 +1444,10 @@ test "coerce between pointers of compatible differently-named floats" {...@@ -1444,3 +1444,10 @@ test "coerce between pointers of compatible differently-named floats" {
1444 f2.* += 1;1444 f2.* += 1;
1445 try expect(f1 == @as(F, 12.34) + 1);1445 try expect(f1 == @as(F, 12.34) + 1);
1446}1446}
1447
1448test "peer type resolution of const and non-const pointer to array" {
1449 const a = @intToPtr(*[1024]u8, 42);
1450 const b = @intToPtr(*const [1024]u8, 42);
1451 try std.testing.expect(@TypeOf(a, b) == *const [1024]u8);
1452 try std.testing.expect(a == b);
1453}