Sema: check coerceInMemoryAllowed earlier in resolvePeerTypes

Closes #13310
This commit is contained in:
Veikka Tuominen 2022-10-27 00:48:56 +03:00
parent f3a3fb3d88
commit b937a04560
2 changed files with 16 additions and 12 deletions

View File

@ -28340,8 +28340,16 @@ fn resolvePeerTypes(
const candidate_ty_tag = try candidate_ty.zigTypeTagOrPoison();
const chosen_ty_tag = try chosen_ty.zigTypeTagOrPoison();
if (candidate_ty.eql(chosen_ty, sema.mod))
// If the candidate can coerce into our chosen type, we're done.
// If the chosen type can coerce into the candidate, use that.
if ((try sema.coerceInMemoryAllowed(block, chosen_ty, candidate_ty, false, target, src, src)) == .ok) {
continue;
}
if ((try sema.coerceInMemoryAllowed(block, candidate_ty, chosen_ty, false, target, src, src)) == .ok) {
chosen = candidate;
chosen_i = candidate_i + 1;
continue;
}
switch (candidate_ty_tag) {
.NoReturn, .Undefined => continue,
@ -28741,17 +28749,6 @@ fn resolvePeerTypes(
else => {},
}
// If the candidate can coerce into our chosen type, we're done.
// If the chosen type can coerce into the candidate, use that.
if ((try sema.coerceInMemoryAllowed(block, chosen_ty, candidate_ty, false, target, src, src)) == .ok) {
continue;
}
if ((try sema.coerceInMemoryAllowed(block, candidate_ty, chosen_ty, false, target, src, src)) == .ok) {
chosen = candidate;
chosen_i = candidate_i + 1;
continue;
}
// At this point, we hit a compile error. We need to recover
// the source locations.
const chosen_src = candidate_srcs.resolve(

View File

@ -1444,3 +1444,10 @@ test "coerce between pointers of compatible differently-named floats" {
f2.* += 1;
try expect(f1 == @as(F, 12.34) + 1);
}
test "peer type resolution of const and non-const pointer to array" {
const a = @intToPtr(*[1024]u8, 42);
const b = @intToPtr(*const [1024]u8, 42);
try std.testing.expect(@TypeOf(a, b) == *const [1024]u8);
try std.testing.expect(a == b);
}