From 33e3d5645344d5c8076a1f020d49917334f6902a Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 13 May 2016 13:08:54 -0700 Subject: [PATCH] add error for wrong return type of main --- src/analyze.cpp | 11 +++++++++++ test/run_tests.cpp | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/src/analyze.cpp b/src/analyze.cpp index aa8fe0d060..ee3235d342 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -1560,6 +1560,17 @@ static void preview_fn_proto_instance(CodeGen *g, ImportTableEntry *import, AstN proto_node->data.fn_proto.fn_table_entry = fn_table_entry; resolve_function_proto(g, proto_node, fn_table_entry, import, containing_context); + + if (is_main_fn && !g->link_libc) { + TypeTableEntry *err_void = get_error_type(g, g->builtin_types.entry_void); + TypeTableEntry *actual_return_type = fn_table_entry->type_entry->data.fn.fn_type_id.return_type; + if (actual_return_type != err_void) { + AstNode *return_type_node = fn_table_entry->proto_node->data.fn_proto.return_type; + add_node_error(g, return_type_node, + buf_sprintf("expected return type of main to be '%%void', instead is '%s'", + buf_ptr(&actual_return_type->name))); + } + } } static void preview_fn_proto(CodeGen *g, ImportTableEntry *import, AstNode *proto_node) { diff --git a/test/run_tests.cpp b/test/run_tests.cpp index 1d7e82e0b0..5df005e648 100644 --- a/test/run_tests.cpp +++ b/test/run_tests.cpp @@ -1391,6 +1391,10 @@ fn something() -> %void { } ".tmp_source.zig:3:5: error: %return statement in function with return type 'void'", ".tmp_source.zig:2:8: note: function return type here"); + add_compile_fail_case("wrong return type for main", R"SOURCE( +pub fn main(args: [][]u8) { } + )SOURCE", 1, ".tmp_source.zig:2:27: error: expected return type of main to be '%void', instead is 'void'"); + } //////////////////////////////////////////////////////////////////////////////