authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-05-03 17:23:11-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-05-03 17:23:11-04:00
log644ea2dde9fbb1f948cf12115df2a15e908f3c29
tree7d246fb67e4ffeadff496779a4ac4243fbbc46bc
parent0940d46c0160683fb5a28f66589e53ec5b64241d

remove test and try expressions in favor of if expressions

See #357

19 files changed, 148 insertions(+), 226 deletions(-)

doc/langref.md+2-2
......@@ -91,9 +91,9 @@ Defer(body) = option("%") "defer" body
9191
9292IfExpression(body) = "if" "(" Expression ")" body option("else" BlockExpression(body))
9393
94TryExpression(body) = "try" "(" Expression ")" option("|" option("*") Symbol "|") body option("else" option("|" Symbol "|") BlockExpression(body))
94TryExpression(body) = "if" "(" Expression ")" option("|" option("*") Symbol "|") body "else" "|" Symbol "|" BlockExpression(body)
9595
96TestExpression(body) = "test" "(" Expression ")" option("|" option("*") Symbol "|") body option("else" option("|" Symbol "|") BlockExpression(body))
96TestExpression(body) = "if" "(" Expression ")" option("|" option("*") Symbol "|") body option("else" BlockExpression(body))
9797
9898BoolAndExpression = ComparisonExpression "and" BoolAndExpression | ComparisonExpression
9999
src/ast_render.cpp+2-2
......@@ -763,7 +763,7 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
763763 }
764764 case NodeTypeTryExpr:
765765 {
766 fprintf(ar->f, "try (");
766 fprintf(ar->f, "if (");
767767 render_node_grouped(ar, node->data.try_expr.target_node);
768768 fprintf(ar->f, ") ");
769769 if (node->data.try_expr.var_symbol) {
......@@ -783,7 +783,7 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
783783 }
784784 case NodeTypeTestExpr:
785785 {
786 fprintf(ar->f, "test (");
786 fprintf(ar->f, "if (");
787787 render_node_grouped(ar, node->data.test_expr.target_node);
788788 fprintf(ar->f, ") ");
789789 if (node->data.test_expr.var_symbol) {
src/parser.cpp+66-123
......@@ -215,7 +215,7 @@ static AstNode *ast_parse_block_or_expression(ParseContext *pc, size_t *token_in
215215static AstNode *ast_parse_block_expr_or_expression(ParseContext *pc, size_t *token_index, bool mandatory);
216216static AstNode *ast_parse_expression(ParseContext *pc, size_t *token_index, bool mandatory);
217217static AstNode *ast_parse_block(ParseContext *pc, size_t *token_index, bool mandatory);
218static AstNode *ast_parse_if_expr(ParseContext *pc, size_t *token_index, bool mandatory);
218static AstNode *ast_parse_if_try_test_expr(ParseContext *pc, size_t *token_index, bool mandatory);
219219static AstNode *ast_parse_block_expr(ParseContext *pc, size_t *token_index, bool mandatory);
220220static AstNode *ast_parse_unwrap_expr(ParseContext *pc, size_t *token_index, bool mandatory);
221221static AstNode *ast_parse_prefix_op_expr(ParseContext *pc, size_t *token_index, bool mandatory);
......@@ -639,110 +639,6 @@ static AstNode *ast_parse_comptime_expr(ParseContext *pc, size_t *token_index, b
639639 return node;
640640}
641641
642/*
643TryExpression(body) = "try" "(" Expression ")" option("|" option("*") Symbol "|") body option("else" option("|" Symbol "|") BlockExpression(body))
644*/
645static AstNode *ast_parse_try_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
646 Token *try_token = &pc->tokens->at(*token_index);
647 if (try_token->id == TokenIdKeywordTry) {
648 *token_index += 1;
649 } else if (mandatory) {
650 ast_expect_token(pc, try_token, TokenIdKeywordTry);
651 zig_unreachable();
652 } else {
653 return nullptr;
654 }
655
656 AstNode *node = ast_create_node(pc, NodeTypeTryExpr, try_token);
657
658 ast_eat_token(pc, token_index, TokenIdLParen);
659 node->data.try_expr.target_node = ast_parse_expression(pc, token_index, true);
660 ast_eat_token(pc, token_index, TokenIdRParen);
661
662 Token *open_bar_tok = &pc->tokens->at(*token_index);
663 if (open_bar_tok->id == TokenIdBinOr) {
664 *token_index += 1;
665
666 Token *star_tok = &pc->tokens->at(*token_index);
667 if (star_tok->id == TokenIdStar) {
668 *token_index += 1;
669 node->data.try_expr.var_is_ptr = true;
670 }
671
672 Token *var_name_tok = ast_eat_token(pc, token_index, TokenIdSymbol);
673 node->data.try_expr.var_symbol = token_buf(var_name_tok);
674
675 ast_eat_token(pc, token_index, TokenIdBinOr);
676 }
677
678 node->data.try_expr.then_node = ast_parse_block_or_expression(pc, token_index, true);
679
680 Token *else_token = &pc->tokens->at(*token_index);
681 if (else_token->id == TokenIdKeywordElse) {
682 *token_index += 1;
683 Token *open_bar_tok = &pc->tokens->at(*token_index);
684 if (open_bar_tok->id == TokenIdBinOr) {
685 *token_index += 1;
686
687 Token *err_name_tok = ast_eat_token(pc, token_index, TokenIdSymbol);
688 node->data.try_expr.err_symbol = token_buf(err_name_tok);
689
690 ast_eat_token(pc, token_index, TokenIdBinOr);
691 }
692
693 node->data.try_expr.else_node = ast_parse_block_expr_or_expression(pc, token_index, true);
694 }
695
696 return node;
697}
698
699/*
700TestExpression(body) = "test" "(" Expression ")" option("|" option("*") Symbol "|") body option("else" BlockExpression(body))
701*/
702static AstNode *ast_parse_test_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
703 Token *test_token = &pc->tokens->at(*token_index);
704 if (test_token->id == TokenIdKeywordTest) {
705 *token_index += 1;
706 } else if (mandatory) {
707 ast_expect_token(pc, test_token, TokenIdKeywordTest);
708 zig_unreachable();
709 } else {
710 return nullptr;
711 }
712
713 AstNode *node = ast_create_node(pc, NodeTypeTestExpr, test_token);
714
715 ast_eat_token(pc, token_index, TokenIdLParen);
716 node->data.test_expr.target_node = ast_parse_expression(pc, token_index, true);
717 ast_eat_token(pc, token_index, TokenIdRParen);
718
719 Token *open_bar_tok = &pc->tokens->at(*token_index);
720 if (open_bar_tok->id == TokenIdBinOr) {
721 *token_index += 1;
722
723 Token *star_tok = &pc->tokens->at(*token_index);
724 if (star_tok->id == TokenIdStar) {
725 *token_index += 1;
726 node->data.test_expr.var_is_ptr = true;
727 }
728
729 Token *var_name_tok = ast_eat_token(pc, token_index, TokenIdSymbol);
730 node->data.test_expr.var_symbol = token_buf(var_name_tok);
731
732 ast_eat_token(pc, token_index, TokenIdBinOr);
733 }
734
735 node->data.test_expr.then_node = ast_parse_block_or_expression(pc, token_index, true);
736
737 Token *else_token = &pc->tokens->at(*token_index);
738 if (else_token->id == TokenIdKeywordElse) {
739 *token_index += 1;
740 node->data.test_expr.else_node = ast_parse_block_expr_or_expression(pc, token_index, true);
741 }
742
743 return node;
744}
745
746642/*
747643PrimaryExpression = Number | String | CharLiteral | KeywordLiteral | GroupedExpression | GotoExpression | BlockExpression(BlockOrExpression) | Symbol | ("@" Symbol FnCallExpression) | ArrayType | (option("extern") FnProto) | AsmExpression | ("error" "." Symbol) | ContainerDecl
748644KeywordLiteral = "true" | "false" | "null" | "break" | "continue" | "undefined" | "error" | "this" | "unreachable"
......@@ -1434,8 +1330,10 @@ static AstNode *ast_parse_bool_and_expr(ParseContext *pc, size_t *token_index, b
14341330
14351331/*
14361332IfExpression(body) = "if" "(" Expression ")" body option("else" BlockExpression(body))
1333TryExpression(body) = "if" "(" Expression ")" option("|" option("*") Symbol "|") body "else" "|" Symbol "|" BlockExpression(body)
1334TestExpression(body) = "if" "(" Expression ")" "|" option("*") Symbol "|" body option("else" BlockExpression(body))
14371335*/
1438static AstNode *ast_parse_if_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
1336static AstNode *ast_parse_if_try_test_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
14391337 Token *if_token = &pc->tokens->at(*token_index);
14401338
14411339 if (if_token->id == TokenIdKeywordIf) {
......@@ -1448,19 +1346,72 @@ static AstNode *ast_parse_if_expr(ParseContext *pc, size_t *token_index, bool ma
14481346 }
14491347
14501348 ast_eat_token(pc, token_index, TokenIdLParen);
1451
1452 AstNode *node = ast_create_node(pc, NodeTypeIfBoolExpr, if_token);
1453 node->data.if_bool_expr.condition = ast_parse_expression(pc, token_index, true);
1349 AstNode *condition = ast_parse_expression(pc, token_index, true);
14541350 ast_eat_token(pc, token_index, TokenIdRParen);
1455 node->data.if_bool_expr.then_block = ast_parse_block_or_expression(pc, token_index, true);
14561351
1457 Token *else_token = &pc->tokens->at(*token_index);
1458 if (else_token->id == TokenIdKeywordElse) {
1352 Token *open_bar_tok = &pc->tokens->at(*token_index);
1353 Token *var_name_tok = nullptr;
1354 bool var_is_ptr = false;
1355 if (open_bar_tok->id == TokenIdBinOr) {
1356 *token_index += 1;
1357
1358 Token *star_tok = &pc->tokens->at(*token_index);
1359 if (star_tok->id == TokenIdStar) {
1360 *token_index += 1;
1361 var_is_ptr = true;
1362 }
1363
1364 var_name_tok = ast_eat_token(pc, token_index, TokenIdSymbol);
1365
1366 ast_eat_token(pc, token_index, TokenIdBinOr);
1367 }
1368
1369 AstNode *body_node = ast_parse_block_or_expression(pc, token_index, true);
1370
1371 Token *else_tok = &pc->tokens->at(*token_index);
1372 AstNode *else_node = nullptr;
1373 Token *err_name_tok = nullptr;
1374 if (else_tok->id == TokenIdKeywordElse) {
14591375 *token_index += 1;
1460 node->data.if_bool_expr.else_node = ast_parse_block_expr_or_expression(pc, token_index, true);
1376
1377 Token *else_bar_tok = &pc->tokens->at(*token_index);
1378 if (else_bar_tok->id == TokenIdBinOr) {
1379 *token_index += 1;
1380
1381 err_name_tok = ast_eat_token(pc, token_index, TokenIdSymbol);
1382
1383 ast_eat_token(pc, token_index, TokenIdBinOr);
1384 }
1385
1386 else_node = ast_parse_block_expr_or_expression(pc, token_index, true);
14611387 }
14621388
1463 return node;
1389 if (err_name_tok != nullptr) {
1390 AstNode *node = ast_create_node(pc, NodeTypeTryExpr, if_token);
1391 node->data.try_expr.target_node = condition;
1392 node->data.try_expr.var_is_ptr = var_is_ptr;
1393 if (var_name_tok != nullptr) {
1394 node->data.try_expr.var_symbol = token_buf(var_name_tok);
1395 }
1396 node->data.try_expr.then_node = body_node;
1397 node->data.try_expr.err_symbol = token_buf(err_name_tok);
1398 node->data.try_expr.else_node = else_node;
1399 return node;
1400 } else if (var_name_tok != nullptr) {
1401 AstNode *node = ast_create_node(pc, NodeTypeTestExpr, if_token);
1402 node->data.test_expr.target_node = condition;
1403 node->data.test_expr.var_is_ptr = var_is_ptr;
1404 node->data.test_expr.var_symbol = token_buf(var_name_tok);
1405 node->data.test_expr.then_node = body_node;
1406 node->data.test_expr.else_node = else_node;
1407 return node;
1408 } else {
1409 AstNode *node = ast_create_node(pc, NodeTypeIfBoolExpr, if_token);
1410 node->data.if_bool_expr.condition = condition;
1411 node->data.if_bool_expr.then_block = body_node;
1412 node->data.if_bool_expr.else_node = else_node;
1413 return node;
1414 }
14641415}
14651416
14661417/*
......@@ -1848,7 +1799,7 @@ BlockExpression(body) = Block | IfExpression(body) | TryExpression(body) | TestE
18481799static AstNode *ast_parse_block_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
18491800 Token *token = &pc->tokens->at(*token_index);
18501801
1851 AstNode *if_expr = ast_parse_if_expr(pc, token_index, false);
1802 AstNode *if_expr = ast_parse_if_try_test_expr(pc, token_index, false);
18521803 if (if_expr)
18531804 return if_expr;
18541805
......@@ -1872,14 +1823,6 @@ static AstNode *ast_parse_block_expr(ParseContext *pc, size_t *token_index, bool
18721823 if (comptime_node)
18731824 return comptime_node;
18741825
1875 AstNode *try_node = ast_parse_try_expr(pc, token_index, false);
1876 if (try_node)
1877 return try_node;
1878
1879 AstNode *test_node = ast_parse_test_expr(pc, token_index, false);
1880 if (test_node)
1881 return test_node;
1882
18831826 if (mandatory)
18841827 ast_invalid_token_error(pc, token);
18851828
src/tokenizer.cpp-2
......@@ -138,7 +138,6 @@ static const struct ZigKeyword zig_keywords[] = {
138138 {"test", TokenIdKeywordTest},
139139 {"this", TokenIdKeywordThis},
140140 {"true", TokenIdKeywordTrue},
141 {"try", TokenIdKeywordTry},
142141 {"undefined", TokenIdKeywordUndefined},
143142 {"union", TokenIdKeywordUnion},
144143 {"unreachable", TokenIdKeywordUnreachable},
......@@ -1472,7 +1471,6 @@ const char * token_name(TokenId id) {
14721471 case TokenIdKeywordTest: return "test";
14731472 case TokenIdKeywordThis: return "this";
14741473 case TokenIdKeywordTrue: return "true";
1475 case TokenIdKeywordTry: return "try";
14761474 case TokenIdKeywordUndefined: return "undefined";
14771475 case TokenIdKeywordUnion: return "union";
14781476 case TokenIdKeywordUnreachable: return "unreachable";
src/tokenizer.hpp-1
......@@ -75,7 +75,6 @@ enum TokenId {
7575 TokenIdKeywordTest,
7676 TokenIdKeywordThis,
7777 TokenIdKeywordTrue,
78 TokenIdKeywordTry,
7978 TokenIdKeywordUndefined,
8079 TokenIdKeywordUnion,
8180 TokenIdKeywordUnreachable,
std/buf_map.zig+1-1
......@@ -28,7 +28,7 @@ pub const BufMap = struct {
2828 }
2929
3030 pub fn set(self: &BufMap, key: []const u8, value: []const u8) -> %void {
31 test (self.hash_map.get(key)) |entry| {
31 if (self.hash_map.get(key)) |entry| {
3232 const value_copy = %return self.copy(value);
3333 %defer self.free(value_copy);
3434 _ = %return self.hash_map.put(key, value_copy);
std/build.zig+13-13
......@@ -306,7 +306,7 @@ pub const Builder = struct {
306306 }
307307
308308 fn processNixOSEnvVars(self: &Builder) {
309 test (os.getEnv("NIX_CFLAGS_COMPILE")) |nix_cflags_compile| {
309 if (os.getEnv("NIX_CFLAGS_COMPILE")) |nix_cflags_compile| {
310310 var it = mem.split(nix_cflags_compile, ' ');
311311 while (true) {
312312 const word = it.next() ?? break;
......@@ -322,7 +322,7 @@ pub const Builder = struct {
322322 }
323323 }
324324 }
325 test (os.getEnv("NIX_LDFLAGS")) |nix_ldflags| {
325 if (os.getEnv("NIX_LDFLAGS")) |nix_ldflags| {
326326 var it = mem.split(nix_ldflags, ' ');
327327 while (true) {
328328 const word = it.next() ?? break;
......@@ -350,7 +350,7 @@ pub const Builder = struct {
350350 .type_id = type_id,
351351 .description = description,
352352 };
353 test (%%self.available_options_map.put(name, available_option)) {
353 if (%%self.available_options_map.put(name, available_option) != null) {
354354 debug.panic("Option '{}' declared twice", name);
355355 }
356356 %%self.available_options_list.append(available_option);
......@@ -424,7 +424,7 @@ pub const Builder = struct {
424424 }
425425
426426 pub fn addUserInputOption(self: &Builder, name: []const u8, value: []const u8) -> bool {
427 test (%%self.user_input_options.put(name, UserInputOption {
427 if (%%self.user_input_options.put(name, UserInputOption {
428428 .name = name,
429429 .value = UserValue.Scalar { value },
430430 .used = false,
......@@ -461,7 +461,7 @@ pub const Builder = struct {
461461 }
462462
463463 pub fn addUserInputFlag(self: &Builder, name: []const u8) -> bool {
464 test (%%self.user_input_options.put(name, UserInputOption {
464 if (%%self.user_input_options.put(name, UserInputOption {
465465 .name = name,
466466 .value = UserValue.Flag,
467467 .used = false,
......@@ -530,7 +530,7 @@ pub const Builder = struct {
530530 exe_path: []const u8, args: []const []const u8) -> %void
531531 {
532532 if (self.verbose) {
533 test (cwd) |yes_cwd| %%io.stderr.print("cd {}; ", yes_cwd);
533 if (cwd) |yes_cwd| %%io.stderr.print("cd {}; ", yes_cwd);
534534 %%io.stderr.print("{}", exe_path);
535535 for (args) |arg| {
536536 %%io.stderr.print(" {}", arg);
......@@ -821,7 +821,7 @@ pub const LibExeObjStep = struct {
821821 }
822822
823823 pub fn getOutputPath(self: &LibExeObjStep) -> []const u8 {
824 test (self.output_path) |output_path| {
824 if (self.output_path) |output_path| {
825825 output_path
826826 } else {
827827 %%os.path.join(self.builder.allocator, self.builder.cache_root, self.out_filename)
......@@ -833,7 +833,7 @@ pub const LibExeObjStep = struct {
833833 }
834834
835835 pub fn getOutputHPath(self: &LibExeObjStep) -> []const u8 {
836 test (self.output_h_path) |output_h_path| {
836 if (self.output_h_path) |output_h_path| {
837837 output_h_path
838838 } else {
839839 %%os.path.join(self.builder.allocator, self.builder.cache_root, self.out_h_filename)
......@@ -885,7 +885,7 @@ pub const LibExeObjStep = struct {
885885 };
886886 %%zig_args.append(cmd);
887887
888 test (self.root_src) |root_src| {
888 if (self.root_src) |root_src| {
889889 %%zig_args.append(builder.pathFromRoot(root_src));
890890 }
891891
......@@ -950,7 +950,7 @@ pub const LibExeObjStep = struct {
950950 },
951951 }
952952
953 test (self.linker_script) |linker_script| {
953 if (self.linker_script) |linker_script| {
954954 %%zig_args.append("--linker-script");
955955 %%zig_args.append(linker_script);
956956 }
......@@ -1059,7 +1059,7 @@ pub const TestStep = struct {
10591059 builtin.Mode.ReleaseFast => %%zig_args.append("--release-fast"),
10601060 }
10611061
1062 test (self.filter) |filter| {
1062 if (self.filter) |filter| {
10631063 %%zig_args.append("--test-filter");
10641064 %%zig_args.append(filter);
10651065 }
......@@ -1203,7 +1203,7 @@ pub const CLibExeObjStep = struct {
12031203 }
12041204
12051205 pub fn getOutputPath(self: &CLibExeObjStep) -> []const u8 {
1206 test (self.output_path) |output_path| {
1206 if (self.output_path) |output_path| {
12071207 output_path
12081208 } else {
12091209 %%os.path.join(self.builder.allocator, self.builder.cache_root, self.out_filename)
......@@ -1492,7 +1492,7 @@ pub const CommandStep = struct {
14921492 fn make(step: &Step) -> %void {
14931493 const self = @fieldParentPtr(CommandStep, "step", step);
14941494
1495 const cwd = test (self.cwd) |cwd| self.builder.pathFromRoot(cwd) else null;
1495 const cwd = if (self.cwd) |cwd| self.builder.pathFromRoot(cwd) else null;
14961496 return self.builder.spawnChildEnvMap(cwd, self.env_map, self.exe_path, self.args);
14971497 }
14981498};
std/debug.zig+8-8
......@@ -98,13 +98,13 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty
9898 continue;
9999 };
100100 const compile_unit_name = %return compile_unit.die.getAttrString(st, DW.AT_name);
101 try (getLineNumberInfo(st, compile_unit, usize(return_address) - 1)) |line_info| {
101 if (getLineNumberInfo(st, compile_unit, usize(return_address) - 1)) |line_info| {
102102 defer line_info.deinit();
103103 %return out_stream.print(WHITE ++ "{}:{}:{}" ++ RESET ++ ": " ++
104104 DIM ++ ptr_hex ++ " in ??? ({})" ++ RESET ++ "\n",
105105 line_info.file_name, line_info.line, line_info.column,
106106 return_address, compile_unit_name);
107 try (printLineFromFile(st.allocator(), out_stream, line_info)) {
107 if (printLineFromFile(st.allocator(), out_stream, line_info)) {
108108 if (line_info.column == 0) {
109109 %return out_stream.write("\n");
110110 } else {
......@@ -679,7 +679,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
679679 DW.LNE_end_sequence => {
680680 //%%io.stdout.printf(" [0x{x8}] End Sequence\n", pos);
681681 prog.end_sequence = true;
682 test (%return prog.checkLineMatch()) |info| return info;
682 if (%return prog.checkLineMatch()) |info| return info;
683683 return error.MissingDebugInfo;
684684 },
685685 DW.LNE_set_address => {
......@@ -717,14 +717,14 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
717717 //%%io.stdout.printf(
718718 // " [0x{x8}] Special opcode {}: advance Address by {} to 0x{x} and Line by {} to {}\n",
719719 // pos, adjusted_opcode, inc_addr, prog.address, inc_line, prog.line);
720 test (%return prog.checkLineMatch()) |info| return info;
720 if (%return prog.checkLineMatch()) |info| return info;
721721 prog.basic_block = false;
722722 } else {
723723 switch (opcode) {
724724 DW.LNS_copy => {
725725 //%%io.stdout.printf(" [0x{x8}] Copy\n", pos);
726726
727 test (%return prog.checkLineMatch()) |info| return info;
727 if (%return prog.checkLineMatch()) |info| return info;
728728 prog.basic_block = false;
729729 },
730730 DW.LNS_advance_pc => {
......@@ -828,8 +828,8 @@ fn scanAllCompileUnits(st: &ElfStackTrace) -> %void {
828828 return error.InvalidDebugInfo;
829829
830830 const pc_range = {
831 try (compile_unit_die.getAttrAddr(DW.AT_low_pc)) |low_pc| {
832 test (compile_unit_die.getAttr(DW.AT_high_pc)) |high_pc_value| {
831 if (compile_unit_die.getAttrAddr(DW.AT_low_pc)) |low_pc| {
832 if (compile_unit_die.getAttr(DW.AT_high_pc)) |high_pc_value| {
833833 const pc_end = switch (*high_pc_value) {
834834 FormValue.Address => |value| value,
835835 FormValue.Const => |value| {
......@@ -867,7 +867,7 @@ fn scanAllCompileUnits(st: &ElfStackTrace) -> %void {
867867
868868fn findCompileUnit(st: &ElfStackTrace, target_address: u64) -> ?&const CompileUnit {
869869 for (st.compile_unit_list.toSlice()) |*compile_unit| {
870 test (compile_unit.pc_range) |range| {
870 if (compile_unit.pc_range) |range| {
871871 if (target_address >= range.start and target_address < range.end)
872872 return compile_unit;
873873 }
std/hash_map.zig+1-1
......@@ -247,7 +247,7 @@ test "basicHashMapTest" {
247247 assert((??map.get(2)).value == 22);
248248 _ = map.remove(2);
249249 assert(map.remove(2) == null);
250 assert(test (map.get(2)) false else true);
250 assert(map.get(2) == null);
251251}
252252
253253fn hash_i32(x: i32) -> u32 {
std/linked_list.zig+6-6
......@@ -43,7 +43,7 @@ pub fn LinkedList(comptime T: type) -> type {
4343 /// new_node: Pointer to the new node to insert.
4444 pub fn insertAfter(list: &List, node: &Node, new_node: &Node) {
4545 new_node.prev = node;
46 test (node.next) |next_node| {
46 if (node.next) |next_node| {
4747 // Intermediate node.
4848 new_node.next = next_node;
4949 next_node.prev = new_node;
......@@ -64,7 +64,7 @@ pub fn LinkedList(comptime T: type) -> type {
6464 /// new_node: Pointer to the new node to insert.
6565 pub fn insertBefore(list: &List, node: &Node, new_node: &Node) {
6666 new_node.next = node;
67 test (node.prev) |prev_node| {
67 if (node.prev) |prev_node| {
6868 // Intermediate node.
6969 new_node.prev = prev_node;
7070 prev_node.next = new_node;
......@@ -83,7 +83,7 @@ pub fn LinkedList(comptime T: type) -> type {
8383 /// Arguments:
8484 /// new_node: Pointer to the new node to insert.
8585 pub fn append(list: &List, new_node: &Node) {
86 test (list.last) |last| {
86 if (list.last) |last| {
8787 // Insert after last.
8888 list.insertAfter(last, new_node);
8989 } else {
......@@ -97,7 +97,7 @@ pub fn LinkedList(comptime T: type) -> type {
9797 /// Arguments:
9898 /// new_node: Pointer to the new node to insert.
9999 pub fn prepend(list: &List, new_node: &Node) {
100 test (list.first) |first| {
100 if (list.first) |first| {
101101 // Insert before first.
102102 list.insertBefore(first, new_node);
103103 } else {
......@@ -116,7 +116,7 @@ pub fn LinkedList(comptime T: type) -> type {
116116 /// Arguments:
117117 /// node: Pointer to the node to be removed.
118118 pub fn remove(list: &List, node: &Node) {
119 test (node.prev) |prev_node| {
119 if (node.prev) |prev_node| {
120120 // Intermediate node.
121121 prev_node.next = node.next;
122122 } else {
......@@ -124,7 +124,7 @@ pub fn LinkedList(comptime T: type) -> type {
124124 list.first = node.next;
125125 }
126126
127 test (node.next) |next_node| {
127 if (node.next) |next_node| {
128128 // Intermediate node.
129129 next_node.prev = node.prev;
130130 } else {
std/os/child_process.zig+7-7
......@@ -59,9 +59,9 @@ pub const ChildProcess = struct {
5959 errno.EINVAL, errno.ECHILD => unreachable,
6060 errno.EINTR => continue,
6161 else => {
62 test (self.stdin) |*stdin| { stdin.close(); }
63 test (self.stdout) |*stdout| { stdout.close(); }
64 test (self.stderr) |*stderr| { stderr.close(); }
62 if (self.stdin) |*stdin| { stdin.close(); }
63 if (self.stdout) |*stdout| { stdout.close(); }
64 if (self.stderr) |*stderr| { stderr.close(); }
6565 return error.Unexpected;
6666 },
6767 }
......@@ -69,9 +69,9 @@ pub const ChildProcess = struct {
6969 break;
7070 }
7171
72 test (self.stdin) |*stdin| { stdin.close(); }
73 test (self.stdout) |*stdout| { stdout.close(); }
74 test (self.stderr) |*stderr| { stderr.close(); }
72 if (self.stdin) |*stdin| { stdin.close(); }
73 if (self.stdout) |*stdout| { stdout.close(); }
74 if (self.stderr) |*stderr| { stderr.close(); }
7575
7676 // Write @maxValue(ErrInt) to the write end of the err_pipe. This is after
7777 // waitpid, so this write is guaranteed to be after the child
......@@ -143,7 +143,7 @@ pub const ChildProcess = struct {
143143 setUpChildIo(stderr, stderr_pipe[1], posix.STDERR_FILENO, dev_null_fd) %%
144144 |err| forkChildErrReport(err_pipe[1], err);
145145
146 test (maybe_cwd) |cwd| {
146 if (maybe_cwd) |cwd| {
147147 os.changeCurDir(allocator, cwd) %%
148148 |err| forkChildErrReport(err_pipe[1], err);
149149 }
std/os/index.zig+6-6
......@@ -173,7 +173,7 @@ pub fn posixOpen(file_path: []const u8, flags: usize, perm: usize, allocator: ?&
173173
174174 if (file_path.len < stack_buf.len) {
175175 path0 = stack_buf[0...file_path.len + 1];
176 } else test (allocator) |a| {
176 } else if (allocator) |a| {
177177 path0 = %return a.alloc(u8, file_path.len + 1);
178178 need_free = true;
179179 } else {
......@@ -241,7 +241,7 @@ pub fn posixExecve(exe_path: []const u8, argv: []const []const u8, env_map: &con
241241 mem.set(?&u8, argv_buf, null);
242242 defer {
243243 for (argv_buf) |arg| {
244 const arg_buf = test (arg) |ptr| cstr.toSlice(ptr) else break;
244 const arg_buf = if (arg) |ptr| cstr.toSlice(ptr) else break;
245245 allocator.free(arg_buf);
246246 }
247247 allocator.free(argv_buf);
......@@ -268,7 +268,7 @@ pub fn posixExecve(exe_path: []const u8, argv: []const []const u8, env_map: &con
268268 mem.set(?&u8, envp_buf, null);
269269 defer {
270270 for (envp_buf) |env| {
271 const env_buf = test (env) |ptr| cstr.toSlice(ptr) else break;
271 const env_buf = if (env) |ptr| cstr.toSlice(ptr) else break;
272272 allocator.free(env_buf);
273273 }
274274 allocator.free(envp_buf);
......@@ -448,7 +448,7 @@ pub fn symLink(allocator: &Allocator, existing_path: []const u8, new_path: []con
448448const b64_fs_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";
449449
450450pub fn atomicSymLink(allocator: &Allocator, existing_path: []const u8, new_path: []const u8) -> %void {
451 try (symLink(allocator, existing_path, new_path)) {
451 if (symLink(allocator, existing_path, new_path)) {
452452 return;
453453 } else |err| {
454454 if (err != error.PathAlreadyExists) {
......@@ -463,7 +463,7 @@ pub fn atomicSymLink(allocator: &Allocator, existing_path: []const u8, new_path:
463463 while (true) {
464464 %return getRandomBytes(rand_buf[0...]);
465465 _ = base64.encodeWithAlphabet(tmp_path[new_path.len...], rand_buf, b64_fs_alphabet);
466 try (symLink(allocator, existing_path, tmp_path)) {
466 if (symLink(allocator, existing_path, tmp_path)) {
467467 return rename(allocator, tmp_path, new_path);
468468 } else |err| {
469469 if (err == error.PathAlreadyExists) {
......@@ -668,7 +668,7 @@ pub fn deleteDir(allocator: &Allocator, dir_path: []const u8) -> %void {
668668pub fn deleteTree(allocator: &Allocator, full_path: []const u8) -> %void {
669669start_over:
670670 // First, try deleting the item as a file. This way we don't follow sym links.
671 try (deleteFile(allocator, full_path)) {
671 if (deleteFile(allocator, full_path)) {
672672 return;
673673 } else |err| {
674674 if (err == error.FileNotFound)
std/os/path.zig+1-1
......@@ -243,7 +243,7 @@ pub fn relative(allocator: &Allocator, from: []const u8, to: []const u8) -> %[]u
243243 while (true) {
244244 const from_component = from_it.next() ?? return mem.dupe(allocator, u8, to_it.rest());
245245 const to_rest = to_it.rest();
246 test(to_it.next()) |to_component| {
246 if (to_it.next()) |to_component| {
247247 if (mem.eql(u8, from_component, to_component))
248248 continue;
249249 }
std/special/build_runner.zig+1-1
......@@ -63,7 +63,7 @@ pub fn main() -> %void {
6363 %%io.stderr.printf("Expected option name after '-D'\n\n");
6464 return usage(&builder, false, &io.stderr);
6565 }
66 test (mem.indexOfScalar(u8, option_contents, '=')) |name_end| {
66 if (mem.indexOfScalar(u8, option_contents, '=')) |name_end| {
6767 const option_name = option_contents[0...name_end];
6868 const option_value = option_contents[name_end + 1...];
6969 if (builder.addUserInputOption(option_name, option_value))
std/special/compiler_rt.zig+9-9
......@@ -36,7 +36,7 @@ export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
3636 // 0 X
3737 // ---
3838 // 0 X
39 test (maybe_rem) |rem| {
39 if (maybe_rem) |rem| {
4040 *rem = n[low] % d[low];
4141 }
4242 return n[low] / d[low];
......@@ -44,7 +44,7 @@ export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
4444 // 0 X
4545 // ---
4646 // K X
47 test (maybe_rem) |rem| {
47 if (maybe_rem) |rem| {
4848 *rem = n[low];
4949 }
5050 return 0;
......@@ -55,7 +55,7 @@ export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
5555 // K X
5656 // ---
5757 // 0 0
58 test (maybe_rem) |rem| {
58 if (maybe_rem) |rem| {
5959 *rem = n[high] % d[low];
6060 }
6161 return n[high] / d[low];
......@@ -65,7 +65,7 @@ export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
6565 // K 0
6666 // ---
6767 // K 0
68 test (maybe_rem) |rem| {
68 if (maybe_rem) |rem| {
6969 r[high] = n[high] % d[high];
7070 r[low] = 0;
7171 *rem = *@ptrCast(&du_int, &r[0]);
......@@ -77,7 +77,7 @@ export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
7777 // K 0
7878 // if d is a power of 2
7979 if ((d[high] & (d[high] - 1)) == 0) {
80 test (maybe_rem) |rem| {
80 if (maybe_rem) |rem| {
8181 r[low] = n[low];
8282 r[high] = n[high] & (d[high] - 1);
8383 *rem = *@ptrCast(&du_int, &r[0]);
......@@ -90,7 +90,7 @@ export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
9090 sr = @clz(su_int(d[high])) - @clz(su_int(n[high]));
9191 // 0 <= sr <= n_uword_bits - 2 or sr large
9292 if (sr > n_uword_bits - 2) {
93 test (maybe_rem) |rem| {
93 if (maybe_rem) |rem| {
9494 *rem = *@ptrCast(&du_int, &n[0]);
9595 }
9696 return 0;
......@@ -111,7 +111,7 @@ export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
111111 // 0 K
112112 // if d is a power of 2
113113 if ((d[low] & (d[low] - 1)) == 0) {
114 test (maybe_rem) |rem| {
114 if (maybe_rem) |rem| {
115115 *rem = n[low] & (d[low] - 1);
116116 }
117117 if (d[low] == 1) {
......@@ -155,7 +155,7 @@ export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
155155 sr = @clz(su_int(d[high])) - @clz(su_int(n[high]));
156156 // 0 <= sr <= n_uword_bits - 1 or sr large
157157 if (sr > n_uword_bits - 1) {
158 test (maybe_rem) |rem| {
158 if (maybe_rem) |rem| {
159159 *rem = *@ptrCast(&du_int, &n[0]);
160160 }
161161 return 0;
......@@ -200,7 +200,7 @@ export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
200200 sr -= 1;
201201 }
202202 *@ptrCast(&du_int, &q[0]) = (*@ptrCast(&du_int, &q[0]) << 1) | u64(carry);
203 test (maybe_rem) |rem| {
203 if (maybe_rem) |rem| {
204204 *rem = *@ptrCast(&du_int, &r[0]);
205205 }
206206 return *@ptrCast(&du_int, &q[0]);
test/cases/null.zig+6-6
......@@ -3,7 +3,7 @@ const assert = @import("std").debug.assert;
33test "nullableType" {
44 const x : ?bool = @generatedCode(true);
55
6 test (x) |y| {
6 if (x) |y| {
77 if (y) {
88 // OK
99 } else {
......@@ -29,7 +29,7 @@ test "nullableType" {
2929test "test maybe object and get a pointer to the inner value" {
3030 var maybe_bool: ?bool = true;
3131
32 test (maybe_bool) |*b| {
32 if (maybe_bool) |*b| {
3333 *b = false;
3434 }
3535
......@@ -50,7 +50,7 @@ test "maybe return" {
5050
5151fn maybeReturnImpl() {
5252 assert(??foo(1235));
53 test (foo(null))
53 if (foo(null) != null)
5454 unreachable;
5555 assert(!??foo(1234));
5656}
......@@ -66,10 +66,10 @@ test "ifVarMaybePointer" {
6666}
6767fn shouldBeAPlus1(p: &const Particle) -> u64 {
6868 var maybe_particle: ?Particle = *p;
69 test (maybe_particle) |*particle| {
69 if (maybe_particle) |*particle| {
7070 particle.a += 1;
7171 }
72 test (maybe_particle) |particle| {
72 if (maybe_particle) |particle| {
7373 return particle.a;
7474 }
7575 return 0;
......@@ -116,7 +116,7 @@ fn nullableVoidImpl() {
116116}
117117
118118fn bar(x: ?void) -> ?void {
119 test (x) {
119 if (x) |_| {
120120 return {};
121121 } else {
122122 return null;
test/cases/try.zig+6-6
......@@ -7,7 +7,7 @@ test "tryOnErrorUnion" {
77}
88
99fn tryOnErrorUnionImpl() {
10 const x = try (returnsTen()) |val| {
10 const x = if (returnsTen()) |val| {
1111 val + 1
1212 } else |err| switch (err) {
1313 error.ItBroke, error.NoMem => 1,
......@@ -24,16 +24,16 @@ fn returnsTen() -> %i32 {
2424}
2525
2626test "tryWithoutVars" {
27 const result1 = try (failIfTrue(true)) {
27 const result1 = if (failIfTrue(true)) {
2828 1
29 } else {
29 } else |_| {
3030 i32(2)
3131 };
3232 assert(result1 == 2);
3333
34 const result2 = try (failIfTrue(false)) {
34 const result2 = if (failIfTrue(false)) {
3535 1
36 } else {
36 } else |_| {
3737 i32(2)
3838 };
3939 assert(result2 == 1);
......@@ -48,7 +48,7 @@ fn failIfTrue(ok: bool) -> %void {
4848}
4949
5050test "try then not executed with assignment" {
51 try (failIfTrue(true)) {
51 if (failIfTrue(true)) {
5252 unreachable;
5353 } else |err| {
5454 assert(err == error.ItBroke);
test/compile_errors.zig+6-24
......@@ -118,38 +118,20 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
118118 \\}
119119 , ".tmp_source.zig:5:5: error: invalid token: 'var'");
120120
121 cases.add("implicit semicolon - try statement",
122 \\export fn entry() {
123 \\ try (foo()) {}
124 \\ var good = {};
125 \\ try (foo()) ({})
126 \\ var bad = {};
127 \\}
128 , ".tmp_source.zig:5:5: error: invalid token: 'var'");
129
130 cases.add("implicit semicolon - try expression",
131 \\export fn entry() {
132 \\ _ = try (foo()) {};
133 \\ var good = {};
134 \\ _ = try (foo()) {}
135 \\ var bad = {};
136 \\}
137 , ".tmp_source.zig:5:5: error: invalid token: 'var'");
138
139121 cases.add("implicit semicolon - test statement",
140122 \\export fn entry() {
141 \\ test (foo()) {}
123 \\ if (foo()) |_| {}
142124 \\ var good = {};
143 \\ test (foo()) ({})
125 \\ if (foo()) |_| ({})
144126 \\ var bad = {};
145127 \\}
146128 , ".tmp_source.zig:5:5: error: invalid token: 'var'");
147129
148130 cases.add("implicit semicolon - test expression",
149131 \\export fn entry() {
150 \\ _ = test (foo()) {};
132 \\ _ = if (foo()) |_| {};
151133 \\ var good = {};
152 \\ _ = test (foo()) {}
134 \\ _ = if (foo()) |_| {}
153135 \\ var bad = {};
154136 \\}
155137 , ".tmp_source.zig:5:5: error: invalid token: 'var'");
......@@ -500,9 +482,9 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
500482
501483 cases.add("invalid maybe type",
502484 \\export fn f() {
503 \\ test (true) |x| { }
485 \\ if (true) |x| { }
504486 \\}
505 , ".tmp_source.zig:2:11: error: expected nullable type, found 'bool'");
487 , ".tmp_source.zig:2:9: error: expected nullable type, found 'bool'");
506488
507489 cases.add("cast unreachable",
508490 \\fn f() -> i32 {
test/tests.zig+7-7
......@@ -349,7 +349,7 @@ pub const CompareOutputContext = struct {
349349 switch (case.special) {
350350 Special.Asm => {
351351 const annotated_case_name = %%fmt.allocPrint(self.b.allocator, "assemble-and-link {}", case.name);
352 test (self.test_filter) |filter| {
352 if (self.test_filter) |filter| {
353353 if (mem.indexOf(u8, annotated_case_name, filter) == null)
354354 return;
355355 }
......@@ -373,7 +373,7 @@ pub const CompareOutputContext = struct {
373373 for ([]Mode{Mode.Debug, Mode.ReleaseFast}) |mode| {
374374 const annotated_case_name = %%fmt.allocPrint(self.b.allocator, "{} {} ({})",
375375 "compare-output", case.name, @enumTagName(mode));
376 test (self.test_filter) |filter| {
376 if (self.test_filter) |filter| {
377377 if (mem.indexOf(u8, annotated_case_name, filter) == null)
378378 continue;
379379 }
......@@ -399,7 +399,7 @@ pub const CompareOutputContext = struct {
399399 },
400400 Special.DebugSafety => {
401401 const annotated_case_name = %%fmt.allocPrint(self.b.allocator, "safety {}", case.name);
402 test (self.test_filter) |filter| {
402 if (self.test_filter) |filter| {
403403 if (mem.indexOf(u8, annotated_case_name, filter) == null)
404404 return;
405405 }
......@@ -620,7 +620,7 @@ pub const CompileErrorContext = struct {
620620 for ([]Mode{Mode.Debug, Mode.ReleaseFast}) |mode| {
621621 const annotated_case_name = %%fmt.allocPrint(self.b.allocator, "compile-error {} ({})",
622622 case.name, @enumTagName(mode));
623 test (self.test_filter) |filter| {
623 if (self.test_filter) |filter| {
624624 if (mem.indexOf(u8, annotated_case_name, filter) == null)
625625 continue;
626626 }
......@@ -655,7 +655,7 @@ pub const BuildExamplesContext = struct {
655655 const b = self.b;
656656
657657 const annotated_case_name = b.fmt("build {} (Debug)", build_file);
658 test (self.test_filter) |filter| {
658 if (self.test_filter) |filter| {
659659 if (mem.indexOf(u8, annotated_case_name, filter) == null)
660660 return;
661661 }
......@@ -686,7 +686,7 @@ pub const BuildExamplesContext = struct {
686686 for ([]Mode{Mode.Debug, Mode.ReleaseFast}) |mode| {
687687 const annotated_case_name = %%fmt.allocPrint(self.b.allocator, "build {} ({})",
688688 root_src, @enumTagName(mode));
689 test (self.test_filter) |filter| {
689 if (self.test_filter) |filter| {
690690 if (mem.indexOf(u8, annotated_case_name, filter) == null)
691691 continue;
692692 }
......@@ -874,7 +874,7 @@ pub const ParseHContext = struct {
874874 const b = self.b;
875875
876876 const annotated_case_name = %%fmt.allocPrint(self.b.allocator, "parseh {}", case.name);
877 test (self.test_filter) |filter| {
877 if (self.test_filter) |filter| {
878878 if (mem.indexOf(u8, annotated_case_name, filter) == null)
879879 return;
880880 }