From 045a5e924ce9abe239634545b0ad35576cc2fafc Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Tue, 7 Nov 2023 12:11:57 -0500 Subject: [PATCH 1/2] Sema: implement vararg integer promotions --- src/Sema.zig | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index 8e34c9e4dc..241f65a9a0 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -29418,10 +29418,10 @@ fn coerceVarArgParam( "integer and float literals passed to variadic function must be casted to a fixed-size number type", .{}, ), - .Fn => blk: { + .Fn => fn_ptr: { const fn_val = try sema.resolveConstDefinedValue(block, .unneeded, inst, undefined); const fn_decl = fn_val.pointerDecl(mod).?; - break :blk try sema.analyzeDeclRef(fn_decl); + break :fn_ptr try sema.analyzeDeclRef(fn_decl); }, .Array => return sema.fail(block, inst_src, "arrays must be passed by reference to variadic function", .{}), .Float => float: { @@ -29435,7 +29435,32 @@ fn coerceVarArgParam( else => unreachable, } }, - else => inst, + else => if (uncasted_ty.isAbiInt(mod)) int: { + const target = sema.mod.getTarget(); + const uncasted_info = uncasted_ty.intInfo(mod); + if (uncasted_info.bits <= target.c_type_bit_size(switch (uncasted_info.signedness) { + .signed => .int, + .unsigned => .uint, + })) break :int try sema.coerce(block, switch (uncasted_info.signedness) { + .signed => Type.c_int, + .unsigned => Type.c_uint, + }, inst, inst_src); + if (uncasted_info.bits <= target.c_type_bit_size(switch (uncasted_info.signedness) { + .signed => .long, + .unsigned => .ulong, + })) break :int try sema.coerce(block, switch (uncasted_info.signedness) { + .signed => Type.c_long, + .unsigned => Type.c_ulong, + }, inst, inst_src); + if (uncasted_info.bits <= target.c_type_bit_size(switch (uncasted_info.signedness) { + .signed => .longlong, + .unsigned => .ulonglong, + })) break :int try sema.coerce(block, switch (uncasted_info.signedness) { + .signed => Type.c_longlong, + .unsigned => Type.c_ulonglong, + }, inst, inst_src); + break :int inst; + } else inst, }; const coerced_ty = sema.typeOf(coerced); From 52d8099daeb34edc00898ef8cc34bd6f136949aa Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Wed, 8 Nov 2023 14:42:45 -0500 Subject: [PATCH 2/2] Sema: don't allow passing non-extern types to varargs parameters --- src/Sema.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Sema.zig b/src/Sema.zig index 241f65a9a0..0911f75feb 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -29436,6 +29436,7 @@ fn coerceVarArgParam( } }, else => if (uncasted_ty.isAbiInt(mod)) int: { + if (!try sema.validateExternType(uncasted_ty, .param_ty)) break :int inst; const target = sema.mod.getTarget(); const uncasted_info = uncasted_ty.intInfo(mod); if (uncasted_info.bits <= target.c_type_bit_size(switch (uncasted_info.signedness) {