authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-04 14:33:57-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-04 14:33:57-07:00
logc7615c1a8055effe5e29bc2329278ca41a258804
tree6705ba3000e3816ad1b8f29f466a152f881c1723
parent139e5ca08f3b15cf900be7096d7be0db4e0eced8

error for extern function with void parameter


4 files changed, 27 insertions(+), 18 deletions(-)

README.md-1
...@@ -43,7 +43,6 @@ make...@@ -43,7 +43,6 @@ make
43## Roadmap43## Roadmap
4444
45 * parseh: unreachable <--> noreturn attribute45 * parseh: unreachable <--> noreturn attribute
46 * error for extern function with void parameter
47 * unused label error46 * unused label error
48 * loops47 * loops
49 * structs48 * structs
src/analyze.cpp+14-10
...@@ -115,7 +115,16 @@ static void resolve_function_proto(CodeGen *g, AstNode *node, FnTableEntry *fn_t...@@ -115,7 +115,16 @@ static void resolve_function_proto(CodeGen *g, AstNode *node, FnTableEntry *fn_t
115 for (int i = 0; i < node->data.fn_proto.params.length; i += 1) {115 for (int i = 0; i < node->data.fn_proto.params.length; i += 1) {
116 AstNode *child = node->data.fn_proto.params.at(i);116 AstNode *child = node->data.fn_proto.params.at(i);
117 assert(child->type == NodeTypeParamDecl);117 assert(child->type == NodeTypeParamDecl);
118 resolve_type(g, child->data.param_decl.type);118 TypeTableEntry *type_entry = resolve_type(g, child->data.param_decl.type);
119 if (type_entry == g->builtin_types.entry_unreachable) {
120 add_node_error(g, child->data.param_decl.type,
121 buf_sprintf("parameter of type 'unreachable' not allowed"));
122 } else if (type_entry == g->builtin_types.entry_void) {
123 if (node->data.fn_proto.visib_mod == FnProtoVisibModExport) {
124 add_node_error(g, child->data.param_decl.type,
125 buf_sprintf("parameter of type 'void' not allowed on exported functions"));
126 }
127 }
119 }128 }
120129
121 resolve_type(g, node->data.fn_proto.return_type);130 resolve_type(g, node->data.fn_proto.return_type);
...@@ -423,18 +432,18 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,...@@ -423,18 +432,18 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
423 resolve_type(g, variable_declaration->type) : nullptr;432 resolve_type(g, variable_declaration->type) : nullptr;
424 if (explicit_type == g->builtin_types.entry_unreachable) {433 if (explicit_type == g->builtin_types.entry_unreachable) {
425 add_node_error(g, variable_declaration->type,434 add_node_error(g, variable_declaration->type,
426 buf_sprintf("variable of type 'unreachable' is not allowed."));435 buf_sprintf("variable of type 'unreachable' not allowed"));
427 }436 }
428437
429 TypeTableEntry *implicit_type = variable_declaration->expr != nullptr ?438 TypeTableEntry *implicit_type = variable_declaration->expr != nullptr ?
430 analyze_expression(g, import, context, explicit_type, variable_declaration->expr) : nullptr;439 analyze_expression(g, import, context, explicit_type, variable_declaration->expr) : nullptr;
431 if (implicit_type == g->builtin_types.entry_unreachable) {440 if (implicit_type == g->builtin_types.entry_unreachable) {
432 add_node_error(g, node,441 add_node_error(g, node,
433 buf_sprintf("variable initialization is unreachable."));442 buf_sprintf("variable initialization is unreachable"));
434 }443 }
435444
436 if (implicit_type == nullptr) {445 if (implicit_type == nullptr) {
437 add_node_error(g, node, buf_sprintf("initial values are required for variable declaration."));446 add_node_error(g, node, buf_sprintf("initial values are required for variable declaration"));
438 }447 }
439448
440 TypeTableEntry *type = explicit_type != nullptr ? explicit_type : implicit_type;449 TypeTableEntry *type = explicit_type != nullptr ? explicit_type : implicit_type;
...@@ -443,7 +452,7 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,...@@ -443,7 +452,7 @@ static TypeTableEntry * analyze_expression(CodeGen *g, ImportTableEntry *import,
443 LocalVariableTableEntry *existing_variable = find_local_variable(context, &variable_declaration->symbol);452 LocalVariableTableEntry *existing_variable = find_local_variable(context, &variable_declaration->symbol);
444 if (existing_variable) {453 if (existing_variable) {
445 add_node_error(g, node,454 add_node_error(g, node,
446 buf_sprintf("redeclaration of variable '%s'.", buf_ptr(&variable_declaration->symbol)));455 buf_sprintf("redeclaration of variable '%s'", buf_ptr(&variable_declaration->symbol)));
447 } else {456 } else {
448 LocalVariableTableEntry *variable_entry = allocate<LocalVariableTableEntry>(1);457 LocalVariableTableEntry *variable_entry = allocate<LocalVariableTableEntry>(1);
449 buf_init_from_buf(&variable_entry->name, &variable_declaration->symbol);458 buf_init_from_buf(&variable_entry->name, &variable_declaration->symbol);
...@@ -723,11 +732,6 @@ static void analyze_top_level_declaration(CodeGen *g, ImportTableEntry *import,...@@ -723,11 +732,6 @@ static void analyze_top_level_declaration(CodeGen *g, ImportTableEntry *import,
723 assert(param_decl->type->type == NodeTypeType);732 assert(param_decl->type->type == NodeTypeType);
724 TypeTableEntry *type = param_decl->type->codegen_node->data.type_node.entry;733 TypeTableEntry *type = param_decl->type->codegen_node->data.type_node.entry;
725734
726 if (type == g->builtin_types.entry_unreachable) {
727 add_node_error(g, param_decl->type,
728 buf_sprintf("parameter of type 'unreachable' is not allowed."));
729 }
730
731 LocalVariableTableEntry *variable_entry = allocate<LocalVariableTableEntry>(1);735 LocalVariableTableEntry *variable_entry = allocate<LocalVariableTableEntry>(1);
732 buf_init_from_buf(&variable_entry->name, &param_decl->name);736 buf_init_from_buf(&variable_entry->name, &param_decl->name);
733 variable_entry->type = type;737 variable_entry->type = type;
src/errmsg.cpp+3-1
...@@ -19,7 +19,9 @@ void print_err_msg(ErrorMsg *err, ErrColor color) {...@@ -19,7 +19,9 @@ void print_err_msg(ErrorMsg *err, ErrColor color) {
19 assert(err->line_offsets);19 assert(err->line_offsets);
2020
21 int line_start_offset = err->line_offsets->at(err->line_start);21 int line_start_offset = err->line_offsets->at(err->line_start);
22 int line_end_offset = err->line_offsets->at(err->line_start + 1);22 int end_line = err->line_start + 1;
23 int line_end_offset = (end_line >= err->line_offsets->length) ?
24 buf_len(err->source) : err->line_offsets->at(err->line_start + 1);
2325
24 fwrite(buf_ptr(err->source) + line_start_offset, 1, line_end_offset - line_start_offset - 1, stderr);26 fwrite(buf_ptr(err->source) + line_start_offset, 1, line_end_offset - line_start_offset - 1, stderr);
25 fprintf(stderr, "\n");27 fprintf(stderr, "\n");
test/run_tests.cpp+10-6
...@@ -418,20 +418,20 @@ fn b() {}...@@ -418,20 +418,20 @@ fn b() {}
418 add_compile_fail_case("parameter redeclaration", R"SOURCE(418 add_compile_fail_case("parameter redeclaration", R"SOURCE(
419fn f(a : i32, a : i32) {419fn f(a : i32, a : i32) {
420}420}
421 )SOURCE", 1, ".tmp_source.zig:2:1: error: redeclaration of parameter 'a'.");421 )SOURCE", 1, ".tmp_source.zig:2:1: error: redeclaration of parameter 'a'");
422422
423 add_compile_fail_case("local variable redeclaration", R"SOURCE(423 add_compile_fail_case("local variable redeclaration", R"SOURCE(
424fn f() {424fn f() {
425 let a : i32 = 0;425 let a : i32 = 0;
426 let a = 0;426 let a = 0;
427}427}
428 )SOURCE", 1, ".tmp_source.zig:4:5: error: redeclaration of variable 'a'.");428 )SOURCE", 1, ".tmp_source.zig:4:5: error: redeclaration of variable 'a'");
429429
430 add_compile_fail_case("local variable redeclares parameter", R"SOURCE(430 add_compile_fail_case("local variable redeclares parameter", R"SOURCE(
431fn f(a : i32) {431fn f(a : i32) {
432 let a = 0;432 let a = 0;
433}433}
434 )SOURCE", 1, ".tmp_source.zig:3:5: error: redeclaration of variable 'a'.");434 )SOURCE", 1, ".tmp_source.zig:3:5: error: redeclaration of variable 'a'");
435435
436 add_compile_fail_case("variable has wrong type", R"SOURCE(436 add_compile_fail_case("variable has wrong type", R"SOURCE(
437fn f() -> i32 {437fn f() -> i32 {
...@@ -450,17 +450,21 @@ fn f() {...@@ -450,17 +450,21 @@ fn f() {
450fn f() {450fn f() {
451 let a = return;451 let a = return;
452}452}
453 )SOURCE", 1, ".tmp_source.zig:3:5: error: variable initialization is unreachable.");453 )SOURCE", 1, ".tmp_source.zig:3:5: error: variable initialization is unreachable");
454454
455 add_compile_fail_case("unreachable variable", R"SOURCE(455 add_compile_fail_case("unreachable variable", R"SOURCE(
456fn f() {456fn f() {
457 let a : unreachable = return;457 let a : unreachable = return;
458}458}
459 )SOURCE", 1, ".tmp_source.zig:3:13: error: variable of type 'unreachable' is not allowed.");459 )SOURCE", 1, ".tmp_source.zig:3:13: error: variable of type 'unreachable' not allowed");
460460
461 add_compile_fail_case("unreachable parameter", R"SOURCE(461 add_compile_fail_case("unreachable parameter", R"SOURCE(
462fn f(a : unreachable) {}462fn f(a : unreachable) {}
463 )SOURCE", 1, ".tmp_source.zig:2:10: error: parameter of type 'unreachable' is not allowed.");463 )SOURCE", 1, ".tmp_source.zig:2:10: error: parameter of type 'unreachable' not allowed");
464
465 add_compile_fail_case("exporting a void parameter", R"SOURCE(
466export fn f(a : void) {}
467 )SOURCE", 1, ".tmp_source.zig:2:17: error: parameter of type 'void' not allowed on exported functions");
464468
465}469}
466470