zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 18d412ab2fb7bda92f7bfbdf732849bbcd066c33 (tree)
parent 9b292c094909e51718c1d37dbeb859674dd98c8a
Author: WillLillis <wlillis@umass.edu>
Date:   Sat, 20 Jul 2024 02:31:19 -0400

fix: remove misleading error note for failed array coercions

Diffstat:
Msrc/Sema.zig | 18+++++++++++-------
Atest/cases/compile_errors/invalid_array_assignment_with_valid_elems.zig | 11+++++++++++
2 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/src/Sema.zig b/src/Sema.zig @@ -30279,7 +30279,7 @@ pub fn coerceInMemoryAllowed( if ((src_info.signedness == dest_info.signedness and dest_info.bits < src_info.bits) or // small enough unsigned ints can get casted to large enough signed ints - (dest_info.signedness == .signed and (src_info.signedness == .unsigned or dest_info.bits <= src_info.bits)) or + (dest_info.signedness == .signed and src_info.signedness == .unsigned and dest_info.bits <= src_info.bits) or (dest_info.signedness == .unsigned and src_info.signedness == .signed)) { return InMemoryCoercionResult{ .int_not_coercible = .{ @@ -30360,12 +30360,16 @@ pub fn coerceInMemoryAllowed( } const child = try sema.coerceInMemoryAllowed(block, dest_info.elem_type, src_info.elem_type, dest_is_mut, target, dest_src, src_src, null); - if (child != .ok) { - return InMemoryCoercionResult{ .array_elem = .{ - .child = try child.dupe(sema.arena), - .actual = src_info.elem_type, - .wanted = dest_info.elem_type, - } }; + switch (child) { + .ok => {}, + .no_match => return child, + else => { + return InMemoryCoercionResult{ .array_elem = .{ + .child = try child.dupe(sema.arena), + .actual = src_info.elem_type, + .wanted = dest_info.elem_type, + } }; + }, } const ok_sent = (dest_info.sentinel == null and src_info.sentinel == null) or (src_info.sentinel != null and diff --git a/test/cases/compile_errors/invalid_array_assignment_with_valid_elems.zig b/test/cases/compile_errors/invalid_array_assignment_with_valid_elems.zig @@ -0,0 +1,11 @@ +export fn a() void { + const x = [_]u16{ 1, 2, 3 }; + const y: [3]i32 = x; + _ = y; +} + +// error +// backend=stage2 +// target=native +// +// 3:23: error: expected type '[3]i32', found '[3]u16'