Commit 901b5c1566

Andrew Kelley <superjoe30@gmail.com>
2018-08-28 21:39:32
add compile error for function prototype with no body
closes #1231
1 parent 09cc1dc
Changed files (2)
src/analyze.cpp
@@ -3242,6 +3242,13 @@ static void add_top_level_decl(CodeGen *g, ScopeDecls *decls_scope, Tld *tld) {
     } else if (tld->id == TldIdFn) {
         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 &&
+            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 (is_export) {
         g->resolve_queue.append(tld);
test/compile_errors.zig
@@ -1,6 +1,16 @@
 const tests = @import("tests.zig");
 
 pub fn addCases(cases: *tests.CompileErrorContext) void {
+    cases.add(
+        "function protoype with no body",
+        \\fn foo() void;
+        \\export fn entry() void {
+        \\    foo();
+        \\}
+    ,
+        ".tmp_source.zig:1:1: error: non-extern function has no body",
+    );
+
     cases.add(
         "@typeInfo causing depend on itself compile error",
         \\const start = struct {