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 90fe93ed88056e13b2bb4f1a0862244c61bda694..ae6d95ef960fd7c89383821927fb4c7cb2a09a3c 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 0473a3603388b9e96b5d6184b2a00a8804b9dbf6..8793e7cb19af2a9f3229c9559f198a4312623b95 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" { -- 2.54.0