Commit b937a04560

Veikka Tuominen <git@vexu.eu>
2022-10-26 23:48:56
Sema: check `coerceInMemoryAllowed` earlier in `resolvePeerTypes`
Closes #13310
1 parent f3a3fb3
Changed files (2)
src
test
behavior
src/Sema.zig
@@ -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(
test/behavior/cast.zig
@@ -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);
+}