authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-04-06 02:39:55-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-04-06 02:39:55-07:00
log62f54aa39cb3dca3055033a57bc4626e94061aec
treebb358aa300f71f548f3ac8847ff8130d44bed9fb
parent9213aa789b4b44711f2747e5ab053a2b1f57a15b

Sema: in-memory coercion of differently named int types

which have the same number of bits and the same signedness.

2 files changed, 14 insertions(+), 1 deletions(-)

src/Sema.zig+11
......@@ -18458,6 +18458,17 @@ fn coerceInMemoryAllowed(
1845818458 if (dest_ty.eql(src_ty, target))
1845918459 return .ok;
1846018460
18461 // Differently-named integers with the same number of bits.
18462 if (dest_ty.zigTypeTag() == .Int and src_ty.zigTypeTag() == .Int) {
18463 const dest_info = dest_ty.intInfo(target);
18464 const src_info = src_ty.intInfo(target);
18465 if (dest_info.signedness == src_info.signedness and
18466 dest_info.bits == src_info.bits)
18467 {
18468 return .ok;
18469 }
18470 }
18471
1846118472 // Pointers / Pointer-like Optionals
1846218473 var dest_buf: Type.Payload.ElemType = undefined;
1846318474 var src_buf: Type.Payload.ElemType = undefined;
test/behavior/cast.zig+3-1
......@@ -973,7 +973,8 @@ test "variable initialization uses result locations properly with regards to the
973973}
974974
975975test "cast between C pointer with different but compatible types" {
976 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
976 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
977 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
977978
978979 const S = struct {
979980 fn foo(arg: [*]c_ushort) u16 {
......@@ -985,6 +986,7 @@ test "cast between C pointer with different but compatible types" {
985986 }
986987 };
987988 try S.doTheTest();
989 comptime try S.doTheTest();
988990}
989991
990992test "peer type resolve string lit with sentinel-terminated mutable slice" {