Sema: fix crash when coercion dest is var args

When analyzing the `as` ZIR instruction, an assertion would trip if the
result type was a var args function argument. The fix is simple: inline
a little bit of the `resolveType` logic into `analyzeAs` to make it
detect this situation - which it was already attempting to do.

Closes #16197
This commit is contained in:
Andrew Kelley 2023-10-12 21:54:59 -07:00
parent 2769215b90
commit e6b73be870
2 changed files with 19 additions and 2 deletions

View File

@ -9846,11 +9846,19 @@ fn analyzeAs(
const mod = sema.mod; const mod = sema.mod;
const operand = try sema.resolveInst(zir_operand); const operand = try sema.resolveInst(zir_operand);
if (zir_dest_type == .var_args_param_type) return operand; if (zir_dest_type == .var_args_param_type) return operand;
const dest_ty = sema.resolveType(block, src, zir_dest_type) catch |err| switch (err) { const operand_air_inst = sema.resolveInst(zir_dest_type) catch |err| switch (err) {
error.GenericPoison => return operand, error.GenericPoison => return operand,
else => |e| return e, else => |e| return e,
}; };
if (dest_ty.zigTypeTag(mod) == .NoReturn) { if (operand_air_inst == .var_args_param_type) return operand;
const dest_ty = sema.analyzeAsType(block, src, operand_air_inst) catch |err| switch (err) {
error.GenericPoison => return operand,
else => |e| return e,
};
const dest_ty_tag = dest_ty.zigTypeTagOrPoison(mod) catch |err| switch (err) {
error.GenericPoison => return operand,
};
if (dest_ty_tag == .NoReturn) {
return sema.fail(block, src, "cannot cast to noreturn", .{}); return sema.fail(block, src, "cannot cast to noreturn", .{});
} }
const is_ret = if (Zir.refToIndex(zir_dest_type)) |ptr_index| const is_ret = if (Zir.refToIndex(zir_dest_type)) |ptr_index|

View File

@ -150,6 +150,15 @@ test "simple variadic function" {
try std.testing.expectEqual(@as(c_int, 0), S.add(0)); try std.testing.expectEqual(@as(c_int, 0), S.add(0));
try std.testing.expectEqual(@as(c_int, 1), S.add(1, @as(c_int, 1))); try std.testing.expectEqual(@as(c_int, 1), S.add(1, @as(c_int, 1)));
try std.testing.expectEqual(@as(c_int, 3), S.add(2, @as(c_int, 1), @as(c_int, 2))); try std.testing.expectEqual(@as(c_int, 3), S.add(2, @as(c_int, 1), @as(c_int, 2)));
{
// Test type coercion of a var args argument.
// Originally reported at https://github.com/ziglang/zig/issues/16197
var runtime: bool = true;
var a: i32 = 1;
var b: i32 = 2;
try expect(1 == S.add(1, if (runtime) a else b));
}
} }
test "variadic functions" { test "variadic functions" {