mirror of
https://github.com/ziglang/zig.git
synced 2025-12-28 00:53:18 +00:00
Merge pull request #17504 from ziglang/fix-var-args-coercion
Sema: fix crash when coercion dest is var args
This commit is contained in:
commit
7aa85691b0
28
src/Sema.zig
28
src/Sema.zig
@ -4371,13 +4371,19 @@ fn zirValidateRefTy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
|
||||
const mod = sema.mod;
|
||||
const un_tok = sema.code.instructions.items(.data)[inst].un_tok;
|
||||
const src = un_tok.src();
|
||||
const ty_operand = sema.resolveType(block, src, un_tok.operand) catch |err| switch (err) {
|
||||
error.GenericPoison => {
|
||||
// We don't actually have a type, so this will be treated as an untyped address-of operator.
|
||||
return;
|
||||
},
|
||||
// In case of GenericPoison, we don't actually have a type, so this will be
|
||||
// treated as an untyped address-of operator.
|
||||
if (un_tok.operand == .var_args_param_type) return;
|
||||
const operand_air_inst = sema.resolveInst(un_tok.operand) catch |err| switch (err) {
|
||||
error.GenericPoison => return,
|
||||
else => |e| return e,
|
||||
};
|
||||
if (operand_air_inst == .var_args_param_type) return;
|
||||
const ty_operand = sema.analyzeAsType(block, src, operand_air_inst) catch |err| switch (err) {
|
||||
error.GenericPoison => return,
|
||||
else => |e| return e,
|
||||
};
|
||||
if (ty_operand.isGenericPoison()) return;
|
||||
if (ty_operand.optEuBaseType(mod).zigTypeTag(mod) != .Pointer) {
|
||||
return sema.failWithOwnedErrorMsg(block, msg: {
|
||||
const msg = try sema.errMsg(block, src, "expected type '{}', found pointer", .{ty_operand.fmt(mod)});
|
||||
@ -9846,11 +9852,19 @@ fn analyzeAs(
|
||||
const mod = sema.mod;
|
||||
const operand = try sema.resolveInst(zir_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,
|
||||
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", .{});
|
||||
}
|
||||
const is_ret = if (Zir.refToIndex(zir_dest_type)) |ptr_index|
|
||||
|
||||
@ -150,6 +150,46 @@ test "simple variadic function" {
|
||||
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, 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 "coerce reference to var arg" {
|
||||
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
|
||||
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
|
||||
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
|
||||
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
|
||||
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
|
||||
if (builtin.os.tag != .macos and comptime builtin.cpu.arch.isAARCH64()) {
|
||||
// https://github.com/ziglang/zig/issues/14096
|
||||
return error.SkipZigTest;
|
||||
}
|
||||
if (builtin.cpu.arch == .x86_64 and builtin.os.tag == .windows) return error.SkipZigTest; // TODO
|
||||
|
||||
const S = struct {
|
||||
fn addPtr(count: c_int, ...) callconv(.C) c_int {
|
||||
var ap = @cVaStart();
|
||||
defer @cVaEnd(&ap);
|
||||
var i: usize = 0;
|
||||
var sum: c_int = 0;
|
||||
while (i < count) : (i += 1) {
|
||||
sum += @cVaArg(&ap, *c_int).*;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
};
|
||||
|
||||
// Originally reported at https://github.com/ziglang/zig/issues/17494
|
||||
var a: i32 = 12;
|
||||
var b: i32 = 34;
|
||||
try expect(46 == S.addPtr(2, &a, &b));
|
||||
}
|
||||
|
||||
test "variadic functions" {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user