From 1afaf42525760edb78c287c216fda4aafc03d68f Mon Sep 17 00:00:00 2001 From: Vexu Date: Fri, 17 Apr 2020 22:02:49 +0300 Subject: [PATCH] add error for non-exter variadic functions --- src/analyze.cpp | 8 +++++++- src/ir.cpp | 2 +- test/compile_errors.zig | 15 ++++++++++++--- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/analyze.cpp b/src/analyze.cpp index d7064ed59..f3f2a30dd 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -3619,12 +3619,18 @@ static void add_top_level_decl(CodeGen *g, ScopeDecls *decls_scope, Tld *tld) { assert(tld->source_node->type == NodeTypeFnProto); is_export = tld->source_node->data.fn_proto.is_export; - if (!is_export && !tld->source_node->data.fn_proto.is_extern && + if (!tld->source_node->data.fn_proto.is_extern && tld->source_node->data.fn_proto.fn_def_node == nullptr) { add_node_error(g, tld->source_node, buf_sprintf("non-extern function has no body")); return; } + if (!tld->source_node->data.fn_proto.is_extern && + tld->source_node->data.fn_proto.is_var_args) + { + add_node_error(g, tld->source_node, buf_sprintf("non-extern function is variadic")); + return; + } } else if (tld->id == TldIdUsingNamespace) { g->resolve_queue.append(tld); } diff --git a/src/ir.cpp b/src/ir.cpp index e6334d39e..c3a9840ce 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -25375,7 +25375,7 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI case ZigTypeIdBoundFn: case ZigTypeIdStruct: ir_add_error(ira, source_instr, buf_sprintf( - "@Type not availble for 'TypeInfo.%s'", type_id_name(tagTypeId))); + "@Type not available for 'TypeInfo.%s'", type_id_name(tagTypeId))); return ira->codegen->invalid_inst_gen->value->type; } zig_unreachable(); diff --git a/test/compile_errors.zig b/test/compile_errors.zig index cbad5b882..c230e95af 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -2,6 +2,15 @@ const tests = @import("tests.zig"); const std = @import("std"); pub fn addCases(cases: *tests.CompileErrorContext) void { + cases.add("non-extern function with var args", + \\fn foo(args: ...) void {} + \\export fn entry() void { + \\ foo(); + \\} + , &[_][]const u8{ + "tmp.zig:1:1: error: non-extern function is variadic", + }); + cases.addTest("invalid int casts", \\export fn foo() void { \\ var a: u32 = 2; @@ -856,7 +865,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:11:25: error: expected type 'u32', found '@TypeOf(get_uval).ReturnType.ErrorSet!u32'", }); - cases.add("asigning to struct or union fields that are not optionals with a function that returns an optional", + cases.add("assigning to struct or union fields that are not optionals with a function that returns an optional", \\fn maybe(is: bool) ?u8 { \\ if (is) return @as(u8, 10) else return null; \\} @@ -1084,7 +1093,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ _ = @Type(@typeInfo(struct { })); \\} , &[_][]const u8{ - "tmp.zig:2:15: error: @Type not availble for 'TypeInfo.Struct'", + "tmp.zig:2:15: error: @Type not available for 'TypeInfo.Struct'", }); cases.add("wrong type for result ptr to @asyncCall", @@ -2659,7 +2668,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:7:17: error: switch on type 'type' provides no expression parameter", }); - cases.add("function protoype with no body", + cases.add("function prototype with no body", \\fn foo() void; \\export fn entry() void { \\ foo();