authorgravatar for samuel.tebbs@gmail.comSamTebbs33 <samuel.tebbs@gmail.com> 2019-06-08 15:58:11+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-10 00:41:33-04:00
log6c160b8856921e5e1d0a437794b3b7ee7e1a4d0b
tree81021655a5c697e02b2475422139292a60561d06
parent39bc82561a778f14aca69929690e6cb225f79ed4

Add check for null body in if, for and while


2 files changed, 42 insertions(+), 0 deletions(-)

src/parser.cpp+15
...@@ -890,6 +890,11 @@ static AstNode *ast_parse_if_statement(ParseContext *pc) {...@@ -890,6 +890,11 @@ static AstNode *ast_parse_if_statement(ParseContext *pc) {
890 body = ast_parse_assign_expr(pc);890 body = ast_parse_assign_expr(pc);
891 }891 }
892892
893 if (body == nullptr) {
894 Token *tok = eat_token(pc);
895 ast_error(pc, tok, "expected if body, found '%s'", token_name(tok->id));
896 }
897
893 Token *err_payload = nullptr;898 Token *err_payload = nullptr;
894 AstNode *else_body = nullptr;899 AstNode *else_body = nullptr;
895 if (eat_token_if(pc, TokenIdKeywordElse) != nullptr) {900 if (eat_token_if(pc, TokenIdKeywordElse) != nullptr) {
...@@ -994,6 +999,11 @@ static AstNode *ast_parse_for_statement(ParseContext *pc) {...@@ -994,6 +999,11 @@ static AstNode *ast_parse_for_statement(ParseContext *pc) {
994 body = ast_parse_assign_expr(pc);999 body = ast_parse_assign_expr(pc);
995 }1000 }
9961001
1002 if (body == nullptr) {
1003 Token *tok = eat_token(pc);
1004 ast_error(pc, tok, "expected loop body, found '%s'", token_name(tok->id));
1005 }
1006
997 AstNode *else_body = nullptr;1007 AstNode *else_body = nullptr;
998 if (eat_token_if(pc, TokenIdKeywordElse) != nullptr) {1008 if (eat_token_if(pc, TokenIdKeywordElse) != nullptr) {
999 else_body = ast_expect(pc, ast_parse_statement);1009 else_body = ast_expect(pc, ast_parse_statement);
...@@ -1023,6 +1033,11 @@ static AstNode *ast_parse_while_statement(ParseContext *pc) {...@@ -1023,6 +1033,11 @@ static AstNode *ast_parse_while_statement(ParseContext *pc) {
1023 body = ast_parse_assign_expr(pc);1033 body = ast_parse_assign_expr(pc);
1024 }1034 }
10251035
1036 if (body == nullptr) {
1037 Token *tok = eat_token(pc);
1038 ast_error(pc, tok, "expected loop body, found '%s'", token_name(tok->id));
1039 }
1040
1026 Token *err_payload = nullptr;1041 Token *err_payload = nullptr;
1027 AstNode *else_body = nullptr;1042 AstNode *else_body = nullptr;
1028 if (eat_token_if(pc, TokenIdKeywordElse) != nullptr) {1043 if (eat_token_if(pc, TokenIdKeywordElse) != nullptr) {
test/compile_errors.zig+27
...@@ -230,6 +230,33 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -230,6 +230,33 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
230 "tmp.zig:10:25: error: expression value is ignored",230 "tmp.zig:10:25: error: expression value is ignored",
231 );231 );
232232
233 cases.add(
234 "empty while loop body",
235 \\export fn a() void {
236 \\ while(true);
237 \\}
238 ,
239 "tmp.zig:2:16: error: expected loop body, found ';'",
240 );
241
242 cases.add(
243 "empty for loop body",
244 \\export fn a() void {
245 \\ for(undefined) |x|;
246 \\}
247 ,
248 "tmp.zig:2:23: error: expected loop body, found ';'",
249 );
250
251 cases.add(
252 "empty if body",
253 \\export fn a() void {
254 \\ if(true);
255 \\}
256 ,
257 "tmp.zig:2:13: error: expected if body, found ';'",
258 );
259
233 cases.add(260 cases.add(
234 "import outside package path",261 "import outside package path",
235 \\comptime{262 \\comptime{