authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-11 11:38:04+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-11 11:59:26+03:00
log0370006c1f5a23e5e2d0993fb71f825791d64957
tree9be5ac0a09bd6c2a7efa711b652d6a1c2e57b152
parente644a2ab6a99951ac8d367f7a6bac985cf16f9cc

Sema: only add note about int mismatch if not coercible

`unsigned 64-bit int cannot represent all possible unsigned 63-bit values` is nonsensical.

2 files changed, 25 insertions(+), 7 deletions(-)

src/Sema.zig+14-6
...@@ -20411,7 +20411,7 @@ fn coerceExtra(...@@ -20411,7 +20411,7 @@ fn coerceExtra(
20411const InMemoryCoercionResult = union(enum) {20411const InMemoryCoercionResult = union(enum) {
20412 ok,20412 ok,
20413 no_match: Pair,20413 no_match: Pair,
20414 int_mismatch: Int,20414 int_not_coercible: Int,
20415 error_union_payload: PairAndChild,20415 error_union_payload: PairAndChild,
20416 array_len: IntPair,20416 array_len: IntPair,
20417 array_sentinel: Sentinel,20417 array_sentinel: Sentinel,
...@@ -20527,7 +20527,7 @@ const InMemoryCoercionResult = union(enum) {...@@ -20527,7 +20527,7 @@ const InMemoryCoercionResult = union(enum) {
20527 try sema.addDeclaredHereNote(msg, types.actual);20527 try sema.addDeclaredHereNote(msg, types.actual);
20528 break;20528 break;
20529 },20529 },
20530 .int_mismatch => |int| {20530 .int_not_coercible => |int| {
20531 try sema.errNote(block, src, msg, "{s} {d}-bit int cannot represent all possible {s} {d}-bit values", .{20531 try sema.errNote(block, src, msg, "{s} {d}-bit int cannot represent all possible {s} {d}-bit values", .{
20532 @tagName(int.wanted_signedness), int.wanted_bits, @tagName(int.actual_signedness), int.actual_bits,20532 @tagName(int.wanted_signedness), int.wanted_bits, @tagName(int.actual_signedness), int.actual_bits,
20533 });20533 });
...@@ -20768,17 +20768,25 @@ fn coerceInMemoryAllowed(...@@ -20768,17 +20768,25 @@ fn coerceInMemoryAllowed(
20768 if (dest_ty.zigTypeTag() == .Int and src_ty.zigTypeTag() == .Int) {20768 if (dest_ty.zigTypeTag() == .Int and src_ty.zigTypeTag() == .Int) {
20769 const dest_info = dest_ty.intInfo(target);20769 const dest_info = dest_ty.intInfo(target);
20770 const src_info = src_ty.intInfo(target);20770 const src_info = src_ty.intInfo(target);
20771 if (dest_info.signedness != src_info.signedness or20771
20772 dest_info.bits != src_info.bits)20772 if (dest_info.signedness == src_info.signedness and
20773 dest_info.bits == src_info.bits)
20774 {
20775 return .ok;
20776 }
20777
20778 if ((src_info.signedness == dest_info.signedness and dest_info.bits < src_info.bits) or
20779 // small enough unsigned ints can get casted to large enough signed ints
20780 (dest_info.signedness == .signed and (src_info.signedness == .unsigned or dest_info.bits <= src_info.bits)) or
20781 (dest_info.signedness == .unsigned and src_info.signedness == .signed))
20773 {20782 {
20774 return InMemoryCoercionResult{ .int_mismatch = .{20783 return InMemoryCoercionResult{ .int_not_coercible = .{
20775 .actual_signedness = src_info.signedness,20784 .actual_signedness = src_info.signedness,
20776 .wanted_signedness = dest_info.signedness,20785 .wanted_signedness = dest_info.signedness,
20777 .actual_bits = src_info.bits,20786 .actual_bits = src_info.bits,
20778 .wanted_bits = dest_info.bits,20787 .wanted_bits = dest_info.bits,
20779 } };20788 } };
20780 }20789 }
20781 return .ok;
20782 }20790 }
2078320791
20784 // Differently-named floats with the same number of bits.20792 // Differently-named floats with the same number of bits.
test/cases/compile_errors/cast_between_optional_T_where_T_is_not_a_pointer.zig+11-1
...@@ -1,11 +1,18 @@...@@ -1,11 +1,18 @@
1pub const fnty1 = ?*const fn (i8) void;1pub const fnty1 = ?*const fn (i8) void;
2pub const fnty2 = ?*const fn (u64) void;2pub const fnty2 = ?*const fn (u64) void;
3export fn entry() void {3export fn entry1() void {
4 var a: fnty1 = undefined;4 var a: fnty1 = undefined;
5 var b: fnty2 = undefined;5 var b: fnty2 = undefined;
6 a = b;6 a = b;
7}7}
88
9pub const fnty3 = ?*const fn (u63) void;
10export fn entry2() void {
11 var a: fnty3 = undefined;
12 var b: fnty2 = undefined;
13 a = b;
14}
15
9// error16// error
10// backend=stage217// backend=stage2
11// target=native18// target=native
...@@ -14,3 +21,6 @@ export fn entry() void {...@@ -14,3 +21,6 @@ export fn entry() void {
14// :6:9: note: pointer type child 'fn(u64) void' cannot cast into pointer type child 'fn(i8) void'21// :6:9: note: pointer type child 'fn(u64) void' cannot cast into pointer type child 'fn(i8) void'
15// :6:9: note: parameter 0 'u64' cannot cast into 'i8'22// :6:9: note: parameter 0 'u64' cannot cast into 'i8'
16// :6:9: note: unsigned 64-bit int cannot represent all possible signed 8-bit values23// :6:9: note: unsigned 64-bit int cannot represent all possible signed 8-bit values
24// :13:9: error: expected type '?*const fn(u63) void', found '?*const fn(u64) void'
25// :13:9: note: pointer type child 'fn(u64) void' cannot cast into pointer type child 'fn(u63) void'
26// :13:9: note: parameter 0 'u64' cannot cast into 'u63'