From 62f54aa39cb3dca3055033a57bc4626e94061aec Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 6 Apr 2022 02:39:55 -0700 Subject: [PATCH] Sema: in-memory coercion of differently named int types which have the same number of bits and the same signedness. --- src/Sema.zig | 11 +++++++++++ test/behavior/cast.zig | 4 +++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Sema.zig b/src/Sema.zig index 90fe93ed88..ae6d95ef96 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -18458,6 +18458,17 @@ fn coerceInMemoryAllowed( if (dest_ty.eql(src_ty, target)) return .ok; + // Differently-named integers with the same number of bits. + if (dest_ty.zigTypeTag() == .Int and src_ty.zigTypeTag() == .Int) { + const dest_info = dest_ty.intInfo(target); + const src_info = src_ty.intInfo(target); + if (dest_info.signedness == src_info.signedness and + dest_info.bits == src_info.bits) + { + return .ok; + } + } + // Pointers / Pointer-like Optionals var dest_buf: Type.Payload.ElemType = undefined; var src_buf: Type.Payload.ElemType = undefined; diff --git a/test/behavior/cast.zig b/test/behavior/cast.zig index 0473a36033..8793e7cb19 100644 --- a/test/behavior/cast.zig +++ b/test/behavior/cast.zig @@ -973,7 +973,8 @@ test "variable initialization uses result locations properly with regards to the } test "cast between C pointer with different but compatible types" { - if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO const S = struct { fn foo(arg: [*]c_ushort) u16 { @@ -985,6 +986,7 @@ test "cast between C pointer with different but compatible types" { } }; try S.doTheTest(); + comptime try S.doTheTest(); } test "peer type resolve string lit with sentinel-terminated mutable slice" {