aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorVeikka Tuominen <git@vexu.eu>2022-09-01 13:16:06 +0300
committerVeikka Tuominen <git@vexu.eu>2022-09-01 13:16:33 +0300
commit2cd3989cb32d97c8a60d9cf4154e049815259b46 (patch)
tree989dd82927529f4133259e26c36d1c0ef0e19a44 /src
parent2b92c5a23e912df56885ff10d690ff7bfd1e3f47 (diff)
downloadzig-2cd3989cb32d97c8a60d9cf4154e049815259b46.tar.gz
zig-2cd3989cb32d97c8a60d9cf4154e049815259b46.zip
Sema: add more validation to coerceVarArgParam
Closes #12706
Diffstat (limited to 'src')
-rw-r--r--src/Sema.zig36
1 files changed, 30 insertions, 6 deletions
diff --git a/src/Sema.zig b/src/Sema.zig
index 7a96fd51cd..e643f73962 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -24075,16 +24075,40 @@ fn coerceVarArgParam(
inst: Air.Inst.Ref,
inst_src: LazySrcLoc,
) !Air.Inst.Ref {
- const inst_ty = sema.typeOf(inst);
if (block.is_typeof) return inst;
- switch (inst_ty.zigTypeTag()) {
+ const coerced = switch (sema.typeOf(inst).zigTypeTag()) {
// TODO consider casting to c_int/f64 if they fit
- .ComptimeInt, .ComptimeFloat => return sema.fail(block, inst_src, "integer and float literals in var args function must be casted", .{}),
- else => {},
+ .ComptimeInt, .ComptimeFloat => return sema.fail(
+ block,
+ inst_src,
+ "integer and float literals passed variadic function must be casted to a fixed-size number type",
+ .{},
+ ),
+ .Fn => blk: {
+ const fn_val = try sema.resolveConstValue(block, .unneeded, inst, undefined);
+ const fn_decl = fn_val.pointerDecl().?;
+ break :blk try sema.analyzeDeclRef(fn_decl);
+ },
+ .Array => return sema.fail(block, inst_src, "arrays must be passed by reference to variadic function", .{}),
+ else => inst,
+ };
+
+ const coerced_ty = sema.typeOf(coerced);
+ if (!sema.validateExternType(coerced_ty, .other)) {
+ const msg = msg: {
+ const msg = try sema.errMsg(block, inst_src, "cannot pass '{}' to variadic function", .{coerced_ty.fmt(sema.mod)});
+ errdefer msg.destroy(sema.gpa);
+
+ const src_decl = sema.mod.declPtr(block.src_decl);
+ try sema.explainWhyTypeIsNotExtern(msg, inst_src.toSrcLoc(src_decl), coerced_ty, .other);
+
+ try sema.addDeclaredHereNote(msg, coerced_ty);
+ break :msg msg;
+ };
+ return sema.failWithOwnedErrorMsg(msg);
}
- // TODO implement more of this function.
- return inst;
+ return coerced;
}
// TODO migrate callsites to use storePtr2 instead.