remove test and try expressions in favor of if expressions

See #357
This commit is contained in:
Andrew Kelley 2017-05-03 17:23:11 -04:00
parent 0940d46c01
commit 644ea2dde9
19 changed files with 148 additions and 226 deletions

View File

@ -91,9 +91,9 @@ Defer(body) = option("%") "defer" body
IfExpression(body) = "if" "(" Expression ")" body option("else" BlockExpression(body))
TryExpression(body) = "try" "(" Expression ")" option("|" option("*") Symbol "|") body option("else" option("|" Symbol "|") BlockExpression(body))
TryExpression(body) = "if" "(" Expression ")" option("|" option("*") Symbol "|") body "else" "|" Symbol "|" BlockExpression(body)
TestExpression(body) = "test" "(" Expression ")" option("|" option("*") Symbol "|") body option("else" option("|" Symbol "|") BlockExpression(body))
TestExpression(body) = "if" "(" Expression ")" option("|" option("*") Symbol "|") body option("else" BlockExpression(body))
BoolAndExpression = ComparisonExpression "and" BoolAndExpression | ComparisonExpression

View File

@ -763,7 +763,7 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
}
case NodeTypeTryExpr:
{
fprintf(ar->f, "try (");
fprintf(ar->f, "if (");
render_node_grouped(ar, node->data.try_expr.target_node);
fprintf(ar->f, ") ");
if (node->data.try_expr.var_symbol) {
@ -783,7 +783,7 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
}
case NodeTypeTestExpr:
{
fprintf(ar->f, "test (");
fprintf(ar->f, "if (");
render_node_grouped(ar, node->data.test_expr.target_node);
fprintf(ar->f, ") ");
if (node->data.test_expr.var_symbol) {

View File

@ -215,7 +215,7 @@ static AstNode *ast_parse_block_or_expression(ParseContext *pc, size_t *token_in
static AstNode *ast_parse_block_expr_or_expression(ParseContext *pc, size_t *token_index, bool mandatory);
static AstNode *ast_parse_expression(ParseContext *pc, size_t *token_index, bool mandatory);
static AstNode *ast_parse_block(ParseContext *pc, size_t *token_index, bool mandatory);
static AstNode *ast_parse_if_expr(ParseContext *pc, size_t *token_index, bool mandatory);
static AstNode *ast_parse_if_try_test_expr(ParseContext *pc, size_t *token_index, bool mandatory);
static AstNode *ast_parse_block_expr(ParseContext *pc, size_t *token_index, bool mandatory);
static AstNode *ast_parse_unwrap_expr(ParseContext *pc, size_t *token_index, bool mandatory);
static 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
return node;
}
/*
TryExpression(body) = "try" "(" Expression ")" option("|" option("*") Symbol "|") body option("else" option("|" Symbol "|") BlockExpression(body))
*/
static AstNode *ast_parse_try_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
Token *try_token = &pc->tokens->at(*token_index);
if (try_token->id == TokenIdKeywordTry) {
*token_index += 1;
} else if (mandatory) {
ast_expect_token(pc, try_token, TokenIdKeywordTry);
zig_unreachable();
} else {
return nullptr;
}
AstNode *node = ast_create_node(pc, NodeTypeTryExpr, try_token);
ast_eat_token(pc, token_index, TokenIdLParen);
node->data.try_expr.target_node = ast_parse_expression(pc, token_index, true);
ast_eat_token(pc, token_index, TokenIdRParen);
Token *open_bar_tok = &pc->tokens->at(*token_index);
if (open_bar_tok->id == TokenIdBinOr) {
*token_index += 1;
Token *star_tok = &pc->tokens->at(*token_index);
if (star_tok->id == TokenIdStar) {
*token_index += 1;
node->data.try_expr.var_is_ptr = true;
}
Token *var_name_tok = ast_eat_token(pc, token_index, TokenIdSymbol);
node->data.try_expr.var_symbol = token_buf(var_name_tok);
ast_eat_token(pc, token_index, TokenIdBinOr);
}
node->data.try_expr.then_node = ast_parse_block_or_expression(pc, token_index, true);
Token *else_token = &pc->tokens->at(*token_index);
if (else_token->id == TokenIdKeywordElse) {
*token_index += 1;
Token *open_bar_tok = &pc->tokens->at(*token_index);
if (open_bar_tok->id == TokenIdBinOr) {
*token_index += 1;
Token *err_name_tok = ast_eat_token(pc, token_index, TokenIdSymbol);
node->data.try_expr.err_symbol = token_buf(err_name_tok);
ast_eat_token(pc, token_index, TokenIdBinOr);
}
node->data.try_expr.else_node = ast_parse_block_expr_or_expression(pc, token_index, true);
}
return node;
}
/*
TestExpression(body) = "test" "(" Expression ")" option("|" option("*") Symbol "|") body option("else" BlockExpression(body))
*/
static AstNode *ast_parse_test_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
Token *test_token = &pc->tokens->at(*token_index);
if (test_token->id == TokenIdKeywordTest) {
*token_index += 1;
} else if (mandatory) {
ast_expect_token(pc, test_token, TokenIdKeywordTest);
zig_unreachable();
} else {
return nullptr;
}
AstNode *node = ast_create_node(pc, NodeTypeTestExpr, test_token);
ast_eat_token(pc, token_index, TokenIdLParen);
node->data.test_expr.target_node = ast_parse_expression(pc, token_index, true);
ast_eat_token(pc, token_index, TokenIdRParen);
Token *open_bar_tok = &pc->tokens->at(*token_index);
if (open_bar_tok->id == TokenIdBinOr) {
*token_index += 1;
Token *star_tok = &pc->tokens->at(*token_index);
if (star_tok->id == TokenIdStar) {
*token_index += 1;
node->data.test_expr.var_is_ptr = true;
}
Token *var_name_tok = ast_eat_token(pc, token_index, TokenIdSymbol);
node->data.test_expr.var_symbol = token_buf(var_name_tok);
ast_eat_token(pc, token_index, TokenIdBinOr);
}
node->data.test_expr.then_node = ast_parse_block_or_expression(pc, token_index, true);
Token *else_token = &pc->tokens->at(*token_index);
if (else_token->id == TokenIdKeywordElse) {
*token_index += 1;
node->data.test_expr.else_node = ast_parse_block_expr_or_expression(pc, token_index, true);
}
return node;
}
/*
PrimaryExpression = Number | String | CharLiteral | KeywordLiteral | GroupedExpression | GotoExpression | BlockExpression(BlockOrExpression) | Symbol | ("@" Symbol FnCallExpression) | ArrayType | (option("extern") FnProto) | AsmExpression | ("error" "." Symbol) | ContainerDecl
KeywordLiteral = "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
/*
IfExpression(body) = "if" "(" Expression ")" body option("else" BlockExpression(body))
TryExpression(body) = "if" "(" Expression ")" option("|" option("*") Symbol "|") body "else" "|" Symbol "|" BlockExpression(body)
TestExpression(body) = "if" "(" Expression ")" "|" option("*") Symbol "|" body option("else" BlockExpression(body))
*/
static AstNode *ast_parse_if_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
static AstNode *ast_parse_if_try_test_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
Token *if_token = &pc->tokens->at(*token_index);
if (if_token->id == TokenIdKeywordIf) {
@ -1448,19 +1346,72 @@ static AstNode *ast_parse_if_expr(ParseContext *pc, size_t *token_index, bool ma
}
ast_eat_token(pc, token_index, TokenIdLParen);
AstNode *node = ast_create_node(pc, NodeTypeIfBoolExpr, if_token);
node->data.if_bool_expr.condition = ast_parse_expression(pc, token_index, true);
AstNode *condition = ast_parse_expression(pc, token_index, true);
ast_eat_token(pc, token_index, TokenIdRParen);
node->data.if_bool_expr.then_block = ast_parse_block_or_expression(pc, token_index, true);
Token *else_token = &pc->tokens->at(*token_index);
if (else_token->id == TokenIdKeywordElse) {
Token *open_bar_tok = &pc->tokens->at(*token_index);
Token *var_name_tok = nullptr;
bool var_is_ptr = false;
if (open_bar_tok->id == TokenIdBinOr) {
*token_index += 1;
node->data.if_bool_expr.else_node = ast_parse_block_expr_or_expression(pc, token_index, true);
Token *star_tok = &pc->tokens->at(*token_index);
if (star_tok->id == TokenIdStar) {
*token_index += 1;
var_is_ptr = true;
}
var_name_tok = ast_eat_token(pc, token_index, TokenIdSymbol);
ast_eat_token(pc, token_index, TokenIdBinOr);
}
return node;
AstNode *body_node = ast_parse_block_or_expression(pc, token_index, true);
Token *else_tok = &pc->tokens->at(*token_index);
AstNode *else_node = nullptr;
Token *err_name_tok = nullptr;
if (else_tok->id == TokenIdKeywordElse) {
*token_index += 1;
Token *else_bar_tok = &pc->tokens->at(*token_index);
if (else_bar_tok->id == TokenIdBinOr) {
*token_index += 1;
err_name_tok = ast_eat_token(pc, token_index, TokenIdSymbol);
ast_eat_token(pc, token_index, TokenIdBinOr);
}
else_node = ast_parse_block_expr_or_expression(pc, token_index, true);
}
if (err_name_tok != nullptr) {
AstNode *node = ast_create_node(pc, NodeTypeTryExpr, if_token);
node->data.try_expr.target_node = condition;
node->data.try_expr.var_is_ptr = var_is_ptr;
if (var_name_tok != nullptr) {
node->data.try_expr.var_symbol = token_buf(var_name_tok);
}
node->data.try_expr.then_node = body_node;
node->data.try_expr.err_symbol = token_buf(err_name_tok);
node->data.try_expr.else_node = else_node;
return node;
} else if (var_name_tok != nullptr) {
AstNode *node = ast_create_node(pc, NodeTypeTestExpr, if_token);
node->data.test_expr.target_node = condition;
node->data.test_expr.var_is_ptr = var_is_ptr;
node->data.test_expr.var_symbol = token_buf(var_name_tok);
node->data.test_expr.then_node = body_node;
node->data.test_expr.else_node = else_node;
return node;
} else {
AstNode *node = ast_create_node(pc, NodeTypeIfBoolExpr, if_token);
node->data.if_bool_expr.condition = condition;
node->data.if_bool_expr.then_block = body_node;
node->data.if_bool_expr.else_node = else_node;
return node;
}
}
/*
@ -1848,7 +1799,7 @@ BlockExpression(body) = Block | IfExpression(body) | TryExpression(body) | TestE
static AstNode *ast_parse_block_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
Token *token = &pc->tokens->at(*token_index);
AstNode *if_expr = ast_parse_if_expr(pc, token_index, false);
AstNode *if_expr = ast_parse_if_try_test_expr(pc, token_index, false);
if (if_expr)
return if_expr;
@ -1872,14 +1823,6 @@ static AstNode *ast_parse_block_expr(ParseContext *pc, size_t *token_index, bool
if (comptime_node)
return comptime_node;
AstNode *try_node = ast_parse_try_expr(pc, token_index, false);
if (try_node)
return try_node;
AstNode *test_node = ast_parse_test_expr(pc, token_index, false);
if (test_node)
return test_node;
if (mandatory)
ast_invalid_token_error(pc, token);

View File

@ -138,7 +138,6 @@ static const struct ZigKeyword zig_keywords[] = {
{"test", TokenIdKeywordTest},
{"this", TokenIdKeywordThis},
{"true", TokenIdKeywordTrue},
{"try", TokenIdKeywordTry},
{"undefined", TokenIdKeywordUndefined},
{"union", TokenIdKeywordUnion},
{"unreachable", TokenIdKeywordUnreachable},
@ -1472,7 +1471,6 @@ const char * token_name(TokenId id) {
case TokenIdKeywordTest: return "test";
case TokenIdKeywordThis: return "this";
case TokenIdKeywordTrue: return "true";
case TokenIdKeywordTry: return "try";
case TokenIdKeywordUndefined: return "undefined";
case TokenIdKeywordUnion: return "union";
case TokenIdKeywordUnreachable: return "unreachable";

View File

@ -75,7 +75,6 @@ enum TokenId {
TokenIdKeywordTest,
TokenIdKeywordThis,
TokenIdKeywordTrue,
TokenIdKeywordTry,
TokenIdKeywordUndefined,
TokenIdKeywordUnion,
TokenIdKeywordUnreachable,

View File

@ -28,7 +28,7 @@ pub const BufMap = struct {
}
pub fn set(self: &BufMap, key: []const u8, value: []const u8) -> %void {
test (self.hash_map.get(key)) |entry| {
if (self.hash_map.get(key)) |entry| {
const value_copy = %return self.copy(value);
%defer self.free(value_copy);
_ = %return self.hash_map.put(key, value_copy);

View File

@ -306,7 +306,7 @@ pub const Builder = struct {
}
fn processNixOSEnvVars(self: &Builder) {
test (os.getEnv("NIX_CFLAGS_COMPILE")) |nix_cflags_compile| {
if (os.getEnv("NIX_CFLAGS_COMPILE")) |nix_cflags_compile| {
var it = mem.split(nix_cflags_compile, ' ');
while (true) {
const word = it.next() ?? break;
@ -322,7 +322,7 @@ pub const Builder = struct {
}
}
}
test (os.getEnv("NIX_LDFLAGS")) |nix_ldflags| {
if (os.getEnv("NIX_LDFLAGS")) |nix_ldflags| {
var it = mem.split(nix_ldflags, ' ');
while (true) {
const word = it.next() ?? break;
@ -350,7 +350,7 @@ pub const Builder = struct {
.type_id = type_id,
.description = description,
};
test (%%self.available_options_map.put(name, available_option)) {
if (%%self.available_options_map.put(name, available_option) != null) {
debug.panic("Option '{}' declared twice", name);
}
%%self.available_options_list.append(available_option);
@ -424,7 +424,7 @@ pub const Builder = struct {
}
pub fn addUserInputOption(self: &Builder, name: []const u8, value: []const u8) -> bool {
test (%%self.user_input_options.put(name, UserInputOption {
if (%%self.user_input_options.put(name, UserInputOption {
.name = name,
.value = UserValue.Scalar { value },
.used = false,
@ -461,7 +461,7 @@ pub const Builder = struct {
}
pub fn addUserInputFlag(self: &Builder, name: []const u8) -> bool {
test (%%self.user_input_options.put(name, UserInputOption {
if (%%self.user_input_options.put(name, UserInputOption {
.name = name,
.value = UserValue.Flag,
.used = false,
@ -530,7 +530,7 @@ pub const Builder = struct {
exe_path: []const u8, args: []const []const u8) -> %void
{
if (self.verbose) {
test (cwd) |yes_cwd| %%io.stderr.print("cd {}; ", yes_cwd);
if (cwd) |yes_cwd| %%io.stderr.print("cd {}; ", yes_cwd);
%%io.stderr.print("{}", exe_path);
for (args) |arg| {
%%io.stderr.print(" {}", arg);
@ -821,7 +821,7 @@ pub const LibExeObjStep = struct {
}
pub fn getOutputPath(self: &LibExeObjStep) -> []const u8 {
test (self.output_path) |output_path| {
if (self.output_path) |output_path| {
output_path
} else {
%%os.path.join(self.builder.allocator, self.builder.cache_root, self.out_filename)
@ -833,7 +833,7 @@ pub const LibExeObjStep = struct {
}
pub fn getOutputHPath(self: &LibExeObjStep) -> []const u8 {
test (self.output_h_path) |output_h_path| {
if (self.output_h_path) |output_h_path| {
output_h_path
} else {
%%os.path.join(self.builder.allocator, self.builder.cache_root, self.out_h_filename)
@ -885,7 +885,7 @@ pub const LibExeObjStep = struct {
};
%%zig_args.append(cmd);
test (self.root_src) |root_src| {
if (self.root_src) |root_src| {
%%zig_args.append(builder.pathFromRoot(root_src));
}
@ -950,7 +950,7 @@ pub const LibExeObjStep = struct {
},
}
test (self.linker_script) |linker_script| {
if (self.linker_script) |linker_script| {
%%zig_args.append("--linker-script");
%%zig_args.append(linker_script);
}
@ -1059,7 +1059,7 @@ pub const TestStep = struct {
builtin.Mode.ReleaseFast => %%zig_args.append("--release-fast"),
}
test (self.filter) |filter| {
if (self.filter) |filter| {
%%zig_args.append("--test-filter");
%%zig_args.append(filter);
}
@ -1203,7 +1203,7 @@ pub const CLibExeObjStep = struct {
}
pub fn getOutputPath(self: &CLibExeObjStep) -> []const u8 {
test (self.output_path) |output_path| {
if (self.output_path) |output_path| {
output_path
} else {
%%os.path.join(self.builder.allocator, self.builder.cache_root, self.out_filename)
@ -1492,7 +1492,7 @@ pub const CommandStep = struct {
fn make(step: &Step) -> %void {
const self = @fieldParentPtr(CommandStep, "step", step);
const cwd = test (self.cwd) |cwd| self.builder.pathFromRoot(cwd) else null;
const cwd = if (self.cwd) |cwd| self.builder.pathFromRoot(cwd) else null;
return self.builder.spawnChildEnvMap(cwd, self.env_map, self.exe_path, self.args);
}
};

View File

@ -98,13 +98,13 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty
continue;
};
const compile_unit_name = %return compile_unit.die.getAttrString(st, DW.AT_name);
try (getLineNumberInfo(st, compile_unit, usize(return_address) - 1)) |line_info| {
if (getLineNumberInfo(st, compile_unit, usize(return_address) - 1)) |line_info| {
defer line_info.deinit();
%return out_stream.print(WHITE ++ "{}:{}:{}" ++ RESET ++ ": " ++
DIM ++ ptr_hex ++ " in ??? ({})" ++ RESET ++ "\n",
line_info.file_name, line_info.line, line_info.column,
return_address, compile_unit_name);
try (printLineFromFile(st.allocator(), out_stream, line_info)) {
if (printLineFromFile(st.allocator(), out_stream, line_info)) {
if (line_info.column == 0) {
%return out_stream.write("\n");
} else {
@ -679,7 +679,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
DW.LNE_end_sequence => {
//%%io.stdout.printf(" [0x{x8}] End Sequence\n", pos);
prog.end_sequence = true;
test (%return prog.checkLineMatch()) |info| return info;
if (%return prog.checkLineMatch()) |info| return info;
return error.MissingDebugInfo;
},
DW.LNE_set_address => {
@ -717,14 +717,14 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
//%%io.stdout.printf(
// " [0x{x8}] Special opcode {}: advance Address by {} to 0x{x} and Line by {} to {}\n",
// pos, adjusted_opcode, inc_addr, prog.address, inc_line, prog.line);
test (%return prog.checkLineMatch()) |info| return info;
if (%return prog.checkLineMatch()) |info| return info;
prog.basic_block = false;
} else {
switch (opcode) {
DW.LNS_copy => {
//%%io.stdout.printf(" [0x{x8}] Copy\n", pos);
test (%return prog.checkLineMatch()) |info| return info;
if (%return prog.checkLineMatch()) |info| return info;
prog.basic_block = false;
},
DW.LNS_advance_pc => {
@ -828,8 +828,8 @@ fn scanAllCompileUnits(st: &ElfStackTrace) -> %void {
return error.InvalidDebugInfo;
const pc_range = {
try (compile_unit_die.getAttrAddr(DW.AT_low_pc)) |low_pc| {
test (compile_unit_die.getAttr(DW.AT_high_pc)) |high_pc_value| {
if (compile_unit_die.getAttrAddr(DW.AT_low_pc)) |low_pc| {
if (compile_unit_die.getAttr(DW.AT_high_pc)) |high_pc_value| {
const pc_end = switch (*high_pc_value) {
FormValue.Address => |value| value,
FormValue.Const => |value| {
@ -867,7 +867,7 @@ fn scanAllCompileUnits(st: &ElfStackTrace) -> %void {
fn findCompileUnit(st: &ElfStackTrace, target_address: u64) -> ?&const CompileUnit {
for (st.compile_unit_list.toSlice()) |*compile_unit| {
test (compile_unit.pc_range) |range| {
if (compile_unit.pc_range) |range| {
if (target_address >= range.start and target_address < range.end)
return compile_unit;
}

View File

@ -247,7 +247,7 @@ test "basicHashMapTest" {
assert((??map.get(2)).value == 22);
_ = map.remove(2);
assert(map.remove(2) == null);
assert(test (map.get(2)) false else true);
assert(map.get(2) == null);
}
fn hash_i32(x: i32) -> u32 {

View File

@ -43,7 +43,7 @@ pub fn LinkedList(comptime T: type) -> type {
/// new_node: Pointer to the new node to insert.
pub fn insertAfter(list: &List, node: &Node, new_node: &Node) {
new_node.prev = node;
test (node.next) |next_node| {
if (node.next) |next_node| {
// Intermediate node.
new_node.next = next_node;
next_node.prev = new_node;
@ -64,7 +64,7 @@ pub fn LinkedList(comptime T: type) -> type {
/// new_node: Pointer to the new node to insert.
pub fn insertBefore(list: &List, node: &Node, new_node: &Node) {
new_node.next = node;
test (node.prev) |prev_node| {
if (node.prev) |prev_node| {
// Intermediate node.
new_node.prev = prev_node;
prev_node.next = new_node;
@ -83,7 +83,7 @@ pub fn LinkedList(comptime T: type) -> type {
/// Arguments:
/// new_node: Pointer to the new node to insert.
pub fn append(list: &List, new_node: &Node) {
test (list.last) |last| {
if (list.last) |last| {
// Insert after last.
list.insertAfter(last, new_node);
} else {
@ -97,7 +97,7 @@ pub fn LinkedList(comptime T: type) -> type {
/// Arguments:
/// new_node: Pointer to the new node to insert.
pub fn prepend(list: &List, new_node: &Node) {
test (list.first) |first| {
if (list.first) |first| {
// Insert before first.
list.insertBefore(first, new_node);
} else {
@ -116,7 +116,7 @@ pub fn LinkedList(comptime T: type) -> type {
/// Arguments:
/// node: Pointer to the node to be removed.
pub fn remove(list: &List, node: &Node) {
test (node.prev) |prev_node| {
if (node.prev) |prev_node| {
// Intermediate node.
prev_node.next = node.next;
} else {
@ -124,7 +124,7 @@ pub fn LinkedList(comptime T: type) -> type {
list.first = node.next;
}
test (node.next) |next_node| {
if (node.next) |next_node| {
// Intermediate node.
next_node.prev = node.prev;
} else {

View File

@ -59,9 +59,9 @@ pub const ChildProcess = struct {
errno.EINVAL, errno.ECHILD => unreachable,
errno.EINTR => continue,
else => {
test (self.stdin) |*stdin| { stdin.close(); }
test (self.stdout) |*stdout| { stdout.close(); }
test (self.stderr) |*stderr| { stderr.close(); }
if (self.stdin) |*stdin| { stdin.close(); }
if (self.stdout) |*stdout| { stdout.close(); }
if (self.stderr) |*stderr| { stderr.close(); }
return error.Unexpected;
},
}
@ -69,9 +69,9 @@ pub const ChildProcess = struct {
break;
}
test (self.stdin) |*stdin| { stdin.close(); }
test (self.stdout) |*stdout| { stdout.close(); }
test (self.stderr) |*stderr| { stderr.close(); }
if (self.stdin) |*stdin| { stdin.close(); }
if (self.stdout) |*stdout| { stdout.close(); }
if (self.stderr) |*stderr| { stderr.close(); }
// Write @maxValue(ErrInt) to the write end of the err_pipe. This is after
// waitpid, so this write is guaranteed to be after the child
@ -143,7 +143,7 @@ pub const ChildProcess = struct {
setUpChildIo(stderr, stderr_pipe[1], posix.STDERR_FILENO, dev_null_fd) %%
|err| forkChildErrReport(err_pipe[1], err);
test (maybe_cwd) |cwd| {
if (maybe_cwd) |cwd| {
os.changeCurDir(allocator, cwd) %%
|err| forkChildErrReport(err_pipe[1], err);
}

View File

@ -173,7 +173,7 @@ pub fn posixOpen(file_path: []const u8, flags: usize, perm: usize, allocator: ?&
if (file_path.len < stack_buf.len) {
path0 = stack_buf[0...file_path.len + 1];
} else test (allocator) |a| {
} else if (allocator) |a| {
path0 = %return a.alloc(u8, file_path.len + 1);
need_free = true;
} else {
@ -241,7 +241,7 @@ pub fn posixExecve(exe_path: []const u8, argv: []const []const u8, env_map: &con
mem.set(?&u8, argv_buf, null);
defer {
for (argv_buf) |arg| {
const arg_buf = test (arg) |ptr| cstr.toSlice(ptr) else break;
const arg_buf = if (arg) |ptr| cstr.toSlice(ptr) else break;
allocator.free(arg_buf);
}
allocator.free(argv_buf);
@ -268,7 +268,7 @@ pub fn posixExecve(exe_path: []const u8, argv: []const []const u8, env_map: &con
mem.set(?&u8, envp_buf, null);
defer {
for (envp_buf) |env| {
const env_buf = test (env) |ptr| cstr.toSlice(ptr) else break;
const env_buf = if (env) |ptr| cstr.toSlice(ptr) else break;
allocator.free(env_buf);
}
allocator.free(envp_buf);
@ -448,7 +448,7 @@ pub fn symLink(allocator: &Allocator, existing_path: []const u8, new_path: []con
const b64_fs_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=";
pub fn atomicSymLink(allocator: &Allocator, existing_path: []const u8, new_path: []const u8) -> %void {
try (symLink(allocator, existing_path, new_path)) {
if (symLink(allocator, existing_path, new_path)) {
return;
} else |err| {
if (err != error.PathAlreadyExists) {
@ -463,7 +463,7 @@ pub fn atomicSymLink(allocator: &Allocator, existing_path: []const u8, new_path:
while (true) {
%return getRandomBytes(rand_buf[0...]);
_ = base64.encodeWithAlphabet(tmp_path[new_path.len...], rand_buf, b64_fs_alphabet);
try (symLink(allocator, existing_path, tmp_path)) {
if (symLink(allocator, existing_path, tmp_path)) {
return rename(allocator, tmp_path, new_path);
} else |err| {
if (err == error.PathAlreadyExists) {
@ -668,7 +668,7 @@ pub fn deleteDir(allocator: &Allocator, dir_path: []const u8) -> %void {
pub fn deleteTree(allocator: &Allocator, full_path: []const u8) -> %void {
start_over:
// First, try deleting the item as a file. This way we don't follow sym links.
try (deleteFile(allocator, full_path)) {
if (deleteFile(allocator, full_path)) {
return;
} else |err| {
if (err == error.FileNotFound)

View File

@ -243,7 +243,7 @@ pub fn relative(allocator: &Allocator, from: []const u8, to: []const u8) -> %[]u
while (true) {
const from_component = from_it.next() ?? return mem.dupe(allocator, u8, to_it.rest());
const to_rest = to_it.rest();
test(to_it.next()) |to_component| {
if (to_it.next()) |to_component| {
if (mem.eql(u8, from_component, to_component))
continue;
}

View File

@ -63,7 +63,7 @@ pub fn main() -> %void {
%%io.stderr.printf("Expected option name after '-D'\n\n");
return usage(&builder, false, &io.stderr);
}
test (mem.indexOfScalar(u8, option_contents, '=')) |name_end| {
if (mem.indexOfScalar(u8, option_contents, '=')) |name_end| {
const option_name = option_contents[0...name_end];
const option_value = option_contents[name_end + 1...];
if (builder.addUserInputOption(option_name, option_value))

View File

@ -36,7 +36,7 @@ export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
// 0 X
// ---
// 0 X
test (maybe_rem) |rem| {
if (maybe_rem) |rem| {
*rem = n[low] % d[low];
}
return n[low] / d[low];
@ -44,7 +44,7 @@ export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
// 0 X
// ---
// K X
test (maybe_rem) |rem| {
if (maybe_rem) |rem| {
*rem = n[low];
}
return 0;
@ -55,7 +55,7 @@ export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
// K X
// ---
// 0 0
test (maybe_rem) |rem| {
if (maybe_rem) |rem| {
*rem = n[high] % d[low];
}
return n[high] / d[low];
@ -65,7 +65,7 @@ export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
// K 0
// ---
// K 0
test (maybe_rem) |rem| {
if (maybe_rem) |rem| {
r[high] = n[high] % d[high];
r[low] = 0;
*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 {
// K 0
// if d is a power of 2
if ((d[high] & (d[high] - 1)) == 0) {
test (maybe_rem) |rem| {
if (maybe_rem) |rem| {
r[low] = n[low];
r[high] = n[high] & (d[high] - 1);
*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 {
sr = @clz(su_int(d[high])) - @clz(su_int(n[high]));
// 0 <= sr <= n_uword_bits - 2 or sr large
if (sr > n_uword_bits - 2) {
test (maybe_rem) |rem| {
if (maybe_rem) |rem| {
*rem = *@ptrCast(&du_int, &n[0]);
}
return 0;
@ -111,7 +111,7 @@ export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
// 0 K
// if d is a power of 2
if ((d[low] & (d[low] - 1)) == 0) {
test (maybe_rem) |rem| {
if (maybe_rem) |rem| {
*rem = n[low] & (d[low] - 1);
}
if (d[low] == 1) {
@ -155,7 +155,7 @@ export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
sr = @clz(su_int(d[high])) - @clz(su_int(n[high]));
// 0 <= sr <= n_uword_bits - 1 or sr large
if (sr > n_uword_bits - 1) {
test (maybe_rem) |rem| {
if (maybe_rem) |rem| {
*rem = *@ptrCast(&du_int, &n[0]);
}
return 0;
@ -200,7 +200,7 @@ export fn __udivmoddi4(a: du_int, b: du_int, maybe_rem: ?&du_int) -> du_int {
sr -= 1;
}
*@ptrCast(&du_int, &q[0]) = (*@ptrCast(&du_int, &q[0]) << 1) | u64(carry);
test (maybe_rem) |rem| {
if (maybe_rem) |rem| {
*rem = *@ptrCast(&du_int, &r[0]);
}
return *@ptrCast(&du_int, &q[0]);

View File

@ -3,7 +3,7 @@ const assert = @import("std").debug.assert;
test "nullableType" {
const x : ?bool = @generatedCode(true);
test (x) |y| {
if (x) |y| {
if (y) {
// OK
} else {
@ -29,7 +29,7 @@ test "nullableType" {
test "test maybe object and get a pointer to the inner value" {
var maybe_bool: ?bool = true;
test (maybe_bool) |*b| {
if (maybe_bool) |*b| {
*b = false;
}
@ -50,7 +50,7 @@ test "maybe return" {
fn maybeReturnImpl() {
assert(??foo(1235));
test (foo(null))
if (foo(null) != null)
unreachable;
assert(!??foo(1234));
}
@ -66,10 +66,10 @@ test "ifVarMaybePointer" {
}
fn shouldBeAPlus1(p: &const Particle) -> u64 {
var maybe_particle: ?Particle = *p;
test (maybe_particle) |*particle| {
if (maybe_particle) |*particle| {
particle.a += 1;
}
test (maybe_particle) |particle| {
if (maybe_particle) |particle| {
return particle.a;
}
return 0;
@ -116,7 +116,7 @@ fn nullableVoidImpl() {
}
fn bar(x: ?void) -> ?void {
test (x) {
if (x) |_| {
return {};
} else {
return null;

View File

@ -7,7 +7,7 @@ test "tryOnErrorUnion" {
}
fn tryOnErrorUnionImpl() {
const x = try (returnsTen()) |val| {
const x = if (returnsTen()) |val| {
val + 1
} else |err| switch (err) {
error.ItBroke, error.NoMem => 1,
@ -24,16 +24,16 @@ fn returnsTen() -> %i32 {
}
test "tryWithoutVars" {
const result1 = try (failIfTrue(true)) {
const result1 = if (failIfTrue(true)) {
1
} else {
} else |_| {
i32(2)
};
assert(result1 == 2);
const result2 = try (failIfTrue(false)) {
const result2 = if (failIfTrue(false)) {
1
} else {
} else |_| {
i32(2)
};
assert(result2 == 1);
@ -48,7 +48,7 @@ fn failIfTrue(ok: bool) -> %void {
}
test "try then not executed with assignment" {
try (failIfTrue(true)) {
if (failIfTrue(true)) {
unreachable;
} else |err| {
assert(err == error.ItBroke);

View File

@ -118,38 +118,20 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
\\}
, ".tmp_source.zig:5:5: error: invalid token: 'var'");
cases.add("implicit semicolon - try statement",
\\export fn entry() {
\\ try (foo()) {}
\\ var good = {};
\\ try (foo()) ({})
\\ var bad = {};
\\}
, ".tmp_source.zig:5:5: error: invalid token: 'var'");
cases.add("implicit semicolon - try expression",
\\export fn entry() {
\\ _ = try (foo()) {};
\\ var good = {};
\\ _ = try (foo()) {}
\\ var bad = {};
\\}
, ".tmp_source.zig:5:5: error: invalid token: 'var'");
cases.add("implicit semicolon - test statement",
\\export fn entry() {
\\ test (foo()) {}
\\ if (foo()) |_| {}
\\ var good = {};
\\ test (foo()) ({})
\\ if (foo()) |_| ({})
\\ var bad = {};
\\}
, ".tmp_source.zig:5:5: error: invalid token: 'var'");
cases.add("implicit semicolon - test expression",
\\export fn entry() {
\\ _ = test (foo()) {};
\\ _ = if (foo()) |_| {};
\\ var good = {};
\\ _ = test (foo()) {}
\\ _ = if (foo()) |_| {}
\\ var bad = {};
\\}
, ".tmp_source.zig:5:5: error: invalid token: 'var'");
@ -500,9 +482,9 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
cases.add("invalid maybe type",
\\export fn f() {
\\ test (true) |x| { }
\\ if (true) |x| { }
\\}
, ".tmp_source.zig:2:11: error: expected nullable type, found 'bool'");
, ".tmp_source.zig:2:9: error: expected nullable type, found 'bool'");
cases.add("cast unreachable",
\\fn f() -> i32 {

View File

@ -349,7 +349,7 @@ pub const CompareOutputContext = struct {
switch (case.special) {
Special.Asm => {
const annotated_case_name = %%fmt.allocPrint(self.b.allocator, "assemble-and-link {}", case.name);
test (self.test_filter) |filter| {
if (self.test_filter) |filter| {
if (mem.indexOf(u8, annotated_case_name, filter) == null)
return;
}
@ -373,7 +373,7 @@ pub const CompareOutputContext = struct {
for ([]Mode{Mode.Debug, Mode.ReleaseFast}) |mode| {
const annotated_case_name = %%fmt.allocPrint(self.b.allocator, "{} {} ({})",
"compare-output", case.name, @enumTagName(mode));
test (self.test_filter) |filter| {
if (self.test_filter) |filter| {
if (mem.indexOf(u8, annotated_case_name, filter) == null)
continue;
}
@ -399,7 +399,7 @@ pub const CompareOutputContext = struct {
},
Special.DebugSafety => {
const annotated_case_name = %%fmt.allocPrint(self.b.allocator, "safety {}", case.name);
test (self.test_filter) |filter| {
if (self.test_filter) |filter| {
if (mem.indexOf(u8, annotated_case_name, filter) == null)
return;
}
@ -620,7 +620,7 @@ pub const CompileErrorContext = struct {
for ([]Mode{Mode.Debug, Mode.ReleaseFast}) |mode| {
const annotated_case_name = %%fmt.allocPrint(self.b.allocator, "compile-error {} ({})",
case.name, @enumTagName(mode));
test (self.test_filter) |filter| {
if (self.test_filter) |filter| {
if (mem.indexOf(u8, annotated_case_name, filter) == null)
continue;
}
@ -655,7 +655,7 @@ pub const BuildExamplesContext = struct {
const b = self.b;
const annotated_case_name = b.fmt("build {} (Debug)", build_file);
test (self.test_filter) |filter| {
if (self.test_filter) |filter| {
if (mem.indexOf(u8, annotated_case_name, filter) == null)
return;
}
@ -686,7 +686,7 @@ pub const BuildExamplesContext = struct {
for ([]Mode{Mode.Debug, Mode.ReleaseFast}) |mode| {
const annotated_case_name = %%fmt.allocPrint(self.b.allocator, "build {} ({})",
root_src, @enumTagName(mode));
test (self.test_filter) |filter| {
if (self.test_filter) |filter| {
if (mem.indexOf(u8, annotated_case_name, filter) == null)
continue;
}
@ -874,7 +874,7 @@ pub const ParseHContext = struct {
const b = self.b;
const annotated_case_name = %%fmt.allocPrint(self.b.allocator, "parseh {}", case.name);
test (self.test_filter) |filter| {
if (self.test_filter) |filter| {
if (mem.indexOf(u8, annotated_case_name, filter) == null)
return;
}