From 6673b476855931137119ae92e91fdcb2822e9c4f Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 7 Sep 2025 17:28:01 -0700 Subject: [PATCH] frontend: vectors and arrays no longer support in-memory coercion closes #25172 --- doc/langref.html.in | 8 +++++++ doc/langref/test_vector.zig | 2 +- src/Sema.zig | 44 ------------------------------------- 3 files changed, 9 insertions(+), 45 deletions(-) diff --git a/doc/langref.html.in b/doc/langref.html.in index cf1c48f9e0..43d350ca2b 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -1969,6 +1969,14 @@ or

{#see_also|@splat|@shuffle|@select|@reduce#} + {#header_open|Relationship with Arrays#} +

Vectors and {#link|Arrays#} each have a well-defined bit layout + and therefore support {#link|@bitCast#} between each other. {#link|Type Coercion#} implicitly peforms + {#syntax#}@bitCast{#endsyntax#}.

+

Arrays have well-defined byte layout, but vectors do not, making {#link|@ptrCast#} between + them {#link|Illegal Behavior#}.

+ {#header_close#} + {#header_open|Destructuring Vectors#}

Vectors can be destructured: diff --git a/doc/langref/test_vector.zig b/doc/langref/test_vector.zig index cd1e860886..933a282910 100644 --- a/doc/langref/test_vector.zig +++ b/doc/langref/test_vector.zig @@ -17,7 +17,7 @@ test "Basic vector usage" { } test "Conversion between vectors, arrays, and slices" { - // Vectors and fixed-length arrays can be automatically assigned back and forth + // Vectors can be coerced to arrays, and vice versa. const arr1: [4]f32 = [_]f32{ 1.1, 3.2, 4.5, 5.6 }; const vec: @Vector(4, f32) = arr1; const arr2: [4]f32 = vec; diff --git a/src/Sema.zig b/src/Sema.zig index 587c6b2d70..51b93fb308 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -29649,50 +29649,6 @@ pub fn coerceInMemoryAllowed( return .ok; } - // Arrays <-> Vectors - if ((dest_tag == .vector and src_tag == .array) or - (dest_tag == .array and src_tag == .vector)) - { - const dest_len = dest_ty.arrayLen(zcu); - const src_len = src_ty.arrayLen(zcu); - if (dest_len != src_len) { - return .{ .array_len = .{ - .actual = src_len, - .wanted = dest_len, - } }; - } - - const dest_elem_ty = dest_ty.childType(zcu); - const src_elem_ty = src_ty.childType(zcu); - const child = try sema.coerceInMemoryAllowed(block, dest_elem_ty, src_elem_ty, dest_is_mut, target, dest_src, src_src, null); - if (child != .ok) { - return .{ .array_elem = .{ - .child = try child.dupe(sema.arena), - .actual = src_elem_ty, - .wanted = dest_elem_ty, - } }; - } - - if (dest_tag == .array) { - const dest_info = dest_ty.arrayInfo(zcu); - if (dest_info.sentinel != null) { - return .{ .array_sentinel = .{ - .actual = Value.@"unreachable", - .wanted = dest_info.sentinel.?, - .ty = dest_info.elem_type, - } }; - } - } - - // The memory layout of @Vector(N, iM) is the same as the integer type i(N*M), - // that is to say, the padding bits are not in the same place as the array [N]iM. - // If there's no padding, the bitcast is possible. - const elem_bit_size = dest_elem_ty.bitSize(zcu); - const elem_abi_byte_size = dest_elem_ty.abiSize(zcu); - if (elem_abi_byte_size * 8 == elem_bit_size) - return .ok; - } - // Optionals if (dest_tag == .optional and src_tag == .optional) { if ((maybe_dest_ptr_ty != null) != (maybe_src_ptr_ty != null)) {