Sema: in-memory coercion of differently named int types

which have the same number of bits and the same signedness.
This commit is contained in:
Andrew Kelley 2022-04-06 02:39:55 -07:00
parent 9213aa789b
commit 62f54aa39c
2 changed files with 14 additions and 1 deletions

View File

@ -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;

View File

@ -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" {