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) {
890890 body = ast_parse_assign_expr(pc);
891891 }
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
893898 Token *err_payload = nullptr;
894899 AstNode *else_body = nullptr;
895900 if (eat_token_if(pc, TokenIdKeywordElse) != nullptr) {
......@@ -994,6 +999,11 @@ static AstNode *ast_parse_for_statement(ParseContext *pc) {
994999 body = ast_parse_assign_expr(pc);
9951000 }
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
9971007 AstNode *else_body = nullptr;
9981008 if (eat_token_if(pc, TokenIdKeywordElse) != nullptr) {
9991009 else_body = ast_expect(pc, ast_parse_statement);
......@@ -1023,6 +1033,11 @@ static AstNode *ast_parse_while_statement(ParseContext *pc) {
10231033 body = ast_parse_assign_expr(pc);
10241034 }
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
10261041 Token *err_payload = nullptr;
10271042 AstNode *else_body = nullptr;
10281043 if (eat_token_if(pc, TokenIdKeywordElse) != nullptr) {
test/compile_errors.zig+27
......@@ -230,6 +230,33 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
230230 "tmp.zig:10:25: error: expression value is ignored",
231231 );
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
233260 cases.add(
234261 "import outside package path",
235262 \\comptime{