diff options
| -rw-r--r-- | src/parser.cpp | 15 | ||||
| -rw-r--r-- | test/compile_errors.zig | 27 |
2 files changed, 42 insertions, 0 deletions
diff --git a/src/parser.cpp b/src/parser.cpp index 89836f9bc8..af98a4be93 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -890,6 +890,11 @@ static AstNode *ast_parse_if_statement(ParseContext *pc) { body = ast_parse_assign_expr(pc); } + if (body == nullptr) { + Token *tok = eat_token(pc); + ast_error(pc, tok, "expected if body, found '%s'", token_name(tok->id)); + } + Token *err_payload = nullptr; AstNode *else_body = nullptr; if (eat_token_if(pc, TokenIdKeywordElse) != nullptr) { @@ -994,6 +999,11 @@ static AstNode *ast_parse_for_statement(ParseContext *pc) { body = ast_parse_assign_expr(pc); } + if (body == nullptr) { + Token *tok = eat_token(pc); + ast_error(pc, tok, "expected loop body, found '%s'", token_name(tok->id)); + } + AstNode *else_body = nullptr; if (eat_token_if(pc, TokenIdKeywordElse) != nullptr) { else_body = ast_expect(pc, ast_parse_statement); @@ -1023,6 +1033,11 @@ static AstNode *ast_parse_while_statement(ParseContext *pc) { body = ast_parse_assign_expr(pc); } + if (body == nullptr) { + Token *tok = eat_token(pc); + ast_error(pc, tok, "expected loop body, found '%s'", token_name(tok->id)); + } + Token *err_payload = nullptr; AstNode *else_body = nullptr; if (eat_token_if(pc, TokenIdKeywordElse) != nullptr) { diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 15ac7d6f1b..7896a0b732 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -231,6 +231,33 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { ); cases.add( + "empty while loop body", + \\export fn a() void { + \\ while(true); + \\} + , + "tmp.zig:2:16: error: expected loop body, found ';'", + ); + + cases.add( + "empty for loop body", + \\export fn a() void { + \\ for(undefined) |x|; + \\} + , + "tmp.zig:2:23: error: expected loop body, found ';'", + ); + + cases.add( + "empty if body", + \\export fn a() void { + \\ if(true); + \\} + , + "tmp.zig:2:13: error: expected if body, found ';'", + ); + + cases.add( "import outside package path", \\comptime{ \\ _ = @import("../a.zig"); |
