| author | |
| committer | |
| log | d917815d8111b98dc237cbe2c723fa63018e02b1 |
| tree | ce12771a86b2412ee9692ca73d3ca49abe5da3ce |
| parent | 8bc523219c66427951e5339550502871547f2138 |
instead of last statement being expression value
closes #629114 files changed, 1202 insertions(+), 1230 deletions(-)
doc/docgen.zig+1-1| ... | ... | @@ -49,7 +49,7 @@ fn gen(in: &io.InStream, out: &io.OutStream) { |
| 49 | 49 | if (err == error.EndOfStream) { |
| 50 | 50 | return; |
| 51 | 51 | } |
| 52 | std.debug.panic("{}", err) | |
| 52 | std.debug.panic("{}", err); | |
| 53 | 53 | }; |
| 54 | 54 | switch (state) { |
| 55 | 55 | State.Start => switch (byte) { |
doc/langref.html.in+2-3| ... | ... | @@ -3021,14 +3021,13 @@ const assert = @import("std").debug.assert;</code></pre> |
| 3021 | 3021 | <pre><code class="zig">const assert = @import("std").debug.assert; |
| 3022 | 3022 | |
| 3023 | 3023 | // Functions are declared like this |
| 3024 | // The last expression in the function can be used as the return value. | |
| 3025 | 3024 | fn add(a: i8, b: i8) -&gt; i8 { |
| 3026 | 3025 | if (a == 0) { |
| 3027 | 3026 | // You can still return manually if needed. |
| 3028 | 3027 | return b; |
| 3029 | 3028 | } |
| 3030 | 3029 | |
| 3031 | a + b | |
| 3030 | return a + b; | |
| 3032 | 3031 | } |
| 3033 | 3032 | |
| 3034 | 3033 | // The export specifier makes a function externally visible in the generated |
| ... | ... | @@ -5847,7 +5846,7 @@ ParamDeclList = "(" list(ParamDecl, ",") ")" |
| 5847 | 5846 | |
| 5848 | 5847 | ParamDecl = option("noalias" | "comptime") option(Symbol ":") (TypeExpr | "...") |
| 5849 | 5848 | |
| 5850 | Block = option(Symbol ":") "{" many(Statement) option(Expression) "}" | |
| 5849 | Block = option(Symbol ":") "{" many(Statement) "}" | |
| 5851 | 5850 | |
| 5852 | 5851 | Statement = LocalVarDecl ";" | Defer(Block) | Defer(Expression) ";" | BlockExpression(Block) | Expression ";" | ";" |
| 5853 | 5852 |
example/shared_library/mathtest.zig+1-1| ... | ... | @@ -1,3 +1,3 @@ |
| 1 | 1 | export fn add(a: i32, b: i32) -> i32 { |
| 2 | a + b | |
| 2 | return a + b; | |
| 3 | 3 | } |
src-self-hosted/parser.zig+12-12| ... | ... | @@ -111,11 +111,11 @@ pub const Parser = struct { |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | pub fn parse(self: &Parser) -> %&ast.NodeRoot { |
| 114 | const result = self.parseInner() %% |err| { | |
| 114 | const result = self.parseInner() %% |err| x: { | |
| 115 | 115 | if (self.cleanup_root_node) |root_node| { |
| 116 | 116 | self.freeAst(root_node); |
| 117 | 117 | } |
| 118 | err | |
| 118 | break :x err; | |
| 119 | 119 | }; |
| 120 | 120 | self.cleanup_root_node = null; |
| 121 | 121 | return result; |
| ... | ... | @@ -125,12 +125,12 @@ pub const Parser = struct { |
| 125 | 125 | var stack = self.initUtilityArrayList(State); |
| 126 | 126 | defer self.deinitUtilityArrayList(stack); |
| 127 | 127 | |
| 128 | const root_node = { | |
| 128 | const root_node = x: { | |
| 129 | 129 | const root_node = %return self.createRoot(); |
| 130 | 130 | %defer self.allocator.destroy(root_node); |
| 131 | 131 | // This stack append has to succeed for freeAst to work |
| 132 | 132 | %return stack.append(State.TopLevel); |
| 133 | root_node | |
| 133 | break :x root_node; | |
| 134 | 134 | }; |
| 135 | 135 | assert(self.cleanup_root_node == null); |
| 136 | 136 | self.cleanup_root_node = root_node; |
| ... | ... | @@ -462,7 +462,7 @@ pub const Parser = struct { |
| 462 | 462 | } else if (token.id == Token.Id.Keyword_noalias) { |
| 463 | 463 | param_decl.noalias_token = token; |
| 464 | 464 | token = self.getNextToken(); |
| 465 | }; | |
| 465 | } | |
| 466 | 466 | if (token.id == Token.Id.Identifier) { |
| 467 | 467 | const next_token = self.getNextToken(); |
| 468 | 468 | if (next_token.id == Token.Id.Colon) { |
| ... | ... | @@ -793,14 +793,14 @@ pub const Parser = struct { |
| 793 | 793 | } |
| 794 | 794 | |
| 795 | 795 | fn getNextToken(self: &Parser) -> Token { |
| 796 | return if (self.put_back_count != 0) { | |
| 796 | if (self.put_back_count != 0) { | |
| 797 | 797 | const put_back_index = self.put_back_count - 1; |
| 798 | 798 | const put_back_token = self.put_back_tokens[put_back_index]; |
| 799 | 799 | self.put_back_count = put_back_index; |
| 800 | put_back_token | |
| 800 | return put_back_token; | |
| 801 | 801 | } else { |
| 802 | self.tokenizer.next() | |
| 803 | }; | |
| 802 | return self.tokenizer.next(); | |
| 803 | } | |
| 804 | 804 | } |
| 805 | 805 | |
| 806 | 806 | const RenderAstFrame = struct { |
| ... | ... | @@ -873,7 +873,7 @@ pub const Parser = struct { |
| 873 | 873 | Token.Id.Keyword_pub => %return stream.print("pub "), |
| 874 | 874 | Token.Id.Keyword_export => %return stream.print("export "), |
| 875 | 875 | else => unreachable, |
| 876 | }; | |
| 876 | } | |
| 877 | 877 | } |
| 878 | 878 | if (fn_proto.extern_token) |extern_token| { |
| 879 | 879 | %return stream.print("{} ", self.tokenizer.getTokenSlice(extern_token)); |
| ... | ... | @@ -1102,7 +1102,7 @@ fn testParse(source: []const u8, allocator: &mem.Allocator) -> %[]u8 { |
| 1102 | 1102 | // TODO test for memory leaks |
| 1103 | 1103 | // TODO test for valid frees |
| 1104 | 1104 | fn testCanonical(source: []const u8) { |
| 1105 | const needed_alloc_count = { | |
| 1105 | const needed_alloc_count = x: { | |
| 1106 | 1106 | // Try it once with unlimited memory, make sure it works |
| 1107 | 1107 | var fixed_allocator = mem.FixedBufferAllocator.init(fixed_buffer_mem[0..]); |
| 1108 | 1108 | var failing_allocator = std.debug.FailingAllocator.init(&fixed_allocator.allocator, @maxValue(usize)); |
| ... | ... | @@ -1116,7 +1116,7 @@ fn testCanonical(source: []const u8) { |
| 1116 | 1116 | @panic("test failed"); |
| 1117 | 1117 | } |
| 1118 | 1118 | failing_allocator.allocator.free(result_source); |
| 1119 | failing_allocator.index | |
| 1119 | break :x failing_allocator.index; | |
| 1120 | 1120 | }; |
| 1121 | 1121 | |
| 1122 | 1122 | // TODO make this pass |
src/all_types.hpp+6-10| ... | ... | @@ -26,7 +26,6 @@ struct ScopeFnDef; |
| 26 | 26 | struct TypeTableEntry; |
| 27 | 27 | struct VariableTableEntry; |
| 28 | 28 | struct ErrorTableEntry; |
| 29 | struct LabelTableEntry; | |
| 30 | 29 | struct BuiltinFnEntry; |
| 31 | 30 | struct TypeStructField; |
| 32 | 31 | struct CodeGen; |
| ... | ... | @@ -54,7 +53,6 @@ struct IrExecutable { |
| 54 | 53 | size_t *backward_branch_count; |
| 55 | 54 | size_t backward_branch_quota; |
| 56 | 55 | bool invalid; |
| 57 | ZigList<LabelTableEntry *> all_labels; | |
| 58 | 56 | ZigList<IrGotoItem> goto_list; |
| 59 | 57 | bool is_inline; |
| 60 | 58 | FnTableEntry *fn_entry; |
| ... | ... | @@ -452,7 +450,6 @@ struct AstNodeParamDecl { |
| 452 | 450 | struct AstNodeBlock { |
| 453 | 451 | Buf *name; |
| 454 | 452 | ZigList<AstNode *> statements; |
| 455 | bool last_statement_is_result_expression; | |
| 456 | 453 | }; |
| 457 | 454 | |
| 458 | 455 | enum ReturnKind { |
| ... | ... | @@ -1644,12 +1641,6 @@ struct ErrorTableEntry { |
| 1644 | 1641 | ConstExprValue *cached_error_name_val; |
| 1645 | 1642 | }; |
| 1646 | 1643 | |
| 1647 | struct LabelTableEntry { | |
| 1648 | AstNode *decl_node; | |
| 1649 | IrBasicBlock *bb; | |
| 1650 | bool used; | |
| 1651 | }; | |
| 1652 | ||
| 1653 | 1644 | enum ScopeId { |
| 1654 | 1645 | ScopeIdDecls, |
| 1655 | 1646 | ScopeIdBlock, |
| ... | ... | @@ -1693,7 +1684,12 @@ struct ScopeDecls { |
| 1693 | 1684 | struct ScopeBlock { |
| 1694 | 1685 | Scope base; |
| 1695 | 1686 | |
| 1696 | HashMap<Buf *, LabelTableEntry *, buf_hash, buf_eql_buf> label_table; | |
| 1687 | Buf *name; | |
| 1688 | IrBasicBlock *end_block; | |
| 1689 | IrInstruction *is_comptime; | |
| 1690 | ZigList<IrInstruction *> *incoming_values; | |
| 1691 | ZigList<IrBasicBlock *> *incoming_blocks; | |
| 1692 | ||
| 1697 | 1693 | bool safety_off; |
| 1698 | 1694 | AstNode *safety_set_node; |
| 1699 | 1695 | bool fast_math_off; |
src/analyze.cpp+1-1| ... | ... | @@ -110,7 +110,7 @@ ScopeBlock *create_block_scope(AstNode *node, Scope *parent) { |
| 110 | 110 | assert(node->type == NodeTypeBlock); |
| 111 | 111 | ScopeBlock *scope = allocate<ScopeBlock>(1); |
| 112 | 112 | init_scope(&scope->base, ScopeIdBlock, node, parent); |
| 113 | scope->label_table.init(1); | |
| 113 | scope->name = node->data.block.name; | |
| 114 | 114 | return scope; |
| 115 | 115 | } |
| 116 | 116 |
src/ast_render.cpp+1-4| ... | ... | @@ -478,10 +478,7 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) { |
| 478 | 478 | AstNode *statement = node->data.block.statements.at(i); |
| 479 | 479 | print_indent(ar); |
| 480 | 480 | render_node_grouped(ar, statement); |
| 481 | if (!(i == node->data.block.statements.length - 1 && | |
| 482 | node->data.block.last_statement_is_result_expression)) { | |
| 483 | fprintf(ar->f, ";"); | |
| 484 | } | |
| 481 | fprintf(ar->f, ";"); | |
| 485 | 482 | fprintf(ar->f, "\n"); |
| 486 | 483 | } |
| 487 | 484 | ar->indent -= ar->indent_size; |
src/ir.cpp+64-32| ... | ... | @@ -3514,7 +3514,11 @@ static VariableTableEntry *ir_create_var(IrBuilder *irb, AstNode *node, Scope *s |
| 3514 | 3514 | static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode *block_node) { |
| 3515 | 3515 | assert(block_node->type == NodeTypeBlock); |
| 3516 | 3516 | |
| 3517 | ZigList<IrInstruction *> incoming_values = {0}; | |
| 3518 | ZigList<IrBasicBlock *> incoming_blocks = {0}; | |
| 3519 | ||
| 3517 | 3520 | ScopeBlock *scope_block = create_block_scope(block_node, parent_scope); |
| 3521 | ||
| 3518 | 3522 | Scope *outer_block_scope = &scope_block->base; |
| 3519 | 3523 | Scope *child_scope = outer_block_scope; |
| 3520 | 3524 | |
| ... | ... | @@ -3528,9 +3532,15 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode |
| 3528 | 3532 | return ir_mark_gen(ir_build_const_void(irb, child_scope, block_node)); |
| 3529 | 3533 | } |
| 3530 | 3534 | |
| 3535 | if (block_node->data.block.name != nullptr) { | |
| 3536 | scope_block->incoming_blocks = &incoming_blocks; | |
| 3537 | scope_block->incoming_values = &incoming_values; | |
| 3538 | scope_block->end_block = ir_build_basic_block(irb, parent_scope, "BlockEnd"); | |
| 3539 | scope_block->is_comptime = ir_build_const_bool(irb, parent_scope, block_node, ir_should_inline(irb->exec, parent_scope)); | |
| 3540 | } | |
| 3541 | ||
| 3531 | 3542 | bool is_continuation_unreachable = false; |
| 3532 | 3543 | IrInstruction *noreturn_return_value = nullptr; |
| 3533 | IrInstruction *return_value = nullptr; | |
| 3534 | 3544 | for (size_t i = 0; i < block_node->data.block.statements.length; i += 1) { |
| 3535 | 3545 | AstNode *statement_node = block_node->data.block.statements.at(i); |
| 3536 | 3546 | |
| ... | ... | @@ -3548,39 +3558,31 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode |
| 3548 | 3558 | // variable declarations start a new scope |
| 3549 | 3559 | IrInstructionDeclVar *decl_var_instruction = (IrInstructionDeclVar *)statement_value; |
| 3550 | 3560 | child_scope = decl_var_instruction->var->child_scope; |
| 3551 | } else { | |
| 3552 | // label, defer, variable declaration will never be the result expression | |
| 3553 | if (block_node->data.block.last_statement_is_result_expression && | |
| 3554 | i == block_node->data.block.statements.length - 1) { | |
| 3555 | // this is the result value statement | |
| 3556 | return_value = statement_value; | |
| 3557 | } else { | |
| 3558 | // there are more statements ahead of this one. this statement's value must be void | |
| 3559 | if (statement_value != irb->codegen->invalid_instruction) { | |
| 3560 | ir_mark_gen(ir_build_check_statement_is_void(irb, child_scope, statement_node, statement_value)); | |
| 3561 | } | |
| 3562 | } | |
| 3561 | } else if (statement_value != irb->codegen->invalid_instruction) { | |
| 3562 | // this statement's value must be void | |
| 3563 | ir_mark_gen(ir_build_check_statement_is_void(irb, child_scope, statement_node, statement_value)); | |
| 3563 | 3564 | } |
| 3564 | 3565 | } |
| 3565 | 3566 | |
| 3566 | 3567 | if (is_continuation_unreachable) { |
| 3567 | 3568 | assert(noreturn_return_value != nullptr); |
| 3568 | return noreturn_return_value; | |
| 3569 | if (block_node->data.block.name == nullptr || incoming_blocks.length == 0) { | |
| 3570 | return noreturn_return_value; | |
| 3571 | } | |
| 3572 | } else { | |
| 3573 | incoming_blocks.append(irb->current_basic_block); | |
| 3574 | incoming_values.append(ir_mark_gen(ir_build_const_void(irb, parent_scope, block_node))); | |
| 3569 | 3575 | } |
| 3570 | // control flow falls out of block | |
| 3571 | 3576 | |
| 3572 | if (block_node->data.block.last_statement_is_result_expression) { | |
| 3573 | // return value was determined by the last statement | |
| 3574 | assert(return_value != nullptr); | |
| 3577 | if (block_node->data.block.name != nullptr) { | |
| 3578 | ir_gen_defers_for_block(irb, child_scope, outer_block_scope, false); | |
| 3579 | ir_mark_gen(ir_build_br(irb, parent_scope, block_node, scope_block->end_block, scope_block->is_comptime)); | |
| 3580 | ir_set_cursor_at_end(irb, scope_block->end_block); | |
| 3581 | return ir_build_phi(irb, parent_scope, block_node, incoming_blocks.length, incoming_blocks.items, incoming_values.items); | |
| 3575 | 3582 | } else { |
| 3576 | // return value is implicitly void | |
| 3577 | assert(return_value == nullptr); | |
| 3578 | return_value = ir_mark_gen(ir_build_const_void(irb, child_scope, block_node)); | |
| 3583 | ir_gen_defers_for_block(irb, child_scope, outer_block_scope, false); | |
| 3584 | return ir_mark_gen(ir_mark_gen(ir_build_const_void(irb, child_scope, block_node))); | |
| 3579 | 3585 | } |
| 3580 | ||
| 3581 | ir_gen_defers_for_block(irb, child_scope, outer_block_scope, false); | |
| 3582 | ||
| 3583 | return return_value; | |
| 3584 | 3586 | } |
| 3585 | 3587 | |
| 3586 | 3588 | static IrInstruction *ir_gen_bin_op_id(IrBuilder *irb, Scope *scope, AstNode *node, IrBinOp op_id) { |
| ... | ... | @@ -5952,6 +5954,31 @@ static IrInstruction *ir_gen_comptime(IrBuilder *irb, Scope *parent_scope, AstNo |
| 5952 | 5954 | return ir_gen_node_extra(irb, node->data.comptime_expr.expr, child_scope, lval); |
| 5953 | 5955 | } |
| 5954 | 5956 | |
| 5957 | static IrInstruction *ir_gen_return_from_block(IrBuilder *irb, Scope *break_scope, AstNode *node, ScopeBlock *block_scope) { | |
| 5958 | IrInstruction *is_comptime; | |
| 5959 | if (ir_should_inline(irb->exec, break_scope)) { | |
| 5960 | is_comptime = ir_build_const_bool(irb, break_scope, node, true); | |
| 5961 | } else { | |
| 5962 | is_comptime = block_scope->is_comptime; | |
| 5963 | } | |
| 5964 | ||
| 5965 | IrInstruction *result_value; | |
| 5966 | if (node->data.break_expr.expr) { | |
| 5967 | result_value = ir_gen_node(irb, node->data.break_expr.expr, break_scope); | |
| 5968 | if (result_value == irb->codegen->invalid_instruction) | |
| 5969 | return irb->codegen->invalid_instruction; | |
| 5970 | } else { | |
| 5971 | result_value = ir_build_const_void(irb, break_scope, node); | |
| 5972 | } | |
| 5973 | ||
| 5974 | IrBasicBlock *dest_block = block_scope->end_block; | |
| 5975 | ir_gen_defers_for_block(irb, break_scope, dest_block->scope, false); | |
| 5976 | ||
| 5977 | block_scope->incoming_blocks->append(irb->current_basic_block); | |
| 5978 | block_scope->incoming_values->append(result_value); | |
| 5979 | return ir_build_br(irb, break_scope, node, dest_block, is_comptime); | |
| 5980 | } | |
| 5981 | ||
| 5955 | 5982 | static IrInstruction *ir_gen_break(IrBuilder *irb, Scope *break_scope, AstNode *node) { |
| 5956 | 5983 | assert(node->type == NodeTypeBreak); |
| 5957 | 5984 | |
| ... | ... | @@ -5959,14 +5986,14 @@ static IrInstruction *ir_gen_break(IrBuilder *irb, Scope *break_scope, AstNode * |
| 5959 | 5986 | // * function definition scope or global scope => error, break outside loop |
| 5960 | 5987 | // * defer expression scope => error, cannot break out of defer expression |
| 5961 | 5988 | // * loop scope => OK |
| 5989 | // * (if it's a labeled break) labeled block => OK | |
| 5962 | 5990 | |
| 5963 | 5991 | Scope *search_scope = break_scope; |
| 5964 | 5992 | ScopeLoop *loop_scope; |
| 5965 | bool saw_any_loop_scope = false; | |
| 5966 | 5993 | for (;;) { |
| 5967 | 5994 | if (search_scope == nullptr || search_scope->id == ScopeIdFnDef) { |
| 5968 | if (saw_any_loop_scope) { | |
| 5969 | add_node_error(irb->codegen, node, buf_sprintf("labeled loop not found: '%s'", buf_ptr(node->data.break_expr.name))); | |
| 5995 | if (node->data.break_expr.name != nullptr) { | |
| 5996 | add_node_error(irb->codegen, node, buf_sprintf("label not found: '%s'", buf_ptr(node->data.break_expr.name))); | |
| 5970 | 5997 | return irb->codegen->invalid_instruction; |
| 5971 | 5998 | } else { |
| 5972 | 5999 | add_node_error(irb->codegen, node, buf_sprintf("break expression outside loop")); |
| ... | ... | @@ -5977,13 +6004,20 @@ static IrInstruction *ir_gen_break(IrBuilder *irb, Scope *break_scope, AstNode * |
| 5977 | 6004 | return irb->codegen->invalid_instruction; |
| 5978 | 6005 | } else if (search_scope->id == ScopeIdLoop) { |
| 5979 | 6006 | ScopeLoop *this_loop_scope = (ScopeLoop *)search_scope; |
| 5980 | saw_any_loop_scope = true; | |
| 5981 | 6007 | if (node->data.break_expr.name == nullptr || |
| 5982 | 6008 | (this_loop_scope->name != nullptr && buf_eql_buf(node->data.break_expr.name, this_loop_scope->name))) |
| 5983 | 6009 | { |
| 5984 | 6010 | loop_scope = this_loop_scope; |
| 5985 | 6011 | break; |
| 5986 | 6012 | } |
| 6013 | } else if (search_scope->id == ScopeIdBlock) { | |
| 6014 | ScopeBlock *this_block_scope = (ScopeBlock *)search_scope; | |
| 6015 | if (node->data.break_expr.name != nullptr && | |
| 6016 | (this_block_scope->name != nullptr && buf_eql_buf(node->data.break_expr.name, this_block_scope->name))) | |
| 6017 | { | |
| 6018 | assert(this_block_scope->end_block != nullptr); | |
| 6019 | return ir_gen_return_from_block(irb, break_scope, node, this_block_scope); | |
| 6020 | } | |
| 5987 | 6021 | } |
| 5988 | 6022 | search_scope = search_scope->parent; |
| 5989 | 6023 | } |
| ... | ... | @@ -6022,10 +6056,9 @@ static IrInstruction *ir_gen_continue(IrBuilder *irb, Scope *continue_scope, Ast |
| 6022 | 6056 | |
| 6023 | 6057 | Scope *search_scope = continue_scope; |
| 6024 | 6058 | ScopeLoop *loop_scope; |
| 6025 | bool saw_any_loop_scope = false; | |
| 6026 | 6059 | for (;;) { |
| 6027 | 6060 | if (search_scope == nullptr || search_scope->id == ScopeIdFnDef) { |
| 6028 | if (saw_any_loop_scope) { | |
| 6061 | if (node->data.continue_expr.name != nullptr) { | |
| 6029 | 6062 | add_node_error(irb->codegen, node, buf_sprintf("labeled loop not found: '%s'", buf_ptr(node->data.continue_expr.name))); |
| 6030 | 6063 | return irb->codegen->invalid_instruction; |
| 6031 | 6064 | } else { |
| ... | ... | @@ -6037,7 +6070,6 @@ static IrInstruction *ir_gen_continue(IrBuilder *irb, Scope *continue_scope, Ast |
| 6037 | 6070 | return irb->codegen->invalid_instruction; |
| 6038 | 6071 | } else if (search_scope->id == ScopeIdLoop) { |
| 6039 | 6072 | ScopeLoop *this_loop_scope = (ScopeLoop *)search_scope; |
| 6040 | saw_any_loop_scope = true; | |
| 6041 | 6073 | if (node->data.continue_expr.name == nullptr || |
| 6042 | 6074 | (this_loop_scope->name != nullptr && buf_eql_buf(node->data.continue_expr.name, this_loop_scope->name))) |
| 6043 | 6075 | { |
src/parser.cpp+20-33| ... | ... | @@ -748,7 +748,14 @@ static AstNode *ast_parse_primary_expr(ParseContext *pc, size_t *token_index, bo |
| 748 | 748 | node->data.fn_call_expr.is_builtin = true; |
| 749 | 749 | |
| 750 | 750 | return node; |
| 751 | } else if (token->id == TokenIdSymbol) { | |
| 751 | } | |
| 752 | ||
| 753 | AstNode *block_expr_node = ast_parse_block_expr(pc, token_index, false); | |
| 754 | if (block_expr_node) { | |
| 755 | return block_expr_node; | |
| 756 | } | |
| 757 | ||
| 758 | if (token->id == TokenIdSymbol) { | |
| 752 | 759 | *token_index += 1; |
| 753 | 760 | AstNode *node = ast_create_node(pc, NodeTypeSymbol, token); |
| 754 | 761 | node->data.symbol_expr.symbol = token_buf(token); |
| ... | ... | @@ -760,11 +767,6 @@ static AstNode *ast_parse_primary_expr(ParseContext *pc, size_t *token_index, bo |
| 760 | 767 | return grouped_expr_node; |
| 761 | 768 | } |
| 762 | 769 | |
| 763 | AstNode *block_expr_node = ast_parse_block_expr(pc, token_index, false); | |
| 764 | if (block_expr_node) { | |
| 765 | return block_expr_node; | |
| 766 | } | |
| 767 | ||
| 768 | 770 | AstNode *array_type_node = ast_parse_array_type_expr(pc, token_index, false); |
| 769 | 771 | if (array_type_node) { |
| 770 | 772 | return array_type_node; |
| ... | ... | @@ -2145,9 +2147,6 @@ static AstNode *ast_parse_expression(ParseContext *pc, size_t *token_index, bool |
| 2145 | 2147 | return nullptr; |
| 2146 | 2148 | } |
| 2147 | 2149 | |
| 2148 | /* | |
| 2149 | Label: token(Symbol) token(Colon) | |
| 2150 | */ | |
| 2151 | 2150 | static bool statement_terminates_without_semicolon(AstNode *node) { |
| 2152 | 2151 | switch (node->type) { |
| 2153 | 2152 | case NodeTypeIfBoolExpr: |
| ... | ... | @@ -2179,7 +2178,7 @@ static bool statement_terminates_without_semicolon(AstNode *node) { |
| 2179 | 2178 | } |
| 2180 | 2179 | |
| 2181 | 2180 | /* |
| 2182 | Block = option(Symbol ":") "{" many(Statement) option(Expression) "}" | |
| 2181 | Block = option(Symbol ":") "{" many(Statement) "}" | |
| 2183 | 2182 | Statement = Label | VariableDeclaration ";" | Defer(Block) | Defer(Expression) ";" | BlockExpression(Block) | Expression ";" | ";" | ExportDecl |
| 2184 | 2183 | */ |
| 2185 | 2184 | static AstNode *ast_parse_block(ParseContext *pc, size_t *token_index, bool mandatory) { |
| ... | ... | @@ -2220,6 +2219,12 @@ static AstNode *ast_parse_block(ParseContext *pc, size_t *token_index, bool mand |
| 2220 | 2219 | } |
| 2221 | 2220 | |
| 2222 | 2221 | for (;;) { |
| 2222 | last_token = &pc->tokens->at(*token_index); | |
| 2223 | if (last_token->id == TokenIdRBrace) { | |
| 2224 | *token_index += 1; | |
| 2225 | return node; | |
| 2226 | } | |
| 2227 | ||
| 2223 | 2228 | AstNode *statement_node = ast_parse_local_var_decl(pc, token_index); |
| 2224 | 2229 | if (!statement_node) |
| 2225 | 2230 | statement_node = ast_parse_defer_expr(pc, token_index); |
| ... | ... | @@ -2228,32 +2233,14 @@ static AstNode *ast_parse_block(ParseContext *pc, size_t *token_index, bool mand |
| 2228 | 2233 | if (!statement_node) |
| 2229 | 2234 | statement_node = ast_parse_expression(pc, token_index, false); |
| 2230 | 2235 | |
| 2231 | bool semicolon_expected = true; | |
| 2232 | if (statement_node) { | |
| 2233 | node->data.block.statements.append(statement_node); | |
| 2234 | if (statement_terminates_without_semicolon(statement_node)) { | |
| 2235 | semicolon_expected = false; | |
| 2236 | } else { | |
| 2237 | if (statement_node->type == NodeTypeDefer) { | |
| 2238 | // defer without a block body requires a semicolon | |
| 2239 | Token *token = &pc->tokens->at(*token_index); | |
| 2240 | ast_expect_token(pc, token, TokenIdSemicolon); | |
| 2241 | } | |
| 2242 | } | |
| 2236 | if (!statement_node) { | |
| 2237 | ast_invalid_token_error(pc, last_token); | |
| 2243 | 2238 | } |
| 2244 | 2239 | |
| 2245 | node->data.block.last_statement_is_result_expression = statement_node && statement_node->type != NodeTypeDefer; | |
| 2240 | node->data.block.statements.append(statement_node); | |
| 2246 | 2241 | |
| 2247 | last_token = &pc->tokens->at(*token_index); | |
| 2248 | if (last_token->id == TokenIdRBrace) { | |
| 2249 | *token_index += 1; | |
| 2250 | return node; | |
| 2251 | } else if (!semicolon_expected) { | |
| 2252 | continue; | |
| 2253 | } else if (last_token->id == TokenIdSemicolon) { | |
| 2254 | *token_index += 1; | |
| 2255 | } else { | |
| 2256 | ast_invalid_token_error(pc, last_token); | |
| 2242 | if (!statement_terminates_without_semicolon(statement_node)) { | |
| 2243 | ast_eat_token(pc, token_index, TokenIdSemicolon); | |
| 2257 | 2244 | } |
| 2258 | 2245 | } |
| 2259 | 2246 | zig_unreachable(); |
src/translate_c.cpp+54-32| ... | ... | @@ -171,6 +171,20 @@ static AstNode * trans_create_node(Context *c, NodeType id) { |
| 171 | 171 | return node; |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | static AstNode *trans_create_node_break(Context *c, Buf *label_name, AstNode *value_node) { | |
| 175 | AstNode *node = trans_create_node(c, NodeTypeBreak); | |
| 176 | node->data.break_expr.name = label_name; | |
| 177 | node->data.break_expr.expr = value_node; | |
| 178 | return node; | |
| 179 | } | |
| 180 | ||
| 181 | static AstNode *trans_create_node_return(Context *c, AstNode *value_node) { | |
| 182 | AstNode *node = trans_create_node(c, NodeTypeReturnExpr); | |
| 183 | node->data.return_expr.kind = ReturnKindUnconditional; | |
| 184 | node->data.return_expr.expr = value_node; | |
| 185 | return node; | |
| 186 | } | |
| 187 | ||
| 174 | 188 | static AstNode *trans_create_node_if(Context *c, AstNode *cond_node, AstNode *then_node, AstNode *else_node) { |
| 175 | 189 | AstNode *node = trans_create_node(c, NodeTypeIfBoolExpr); |
| 176 | 190 | node->data.if_bool_expr.condition = cond_node; |
| ... | ... | @@ -372,8 +386,7 @@ static AstNode *trans_create_node_inline_fn(Context *c, Buf *fn_name, AstNode *r |
| 372 | 386 | |
| 373 | 387 | AstNode *block = trans_create_node(c, NodeTypeBlock); |
| 374 | 388 | block->data.block.statements.resize(1); |
| 375 | block->data.block.statements.items[0] = fn_call_node; | |
| 376 | block->data.block.last_statement_is_result_expression = true; | |
| 389 | block->data.block.statements.items[0] = trans_create_node_return(c, fn_call_node); | |
| 377 | 390 | |
| 378 | 391 | fn_def->data.fn_def.body = block; |
| 379 | 392 | return fn_def; |
| ... | ... | @@ -1140,13 +1153,15 @@ static AstNode *trans_create_assign(Context *c, ResultUsed result_used, TransSco |
| 1140 | 1153 | } else { |
| 1141 | 1154 | // worst case |
| 1142 | 1155 | // c: lhs = rhs |
| 1143 | // zig: { | |
| 1156 | // zig: x: { | |
| 1144 | 1157 | // zig: const _tmp = rhs; |
| 1145 | 1158 | // zig: lhs = _tmp; |
| 1146 | // zig: _tmp | |
| 1159 | // zig: break :x _tmp | |
| 1147 | 1160 | // zig: } |
| 1148 | 1161 | |
| 1149 | 1162 | TransScopeBlock *child_scope = trans_scope_block_create(c, scope); |
| 1163 | Buf *label_name = buf_create_from_str("x"); | |
| 1164 | child_scope->node->data.block.name = label_name; | |
| 1150 | 1165 | |
| 1151 | 1166 | // const _tmp = rhs; |
| 1152 | 1167 | AstNode *rhs_node = trans_expr(c, ResultUsedYes, &child_scope->base, rhs, TransRValue); |
| ... | ... | @@ -1163,9 +1178,9 @@ static AstNode *trans_create_assign(Context *c, ResultUsed result_used, TransSco |
| 1163 | 1178 | trans_create_node_bin_op(c, lhs_node, BinOpTypeAssign, |
| 1164 | 1179 | trans_create_node_symbol(c, tmp_var_name))); |
| 1165 | 1180 | |
| 1166 | // _tmp | |
| 1167 | child_scope->node->data.block.statements.append(trans_create_node_symbol(c, tmp_var_name)); | |
| 1168 | child_scope->node->data.block.last_statement_is_result_expression = true; | |
| 1181 | // break :x _tmp | |
| 1182 | AstNode *tmp_symbol_node = trans_create_node_symbol(c, tmp_var_name); | |
| 1183 | child_scope->node->data.block.statements.append(trans_create_node_break(c, label_name, tmp_symbol_node)); | |
| 1169 | 1184 | |
| 1170 | 1185 | return child_scope->node; |
| 1171 | 1186 | } |
| ... | ... | @@ -1270,6 +1285,9 @@ static AstNode *trans_binary_operator(Context *c, ResultUsed result_used, TransS |
| 1270 | 1285 | case BO_Comma: |
| 1271 | 1286 | { |
| 1272 | 1287 | TransScopeBlock *scope_block = trans_scope_block_create(c, scope); |
| 1288 | Buf *label_name = buf_create_from_str("x"); | |
| 1289 | scope_block->node->data.block.name = label_name; | |
| 1290 | ||
| 1273 | 1291 | AstNode *lhs = trans_expr(c, ResultUsedNo, &scope_block->base, stmt->getLHS(), TransRValue); |
| 1274 | 1292 | if (lhs == nullptr) |
| 1275 | 1293 | return nullptr; |
| ... | ... | @@ -1278,9 +1296,7 @@ static AstNode *trans_binary_operator(Context *c, ResultUsed result_used, TransS |
| 1278 | 1296 | AstNode *rhs = trans_expr(c, result_used, &scope_block->base, stmt->getRHS(), TransRValue); |
| 1279 | 1297 | if (rhs == nullptr) |
| 1280 | 1298 | return nullptr; |
| 1281 | scope_block->node->data.block.statements.append(maybe_suppress_result(c, result_used, rhs)); | |
| 1282 | ||
| 1283 | scope_block->node->data.block.last_statement_is_result_expression = true; | |
| 1299 | scope_block->node->data.block.statements.append(trans_create_node_break(c, label_name, maybe_suppress_result(c, result_used, rhs))); | |
| 1284 | 1300 | return scope_block->node; |
| 1285 | 1301 | } |
| 1286 | 1302 | case BO_MulAssign: |
| ... | ... | @@ -1320,14 +1336,16 @@ static AstNode *trans_create_compound_assign_shift(Context *c, ResultUsed result |
| 1320 | 1336 | } else { |
| 1321 | 1337 | // need more complexity. worst case, this looks like this: |
| 1322 | 1338 | // c: lhs >>= rhs |
| 1323 | // zig: { | |
| 1339 | // zig: x: { | |
| 1324 | 1340 | // zig: const _ref = &lhs; |
| 1325 | 1341 | // zig: *_ref = result_type(operation_type(*_ref) >> u5(rhs)); |
| 1326 | // zig: *_ref | |
| 1342 | // zig: break :x *_ref | |
| 1327 | 1343 | // zig: } |
| 1328 | 1344 | // where u5 is the appropriate type |
| 1329 | 1345 | |
| 1330 | 1346 | TransScopeBlock *child_scope = trans_scope_block_create(c, scope); |
| 1347 | Buf *label_name = buf_create_from_str("x"); | |
| 1348 | child_scope->node->data.block.name = label_name; | |
| 1331 | 1349 | |
| 1332 | 1350 | // const _ref = &lhs; |
| 1333 | 1351 | AstNode *lhs = trans_expr(c, ResultUsedYes, &child_scope->base, stmt->getLHS(), TransLValue); |
| ... | ... | @@ -1369,11 +1387,11 @@ static AstNode *trans_create_compound_assign_shift(Context *c, ResultUsed result |
| 1369 | 1387 | child_scope->node->data.block.statements.append(assign_statement); |
| 1370 | 1388 | |
| 1371 | 1389 | if (result_used == ResultUsedYes) { |
| 1372 | // *_ref | |
| 1390 | // break :x *_ref | |
| 1373 | 1391 | child_scope->node->data.block.statements.append( |
| 1374 | trans_create_node_prefix_op(c, PrefixOpDereference, | |
| 1375 | trans_create_node_symbol(c, tmp_var_name))); | |
| 1376 | child_scope->node->data.block.last_statement_is_result_expression = true; | |
| 1392 | trans_create_node_break(c, label_name, | |
| 1393 | trans_create_node_prefix_op(c, PrefixOpDereference, | |
| 1394 | trans_create_node_symbol(c, tmp_var_name)))); | |
| 1377 | 1395 | } |
| 1378 | 1396 | |
| 1379 | 1397 | return child_scope->node; |
| ... | ... | @@ -1394,13 +1412,15 @@ static AstNode *trans_create_compound_assign(Context *c, ResultUsed result_used, |
| 1394 | 1412 | } else { |
| 1395 | 1413 | // need more complexity. worst case, this looks like this: |
| 1396 | 1414 | // c: lhs += rhs |
| 1397 | // zig: { | |
| 1415 | // zig: x: { | |
| 1398 | 1416 | // zig: const _ref = &lhs; |
| 1399 | 1417 | // zig: *_ref = *_ref + rhs; |
| 1400 | // zig: *_ref | |
| 1418 | // zig: break :x *_ref | |
| 1401 | 1419 | // zig: } |
| 1402 | 1420 | |
| 1403 | 1421 | TransScopeBlock *child_scope = trans_scope_block_create(c, scope); |
| 1422 | Buf *label_name = buf_create_from_str("x"); | |
| 1423 | child_scope->node->data.block.name = label_name; | |
| 1404 | 1424 | |
| 1405 | 1425 | // const _ref = &lhs; |
| 1406 | 1426 | AstNode *lhs = trans_expr(c, ResultUsedYes, &child_scope->base, stmt->getLHS(), TransLValue); |
| ... | ... | @@ -1427,11 +1447,11 @@ static AstNode *trans_create_compound_assign(Context *c, ResultUsed result_used, |
| 1427 | 1447 | rhs)); |
| 1428 | 1448 | child_scope->node->data.block.statements.append(assign_statement); |
| 1429 | 1449 | |
| 1430 | // *_ref | |
| 1450 | // break :x *_ref | |
| 1431 | 1451 | child_scope->node->data.block.statements.append( |
| 1432 | trans_create_node_prefix_op(c, PrefixOpDereference, | |
| 1433 | trans_create_node_symbol(c, tmp_var_name))); | |
| 1434 | child_scope->node->data.block.last_statement_is_result_expression = true; | |
| 1452 | trans_create_node_break(c, label_name, | |
| 1453 | trans_create_node_prefix_op(c, PrefixOpDereference, | |
| 1454 | trans_create_node_symbol(c, tmp_var_name)))); | |
| 1435 | 1455 | |
| 1436 | 1456 | return child_scope->node; |
| 1437 | 1457 | } |
| ... | ... | @@ -1726,13 +1746,15 @@ static AstNode *trans_create_post_crement(Context *c, ResultUsed result_used, Tr |
| 1726 | 1746 | } |
| 1727 | 1747 | // worst case |
| 1728 | 1748 | // c: expr++ |
| 1729 | // zig: { | |
| 1749 | // zig: x: { | |
| 1730 | 1750 | // zig: const _ref = &expr; |
| 1731 | 1751 | // zig: const _tmp = *_ref; |
| 1732 | 1752 | // zig: *_ref += 1; |
| 1733 | // zig: _tmp | |
| 1753 | // zig: break :x _tmp | |
| 1734 | 1754 | // zig: } |
| 1735 | 1755 | TransScopeBlock *child_scope = trans_scope_block_create(c, scope); |
| 1756 | Buf *label_name = buf_create_from_str("x"); | |
| 1757 | child_scope->node->data.block.name = label_name; | |
| 1736 | 1758 | |
| 1737 | 1759 | // const _ref = &expr; |
| 1738 | 1760 | AstNode *expr = trans_expr(c, ResultUsedYes, &child_scope->base, op_expr, TransLValue); |
| ... | ... | @@ -1758,9 +1780,8 @@ static AstNode *trans_create_post_crement(Context *c, ResultUsed result_used, Tr |
| 1758 | 1780 | trans_create_node_unsigned(c, 1)); |
| 1759 | 1781 | child_scope->node->data.block.statements.append(assign_statement); |
| 1760 | 1782 | |
| 1761 | // _tmp | |
| 1762 | child_scope->node->data.block.statements.append(trans_create_node_symbol(c, tmp_var_name)); | |
| 1763 | child_scope->node->data.block.last_statement_is_result_expression = true; | |
| 1783 | // break :x _tmp | |
| 1784 | child_scope->node->data.block.statements.append(trans_create_node_break(c, label_name, trans_create_node_symbol(c, tmp_var_name))); | |
| 1764 | 1785 | |
| 1765 | 1786 | return child_scope->node; |
| 1766 | 1787 | } |
| ... | ... | @@ -1781,12 +1802,14 @@ static AstNode *trans_create_pre_crement(Context *c, ResultUsed result_used, Tra |
| 1781 | 1802 | } |
| 1782 | 1803 | // worst case |
| 1783 | 1804 | // c: ++expr |
| 1784 | // zig: { | |
| 1805 | // zig: x: { | |
| 1785 | 1806 | // zig: const _ref = &expr; |
| 1786 | 1807 | // zig: *_ref += 1; |
| 1787 | // zig: *_ref | |
| 1808 | // zig: break :x *_ref | |
| 1788 | 1809 | // zig: } |
| 1789 | 1810 | TransScopeBlock *child_scope = trans_scope_block_create(c, scope); |
| 1811 | Buf *label_name = buf_create_from_str("x"); | |
| 1812 | child_scope->node->data.block.name = label_name; | |
| 1790 | 1813 | |
| 1791 | 1814 | // const _ref = &expr; |
| 1792 | 1815 | AstNode *expr = trans_expr(c, ResultUsedYes, &child_scope->base, op_expr, TransLValue); |
| ... | ... | @@ -1805,11 +1828,10 @@ static AstNode *trans_create_pre_crement(Context *c, ResultUsed result_used, Tra |
| 1805 | 1828 | trans_create_node_unsigned(c, 1)); |
| 1806 | 1829 | child_scope->node->data.block.statements.append(assign_statement); |
| 1807 | 1830 | |
| 1808 | // *_ref | |
| 1831 | // break :x *_ref | |
| 1809 | 1832 | AstNode *deref_expr = trans_create_node_prefix_op(c, PrefixOpDereference, |
| 1810 | 1833 | trans_create_node_symbol(c, ref_var_name)); |
| 1811 | child_scope->node->data.block.statements.append(deref_expr); | |
| 1812 | child_scope->node->data.block.last_statement_is_result_expression = true; | |
| 1834 | child_scope->node->data.block.statements.append(trans_create_node_break(c, label_name, deref_expr)); | |
| 1813 | 1835 | |
| 1814 | 1836 | return child_scope->node; |
| 1815 | 1837 | } |
std/array_list.zig+4-4| ... | ... | @@ -8,7 +8,7 @@ pub fn ArrayList(comptime T: type) -> type { |
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | pub fn AlignedArrayList(comptime T: type, comptime A: u29) -> type{ |
| 11 | struct { | |
| 11 | return struct { | |
| 12 | 12 | const Self = this; |
| 13 | 13 | |
| 14 | 14 | /// Use toSlice instead of slicing this directly, because if you don't |
| ... | ... | @@ -20,11 +20,11 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) -> type{ |
| 20 | 20 | |
| 21 | 21 | /// Deinitialize with `deinit` or use `toOwnedSlice`. |
| 22 | 22 | pub fn init(allocator: &Allocator) -> Self { |
| 23 | Self { | |
| 23 | return Self { | |
| 24 | 24 | .items = []align(A) T{}, |
| 25 | 25 | .len = 0, |
| 26 | 26 | .allocator = allocator, |
| 27 | } | |
| 27 | }; | |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | pub fn deinit(l: &Self) { |
| ... | ... | @@ -107,7 +107,7 @@ pub fn AlignedArrayList(comptime T: type, comptime A: u29) -> type{ |
| 107 | 107 | return null; |
| 108 | 108 | return self.pop(); |
| 109 | 109 | } |
| 110 | } | |
| 110 | }; | |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | test "basic ArrayList test" { |
std/buffer.zig+3-3| ... | ... | @@ -30,9 +30,9 @@ pub const Buffer = struct { |
| 30 | 30 | /// * ::replaceContentsBuffer |
| 31 | 31 | /// * ::resize |
| 32 | 32 | pub fn initNull(allocator: &Allocator) -> Buffer { |
| 33 | Buffer { | |
| 33 | return Buffer { | |
| 34 | 34 | .list = ArrayList(u8).init(allocator), |
| 35 | } | |
| 35 | }; | |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | /// Must deinitialize with deinit. |
| ... | ... | @@ -120,7 +120,7 @@ pub const Buffer = struct { |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | pub fn eql(self: &const Buffer, m: []const u8) -> bool { |
| 123 | mem.eql(u8, self.toSliceConst(), m) | |
| 123 | return mem.eql(u8, self.toSliceConst(), m); | |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | pub fn startsWith(self: &const Buffer, m: []const u8) -> bool { |
std/build.zig+26-30| ... | ... | @@ -221,11 +221,11 @@ pub const Builder = struct { |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | pub fn version(self: &const Builder, major: u32, minor: u32, patch: u32) -> Version { |
| 224 | Version { | |
| 224 | return Version { | |
| 225 | 225 | .major = major, |
| 226 | 226 | .minor = minor, |
| 227 | 227 | .patch = patch, |
| 228 | } | |
| 228 | }; | |
| 229 | 229 | } |
| 230 | 230 | |
| 231 | 231 | pub fn addCIncludePath(self: &Builder, path: []const u8) { |
| ... | ... | @@ -432,16 +432,16 @@ pub const Builder = struct { |
| 432 | 432 | const release_safe = self.option(bool, "release-safe", "optimizations on and safety on") ?? false; |
| 433 | 433 | const release_fast = self.option(bool, "release-fast", "optimizations on and safety off") ?? false; |
| 434 | 434 | |
| 435 | const mode = if (release_safe and !release_fast) { | |
| 435 | const mode = if (release_safe and !release_fast) | |
| 436 | 436 | builtin.Mode.ReleaseSafe |
| 437 | } else if (release_fast and !release_safe) { | |
| 437 | else if (release_fast and !release_safe) | |
| 438 | 438 | builtin.Mode.ReleaseFast |
| 439 | } else if (!release_fast and !release_safe) { | |
| 439 | else if (!release_fast and !release_safe) | |
| 440 | 440 | builtin.Mode.Debug |
| 441 | } else { | |
| 441 | else x: { | |
| 442 | 442 | warn("Both -Drelease-safe and -Drelease-fast specified"); |
| 443 | 443 | self.markInvalidUserInput(); |
| 444 | builtin.Mode.Debug | |
| 444 | break :x builtin.Mode.Debug; | |
| 445 | 445 | }; |
| 446 | 446 | self.release_mode = mode; |
| 447 | 447 | return mode; |
| ... | ... | @@ -506,7 +506,7 @@ pub const Builder = struct { |
| 506 | 506 | } |
| 507 | 507 | |
| 508 | 508 | fn typeToEnum(comptime T: type) -> TypeId { |
| 509 | switch (@typeId(T)) { | |
| 509 | return switch (@typeId(T)) { | |
| 510 | 510 | builtin.TypeId.Int => TypeId.Int, |
| 511 | 511 | builtin.TypeId.Float => TypeId.Float, |
| 512 | 512 | builtin.TypeId.Bool => TypeId.Bool, |
| ... | ... | @@ -515,7 +515,7 @@ pub const Builder = struct { |
| 515 | 515 | []const []const u8 => TypeId.List, |
| 516 | 516 | else => @compileError("Unsupported type: " ++ @typeName(T)), |
| 517 | 517 | }, |
| 518 | } | |
| 518 | }; | |
| 519 | 519 | } |
| 520 | 520 | |
| 521 | 521 | fn markInvalidUserInput(self: &Builder) { |
| ... | ... | @@ -590,8 +590,7 @@ pub const Builder = struct { |
| 590 | 590 | |
| 591 | 591 | return error.UncleanExit; |
| 592 | 592 | }, |
| 593 | }; | |
| 594 | ||
| 593 | } | |
| 595 | 594 | } |
| 596 | 595 | |
| 597 | 596 | pub fn makePath(self: &Builder, path: []const u8) -> %void { |
| ... | ... | @@ -662,13 +661,12 @@ pub const Builder = struct { |
| 662 | 661 | if (builtin.environ == builtin.Environ.msvc) { |
| 663 | 662 | return "cl.exe"; |
| 664 | 663 | } else { |
| 665 | return os.getEnvVarOwned(self.allocator, "CC") %% |err| { | |
| 666 | if (err == error.EnvironmentVariableNotFound) { | |
| 664 | return os.getEnvVarOwned(self.allocator, "CC") %% |err| | |
| 665 | if (err == error.EnvironmentVariableNotFound) | |
| 667 | 666 | ([]const u8)("cc") |
| 668 | } else { | |
| 669 | debug.panic("Unable to get environment variable: {}", err); | |
| 670 | } | |
| 671 | }; | |
| 667 | else | |
| 668 | debug.panic("Unable to get environment variable: {}", err) | |
| 669 | ; | |
| 672 | 670 | } |
| 673 | 671 | } |
| 674 | 672 | |
| ... | ... | @@ -1079,11 +1077,10 @@ pub const LibExeObjStep = struct { |
| 1079 | 1077 | } |
| 1080 | 1078 | |
| 1081 | 1079 | pub fn getOutputPath(self: &LibExeObjStep) -> []const u8 { |
| 1082 | if (self.output_path) |output_path| { | |
| 1080 | return if (self.output_path) |output_path| | |
| 1083 | 1081 | output_path |
| 1084 | } else { | |
| 1085 | %%os.path.join(self.builder.allocator, self.builder.cache_root, self.out_filename) | |
| 1086 | } | |
| 1082 | else | |
| 1083 | %%os.path.join(self.builder.allocator, self.builder.cache_root, self.out_filename); | |
| 1087 | 1084 | } |
| 1088 | 1085 | |
| 1089 | 1086 | pub fn setOutputHPath(self: &LibExeObjStep, file_path: []const u8) { |
| ... | ... | @@ -1096,11 +1093,10 @@ pub const LibExeObjStep = struct { |
| 1096 | 1093 | } |
| 1097 | 1094 | |
| 1098 | 1095 | pub fn getOutputHPath(self: &LibExeObjStep) -> []const u8 { |
| 1099 | if (self.output_h_path) |output_h_path| { | |
| 1096 | return if (self.output_h_path) |output_h_path| | |
| 1100 | 1097 | output_h_path |
| 1101 | } else { | |
| 1102 | %%os.path.join(self.builder.allocator, self.builder.cache_root, self.out_h_filename) | |
| 1103 | } | |
| 1098 | else | |
| 1099 | %%os.path.join(self.builder.allocator, self.builder.cache_root, self.out_h_filename); | |
| 1104 | 1100 | } |
| 1105 | 1101 | |
| 1106 | 1102 | pub fn addAssemblyFile(self: &LibExeObjStep, path: []const u8) { |
| ... | ... | @@ -1618,7 +1614,7 @@ pub const TestStep = struct { |
| 1618 | 1614 | |
| 1619 | 1615 | pub fn init(builder: &Builder, root_src: []const u8) -> TestStep { |
| 1620 | 1616 | const step_name = builder.fmt("test {}", root_src); |
| 1621 | TestStep { | |
| 1617 | return TestStep { | |
| 1622 | 1618 | .step = Step.init(step_name, builder.allocator, make), |
| 1623 | 1619 | .builder = builder, |
| 1624 | 1620 | .root_src = root_src, |
| ... | ... | @@ -1629,7 +1625,7 @@ pub const TestStep = struct { |
| 1629 | 1625 | .link_libs = BufSet.init(builder.allocator), |
| 1630 | 1626 | .target = Target { .Native = {} }, |
| 1631 | 1627 | .exec_cmd_args = null, |
| 1632 | } | |
| 1628 | }; | |
| 1633 | 1629 | } |
| 1634 | 1630 | |
| 1635 | 1631 | pub fn setVerbose(self: &TestStep, value: bool) { |
| ... | ... | @@ -1936,16 +1932,16 @@ pub const Step = struct { |
| 1936 | 1932 | done_flag: bool, |
| 1937 | 1933 | |
| 1938 | 1934 | pub fn init(name: []const u8, allocator: &Allocator, makeFn: fn (&Step)->%void) -> Step { |
| 1939 | Step { | |
| 1935 | return Step { | |
| 1940 | 1936 | .name = name, |
| 1941 | 1937 | .makeFn = makeFn, |
| 1942 | 1938 | .dependencies = ArrayList(&Step).init(allocator), |
| 1943 | 1939 | .loop_flag = false, |
| 1944 | 1940 | .done_flag = false, |
| 1945 | } | |
| 1941 | }; | |
| 1946 | 1942 | } |
| 1947 | 1943 | pub fn initNoOp(name: []const u8, allocator: &Allocator) -> Step { |
| 1948 | init(name, allocator, makeNoOp) | |
| 1944 | return init(name, allocator, makeNoOp); | |
| 1949 | 1945 | } |
| 1950 | 1946 | |
| 1951 | 1947 | pub fn make(self: &Step) -> %void { |
std/cstr.zig+1-1| ... | ... | @@ -17,7 +17,7 @@ pub fn cmp(a: &const u8, b: &const u8) -> i8 { |
| 17 | 17 | return -1; |
| 18 | 18 | } else { |
| 19 | 19 | return 0; |
| 20 | }; | |
| 20 | } | |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | pub fn toSliceConst(str: &const u8) -> []const u8 { |
std/debug.zig+37-49| ... | ... | @@ -32,7 +32,7 @@ fn getStderrStream() -> %&io.OutStream { |
| 32 | 32 | const st = &stderr_file_out_stream.stream; |
| 33 | 33 | stderr_stream = st; |
| 34 | 34 | return st; |
| 35 | }; | |
| 35 | } | |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | /// Tries to print a stack trace to stderr, unbuffered, and ignores any error returned. |
| ... | ... | @@ -52,9 +52,9 @@ pub fn assert(ok: bool) { |
| 52 | 52 | // we insert an explicit call to @panic instead of unreachable. |
| 53 | 53 | // TODO we should use `assertOrPanic` in tests and remove this logic. |
| 54 | 54 | if (builtin.is_test) { |
| 55 | @panic("assertion failure") | |
| 55 | @panic("assertion failure"); | |
| 56 | 56 | } else { |
| 57 | unreachable // assertion failure | |
| 57 | unreachable; // assertion failure | |
| 58 | 58 | } |
| 59 | 59 | } |
| 60 | 60 | } |
| ... | ... | @@ -175,7 +175,7 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty |
| 175 | 175 | return_address, compile_unit_name); |
| 176 | 176 | }, |
| 177 | 177 | else => return err, |
| 178 | }; | |
| 178 | } | |
| 179 | 179 | } |
| 180 | 180 | }, |
| 181 | 181 | builtin.ObjectFormat.coff => { |
| ... | ... | @@ -357,7 +357,7 @@ const Die = struct { |
| 357 | 357 | FormValue.String => |value| value, |
| 358 | 358 | FormValue.StrPtr => |offset| getString(st, offset), |
| 359 | 359 | else => error.InvalidDebugInfo, |
| 360 | } | |
| 360 | }; | |
| 361 | 361 | } |
| 362 | 362 | }; |
| 363 | 363 | |
| ... | ... | @@ -403,7 +403,7 @@ const LineNumberProgram = struct { |
| 403 | 403 | pub fn init(is_stmt: bool, include_dirs: []const []const u8, |
| 404 | 404 | file_entries: &ArrayList(FileEntry), target_address: usize) -> LineNumberProgram |
| 405 | 405 | { |
| 406 | LineNumberProgram { | |
| 406 | return LineNumberProgram { | |
| 407 | 407 | .address = 0, |
| 408 | 408 | .file = 1, |
| 409 | 409 | .line = 1, |
| ... | ... | @@ -421,7 +421,7 @@ const LineNumberProgram = struct { |
| 421 | 421 | .prev_is_stmt = undefined, |
| 422 | 422 | .prev_basic_block = undefined, |
| 423 | 423 | .prev_end_sequence = undefined, |
| 424 | } | |
| 424 | }; | |
| 425 | 425 | } |
| 426 | 426 | |
| 427 | 427 | pub fn checkLineMatch(self: &LineNumberProgram) -> %?LineInfo { |
| ... | ... | @@ -430,14 +430,11 @@ const LineNumberProgram = struct { |
| 430 | 430 | return error.MissingDebugInfo; |
| 431 | 431 | } else if (self.prev_file - 1 >= self.file_entries.len) { |
| 432 | 432 | return error.InvalidDebugInfo; |
| 433 | } else { | |
| 434 | &self.file_entries.items[self.prev_file - 1] | |
| 435 | }; | |
| 433 | } else &self.file_entries.items[self.prev_file - 1]; | |
| 434 | ||
| 436 | 435 | const dir_name = if (file_entry.dir_index >= self.include_dirs.len) { |
| 437 | 436 | return error.InvalidDebugInfo; |
| 438 | } else { | |
| 439 | self.include_dirs[file_entry.dir_index] | |
| 440 | }; | |
| 437 | } else self.include_dirs[file_entry.dir_index]; | |
| 441 | 438 | const file_name = %return os.path.join(self.file_entries.allocator, dir_name, file_entry.file_name); |
| 442 | 439 | %defer self.file_entries.allocator.free(file_name); |
| 443 | 440 | return LineInfo { |
| ... | ... | @@ -494,28 +491,21 @@ fn parseFormValueBlock(allocator: &mem.Allocator, in_stream: &io.InStream, size: |
| 494 | 491 | } |
| 495 | 492 | |
| 496 | 493 | fn parseFormValueConstant(allocator: &mem.Allocator, in_stream: &io.InStream, signed: bool, size: usize) -> %FormValue { |
| 497 | FormValue { .Const = Constant { | |
| 494 | return FormValue { .Const = Constant { | |
| 498 | 495 | .signed = signed, |
| 499 | 496 | .payload = %return readAllocBytes(allocator, in_stream, size), |
| 500 | }} | |
| 497 | }}; | |
| 501 | 498 | } |
| 502 | 499 | |
| 503 | 500 | fn parseFormValueDwarfOffsetSize(in_stream: &io.InStream, is_64: bool) -> %u64 { |
| 504 | return if (is_64) { | |
| 505 | %return in_stream.readIntLe(u64) | |
| 506 | } else { | |
| 507 | u64(%return in_stream.readIntLe(u32)) | |
| 508 | }; | |
| 501 | return if (is_64) %return in_stream.readIntLe(u64) | |
| 502 | else u64(%return in_stream.readIntLe(u32)) ; | |
| 509 | 503 | } |
| 510 | 504 | |
| 511 | 505 | fn parseFormValueTargetAddrSize(in_stream: &io.InStream) -> %u64 { |
| 512 | return if (@sizeOf(usize) == 4) { | |
| 513 | u64(%return in_stream.readIntLe(u32)) | |
| 514 | } else if (@sizeOf(usize) == 8) { | |
| 515 | %return in_stream.readIntLe(u64) | |
| 516 | } else { | |
| 517 | unreachable; | |
| 518 | }; | |
| 506 | return if (@sizeOf(usize) == 4) u64(%return in_stream.readIntLe(u32)) | |
| 507 | else if (@sizeOf(usize) == 8) %return in_stream.readIntLe(u64) | |
| 508 | else unreachable; | |
| 519 | 509 | } |
| 520 | 510 | |
| 521 | 511 | fn parseFormValueRefLen(allocator: &mem.Allocator, in_stream: &io.InStream, size: usize) -> %FormValue { |
| ... | ... | @@ -534,9 +524,9 @@ fn parseFormValue(allocator: &mem.Allocator, in_stream: &io.InStream, form_id: u |
| 534 | 524 | DW.FORM_block1 => parseFormValueBlock(allocator, in_stream, 1), |
| 535 | 525 | DW.FORM_block2 => parseFormValueBlock(allocator, in_stream, 2), |
| 536 | 526 | DW.FORM_block4 => parseFormValueBlock(allocator, in_stream, 4), |
| 537 | DW.FORM_block => { | |
| 527 | DW.FORM_block => x: { | |
| 538 | 528 | const block_len = %return readULeb128(in_stream); |
| 539 | parseFormValueBlockLen(allocator, in_stream, block_len) | |
| 529 | return parseFormValueBlockLen(allocator, in_stream, block_len); | |
| 540 | 530 | }, |
| 541 | 531 | DW.FORM_data1 => parseFormValueConstant(allocator, in_stream, false, 1), |
| 542 | 532 | DW.FORM_data2 => parseFormValueConstant(allocator, in_stream, false, 2), |
| ... | ... | @@ -545,7 +535,7 @@ fn parseFormValue(allocator: &mem.Allocator, in_stream: &io.InStream, form_id: u |
| 545 | 535 | DW.FORM_udata, DW.FORM_sdata => { |
| 546 | 536 | const block_len = %return readULeb128(in_stream); |
| 547 | 537 | const signed = form_id == DW.FORM_sdata; |
| 548 | parseFormValueConstant(allocator, in_stream, signed, block_len) | |
| 538 | return parseFormValueConstant(allocator, in_stream, signed, block_len); | |
| 549 | 539 | }, |
| 550 | 540 | DW.FORM_exprloc => { |
| 551 | 541 | const size = %return readULeb128(in_stream); |
| ... | ... | @@ -562,7 +552,7 @@ fn parseFormValue(allocator: &mem.Allocator, in_stream: &io.InStream, form_id: u |
| 562 | 552 | DW.FORM_ref8 => parseFormValueRef(allocator, in_stream, u64), |
| 563 | 553 | DW.FORM_ref_udata => { |
| 564 | 554 | const ref_len = %return readULeb128(in_stream); |
| 565 | parseFormValueRefLen(allocator, in_stream, ref_len) | |
| 555 | return parseFormValueRefLen(allocator, in_stream, ref_len); | |
| 566 | 556 | }, |
| 567 | 557 | |
| 568 | 558 | DW.FORM_ref_addr => FormValue { .RefAddr = %return parseFormValueDwarfOffsetSize(in_stream, is_64) }, |
| ... | ... | @@ -572,10 +562,10 @@ fn parseFormValue(allocator: &mem.Allocator, in_stream: &io.InStream, form_id: u |
| 572 | 562 | DW.FORM_strp => FormValue { .StrPtr = %return parseFormValueDwarfOffsetSize(in_stream, is_64) }, |
| 573 | 563 | DW.FORM_indirect => { |
| 574 | 564 | const child_form_id = %return readULeb128(in_stream); |
| 575 | parseFormValue(allocator, in_stream, child_form_id, is_64) | |
| 565 | return parseFormValue(allocator, in_stream, child_form_id, is_64); | |
| 576 | 566 | }, |
| 577 | 567 | else => error.InvalidDebugInfo, |
| 578 | } | |
| 568 | }; | |
| 579 | 569 | } |
| 580 | 570 | |
| 581 | 571 | fn parseAbbrevTable(st: &ElfStackTrace) -> %AbbrevTable { |
| ... | ... | @@ -852,11 +842,9 @@ fn scanAllCompileUnits(st: &ElfStackTrace) -> %void { |
| 852 | 842 | const version = %return in_stream.readInt(st.elf.endian, u16); |
| 853 | 843 | if (version < 2 or version > 5) return error.InvalidDebugInfo; |
| 854 | 844 | |
| 855 | const debug_abbrev_offset = if (is_64) { | |
| 856 | %return in_stream.readInt(st.elf.endian, u64) | |
| 857 | } else { | |
| 858 | %return in_stream.readInt(st.elf.endian, u32) | |
| 859 | }; | |
| 845 | const debug_abbrev_offset = | |
| 846 | if (is_64) %return in_stream.readInt(st.elf.endian, u64) | |
| 847 | else %return in_stream.readInt(st.elf.endian, u32); | |
| 860 | 848 | |
| 861 | 849 | const address_size = %return in_stream.readByte(); |
| 862 | 850 | if (address_size != @sizeOf(usize)) return error.InvalidDebugInfo; |
| ... | ... | @@ -872,28 +860,28 @@ fn scanAllCompileUnits(st: &ElfStackTrace) -> %void { |
| 872 | 860 | if (compile_unit_die.tag_id != DW.TAG_compile_unit) |
| 873 | 861 | return error.InvalidDebugInfo; |
| 874 | 862 | |
| 875 | const pc_range = { | |
| 863 | const pc_range = x: { | |
| 876 | 864 | if (compile_unit_die.getAttrAddr(DW.AT_low_pc)) |low_pc| { |
| 877 | 865 | if (compile_unit_die.getAttr(DW.AT_high_pc)) |high_pc_value| { |
| 878 | 866 | const pc_end = switch (*high_pc_value) { |
| 879 | 867 | FormValue.Address => |value| value, |
| 880 | FormValue.Const => |value| { | |
| 868 | FormValue.Const => |value| b: { | |
| 881 | 869 | const offset = %return value.asUnsignedLe(); |
| 882 | low_pc + offset | |
| 870 | break :b (low_pc + offset); | |
| 883 | 871 | }, |
| 884 | 872 | else => return error.InvalidDebugInfo, |
| 885 | 873 | }; |
| 886 | PcRange { | |
| 874 | break :x PcRange { | |
| 887 | 875 | .start = low_pc, |
| 888 | 876 | .end = pc_end, |
| 889 | } | |
| 877 | }; | |
| 890 | 878 | } else { |
| 891 | null | |
| 879 | break :x null; | |
| 892 | 880 | } |
| 893 | 881 | } else |err| { |
| 894 | 882 | if (err != error.MissingDebugInfo) |
| 895 | 883 | return err; |
| 896 | null | |
| 884 | break :x null; | |
| 897 | 885 | } |
| 898 | 886 | }; |
| 899 | 887 | |
| ... | ... | @@ -949,12 +937,12 @@ fn findCompileUnit(st: &ElfStackTrace, target_address: u64) -> %&const CompileUn |
| 949 | 937 | fn readInitialLength(in_stream: &io.InStream, is_64: &bool) -> %u64 { |
| 950 | 938 | const first_32_bits = %return in_stream.readIntLe(u32); |
| 951 | 939 | *is_64 = (first_32_bits == 0xffffffff); |
| 952 | return if (*is_64) { | |
| 953 | %return in_stream.readIntLe(u64) | |
| 940 | if (*is_64) { | |
| 941 | return in_stream.readIntLe(u64); | |
| 954 | 942 | } else { |
| 955 | 943 | if (first_32_bits >= 0xfffffff0) return error.InvalidDebugInfo; |
| 956 | u64(first_32_bits) | |
| 957 | }; | |
| 944 | return u64(first_32_bits); | |
| 945 | } | |
| 958 | 946 | } |
| 959 | 947 | |
| 960 | 948 | fn readULeb128(in_stream: &io.InStream) -> %u64 { |
std/endian.zig+3-3| ... | ... | @@ -2,15 +2,15 @@ const mem = @import("mem.zig"); |
| 2 | 2 | const builtin = @import("builtin"); |
| 3 | 3 | |
| 4 | 4 | pub fn swapIfLe(comptime T: type, x: T) -> T { |
| 5 | swapIf(false, T, x) | |
| 5 | return swapIf(false, T, x); | |
| 6 | 6 | } |
| 7 | 7 | |
| 8 | 8 | pub fn swapIfBe(comptime T: type, x: T) -> T { |
| 9 | swapIf(true, T, x) | |
| 9 | return swapIf(true, T, x); | |
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | pub fn swapIf(endian: builtin.Endian, comptime T: type, x: T) -> T { |
| 13 | if (builtin.endian == endian) swap(T, x) else x | |
| 13 | return if (builtin.endian == endian) swap(T, x) else x; | |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | pub fn swap(comptime T: type, x: T) -> T { |
std/fmt/errol/enum3.zig+2-2| ... | ... | @@ -439,10 +439,10 @@ const Slab = struct { |
| 439 | 439 | }; |
| 440 | 440 | |
| 441 | 441 | fn slab(str: []const u8, exp: i32) -> Slab { |
| 442 | Slab { | |
| 442 | return Slab { | |
| 443 | 443 | .str = str, |
| 444 | 444 | .exp = exp, |
| 445 | } | |
| 445 | }; | |
| 446 | 446 | } |
| 447 | 447 | |
| 448 | 448 | pub const enum3_data = []Slab { |
std/fmt/index.zig+3-4| ... | ... | @@ -251,11 +251,10 @@ pub fn formatFloat(value: var, context: var, output: fn(@typeOf(context), []cons |
| 251 | 251 | %return output(context, float_decimal.digits[0..1]); |
| 252 | 252 | %return output(context, "."); |
| 253 | 253 | if (float_decimal.digits.len > 1) { |
| 254 | const num_digits = if (@typeOf(value) == f32) { | |
| 254 | const num_digits = if (@typeOf(value) == f32) | |
| 255 | 255 | math.min(usize(9), float_decimal.digits.len) |
| 256 | } else { | |
| 257 | float_decimal.digits.len | |
| 258 | }; | |
| 256 | else | |
| 257 | float_decimal.digits.len; | |
| 259 | 258 | %return output(context, float_decimal.digits[1 .. num_digits]); |
| 260 | 259 | } else { |
| 261 | 260 | %return output(context, "0"); |
std/hash_map.zig+10-10| ... | ... | @@ -12,7 +12,7 @@ pub fn HashMap(comptime K: type, comptime V: type, |
| 12 | 12 | comptime hash: fn(key: K)->u32, |
| 13 | 13 | comptime eql: fn(a: K, b: K)->bool) -> type |
| 14 | 14 | { |
| 15 | struct { | |
| 15 | return struct { | |
| 16 | 16 | entries: []Entry, |
| 17 | 17 | size: usize, |
| 18 | 18 | max_distance_from_start_index: usize, |
| ... | ... | @@ -51,19 +51,19 @@ pub fn HashMap(comptime K: type, comptime V: type, |
| 51 | 51 | return entry; |
| 52 | 52 | } |
| 53 | 53 | } |
| 54 | unreachable // no next item | |
| 54 | unreachable; // no next item | |
| 55 | 55 | } |
| 56 | 56 | }; |
| 57 | 57 | |
| 58 | 58 | pub fn init(allocator: &Allocator) -> Self { |
| 59 | Self { | |
| 59 | return Self { | |
| 60 | 60 | .entries = []Entry{}, |
| 61 | 61 | .allocator = allocator, |
| 62 | 62 | .size = 0, |
| 63 | 63 | .max_distance_from_start_index = 0, |
| 64 | 64 | // it doesn't actually matter what we set this to since we use wrapping integer arithmetic |
| 65 | 65 | .modification_count = undefined, |
| 66 | } | |
| 66 | }; | |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | pub fn deinit(hm: &Self) { |
| ... | ... | @@ -133,7 +133,7 @@ pub fn HashMap(comptime K: type, comptime V: type, |
| 133 | 133 | entry.distance_from_start_index -= 1; |
| 134 | 134 | entry = next_entry; |
| 135 | 135 | } |
| 136 | unreachable // shifting everything in the table | |
| 136 | unreachable; // shifting everything in the table | |
| 137 | 137 | }} |
| 138 | 138 | return null; |
| 139 | 139 | } |
| ... | ... | @@ -169,7 +169,7 @@ pub fn HashMap(comptime K: type, comptime V: type, |
| 169 | 169 | const start_index = hm.keyToIndex(key); |
| 170 | 170 | var roll_over: usize = 0; |
| 171 | 171 | var distance_from_start_index: usize = 0; |
| 172 | while (roll_over < hm.entries.len) : ({roll_over += 1; distance_from_start_index += 1}) { | |
| 172 | while (roll_over < hm.entries.len) : ({roll_over += 1; distance_from_start_index += 1;}) { | |
| 173 | 173 | const index = (start_index + roll_over) % hm.entries.len; |
| 174 | 174 | const entry = &hm.entries[index]; |
| 175 | 175 | |
| ... | ... | @@ -210,7 +210,7 @@ pub fn HashMap(comptime K: type, comptime V: type, |
| 210 | 210 | }; |
| 211 | 211 | return result; |
| 212 | 212 | } |
| 213 | unreachable // put into a full map | |
| 213 | unreachable; // put into a full map | |
| 214 | 214 | } |
| 215 | 215 | |
| 216 | 216 | fn internalGet(hm: &Self, key: K) -> ?&Entry { |
| ... | ... | @@ -228,7 +228,7 @@ pub fn HashMap(comptime K: type, comptime V: type, |
| 228 | 228 | fn keyToIndex(hm: &Self, key: K) -> usize { |
| 229 | 229 | return usize(hash(key)) % hm.entries.len; |
| 230 | 230 | } |
| 231 | } | |
| 231 | }; | |
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | test "basicHashMapTest" { |
| ... | ... | @@ -251,9 +251,9 @@ test "basicHashMapTest" { |
| 251 | 251 | } |
| 252 | 252 | |
| 253 | 253 | fn hash_i32(x: i32) -> u32 { |
| 254 | @bitCast(u32, x) | |
| 254 | return @bitCast(u32, x); | |
| 255 | 255 | } |
| 256 | 256 | |
| 257 | 257 | fn eql_i32(a: i32, b: i32) -> bool { |
| 258 | a == b | |
| 258 | return a == b; | |
| 259 | 259 | } |
std/heap.zig+6-7| ... | ... | @@ -17,22 +17,21 @@ pub var c_allocator = Allocator { |
| 17 | 17 | }; |
| 18 | 18 | |
| 19 | 19 | fn cAlloc(self: &Allocator, n: usize, alignment: u29) -> %[]u8 { |
| 20 | if (c.malloc(usize(n))) |buf| { | |
| 20 | return if (c.malloc(usize(n))) |buf| | |
| 21 | 21 | @ptrCast(&u8, buf)[0..n] |
| 22 | } else { | |
| 23 | error.OutOfMemory | |
| 24 | } | |
| 22 | else | |
| 23 | error.OutOfMemory; | |
| 25 | 24 | } |
| 26 | 25 | |
| 27 | 26 | fn cRealloc(self: &Allocator, old_mem: []u8, new_size: usize, alignment: u29) -> %[]u8 { |
| 28 | 27 | if (new_size <= old_mem.len) { |
| 29 | old_mem[0..new_size] | |
| 28 | return old_mem[0..new_size]; | |
| 30 | 29 | } else { |
| 31 | 30 | const old_ptr = @ptrCast(&c_void, old_mem.ptr); |
| 32 | 31 | if (c.realloc(old_ptr, usize(new_size))) |buf| { |
| 33 | @ptrCast(&u8, buf)[0..new_size] | |
| 32 | return @ptrCast(&u8, buf)[0..new_size]; | |
| 34 | 33 | } else { |
| 35 | error.OutOfMemory | |
| 34 | return error.OutOfMemory; | |
| 36 | 35 | } |
| 37 | 36 | } |
| 38 | 37 | } |
std/io.zig+13-16| ... | ... | @@ -50,35 +50,32 @@ error Unseekable; |
| 50 | 50 | error EndOfFile; |
| 51 | 51 | |
| 52 | 52 | pub fn getStdErr() -> %File { |
| 53 | const handle = if (is_windows) { | |
| 53 | const handle = if (is_windows) | |
| 54 | 54 | %return os.windowsGetStdHandle(system.STD_ERROR_HANDLE) |
| 55 | } else if (is_posix) { | |
| 55 | else if (is_posix) | |
| 56 | 56 | system.STDERR_FILENO |
| 57 | } else { | |
| 58 | unreachable | |
| 59 | }; | |
| 57 | else | |
| 58 | unreachable; | |
| 60 | 59 | return File.openHandle(handle); |
| 61 | 60 | } |
| 62 | 61 | |
| 63 | 62 | pub fn getStdOut() -> %File { |
| 64 | const handle = if (is_windows) { | |
| 63 | const handle = if (is_windows) | |
| 65 | 64 | %return os.windowsGetStdHandle(system.STD_OUTPUT_HANDLE) |
| 66 | } else if (is_posix) { | |
| 65 | else if (is_posix) | |
| 67 | 66 | system.STDOUT_FILENO |
| 68 | } else { | |
| 69 | unreachable | |
| 70 | }; | |
| 67 | else | |
| 68 | unreachable; | |
| 71 | 69 | return File.openHandle(handle); |
| 72 | 70 | } |
| 73 | 71 | |
| 74 | 72 | pub fn getStdIn() -> %File { |
| 75 | const handle = if (is_windows) { | |
| 73 | const handle = if (is_windows) | |
| 76 | 74 | %return os.windowsGetStdHandle(system.STD_INPUT_HANDLE) |
| 77 | } else if (is_posix) { | |
| 75 | else if (is_posix) | |
| 78 | 76 | system.STDIN_FILENO |
| 79 | } else { | |
| 80 | unreachable | |
| 81 | }; | |
| 77 | else | |
| 78 | unreachable; | |
| 82 | 79 | return File.openHandle(handle); |
| 83 | 80 | } |
| 84 | 81 | |
| ... | ... | @@ -261,7 +258,7 @@ pub const File = struct { |
| 261 | 258 | system.EBADF => error.BadFd, |
| 262 | 259 | system.ENOMEM => error.SystemResources, |
| 263 | 260 | else => os.unexpectedErrorPosix(err), |
| 264 | } | |
| 261 | }; | |
| 265 | 262 | } |
| 266 | 263 | |
| 267 | 264 | return usize(stat.size); |
std/linked_list.zig+7-7| ... | ... | @@ -5,7 +5,7 @@ const Allocator = mem.Allocator; |
| 5 | 5 | |
| 6 | 6 | /// Generic doubly linked list. |
| 7 | 7 | pub fn LinkedList(comptime T: type) -> type { |
| 8 | struct { | |
| 8 | return struct { | |
| 9 | 9 | const Self = this; |
| 10 | 10 | |
| 11 | 11 | /// Node inside the linked list wrapping the actual data. |
| ... | ... | @@ -15,11 +15,11 @@ pub fn LinkedList(comptime T: type) -> type { |
| 15 | 15 | data: T, |
| 16 | 16 | |
| 17 | 17 | pub fn init(data: &const T) -> Node { |
| 18 | Node { | |
| 18 | return Node { | |
| 19 | 19 | .prev = null, |
| 20 | 20 | .next = null, |
| 21 | 21 | .data = *data, |
| 22 | } | |
| 22 | }; | |
| 23 | 23 | } |
| 24 | 24 | }; |
| 25 | 25 | |
| ... | ... | @@ -32,11 +32,11 @@ pub fn LinkedList(comptime T: type) -> type { |
| 32 | 32 | /// Returns: |
| 33 | 33 | /// An empty linked list. |
| 34 | 34 | pub fn init() -> Self { |
| 35 | Self { | |
| 35 | return Self { | |
| 36 | 36 | .first = null, |
| 37 | 37 | .last = null, |
| 38 | 38 | .len = 0, |
| 39 | } | |
| 39 | }; | |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | /// Insert a new node after an existing one. |
| ... | ... | @@ -166,7 +166,7 @@ pub fn LinkedList(comptime T: type) -> type { |
| 166 | 166 | /// Returns: |
| 167 | 167 | /// A pointer to the new node. |
| 168 | 168 | pub fn allocateNode(list: &Self, allocator: &Allocator) -> %&Node { |
| 169 | allocator.create(Node) | |
| 169 | return allocator.create(Node); | |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | /// Deallocate a node. |
| ... | ... | @@ -191,7 +191,7 @@ pub fn LinkedList(comptime T: type) -> type { |
| 191 | 191 | *node = Node.init(data); |
| 192 | 192 | return node; |
| 193 | 193 | } |
| 194 | } | |
| 194 | }; | |
| 195 | 195 | } |
| 196 | 196 | |
| 197 | 197 | test "basic linked list test" { |
std/math/acos.zig+6-6| ... | ... | @@ -7,11 +7,11 @@ const assert = @import("../debug.zig").assert; |
| 7 | 7 | |
| 8 | 8 | pub fn acos(x: var) -> @typeOf(x) { |
| 9 | 9 | const T = @typeOf(x); |
| 10 | switch (T) { | |
| 10 | return switch (T) { | |
| 11 | 11 | f32 => @inlineCall(acos32, x), |
| 12 | 12 | f64 => @inlineCall(acos64, x), |
| 13 | 13 | else => @compileError("acos not implemented for " ++ @typeName(T)), |
| 14 | } | |
| 14 | }; | |
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | fn r32(z: f32) -> f32 { |
| ... | ... | @@ -22,7 +22,7 @@ fn r32(z: f32) -> f32 { |
| 22 | 22 | |
| 23 | 23 | const p = z * (pS0 + z * (pS1 + z * pS2)); |
| 24 | 24 | const q = 1.0 + z * qS1; |
| 25 | p / q | |
| 25 | return p / q; | |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | fn acos32(x: f32) -> f32 { |
| ... | ... | @@ -69,7 +69,7 @@ fn acos32(x: f32) -> f32 { |
| 69 | 69 | const df = @bitCast(f32, jx & 0xFFFFF000); |
| 70 | 70 | const c = (z - df * df) / (s + df); |
| 71 | 71 | const w = r32(z) * s + c; |
| 72 | 2 * (df + w) | |
| 72 | return 2 * (df + w); | |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | fn r64(z: f64) -> f64 { |
| ... | ... | @@ -86,7 +86,7 @@ fn r64(z: f64) -> f64 { |
| 86 | 86 | |
| 87 | 87 | const p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5))))); |
| 88 | 88 | const q = 1.0 + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4))); |
| 89 | p / q | |
| 89 | return p / q; | |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | fn acos64(x: f64) -> f64 { |
| ... | ... | @@ -138,7 +138,7 @@ fn acos64(x: f64) -> f64 { |
| 138 | 138 | const df = @bitCast(f64, jx & 0xFFFFFFFF00000000); |
| 139 | 139 | const c = (z - df * df) / (s + df); |
| 140 | 140 | const w = r64(z) * s + c; |
| 141 | 2 * (df + w) | |
| 141 | return 2 * (df + w); | |
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | test "math.acos" { |
std/math/acosh.zig+8-8| ... | ... | @@ -9,11 +9,11 @@ const assert = @import("../debug.zig").assert; |
| 9 | 9 | |
| 10 | 10 | pub fn acosh(x: var) -> @typeOf(x) { |
| 11 | 11 | const T = @typeOf(x); |
| 12 | switch (T) { | |
| 12 | return switch (T) { | |
| 13 | 13 | f32 => @inlineCall(acosh32, x), |
| 14 | 14 | f64 => @inlineCall(acosh64, x), |
| 15 | 15 | else => @compileError("acosh not implemented for " ++ @typeName(T)), |
| 16 | } | |
| 16 | }; | |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | // acosh(x) = log(x + sqrt(x * x - 1)) |
| ... | ... | @@ -23,15 +23,15 @@ fn acosh32(x: f32) -> f32 { |
| 23 | 23 | |
| 24 | 24 | // |x| < 2, invalid if x < 1 or nan |
| 25 | 25 | if (i < 0x3F800000 + (1 << 23)) { |
| 26 | math.log1p(x - 1 + math.sqrt((x - 1) * (x - 1) + 2 * (x - 1))) | |
| 26 | return math.log1p(x - 1 + math.sqrt((x - 1) * (x - 1) + 2 * (x - 1))); | |
| 27 | 27 | } |
| 28 | 28 | // |x| < 0x1p12 |
| 29 | 29 | else if (i < 0x3F800000 + (12 << 23)) { |
| 30 | math.ln(2 * x - 1 / (x + math.sqrt(x * x - 1))) | |
| 30 | return math.ln(2 * x - 1 / (x + math.sqrt(x * x - 1))); | |
| 31 | 31 | } |
| 32 | 32 | // |x| >= 0x1p12 |
| 33 | 33 | else { |
| 34 | math.ln(x) + 0.693147180559945309417232121458176568 | |
| 34 | return math.ln(x) + 0.693147180559945309417232121458176568; | |
| 35 | 35 | } |
| 36 | 36 | } |
| 37 | 37 | |
| ... | ... | @@ -41,15 +41,15 @@ fn acosh64(x: f64) -> f64 { |
| 41 | 41 | |
| 42 | 42 | // |x| < 2, invalid if x < 1 or nan |
| 43 | 43 | if (e < 0x3FF + 1) { |
| 44 | math.log1p(x - 1 + math.sqrt((x - 1) * (x - 1) + 2 * (x - 1))) | |
| 44 | return math.log1p(x - 1 + math.sqrt((x - 1) * (x - 1) + 2 * (x - 1))); | |
| 45 | 45 | } |
| 46 | 46 | // |x| < 0x1p26 |
| 47 | 47 | else if (e < 0x3FF + 26) { |
| 48 | math.ln(2 * x - 1 / (x + math.sqrt(x * x - 1))) | |
| 48 | return math.ln(2 * x - 1 / (x + math.sqrt(x * x - 1))); | |
| 49 | 49 | } |
| 50 | 50 | // |x| >= 0x1p26 or nan |
| 51 | 51 | else { |
| 52 | math.ln(x) + 0.693147180559945309417232121458176568 | |
| 52 | return math.ln(x) + 0.693147180559945309417232121458176568; | |
| 53 | 53 | } |
| 54 | 54 | } |
| 55 | 55 |
std/math/asin.zig+9-9| ... | ... | @@ -8,11 +8,11 @@ const assert = @import("../debug.zig").assert; |
| 8 | 8 | |
| 9 | 9 | pub fn asin(x: var) -> @typeOf(x) { |
| 10 | 10 | const T = @typeOf(x); |
| 11 | switch (T) { | |
| 11 | return switch (T) { | |
| 12 | 12 | f32 => @inlineCall(asin32, x), |
| 13 | 13 | f64 => @inlineCall(asin64, x), |
| 14 | 14 | else => @compileError("asin not implemented for " ++ @typeName(T)), |
| 15 | } | |
| 15 | }; | |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | fn r32(z: f32) -> f32 { |
| ... | ... | @@ -23,7 +23,7 @@ fn r32(z: f32) -> f32 { |
| 23 | 23 | |
| 24 | 24 | const p = z * (pS0 + z * (pS1 + z * pS2)); |
| 25 | 25 | const q = 1.0 + z * qS1; |
| 26 | p / q | |
| 26 | return p / q; | |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | fn asin32(x: f32) -> f32 { |
| ... | ... | @@ -58,9 +58,9 @@ fn asin32(x: f32) -> f32 { |
| 58 | 58 | const fx = pio2 - 2 * (s + s * r32(z)); |
| 59 | 59 | |
| 60 | 60 | if (hx >> 31 != 0) { |
| 61 | -fx | |
| 61 | return -fx; | |
| 62 | 62 | } else { |
| 63 | fx | |
| 63 | return fx; | |
| 64 | 64 | } |
| 65 | 65 | } |
| 66 | 66 | |
| ... | ... | @@ -78,7 +78,7 @@ fn r64(z: f64) -> f64 { |
| 78 | 78 | |
| 79 | 79 | const p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5))))); |
| 80 | 80 | const q = 1.0 + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4))); |
| 81 | p / q | |
| 81 | return p / q; | |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | fn asin64(x: f64) -> f64 { |
| ... | ... | @@ -119,7 +119,7 @@ fn asin64(x: f64) -> f64 { |
| 119 | 119 | |
| 120 | 120 | // |x| > 0.975 |
| 121 | 121 | if (ix >= 0x3FEF3333) { |
| 122 | fx = pio2_hi - 2 * (s + s * r) | |
| 122 | fx = pio2_hi - 2 * (s + s * r); | |
| 123 | 123 | } else { |
| 124 | 124 | const jx = @bitCast(u64, s); |
| 125 | 125 | const df = @bitCast(f64, jx & 0xFFFFFFFF00000000); |
| ... | ... | @@ -128,9 +128,9 @@ fn asin64(x: f64) -> f64 { |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | if (hx >> 31 != 0) { |
| 131 | -fx | |
| 131 | return -fx; | |
| 132 | 132 | } else { |
| 133 | fx | |
| 133 | return fx; | |
| 134 | 134 | } |
| 135 | 135 | } |
| 136 | 136 |
std/math/asinh.zig+4-4| ... | ... | @@ -9,11 +9,11 @@ const assert = @import("../debug.zig").assert; |
| 9 | 9 | |
| 10 | 10 | pub fn asinh(x: var) -> @typeOf(x) { |
| 11 | 11 | const T = @typeOf(x); |
| 12 | switch (T) { | |
| 12 | return switch (T) { | |
| 13 | 13 | f32 => @inlineCall(asinh32, x), |
| 14 | 14 | f64 => @inlineCall(asinh64, x), |
| 15 | 15 | else => @compileError("asinh not implemented for " ++ @typeName(T)), |
| 16 | } | |
| 16 | }; | |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | // asinh(x) = sign(x) * log(|x| + sqrt(x * x + 1)) ~= x - x^3/6 + o(x^5) |
| ... | ... | @@ -46,7 +46,7 @@ fn asinh32(x: f32) -> f32 { |
| 46 | 46 | math.forceEval(x + 0x1.0p120); |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | if (s != 0) -rx else rx | |
| 49 | return if (s != 0) -rx else rx; | |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | fn asinh64(x: f64) -> f64 { |
| ... | ... | @@ -77,7 +77,7 @@ fn asinh64(x: f64) -> f64 { |
| 77 | 77 | math.forceEval(x + 0x1.0p120); |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | if (s != 0) -rx else rx | |
| 80 | return if (s != 0) -rx else rx; | |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | test "math.asinh" { |
std/math/atan.zig+6-6| ... | ... | @@ -8,11 +8,11 @@ const assert = @import("../debug.zig").assert; |
| 8 | 8 | |
| 9 | 9 | pub fn atan(x: var) -> @typeOf(x) { |
| 10 | 10 | const T = @typeOf(x); |
| 11 | switch (T) { | |
| 11 | return switch (T) { | |
| 12 | 12 | f32 => @inlineCall(atan32, x), |
| 13 | 13 | f64 => @inlineCall(atan64, x), |
| 14 | 14 | else => @compileError("atan not implemented for " ++ @typeName(T)), |
| 15 | } | |
| 15 | }; | |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | fn atan32(x_: f32) -> f32 { |
| ... | ... | @@ -100,10 +100,10 @@ fn atan32(x_: f32) -> f32 { |
| 100 | 100 | const s2 = w * (aT[1] + w * aT[3]); |
| 101 | 101 | |
| 102 | 102 | if (id == null) { |
| 103 | x - x * (s1 + s2) | |
| 103 | return x - x * (s1 + s2); | |
| 104 | 104 | } else { |
| 105 | 105 | const zz = atanhi[??id] - ((x * (s1 + s2) - atanlo[??id]) - x); |
| 106 | if (sign != 0) -zz else zz | |
| 106 | return if (sign != 0) -zz else zz; | |
| 107 | 107 | } |
| 108 | 108 | } |
| 109 | 109 | |
| ... | ... | @@ -199,10 +199,10 @@ fn atan64(x_: f64) -> f64 { |
| 199 | 199 | const s2 = w * (aT[1] + w * (aT[3] + w * (aT[5] + w * (aT[7] + w * aT[9])))); |
| 200 | 200 | |
| 201 | 201 | if (id == null) { |
| 202 | x - x * (s1 + s2) | |
| 202 | return x - x * (s1 + s2); | |
| 203 | 203 | } else { |
| 204 | 204 | const zz = atanhi[??id] - ((x * (s1 + s2) - atanlo[??id]) - x); |
| 205 | if (sign != 0) -zz else zz | |
| 205 | return if (sign != 0) -zz else zz; | |
| 206 | 206 | } |
| 207 | 207 | } |
| 208 | 208 |
std/math/atan2.zig+8-8| ... | ... | @@ -22,11 +22,11 @@ const math = @import("index.zig"); |
| 22 | 22 | const assert = @import("../debug.zig").assert; |
| 23 | 23 | |
| 24 | 24 | fn atan2(comptime T: type, x: T, y: T) -> T { |
| 25 | switch (T) { | |
| 25 | return switch (T) { | |
| 26 | 26 | f32 => @inlineCall(atan2_32, x, y), |
| 27 | 27 | f64 => @inlineCall(atan2_64, x, y), |
| 28 | 28 | else => @compileError("atan2 not implemented for " ++ @typeName(T)), |
| 29 | } | |
| 29 | }; | |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | fn atan2_32(y: f32, x: f32) -> f32 { |
| ... | ... | @@ -97,11 +97,11 @@ fn atan2_32(y: f32, x: f32) -> f32 { |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | // z = atan(|y / x|) with correct underflow |
| 100 | var z = { | |
| 100 | var z = z: { | |
| 101 | 101 | if ((m & 2) != 0 and iy + (26 << 23) < ix) { |
| 102 | 0.0 | |
| 102 | break :z 0.0; | |
| 103 | 103 | } else { |
| 104 | math.atan(math.fabs(y / x)) | |
| 104 | break :z math.atan(math.fabs(y / x)); | |
| 105 | 105 | } |
| 106 | 106 | }; |
| 107 | 107 | |
| ... | ... | @@ -187,11 +187,11 @@ fn atan2_64(y: f64, x: f64) -> f64 { |
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | // z = atan(|y / x|) with correct underflow |
| 190 | var z = { | |
| 190 | var z = z: { | |
| 191 | 191 | if ((m & 2) != 0 and iy +% (64 << 20) < ix) { |
| 192 | 0.0 | |
| 192 | break :z 0.0; | |
| 193 | 193 | } else { |
| 194 | math.atan(math.fabs(y / x)) | |
| 194 | break :z math.atan(math.fabs(y / x)); | |
| 195 | 195 | } |
| 196 | 196 | }; |
| 197 | 197 |
std/math/atanh.zig+5-5| ... | ... | @@ -9,11 +9,11 @@ const assert = @import("../debug.zig").assert; |
| 9 | 9 | |
| 10 | 10 | pub fn atanh(x: var) -> @typeOf(x) { |
| 11 | 11 | const T = @typeOf(x); |
| 12 | switch (T) { | |
| 12 | return switch (T) { | |
| 13 | 13 | f32 => @inlineCall(atanh_32, x), |
| 14 | 14 | f64 => @inlineCall(atanh_64, x), |
| 15 | 15 | else => @compileError("atanh not implemented for " ++ @typeName(T)), |
| 16 | } | |
| 16 | }; | |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | // atanh(x) = log((1 + x) / (1 - x)) / 2 = log1p(2x / (1 - x)) / 2 ~= x + x^3 / 3 + o(x^5) |
| ... | ... | @@ -32,7 +32,7 @@ fn atanh_32(x: f32) -> f32 { |
| 32 | 32 | if (u < 0x3F800000 - (32 << 23)) { |
| 33 | 33 | // underflow |
| 34 | 34 | if (u < (1 << 23)) { |
| 35 | math.forceEval(y * y) | |
| 35 | math.forceEval(y * y); | |
| 36 | 36 | } |
| 37 | 37 | } |
| 38 | 38 | // |x| < 0.5 |
| ... | ... | @@ -43,7 +43,7 @@ fn atanh_32(x: f32) -> f32 { |
| 43 | 43 | y = 0.5 * math.log1p(2 * (y / (1 - y))); |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | if (s != 0) -y else y | |
| 46 | return if (s != 0) -y else y; | |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | fn atanh_64(x: f64) -> f64 { |
| ... | ... | @@ -72,7 +72,7 @@ fn atanh_64(x: f64) -> f64 { |
| 72 | 72 | y = 0.5 * math.log1p(2 * (y / (1 - y))); |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | if (s != 0) -y else y | |
| 75 | return if (s != 0) -y else y; | |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | test "math.atanh" { |
std/math/cbrt.zig+4-4| ... | ... | @@ -9,11 +9,11 @@ const assert = @import("../debug.zig").assert; |
| 9 | 9 | |
| 10 | 10 | pub fn cbrt(x: var) -> @typeOf(x) { |
| 11 | 11 | const T = @typeOf(x); |
| 12 | switch (T) { | |
| 12 | return switch (T) { | |
| 13 | 13 | f32 => @inlineCall(cbrt32, x), |
| 14 | 14 | f64 => @inlineCall(cbrt64, x), |
| 15 | 15 | else => @compileError("cbrt not implemented for " ++ @typeName(T)), |
| 16 | } | |
| 16 | }; | |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | fn cbrt32(x: f32) -> f32 { |
| ... | ... | @@ -53,7 +53,7 @@ fn cbrt32(x: f32) -> f32 { |
| 53 | 53 | r = t * t * t; |
| 54 | 54 | t = t * (f64(x) + x + r) / (x + r + r); |
| 55 | 55 | |
| 56 | f32(t) | |
| 56 | return f32(t); | |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | fn cbrt64(x: f64) -> f64 { |
| ... | ... | @@ -109,7 +109,7 @@ fn cbrt64(x: f64) -> f64 { |
| 109 | 109 | var w = t + t; |
| 110 | 110 | q = (q - t) / (w + q); |
| 111 | 111 | |
| 112 | t + t * q | |
| 112 | return t + t * q; | |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | test "math.cbrt" { |
std/math/ceil.zig+8-8| ... | ... | @@ -10,11 +10,11 @@ const assert = @import("../debug.zig").assert; |
| 10 | 10 | |
| 11 | 11 | pub fn ceil(x: var) -> @typeOf(x) { |
| 12 | 12 | const T = @typeOf(x); |
| 13 | switch (T) { | |
| 13 | return switch (T) { | |
| 14 | 14 | f32 => @inlineCall(ceil32, x), |
| 15 | 15 | f64 => @inlineCall(ceil64, x), |
| 16 | 16 | else => @compileError("ceil not implemented for " ++ @typeName(T)), |
| 17 | } | |
| 17 | }; | |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | fn ceil32(x: f32) -> f32 { |
| ... | ... | @@ -39,13 +39,13 @@ fn ceil32(x: f32) -> f32 { |
| 39 | 39 | u += m; |
| 40 | 40 | } |
| 41 | 41 | u &= ~m; |
| 42 | @bitCast(f32, u) | |
| 42 | return @bitCast(f32, u); | |
| 43 | 43 | } else { |
| 44 | 44 | math.forceEval(x + 0x1.0p120); |
| 45 | 45 | if (u >> 31 != 0) { |
| 46 | 46 | return -0.0; |
| 47 | 47 | } else { |
| 48 | 1.0 | |
| 48 | return 1.0; | |
| 49 | 49 | } |
| 50 | 50 | } |
| 51 | 51 | } |
| ... | ... | @@ -70,14 +70,14 @@ fn ceil64(x: f64) -> f64 { |
| 70 | 70 | if (e <= 0x3FF-1) { |
| 71 | 71 | math.forceEval(y); |
| 72 | 72 | if (u >> 63 != 0) { |
| 73 | return -0.0; // Compiler requires return. | |
| 73 | return -0.0; | |
| 74 | 74 | } else { |
| 75 | 1.0 | |
| 75 | return 1.0; | |
| 76 | 76 | } |
| 77 | 77 | } else if (y < 0) { |
| 78 | x + y + 1 | |
| 78 | return x + y + 1; | |
| 79 | 79 | } else { |
| 80 | x + y | |
| 80 | return x + y; | |
| 81 | 81 | } |
| 82 | 82 | } |
| 83 | 83 |
std/math/copysign.zig+4-4| ... | ... | @@ -2,11 +2,11 @@ const math = @import("index.zig"); |
| 2 | 2 | const assert = @import("../debug.zig").assert; |
| 3 | 3 | |
| 4 | 4 | pub fn copysign(comptime T: type, x: T, y: T) -> T { |
| 5 | switch (T) { | |
| 5 | return switch (T) { | |
| 6 | 6 | f32 => @inlineCall(copysign32, x, y), |
| 7 | 7 | f64 => @inlineCall(copysign64, x, y), |
| 8 | 8 | else => @compileError("copysign not implemented for " ++ @typeName(T)), |
| 9 | } | |
| 9 | }; | |
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | fn copysign32(x: f32, y: f32) -> f32 { |
| ... | ... | @@ -15,7 +15,7 @@ fn copysign32(x: f32, y: f32) -> f32 { |
| 15 | 15 | |
| 16 | 16 | const h1 = ux & (@maxValue(u32) / 2); |
| 17 | 17 | const h2 = uy & (u32(1) << 31); |
| 18 | @bitCast(f32, h1 | h2) | |
| 18 | return @bitCast(f32, h1 | h2); | |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | fn copysign64(x: f64, y: f64) -> f64 { |
| ... | ... | @@ -24,7 +24,7 @@ fn copysign64(x: f64, y: f64) -> f64 { |
| 24 | 24 | |
| 25 | 25 | const h1 = ux & (@maxValue(u64) / 2); |
| 26 | 26 | const h2 = uy & (u64(1) << 63); |
| 27 | @bitCast(f64, h1 | h2) | |
| 27 | return @bitCast(f64, h1 | h2); | |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | test "math.copysign" { |
std/math/cos.zig+12-12| ... | ... | @@ -9,11 +9,11 @@ const assert = @import("../debug.zig").assert; |
| 9 | 9 | |
| 10 | 10 | pub fn cos(x: var) -> @typeOf(x) { |
| 11 | 11 | const T = @typeOf(x); |
| 12 | switch (T) { | |
| 12 | return switch (T) { | |
| 13 | 13 | f32 => @inlineCall(cos32, x), |
| 14 | 14 | f64 => @inlineCall(cos64, x), |
| 15 | 15 | else => @compileError("cos not implemented for " ++ @typeName(T)), |
| 16 | } | |
| 16 | }; | |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | // sin polynomial coefficients |
| ... | ... | @@ -73,18 +73,18 @@ fn cos32(x_: f32) -> f32 { |
| 73 | 73 | const z = ((x - y * pi4a) - y * pi4b) - y * pi4c; |
| 74 | 74 | const w = z * z; |
| 75 | 75 | |
| 76 | const r = { | |
| 76 | const r = r: { | |
| 77 | 77 | if (j == 1 or j == 2) { |
| 78 | z + z * w * (S5 + w * (S4 + w * (S3 + w * (S2 + w * (S1 + w * S0))))) | |
| 78 | break :r z + z * w * (S5 + w * (S4 + w * (S3 + w * (S2 + w * (S1 + w * S0))))); | |
| 79 | 79 | } else { |
| 80 | 1.0 - 0.5 * w + w * w * (C5 + w * (C4 + w * (C3 + w * (C2 + w * (C1 + w * C0))))) | |
| 80 | break :r 1.0 - 0.5 * w + w * w * (C5 + w * (C4 + w * (C3 + w * (C2 + w * (C1 + w * C0))))); | |
| 81 | 81 | } |
| 82 | 82 | }; |
| 83 | 83 | |
| 84 | 84 | if (sign) { |
| 85 | -r | |
| 85 | return -r; | |
| 86 | 86 | } else { |
| 87 | r | |
| 87 | return r; | |
| 88 | 88 | } |
| 89 | 89 | } |
| 90 | 90 | |
| ... | ... | @@ -124,18 +124,18 @@ fn cos64(x_: f64) -> f64 { |
| 124 | 124 | const z = ((x - y * pi4a) - y * pi4b) - y * pi4c; |
| 125 | 125 | const w = z * z; |
| 126 | 126 | |
| 127 | const r = { | |
| 127 | const r = r: { | |
| 128 | 128 | if (j == 1 or j == 2) { |
| 129 | z + z * w * (S5 + w * (S4 + w * (S3 + w * (S2 + w * (S1 + w * S0))))) | |
| 129 | break :r z + z * w * (S5 + w * (S4 + w * (S3 + w * (S2 + w * (S1 + w * S0))))); | |
| 130 | 130 | } else { |
| 131 | 1.0 - 0.5 * w + w * w * (C5 + w * (C4 + w * (C3 + w * (C2 + w * (C1 + w * C0))))) | |
| 131 | break :r 1.0 - 0.5 * w + w * w * (C5 + w * (C4 + w * (C3 + w * (C2 + w * (C1 + w * C0))))); | |
| 132 | 132 | } |
| 133 | 133 | }; |
| 134 | 134 | |
| 135 | 135 | if (sign) { |
| 136 | -r | |
| 136 | return -r; | |
| 137 | 137 | } else { |
| 138 | r | |
| 138 | return r; | |
| 139 | 139 | } |
| 140 | 140 | } |
| 141 | 141 |
std/math/cosh.zig+4-4| ... | ... | @@ -11,11 +11,11 @@ const assert = @import("../debug.zig").assert; |
| 11 | 11 | |
| 12 | 12 | pub fn cosh(x: var) -> @typeOf(x) { |
| 13 | 13 | const T = @typeOf(x); |
| 14 | switch (T) { | |
| 14 | return switch (T) { | |
| 15 | 15 | f32 => @inlineCall(cosh32, x), |
| 16 | 16 | f64 => @inlineCall(cosh64, x), |
| 17 | 17 | else => @compileError("cosh not implemented for " ++ @typeName(T)), |
| 18 | } | |
| 18 | }; | |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | // cosh(x) = (exp(x) + 1 / exp(x)) / 2 |
| ... | ... | @@ -43,7 +43,7 @@ fn cosh32(x: f32) -> f32 { |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | // |x| > log(FLT_MAX) or nan |
| 46 | expo2(ax) | |
| 46 | return expo2(ax); | |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | fn cosh64(x: f64) -> f64 { |
| ... | ... | @@ -76,7 +76,7 @@ fn cosh64(x: f64) -> f64 { |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | // |x| > log(CBL_MAX) or nan |
| 79 | expo2(ax) | |
| 79 | return expo2(ax); | |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | test "math.cosh" { |
std/math/exp.zig+6-6| ... | ... | @@ -8,11 +8,11 @@ const assert = @import("../debug.zig").assert; |
| 8 | 8 | |
| 9 | 9 | pub fn exp(x: var) -> @typeOf(x) { |
| 10 | 10 | const T = @typeOf(x); |
| 11 | switch (T) { | |
| 11 | return switch (T) { | |
| 12 | 12 | f32 => @inlineCall(exp32, x), |
| 13 | 13 | f64 => @inlineCall(exp64, x), |
| 14 | 14 | else => @compileError("exp not implemented for " ++ @typeName(T)), |
| 15 | } | |
| 15 | }; | |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | fn exp32(x_: f32) -> f32 { |
| ... | ... | @@ -86,9 +86,9 @@ fn exp32(x_: f32) -> f32 { |
| 86 | 86 | const y = 1 + (x * c / (2 - c) - lo + hi); |
| 87 | 87 | |
| 88 | 88 | if (k == 0) { |
| 89 | y | |
| 89 | return y; | |
| 90 | 90 | } else { |
| 91 | math.scalbn(y, k) | |
| 91 | return math.scalbn(y, k); | |
| 92 | 92 | } |
| 93 | 93 | } |
| 94 | 94 | |
| ... | ... | @@ -172,9 +172,9 @@ fn exp64(x_: f64) -> f64 { |
| 172 | 172 | const y = 1 + (x * c / (2 - c) - lo + hi); |
| 173 | 173 | |
| 174 | 174 | if (k == 0) { |
| 175 | y | |
| 175 | return y; | |
| 176 | 176 | } else { |
| 177 | math.scalbn(y, k) | |
| 177 | return math.scalbn(y, k); | |
| 178 | 178 | } |
| 179 | 179 | } |
| 180 | 180 |
std/math/exp2.zig+4-4| ... | ... | @@ -8,11 +8,11 @@ const assert = @import("../debug.zig").assert; |
| 8 | 8 | |
| 9 | 9 | pub fn exp2(x: var) -> @typeOf(x) { |
| 10 | 10 | const T = @typeOf(x); |
| 11 | switch (T) { | |
| 11 | return switch (T) { | |
| 12 | 12 | f32 => @inlineCall(exp2_32, x), |
| 13 | 13 | f64 => @inlineCall(exp2_64, x), |
| 14 | 14 | else => @compileError("exp2 not implemented for " ++ @typeName(T)), |
| 15 | } | |
| 15 | }; | |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | const exp2ft = []const f64 { |
| ... | ... | @@ -88,7 +88,7 @@ fn exp2_32(x: f32) -> f32 { |
| 88 | 88 | var r: f64 = exp2ft[i0]; |
| 89 | 89 | const t: f64 = r * z; |
| 90 | 90 | r = r + t * (P1 + z * P2) + t * (z * z) * (P3 + z * P4); |
| 91 | f32(r * uk) | |
| 91 | return f32(r * uk); | |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | const exp2dt = []f64 { |
| ... | ... | @@ -414,7 +414,7 @@ fn exp2_64(x: f64) -> f64 { |
| 414 | 414 | z -= exp2dt[2 * i0 + 1]; |
| 415 | 415 | const r = t + t * z * (P1 + z * (P2 + z * (P3 + z * (P4 + z * P5)))); |
| 416 | 416 | |
| 417 | math.scalbn(r, ik) | |
| 417 | return math.scalbn(r, ik); | |
| 418 | 418 | } |
| 419 | 419 | |
| 420 | 420 | test "math.exp2" { |
std/math/expm1.zig+2-2| ... | ... | @@ -9,11 +9,11 @@ const assert = @import("../debug.zig").assert; |
| 9 | 9 | |
| 10 | 10 | pub fn expm1(x: var) -> @typeOf(x) { |
| 11 | 11 | const T = @typeOf(x); |
| 12 | switch (T) { | |
| 12 | return switch (T) { | |
| 13 | 13 | f32 => @inlineCall(expm1_32, x), |
| 14 | 14 | f64 => @inlineCall(expm1_64, x), |
| 15 | 15 | else => @compileError("exp1m not implemented for " ++ @typeName(T)), |
| 16 | } | |
| 16 | }; | |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | fn expm1_32(x_: f32) -> f32 { |
std/math/expo2.zig+4-4| ... | ... | @@ -2,11 +2,11 @@ const math = @import("index.zig"); |
| 2 | 2 | |
| 3 | 3 | pub fn expo2(x: var) -> @typeOf(x) { |
| 4 | 4 | const T = @typeOf(x); |
| 5 | switch (T) { | |
| 5 | return switch (T) { | |
| 6 | 6 | f32 => expo2f(x), |
| 7 | 7 | f64 => expo2d(x), |
| 8 | 8 | else => @compileError("expo2 not implemented for " ++ @typeName(T)), |
| 9 | } | |
| 9 | }; | |
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | fn expo2f(x: f32) -> f32 { |
| ... | ... | @@ -15,7 +15,7 @@ fn expo2f(x: f32) -> f32 { |
| 15 | 15 | |
| 16 | 16 | const u = (0x7F + k / 2) << 23; |
| 17 | 17 | const scale = @bitCast(f32, u); |
| 18 | math.exp(x - kln2) * scale * scale | |
| 18 | return math.exp(x - kln2) * scale * scale; | |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | fn expo2d(x: f64) -> f64 { |
| ... | ... | @@ -24,5 +24,5 @@ fn expo2d(x: f64) -> f64 { |
| 24 | 24 | |
| 25 | 25 | const u = (0x3FF + k / 2) << 20; |
| 26 | 26 | const scale = @bitCast(f64, u64(u) << 32); |
| 27 | math.exp(x - kln2) * scale * scale | |
| 27 | return math.exp(x - kln2) * scale * scale; | |
| 28 | 28 | } |
std/math/fabs.zig+4-4| ... | ... | @@ -8,23 +8,23 @@ const assert = @import("../debug.zig").assert; |
| 8 | 8 | |
| 9 | 9 | pub fn fabs(x: var) -> @typeOf(x) { |
| 10 | 10 | const T = @typeOf(x); |
| 11 | switch (T) { | |
| 11 | return switch (T) { | |
| 12 | 12 | f32 => @inlineCall(fabs32, x), |
| 13 | 13 | f64 => @inlineCall(fabs64, x), |
| 14 | 14 | else => @compileError("fabs not implemented for " ++ @typeName(T)), |
| 15 | } | |
| 15 | }; | |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | fn fabs32(x: f32) -> f32 { |
| 19 | 19 | var u = @bitCast(u32, x); |
| 20 | 20 | u &= 0x7FFFFFFF; |
| 21 | @bitCast(f32, u) | |
| 21 | return @bitCast(f32, u); | |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | fn fabs64(x: f64) -> f64 { |
| 25 | 25 | var u = @bitCast(u64, x); |
| 26 | 26 | u &= @maxValue(u64) >> 1; |
| 27 | @bitCast(f64, u) | |
| 27 | return @bitCast(f64, u); | |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | test "math.fabs" { |
std/math/floor.zig+9-9| ... | ... | @@ -10,11 +10,11 @@ const math = @import("index.zig"); |
| 10 | 10 | |
| 11 | 11 | pub fn floor(x: var) -> @typeOf(x) { |
| 12 | 12 | const T = @typeOf(x); |
| 13 | switch (T) { | |
| 13 | return switch (T) { | |
| 14 | 14 | f32 => @inlineCall(floor32, x), |
| 15 | 15 | f64 => @inlineCall(floor64, x), |
| 16 | 16 | else => @compileError("floor not implemented for " ++ @typeName(T)), |
| 17 | } | |
| 17 | }; | |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | fn floor32(x: f32) -> f32 { |
| ... | ... | @@ -40,13 +40,13 @@ fn floor32(x: f32) -> f32 { |
| 40 | 40 | if (u >> 31 != 0) { |
| 41 | 41 | u += m; |
| 42 | 42 | } |
| 43 | @bitCast(f32, u & ~m) | |
| 43 | return @bitCast(f32, u & ~m); | |
| 44 | 44 | } else { |
| 45 | 45 | math.forceEval(x + 0x1.0p120); |
| 46 | 46 | if (u >> 31 == 0) { |
| 47 | return 0.0; // Compiler requires return | |
| 47 | return 0.0; | |
| 48 | 48 | } else { |
| 49 | -1.0 | |
| 49 | return -1.0; | |
| 50 | 50 | } |
| 51 | 51 | } |
| 52 | 52 | } |
| ... | ... | @@ -71,14 +71,14 @@ fn floor64(x: f64) -> f64 { |
| 71 | 71 | if (e <= 0x3FF-1) { |
| 72 | 72 | math.forceEval(y); |
| 73 | 73 | if (u >> 63 != 0) { |
| 74 | return -1.0; // Compiler requires return. | |
| 74 | return -1.0; | |
| 75 | 75 | } else { |
| 76 | 0.0 | |
| 76 | return 0.0; | |
| 77 | 77 | } |
| 78 | 78 | } else if (y > 0) { |
| 79 | x + y - 1 | |
| 79 | return x + y - 1; | |
| 80 | 80 | } else { |
| 81 | x + y | |
| 81 | return x + y; | |
| 82 | 82 | } |
| 83 | 83 | } |
| 84 | 84 |
std/math/fma.zig+10-10| ... | ... | @@ -2,11 +2,11 @@ const math = @import("index.zig"); |
| 2 | 2 | const assert = @import("../debug.zig").assert; |
| 3 | 3 | |
| 4 | 4 | pub fn fma(comptime T: type, x: T, y: T, z: T) -> T { |
| 5 | switch (T) { | |
| 5 | return switch (T) { | |
| 6 | 6 | f32 => @inlineCall(fma32, x, y, z), |
| 7 | 7 | f64 => @inlineCall(fma64, x, y ,z), |
| 8 | 8 | else => @compileError("fma not implemented for " ++ @typeName(T)), |
| 9 | } | |
| 9 | }; | |
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | fn fma32(x: f32, y: f32, z: f32) -> f32 { |
| ... | ... | @@ -16,10 +16,10 @@ fn fma32(x: f32, y: f32, z: f32) -> f32 { |
| 16 | 16 | const e = (u >> 52) & 0x7FF; |
| 17 | 17 | |
| 18 | 18 | if ((u & 0x1FFFFFFF) != 0x10000000 or e == 0x7FF or xy_z - xy == z) { |
| 19 | f32(xy_z) | |
| 19 | return f32(xy_z); | |
| 20 | 20 | } else { |
| 21 | 21 | // TODO: Handle inexact case with double-rounding |
| 22 | f32(xy_z) | |
| 22 | return f32(xy_z); | |
| 23 | 23 | } |
| 24 | 24 | } |
| 25 | 25 | |
| ... | ... | @@ -64,9 +64,9 @@ fn fma64(x: f64, y: f64, z: f64) -> f64 { |
| 64 | 64 | |
| 65 | 65 | const adj = add_adjusted(r.lo, xy.lo); |
| 66 | 66 | if (spread + math.ilogb(r.hi) > -1023) { |
| 67 | math.scalbn(r.hi + adj, spread) | |
| 67 | return math.scalbn(r.hi + adj, spread); | |
| 68 | 68 | } else { |
| 69 | add_and_denorm(r.hi, adj, spread) | |
| 69 | return add_and_denorm(r.hi, adj, spread); | |
| 70 | 70 | } |
| 71 | 71 | } |
| 72 | 72 | |
| ... | ... | @@ -77,7 +77,7 @@ fn dd_add(a: f64, b: f64) -> dd { |
| 77 | 77 | ret.hi = a + b; |
| 78 | 78 | const s = ret.hi - a; |
| 79 | 79 | ret.lo = (a - (ret.hi - s)) + (b - s); |
| 80 | ret | |
| 80 | return ret; | |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | fn dd_mul(a: f64, b: f64) -> dd { |
| ... | ... | @@ -99,7 +99,7 @@ fn dd_mul(a: f64, b: f64) -> dd { |
| 99 | 99 | |
| 100 | 100 | ret.hi = p + q; |
| 101 | 101 | ret.lo = p - ret.hi + q + la * lb; |
| 102 | ret | |
| 102 | return ret; | |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | fn add_adjusted(a: f64, b: f64) -> f64 { |
| ... | ... | @@ -113,7 +113,7 @@ fn add_adjusted(a: f64, b: f64) -> f64 { |
| 113 | 113 | sum.hi = @bitCast(f64, uhii); |
| 114 | 114 | } |
| 115 | 115 | } |
| 116 | sum.hi | |
| 116 | return sum.hi; | |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | fn add_and_denorm(a: f64, b: f64, scale: i32) -> f64 { |
| ... | ... | @@ -127,7 +127,7 @@ fn add_and_denorm(a: f64, b: f64, scale: i32) -> f64 { |
| 127 | 127 | sum.hi = @bitCast(f64, uhii); |
| 128 | 128 | } |
| 129 | 129 | } |
| 130 | math.scalbn(sum.hi, scale) | |
| 130 | return math.scalbn(sum.hi, scale); | |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | test "math.fma" { |
std/math/frexp.zig+6-6| ... | ... | @@ -8,21 +8,21 @@ const math = @import("index.zig"); |
| 8 | 8 | const assert = @import("../debug.zig").assert; |
| 9 | 9 | |
| 10 | 10 | fn frexp_result(comptime T: type) -> type { |
| 11 | struct { | |
| 11 | return struct { | |
| 12 | 12 | significand: T, |
| 13 | 13 | exponent: i32, |
| 14 | } | |
| 14 | }; | |
| 15 | 15 | } |
| 16 | 16 | pub const frexp32_result = frexp_result(f32); |
| 17 | 17 | pub const frexp64_result = frexp_result(f64); |
| 18 | 18 | |
| 19 | 19 | pub fn frexp(x: var) -> frexp_result(@typeOf(x)) { |
| 20 | 20 | const T = @typeOf(x); |
| 21 | switch (T) { | |
| 21 | return switch (T) { | |
| 22 | 22 | f32 => @inlineCall(frexp32, x), |
| 23 | 23 | f64 => @inlineCall(frexp64, x), |
| 24 | 24 | else => @compileError("frexp not implemented for " ++ @typeName(T)), |
| 25 | } | |
| 25 | }; | |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | fn frexp32(x: f32) -> frexp32_result { |
| ... | ... | @@ -59,7 +59,7 @@ fn frexp32(x: f32) -> frexp32_result { |
| 59 | 59 | y &= 0x807FFFFF; |
| 60 | 60 | y |= 0x3F000000; |
| 61 | 61 | result.significand = @bitCast(f32, y); |
| 62 | result | |
| 62 | return result; | |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | fn frexp64(x: f64) -> frexp64_result { |
| ... | ... | @@ -96,7 +96,7 @@ fn frexp64(x: f64) -> frexp64_result { |
| 96 | 96 | y &= 0x800FFFFFFFFFFFFF; |
| 97 | 97 | y |= 0x3FE0000000000000; |
| 98 | 98 | result.significand = @bitCast(f64, y); |
| 99 | result | |
| 99 | return result; | |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | test "math.frexp" { |
std/math/hypot.zig+4-4| ... | ... | @@ -9,11 +9,11 @@ const math = @import("index.zig"); |
| 9 | 9 | const assert = @import("../debug.zig").assert; |
| 10 | 10 | |
| 11 | 11 | pub fn hypot(comptime T: type, x: T, y: T) -> T { |
| 12 | switch (T) { | |
| 12 | return switch (T) { | |
| 13 | 13 | f32 => @inlineCall(hypot32, x, y), |
| 14 | 14 | f64 => @inlineCall(hypot64, x, y), |
| 15 | 15 | else => @compileError("hypot not implemented for " ++ @typeName(T)), |
| 16 | } | |
| 16 | }; | |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | fn hypot32(x: f32, y: f32) -> f32 { |
| ... | ... | @@ -48,7 +48,7 @@ fn hypot32(x: f32, y: f32) -> f32 { |
| 48 | 48 | yy *= 0x1.0p-90; |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | z * math.sqrt(f32(f64(x) * x + f64(y) * y)) | |
| 51 | return z * math.sqrt(f32(f64(x) * x + f64(y) * y)); | |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | fn sq(hi: &f64, lo: &f64, x: f64) { |
| ... | ... | @@ -109,7 +109,7 @@ fn hypot64(x: f64, y: f64) -> f64 { |
| 109 | 109 | sq(&hx, &lx, x); |
| 110 | 110 | sq(&hy, &ly, y); |
| 111 | 111 | |
| 112 | z * math.sqrt(ly + lx + hy + hx) | |
| 112 | return z * math.sqrt(ly + lx + hy + hx); | |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | test "math.hypot" { |
std/math/ilogb.zig+4-4| ... | ... | @@ -9,11 +9,11 @@ const assert = @import("../debug.zig").assert; |
| 9 | 9 | |
| 10 | 10 | pub fn ilogb(x: var) -> i32 { |
| 11 | 11 | const T = @typeOf(x); |
| 12 | switch (T) { | |
| 12 | return switch (T) { | |
| 13 | 13 | f32 => @inlineCall(ilogb32, x), |
| 14 | 14 | f64 => @inlineCall(ilogb64, x), |
| 15 | 15 | else => @compileError("ilogb not implemented for " ++ @typeName(T)), |
| 16 | } | |
| 16 | }; | |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | // NOTE: Should these be exposed publically? |
| ... | ... | @@ -53,7 +53,7 @@ fn ilogb32(x: f32) -> i32 { |
| 53 | 53 | } |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | e - 0x7F | |
| 56 | return e - 0x7F; | |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | fn ilogb64(x: f64) -> i32 { |
| ... | ... | @@ -88,7 +88,7 @@ fn ilogb64(x: f64) -> i32 { |
| 88 | 88 | } |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | e - 0x3FF | |
| 91 | return e - 0x3FF; | |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | test "math.ilogb" { |
std/math/index.zig+8-8| ... | ... | @@ -36,7 +36,7 @@ pub const inf = @import("inf.zig").inf; |
| 36 | 36 | |
| 37 | 37 | pub fn approxEq(comptime T: type, x: T, y: T, epsilon: T) -> bool { |
| 38 | 38 | assert(@typeId(T) == TypeId.Float); |
| 39 | fabs(x - y) < epsilon | |
| 39 | return fabs(x - y) < epsilon; | |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | // TODO: Hide the following in an internal module. |
| ... | ... | @@ -175,7 +175,7 @@ test "math" { |
| 175 | 175 | |
| 176 | 176 | |
| 177 | 177 | pub fn min(x: var, y: var) -> @typeOf(x + y) { |
| 178 | if (x < y) x else y | |
| 178 | return if (x < y) x else y; | |
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | test "math.min" { |
| ... | ... | @@ -183,7 +183,7 @@ test "math.min" { |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | pub fn max(x: var, y: var) -> @typeOf(x + y) { |
| 186 | if (x > y) x else y | |
| 186 | return if (x > y) x else y; | |
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | test "math.max" { |
| ... | ... | @@ -193,19 +193,19 @@ test "math.max" { |
| 193 | 193 | error Overflow; |
| 194 | 194 | pub fn mul(comptime T: type, a: T, b: T) -> %T { |
| 195 | 195 | var answer: T = undefined; |
| 196 | if (@mulWithOverflow(T, a, b, &answer)) error.Overflow else answer | |
| 196 | return if (@mulWithOverflow(T, a, b, &answer)) error.Overflow else answer; | |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | error Overflow; |
| 200 | 200 | pub fn add(comptime T: type, a: T, b: T) -> %T { |
| 201 | 201 | var answer: T = undefined; |
| 202 | if (@addWithOverflow(T, a, b, &answer)) error.Overflow else answer | |
| 202 | return if (@addWithOverflow(T, a, b, &answer)) error.Overflow else answer; | |
| 203 | 203 | } |
| 204 | 204 | |
| 205 | 205 | error Overflow; |
| 206 | 206 | pub fn sub(comptime T: type, a: T, b: T) -> %T { |
| 207 | 207 | var answer: T = undefined; |
| 208 | if (@subWithOverflow(T, a, b, &answer)) error.Overflow else answer | |
| 208 | return if (@subWithOverflow(T, a, b, &answer)) error.Overflow else answer; | |
| 209 | 209 | } |
| 210 | 210 | |
| 211 | 211 | pub fn negate(x: var) -> %@typeOf(x) { |
| ... | ... | @@ -215,7 +215,7 @@ pub fn negate(x: var) -> %@typeOf(x) { |
| 215 | 215 | error Overflow; |
| 216 | 216 | pub fn shlExact(comptime T: type, a: T, shift_amt: Log2Int(T)) -> %T { |
| 217 | 217 | var answer: T = undefined; |
| 218 | if (@shlWithOverflow(T, a, shift_amt, &answer)) error.Overflow else answer | |
| 218 | return if (@shlWithOverflow(T, a, shift_amt, &answer)) error.Overflow else answer; | |
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | /// Shifts left. Overflowed bits are truncated. |
| ... | ... | @@ -267,7 +267,7 @@ test "math.shr" { |
| 267 | 267 | } |
| 268 | 268 | |
| 269 | 269 | pub fn Log2Int(comptime T: type) -> type { |
| 270 | @IntType(false, log2(T.bit_count)) | |
| 270 | return @IntType(false, log2(T.bit_count)); | |
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | test "math overflow functions" { |
std/math/inf.zig+2-2| ... | ... | @@ -2,9 +2,9 @@ const math = @import("index.zig"); |
| 2 | 2 | const assert = @import("../debug.zig").assert; |
| 3 | 3 | |
| 4 | 4 | pub fn inf(comptime T: type) -> T { |
| 5 | switch (T) { | |
| 5 | return switch (T) { | |
| 6 | 6 | f32 => @bitCast(f32, math.inf_u32), |
| 7 | 7 | f64 => @bitCast(f64, math.inf_u64), |
| 8 | 8 | else => @compileError("inf not implemented for " ++ @typeName(T)), |
| 9 | } | |
| 9 | }; | |
| 10 | 10 | } |
std/math/isfinite.zig+2-2| ... | ... | @@ -6,11 +6,11 @@ pub fn isFinite(x: var) -> bool { |
| 6 | 6 | switch (T) { |
| 7 | 7 | f32 => { |
| 8 | 8 | const bits = @bitCast(u32, x); |
| 9 | bits & 0x7FFFFFFF < 0x7F800000 | |
| 9 | return bits & 0x7FFFFFFF < 0x7F800000; | |
| 10 | 10 | }, |
| 11 | 11 | f64 => { |
| 12 | 12 | const bits = @bitCast(u64, x); |
| 13 | bits & (@maxValue(u64) >> 1) < (0x7FF << 52) | |
| 13 | return bits & (@maxValue(u64) >> 1) < (0x7FF << 52); | |
| 14 | 14 | }, |
| 15 | 15 | else => { |
| 16 | 16 | @compileError("isFinite not implemented for " ++ @typeName(T)); |
std/math/isinf.zig+6-6| ... | ... | @@ -6,11 +6,11 @@ pub fn isInf(x: var) -> bool { |
| 6 | 6 | switch (T) { |
| 7 | 7 | f32 => { |
| 8 | 8 | const bits = @bitCast(u32, x); |
| 9 | bits & 0x7FFFFFFF == 0x7F800000 | |
| 9 | return bits & 0x7FFFFFFF == 0x7F800000; | |
| 10 | 10 | }, |
| 11 | 11 | f64 => { |
| 12 | 12 | const bits = @bitCast(u64, x); |
| 13 | bits & (@maxValue(u64) >> 1) == (0x7FF << 52) | |
| 13 | return bits & (@maxValue(u64) >> 1) == (0x7FF << 52); | |
| 14 | 14 | }, |
| 15 | 15 | else => { |
| 16 | 16 | @compileError("isInf not implemented for " ++ @typeName(T)); |
| ... | ... | @@ -22,10 +22,10 @@ pub fn isPositiveInf(x: var) -> bool { |
| 22 | 22 | const T = @typeOf(x); |
| 23 | 23 | switch (T) { |
| 24 | 24 | f32 => { |
| 25 | @bitCast(u32, x) == 0x7F800000 | |
| 25 | return @bitCast(u32, x) == 0x7F800000; | |
| 26 | 26 | }, |
| 27 | 27 | f64 => { |
| 28 | @bitCast(u64, x) == 0x7FF << 52 | |
| 28 | return @bitCast(u64, x) == 0x7FF << 52; | |
| 29 | 29 | }, |
| 30 | 30 | else => { |
| 31 | 31 | @compileError("isPositiveInf not implemented for " ++ @typeName(T)); |
| ... | ... | @@ -37,10 +37,10 @@ pub fn isNegativeInf(x: var) -> bool { |
| 37 | 37 | const T = @typeOf(x); |
| 38 | 38 | switch (T) { |
| 39 | 39 | f32 => { |
| 40 | @bitCast(u32, x) == 0xFF800000 | |
| 40 | return @bitCast(u32, x) == 0xFF800000; | |
| 41 | 41 | }, |
| 42 | 42 | f64 => { |
| 43 | @bitCast(u64, x) == 0xFFF << 52 | |
| 43 | return @bitCast(u64, x) == 0xFFF << 52; | |
| 44 | 44 | }, |
| 45 | 45 | else => { |
| 46 | 46 | @compileError("isNegativeInf not implemented for " ++ @typeName(T)); |
std/math/isnan.zig+3-3| ... | ... | @@ -6,11 +6,11 @@ pub fn isNan(x: var) -> bool { |
| 6 | 6 | switch (T) { |
| 7 | 7 | f32 => { |
| 8 | 8 | const bits = @bitCast(u32, x); |
| 9 | bits & 0x7FFFFFFF > 0x7F800000 | |
| 9 | return bits & 0x7FFFFFFF > 0x7F800000; | |
| 10 | 10 | }, |
| 11 | 11 | f64 => { |
| 12 | 12 | const bits = @bitCast(u64, x); |
| 13 | (bits & (@maxValue(u64) >> 1)) > (u64(0x7FF) << 52) | |
| 13 | return (bits & (@maxValue(u64) >> 1)) > (u64(0x7FF) << 52); | |
| 14 | 14 | }, |
| 15 | 15 | else => { |
| 16 | 16 | @compileError("isNan not implemented for " ++ @typeName(T)); |
| ... | ... | @@ -21,7 +21,7 @@ pub fn isNan(x: var) -> bool { |
| 21 | 21 | // Note: A signalling nan is identical to a standard right now by may have a different bit |
| 22 | 22 | // representation in the future when required. |
| 23 | 23 | pub fn isSignalNan(x: var) -> bool { |
| 24 | isNan(x) | |
| 24 | return isNan(x); | |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | test "math.isNan" { |
std/math/isnormal.zig+2-2| ... | ... | @@ -6,11 +6,11 @@ pub fn isNormal(x: var) -> bool { |
| 6 | 6 | switch (T) { |
| 7 | 7 | f32 => { |
| 8 | 8 | const bits = @bitCast(u32, x); |
| 9 | (bits + 0x00800000) & 0x7FFFFFFF >= 0x01000000 | |
| 9 | return (bits + 0x00800000) & 0x7FFFFFFF >= 0x01000000; | |
| 10 | 10 | }, |
| 11 | 11 | f64 => { |
| 12 | 12 | const bits = @bitCast(u64, x); |
| 13 | (bits + (1 << 52)) & (@maxValue(u64) >> 1) >= (1 << 53) | |
| 13 | return (bits + (1 << 52)) & (@maxValue(u64) >> 1) >= (1 << 53); | |
| 14 | 14 | }, |
| 15 | 15 | else => { |
| 16 | 16 | @compileError("isNormal not implemented for " ++ @typeName(T)); |
std/math/ln.zig+4-4| ... | ... | @@ -14,7 +14,7 @@ pub fn ln(x: var) -> @typeOf(x) { |
| 14 | 14 | const T = @typeOf(x); |
| 15 | 15 | switch (@typeId(T)) { |
| 16 | 16 | TypeId.FloatLiteral => { |
| 17 | return @typeOf(1.0)(ln_64(x)) | |
| 17 | return @typeOf(1.0)(ln_64(x)); | |
| 18 | 18 | }, |
| 19 | 19 | TypeId.Float => { |
| 20 | 20 | return switch (T) { |
| ... | ... | @@ -84,7 +84,7 @@ pub fn ln_32(x_: f32) -> f32 { |
| 84 | 84 | const hfsq = 0.5 * f * f; |
| 85 | 85 | const dk = f32(k); |
| 86 | 86 | |
| 87 | s * (hfsq + R) + dk * ln2_lo - hfsq + f + dk * ln2_hi | |
| 87 | return s * (hfsq + R) + dk * ln2_lo - hfsq + f + dk * ln2_hi; | |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | pub fn ln_64(x_: f64) -> f64 { |
| ... | ... | @@ -116,7 +116,7 @@ pub fn ln_64(x_: f64) -> f64 { |
| 116 | 116 | // subnormal, scale x |
| 117 | 117 | k -= 54; |
| 118 | 118 | x *= 0x1.0p54; |
| 119 | hx = u32(@bitCast(u64, ix) >> 32) | |
| 119 | hx = u32(@bitCast(u64, ix) >> 32); | |
| 120 | 120 | } |
| 121 | 121 | else if (hx >= 0x7FF00000) { |
| 122 | 122 | return x; |
| ... | ... | @@ -142,7 +142,7 @@ pub fn ln_64(x_: f64) -> f64 { |
| 142 | 142 | const R = t2 + t1; |
| 143 | 143 | const dk = f64(k); |
| 144 | 144 | |
| 145 | s * (hfsq + R) + dk * ln2_lo - hfsq + f + dk * ln2_hi | |
| 145 | return s * (hfsq + R) + dk * ln2_lo - hfsq + f + dk * ln2_hi; | |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | test "math.ln" { |
std/math/log.zig+1-1| ... | ... | @@ -29,7 +29,7 @@ pub fn log(comptime T: type, base: T, x: T) -> T { |
| 29 | 29 | f32 => return f32(math.ln(f64(x)) / math.ln(f64(base))), |
| 30 | 30 | f64 => return math.ln(x) / math.ln(f64(base)), |
| 31 | 31 | else => @compileError("log not implemented for " ++ @typeName(T)), |
| 32 | }; | |
| 32 | } | |
| 33 | 33 | }, |
| 34 | 34 | |
| 35 | 35 | else => { |
std/math/log10.zig+4-4| ... | ... | @@ -14,7 +14,7 @@ pub fn log10(x: var) -> @typeOf(x) { |
| 14 | 14 | const T = @typeOf(x); |
| 15 | 15 | switch (@typeId(T)) { |
| 16 | 16 | TypeId.FloatLiteral => { |
| 17 | return @typeOf(1.0)(log10_64(x)) | |
| 17 | return @typeOf(1.0)(log10_64(x)); | |
| 18 | 18 | }, |
| 19 | 19 | TypeId.Float => { |
| 20 | 20 | return switch (T) { |
| ... | ... | @@ -90,7 +90,7 @@ pub fn log10_32(x_: f32) -> f32 { |
| 90 | 90 | const lo = f - hi - hfsq + s * (hfsq + R); |
| 91 | 91 | const dk = f32(k); |
| 92 | 92 | |
| 93 | dk * log10_2lo + (lo + hi) * ivln10lo + lo * ivln10hi + hi * ivln10hi + dk * log10_2hi | |
| 93 | return dk * log10_2lo + (lo + hi) * ivln10lo + lo * ivln10hi + hi * ivln10hi + dk * log10_2hi; | |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | pub fn log10_64(x_: f64) -> f64 { |
| ... | ... | @@ -124,7 +124,7 @@ pub fn log10_64(x_: f64) -> f64 { |
| 124 | 124 | // subnormal, scale x |
| 125 | 125 | k -= 54; |
| 126 | 126 | x *= 0x1.0p54; |
| 127 | hx = u32(@bitCast(u64, x) >> 32) | |
| 127 | hx = u32(@bitCast(u64, x) >> 32); | |
| 128 | 128 | } |
| 129 | 129 | else if (hx >= 0x7FF00000) { |
| 130 | 130 | return x; |
| ... | ... | @@ -167,7 +167,7 @@ pub fn log10_64(x_: f64) -> f64 { |
| 167 | 167 | val_lo += (y - ww) + val_hi; |
| 168 | 168 | val_hi = ww; |
| 169 | 169 | |
| 170 | val_lo + val_hi | |
| 170 | return val_lo + val_hi; | |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | test "math.log10" { |
std/math/log1p.zig+4-4| ... | ... | @@ -11,11 +11,11 @@ const assert = @import("../debug.zig").assert; |
| 11 | 11 | |
| 12 | 12 | pub fn log1p(x: var) -> @typeOf(x) { |
| 13 | 13 | const T = @typeOf(x); |
| 14 | switch (T) { | |
| 14 | return switch (T) { | |
| 15 | 15 | f32 => @inlineCall(log1p_32, x), |
| 16 | 16 | f64 => @inlineCall(log1p_64, x), |
| 17 | 17 | else => @compileError("log1p not implemented for " ++ @typeName(T)), |
| 18 | } | |
| 18 | }; | |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | fn log1p_32(x: f32) -> f32 { |
| ... | ... | @@ -91,7 +91,7 @@ fn log1p_32(x: f32) -> f32 { |
| 91 | 91 | const hfsq = 0.5 * f * f; |
| 92 | 92 | const dk = f32(k); |
| 93 | 93 | |
| 94 | s * (hfsq + R) + (dk * ln2_lo + c) - hfsq + f + dk * ln2_hi | |
| 94 | return s * (hfsq + R) + (dk * ln2_lo + c) - hfsq + f + dk * ln2_hi; | |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | fn log1p_64(x: f64) -> f64 { |
| ... | ... | @@ -172,7 +172,7 @@ fn log1p_64(x: f64) -> f64 { |
| 172 | 172 | const R = t2 + t1; |
| 173 | 173 | const dk = f64(k); |
| 174 | 174 | |
| 175 | s * (hfsq + R) + (dk * ln2_lo + c) - hfsq + f + dk * ln2_hi | |
| 175 | return s * (hfsq + R) + (dk * ln2_lo + c) - hfsq + f + dk * ln2_hi; | |
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | test "math.log1p" { |
std/math/log2.zig+4-4| ... | ... | @@ -14,7 +14,7 @@ pub fn log2(x: var) -> @typeOf(x) { |
| 14 | 14 | const T = @typeOf(x); |
| 15 | 15 | switch (@typeId(T)) { |
| 16 | 16 | TypeId.FloatLiteral => { |
| 17 | return @typeOf(1.0)(log2_64(x)) | |
| 17 | return @typeOf(1.0)(log2_64(x)); | |
| 18 | 18 | }, |
| 19 | 19 | TypeId.Float => { |
| 20 | 20 | return switch (T) { |
| ... | ... | @@ -26,7 +26,7 @@ pub fn log2(x: var) -> @typeOf(x) { |
| 26 | 26 | TypeId.IntLiteral => comptime { |
| 27 | 27 | var result = 0; |
| 28 | 28 | var x_shifted = x; |
| 29 | while ({x_shifted >>= 1; x_shifted != 0}) : (result += 1) {} | |
| 29 | while (b: {x_shifted >>= 1; break :b x_shifted != 0;}) : (result += 1) {} | |
| 30 | 30 | return result; |
| 31 | 31 | }, |
| 32 | 32 | TypeId.Int => { |
| ... | ... | @@ -94,7 +94,7 @@ pub fn log2_32(x_: f32) -> f32 { |
| 94 | 94 | u &= 0xFFFFF000; |
| 95 | 95 | hi = @bitCast(f32, u); |
| 96 | 96 | const lo = f - hi - hfsq + s * (hfsq + R); |
| 97 | (lo + hi) * ivln2lo + lo * ivln2hi + hi * ivln2hi + f32(k) | |
| 97 | return (lo + hi) * ivln2lo + lo * ivln2hi + hi * ivln2hi + f32(k); | |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | pub fn log2_64(x_: f64) -> f64 { |
| ... | ... | @@ -165,7 +165,7 @@ pub fn log2_64(x_: f64) -> f64 { |
| 165 | 165 | val_lo += (y - ww) + val_hi; |
| 166 | 166 | val_hi = ww; |
| 167 | 167 | |
| 168 | val_lo + val_hi | |
| 168 | return val_lo + val_hi; | |
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | test "math.log2" { |
std/math/modf.zig+6-6| ... | ... | @@ -7,21 +7,21 @@ const math = @import("index.zig"); |
| 7 | 7 | const assert = @import("../debug.zig").assert; |
| 8 | 8 | |
| 9 | 9 | fn modf_result(comptime T: type) -> type { |
| 10 | struct { | |
| 10 | return struct { | |
| 11 | 11 | fpart: T, |
| 12 | 12 | ipart: T, |
| 13 | } | |
| 13 | }; | |
| 14 | 14 | } |
| 15 | 15 | pub const modf32_result = modf_result(f32); |
| 16 | 16 | pub const modf64_result = modf_result(f64); |
| 17 | 17 | |
| 18 | 18 | pub fn modf(x: var) -> modf_result(@typeOf(x)) { |
| 19 | 19 | const T = @typeOf(x); |
| 20 | switch (T) { | |
| 20 | return switch (T) { | |
| 21 | 21 | f32 => @inlineCall(modf32, x), |
| 22 | 22 | f64 => @inlineCall(modf64, x), |
| 23 | 23 | else => @compileError("modf not implemented for " ++ @typeName(T)), |
| 24 | } | |
| 24 | }; | |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | fn modf32(x: f32) -> modf32_result { |
| ... | ... | @@ -66,7 +66,7 @@ fn modf32(x: f32) -> modf32_result { |
| 66 | 66 | const uf = @bitCast(f32, u & ~mask); |
| 67 | 67 | result.ipart = uf; |
| 68 | 68 | result.fpart = x - uf; |
| 69 | result | |
| 69 | return result; | |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | fn modf64(x: f64) -> modf64_result { |
| ... | ... | @@ -110,7 +110,7 @@ fn modf64(x: f64) -> modf64_result { |
| 110 | 110 | const uf = @bitCast(f64, u & ~mask); |
| 111 | 111 | result.ipart = uf; |
| 112 | 112 | result.fpart = x - uf; |
| 113 | result | |
| 113 | return result; | |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | test "math.modf" { |
std/math/nan.zig+4-4| ... | ... | @@ -1,19 +1,19 @@ |
| 1 | 1 | const math = @import("index.zig"); |
| 2 | 2 | |
| 3 | 3 | pub fn nan(comptime T: type) -> T { |
| 4 | switch (T) { | |
| 4 | return switch (T) { | |
| 5 | 5 | f32 => @bitCast(f32, math.nan_u32), |
| 6 | 6 | f64 => @bitCast(f64, math.nan_u64), |
| 7 | 7 | else => @compileError("nan not implemented for " ++ @typeName(T)), |
| 8 | } | |
| 8 | }; | |
| 9 | 9 | } |
| 10 | 10 | |
| 11 | 11 | // Note: A signalling nan is identical to a standard right now by may have a different bit |
| 12 | 12 | // representation in the future when required. |
| 13 | 13 | pub fn snan(comptime T: type) -> T { |
| 14 | switch (T) { | |
| 14 | return switch (T) { | |
| 15 | 15 | f32 => @bitCast(f32, math.nan_u32), |
| 16 | 16 | f64 => @bitCast(f64, math.nan_u64), |
| 17 | 17 | else => @compileError("snan not implemented for " ++ @typeName(T)), |
| 18 | } | |
| 18 | }; | |
| 19 | 19 | } |
std/math/pow.zig+2-2| ... | ... | @@ -166,12 +166,12 @@ pub fn pow(comptime T: type, x: T, y: T) -> T { |
| 166 | 166 | ae = -ae; |
| 167 | 167 | } |
| 168 | 168 | |
| 169 | math.scalbn(a1, ae) | |
| 169 | return math.scalbn(a1, ae); | |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | fn isOddInteger(x: f64) -> bool { |
| 173 | 173 | const r = math.modf(x); |
| 174 | r.fpart == 0.0 and i64(r.ipart) & 1 == 1 | |
| 174 | return r.fpart == 0.0 and i64(r.ipart) & 1 == 1; | |
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | test "math.pow" { |
std/math/round.zig+6-6| ... | ... | @@ -10,11 +10,11 @@ const math = @import("index.zig"); |
| 10 | 10 | |
| 11 | 11 | pub fn round(x: var) -> @typeOf(x) { |
| 12 | 12 | const T = @typeOf(x); |
| 13 | switch (T) { | |
| 13 | return switch (T) { | |
| 14 | 14 | f32 => @inlineCall(round32, x), |
| 15 | 15 | f64 => @inlineCall(round64, x), |
| 16 | 16 | else => @compileError("round not implemented for " ++ @typeName(T)), |
| 17 | } | |
| 17 | }; | |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | fn round32(x_: f32) -> f32 { |
| ... | ... | @@ -48,9 +48,9 @@ fn round32(x_: f32) -> f32 { |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | if (u >> 31 != 0) { |
| 51 | -y | |
| 51 | return -y; | |
| 52 | 52 | } else { |
| 53 | y | |
| 53 | return y; | |
| 54 | 54 | } |
| 55 | 55 | } |
| 56 | 56 | |
| ... | ... | @@ -85,9 +85,9 @@ fn round64(x_: f64) -> f64 { |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | 87 | if (u >> 63 != 0) { |
| 88 | -y | |
| 88 | return -y; | |
| 89 | 89 | } else { |
| 90 | y | |
| 90 | return y; | |
| 91 | 91 | } |
| 92 | 92 | } |
| 93 | 93 |
std/math/scalbn.zig+4-4| ... | ... | @@ -3,11 +3,11 @@ const assert = @import("../debug.zig").assert; |
| 3 | 3 | |
| 4 | 4 | pub fn scalbn(x: var, n: i32) -> @typeOf(x) { |
| 5 | 5 | const T = @typeOf(x); |
| 6 | switch (T) { | |
| 6 | return switch (T) { | |
| 7 | 7 | f32 => @inlineCall(scalbn32, x, n), |
| 8 | 8 | f64 => @inlineCall(scalbn64, x, n), |
| 9 | 9 | else => @compileError("scalbn not implemented for " ++ @typeName(T)), |
| 10 | } | |
| 10 | }; | |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | fn scalbn32(x: f32, n_: i32) -> f32 { |
| ... | ... | @@ -37,7 +37,7 @@ fn scalbn32(x: f32, n_: i32) -> f32 { |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | const u = u32(n +% 0x7F) << 23; |
| 40 | y * @bitCast(f32, u) | |
| 40 | return y * @bitCast(f32, u); | |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | fn scalbn64(x: f64, n_: i32) -> f64 { |
| ... | ... | @@ -67,7 +67,7 @@ fn scalbn64(x: f64, n_: i32) -> f64 { |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | const u = u64(n +% 0x3FF) << 52; |
| 70 | y * @bitCast(f64, u) | |
| 70 | return y * @bitCast(f64, u); | |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | test "math.scalbn" { |
std/math/signbit.zig+4-4| ... | ... | @@ -3,21 +3,21 @@ const assert = @import("../debug.zig").assert; |
| 3 | 3 | |
| 4 | 4 | pub fn signbit(x: var) -> bool { |
| 5 | 5 | const T = @typeOf(x); |
| 6 | switch (T) { | |
| 6 | return switch (T) { | |
| 7 | 7 | f32 => @inlineCall(signbit32, x), |
| 8 | 8 | f64 => @inlineCall(signbit64, x), |
| 9 | 9 | else => @compileError("signbit not implemented for " ++ @typeName(T)), |
| 10 | } | |
| 10 | }; | |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | fn signbit32(x: f32) -> bool { |
| 14 | 14 | const bits = @bitCast(u32, x); |
| 15 | bits >> 31 != 0 | |
| 15 | return bits >> 31 != 0; | |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | fn signbit64(x: f64) -> bool { |
| 19 | 19 | const bits = @bitCast(u64, x); |
| 20 | bits >> 63 != 0 | |
| 20 | return bits >> 63 != 0; | |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | test "math.signbit" { |
std/math/sin.zig+13-13| ... | ... | @@ -10,11 +10,11 @@ const assert = @import("../debug.zig").assert; |
| 10 | 10 | |
| 11 | 11 | pub fn sin(x: var) -> @typeOf(x) { |
| 12 | 12 | const T = @typeOf(x); |
| 13 | switch (T) { | |
| 13 | return switch (T) { | |
| 14 | 14 | f32 => @inlineCall(sin32, x), |
| 15 | 15 | f64 => @inlineCall(sin64, x), |
| 16 | 16 | else => @compileError("sin not implemented for " ++ @typeName(T)), |
| 17 | } | |
| 17 | }; | |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | // sin polynomial coefficients |
| ... | ... | @@ -75,18 +75,18 @@ fn sin32(x_: f32) -> f32 { |
| 75 | 75 | const z = ((x - y * pi4a) - y * pi4b) - y * pi4c; |
| 76 | 76 | const w = z * z; |
| 77 | 77 | |
| 78 | const r = { | |
| 78 | const r = r: { | |
| 79 | 79 | if (j == 1 or j == 2) { |
| 80 | 1.0 - 0.5 * w + w * w * (C5 + w * (C4 + w * (C3 + w * (C2 + w * (C1 + w * C0))))) | |
| 80 | break :r 1.0 - 0.5 * w + w * w * (C5 + w * (C4 + w * (C3 + w * (C2 + w * (C1 + w * C0))))); | |
| 81 | 81 | } else { |
| 82 | z + z * w * (S5 + w * (S4 + w * (S3 + w * (S2 + w * (S1 + w * S0))))) | |
| 82 | break :r z + z * w * (S5 + w * (S4 + w * (S3 + w * (S2 + w * (S1 + w * S0))))); | |
| 83 | 83 | } |
| 84 | 84 | }; |
| 85 | 85 | |
| 86 | 86 | if (sign) { |
| 87 | -r | |
| 87 | return -r; | |
| 88 | 88 | } else { |
| 89 | r | |
| 89 | return r; | |
| 90 | 90 | } |
| 91 | 91 | } |
| 92 | 92 | |
| ... | ... | @@ -127,25 +127,25 @@ fn sin64(x_: f64) -> f64 { |
| 127 | 127 | const z = ((x - y * pi4a) - y * pi4b) - y * pi4c; |
| 128 | 128 | const w = z * z; |
| 129 | 129 | |
| 130 | const r = { | |
| 130 | const r = r: { | |
| 131 | 131 | if (j == 1 or j == 2) { |
| 132 | 1.0 - 0.5 * w + w * w * (C5 + w * (C4 + w * (C3 + w * (C2 + w * (C1 + w * C0))))) | |
| 132 | break :r 1.0 - 0.5 * w + w * w * (C5 + w * (C4 + w * (C3 + w * (C2 + w * (C1 + w * C0))))); | |
| 133 | 133 | } else { |
| 134 | z + z * w * (S5 + w * (S4 + w * (S3 + w * (S2 + w * (S1 + w * S0))))) | |
| 134 | break :r z + z * w * (S5 + w * (S4 + w * (S3 + w * (S2 + w * (S1 + w * S0))))); | |
| 135 | 135 | } |
| 136 | 136 | }; |
| 137 | 137 | |
| 138 | 138 | if (sign) { |
| 139 | -r | |
| 139 | return -r; | |
| 140 | 140 | } else { |
| 141 | r | |
| 141 | return r; | |
| 142 | 142 | } |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | test "math.sin" { |
| 146 | 146 | assert(sin(f32(0.0)) == sin32(0.0)); |
| 147 | 147 | assert(sin(f64(0.0)) == sin64(0.0)); |
| 148 | assert(comptime {math.sin(f64(2))} == math.sin(f64(2))); | |
| 148 | assert(comptime (math.sin(f64(2))) == math.sin(f64(2))); | |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | test "math.sin32" { |
std/math/sinh.zig+4-4| ... | ... | @@ -11,11 +11,11 @@ const expo2 = @import("expo2.zig").expo2; |
| 11 | 11 | |
| 12 | 12 | pub fn sinh(x: var) -> @typeOf(x) { |
| 13 | 13 | const T = @typeOf(x); |
| 14 | switch (T) { | |
| 14 | return switch (T) { | |
| 15 | 15 | f32 => @inlineCall(sinh32, x), |
| 16 | 16 | f64 => @inlineCall(sinh64, x), |
| 17 | 17 | else => @compileError("sinh not implemented for " ++ @typeName(T)), |
| 18 | } | |
| 18 | }; | |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | // sinh(x) = (exp(x) - 1 / exp(x)) / 2 |
| ... | ... | @@ -49,7 +49,7 @@ fn sinh32(x: f32) -> f32 { |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | // |x| > log(FLT_MAX) or nan |
| 52 | 2 * h * expo2(ax) | |
| 52 | return 2 * h * expo2(ax); | |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | fn sinh64(x: f64) -> f64 { |
| ... | ... | @@ -83,7 +83,7 @@ fn sinh64(x: f64) -> f64 { |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | // |x| > log(DBL_MAX) or nan |
| 86 | 2 * h * expo2(ax) | |
| 86 | return 2 * h * expo2(ax); | |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | test "math.sinh" { |
std/math/sqrt.zig+5-5| ... | ... | @@ -14,7 +14,7 @@ pub fn sqrt(x: var) -> (if (@typeId(@typeOf(x)) == TypeId.Int) @IntType(false, @ |
| 14 | 14 | const T = @typeOf(x); |
| 15 | 15 | switch (@typeId(T)) { |
| 16 | 16 | TypeId.FloatLiteral => { |
| 17 | return T(sqrt64(x)) | |
| 17 | return T(sqrt64(x)); | |
| 18 | 18 | }, |
| 19 | 19 | TypeId.Float => { |
| 20 | 20 | return switch (T) { |
| ... | ... | @@ -64,7 +64,7 @@ fn sqrt32(x: f32) -> f32 { |
| 64 | 64 | // subnormal |
| 65 | 65 | var i: i32 = 0; |
| 66 | 66 | while (ix & 0x00800000 == 0) : (i += 1) { |
| 67 | ix <<= 1 | |
| 67 | ix <<= 1; | |
| 68 | 68 | } |
| 69 | 69 | m -= i - 1; |
| 70 | 70 | } |
| ... | ... | @@ -112,7 +112,7 @@ fn sqrt32(x: f32) -> f32 { |
| 112 | 112 | |
| 113 | 113 | ix = (q >> 1) + 0x3f000000; |
| 114 | 114 | ix += m << 23; |
| 115 | @bitCast(f32, ix) | |
| 115 | return @bitCast(f32, ix); | |
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | // NOTE: The original code is full of implicit signed -> unsigned assumptions and u32 wraparound |
| ... | ... | @@ -153,7 +153,7 @@ fn sqrt64(x: f64) -> f64 { |
| 153 | 153 | // subnormal |
| 154 | 154 | var i: u32 = 0; |
| 155 | 155 | while (ix0 & 0x00100000 == 0) : (i += 1) { |
| 156 | ix0 <<= 1 | |
| 156 | ix0 <<= 1; | |
| 157 | 157 | } |
| 158 | 158 | m -= i32(i) - 1; |
| 159 | 159 | ix0 |= ix1 >> u5(32 - i); |
| ... | ... | @@ -245,7 +245,7 @@ fn sqrt64(x: f64) -> f64 { |
| 245 | 245 | iix0 = iix0 +% (m << 20); |
| 246 | 246 | |
| 247 | 247 | const uz = (u64(iix0) << 32) | ix1; |
| 248 | @bitCast(f64, uz) | |
| 248 | return @bitCast(f64, uz); | |
| 249 | 249 | } |
| 250 | 250 | |
| 251 | 251 | test "math.sqrt" { |
std/math/tan.zig+10-10| ... | ... | @@ -10,11 +10,11 @@ const assert = @import("../debug.zig").assert; |
| 10 | 10 | |
| 11 | 11 | pub fn tan(x: var) -> @typeOf(x) { |
| 12 | 12 | const T = @typeOf(x); |
| 13 | switch (T) { | |
| 13 | return switch (T) { | |
| 14 | 14 | f32 => @inlineCall(tan32, x), |
| 15 | 15 | f64 => @inlineCall(tan64, x), |
| 16 | 16 | else => @compileError("tan not implemented for " ++ @typeName(T)), |
| 17 | } | |
| 17 | }; | |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | const Tp0 = -1.30936939181383777646E4; |
| ... | ... | @@ -62,11 +62,11 @@ fn tan32(x_: f32) -> f32 { |
| 62 | 62 | const z = ((x - y * pi4a) - y * pi4b) - y * pi4c; |
| 63 | 63 | const w = z * z; |
| 64 | 64 | |
| 65 | var r = { | |
| 65 | var r = r: { | |
| 66 | 66 | if (w > 1e-14) { |
| 67 | z + z * (w * ((Tp0 * w + Tp1) * w + Tp2) / ((((w + Tq1) * w + Tq2) * w + Tq3) * w + Tq4)) | |
| 67 | break :r z + z * (w * ((Tp0 * w + Tp1) * w + Tp2) / ((((w + Tq1) * w + Tq2) * w + Tq3) * w + Tq4)); | |
| 68 | 68 | } else { |
| 69 | z | |
| 69 | break :r z; | |
| 70 | 70 | } |
| 71 | 71 | }; |
| 72 | 72 | |
| ... | ... | @@ -77,7 +77,7 @@ fn tan32(x_: f32) -> f32 { |
| 77 | 77 | r = -r; |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | r | |
| 80 | return r; | |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | fn tan64(x_: f64) -> f64 { |
| ... | ... | @@ -111,11 +111,11 @@ fn tan64(x_: f64) -> f64 { |
| 111 | 111 | const z = ((x - y * pi4a) - y * pi4b) - y * pi4c; |
| 112 | 112 | const w = z * z; |
| 113 | 113 | |
| 114 | var r = { | |
| 114 | var r = r: { | |
| 115 | 115 | if (w > 1e-14) { |
| 116 | z + z * (w * ((Tp0 * w + Tp1) * w + Tp2) / ((((w + Tq1) * w + Tq2) * w + Tq3) * w + Tq4)) | |
| 116 | break :r z + z * (w * ((Tp0 * w + Tp1) * w + Tp2) / ((((w + Tq1) * w + Tq2) * w + Tq3) * w + Tq4)); | |
| 117 | 117 | } else { |
| 118 | z | |
| 118 | break :r z; | |
| 119 | 119 | } |
| 120 | 120 | }; |
| 121 | 121 | |
| ... | ... | @@ -126,7 +126,7 @@ fn tan64(x_: f64) -> f64 { |
| 126 | 126 | r = -r; |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | r | |
| 129 | return r; | |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | test "math.tan" { |
std/math/tanh.zig+6-6| ... | ... | @@ -11,11 +11,11 @@ const expo2 = @import("expo2.zig").expo2; |
| 11 | 11 | |
| 12 | 12 | pub fn tanh(x: var) -> @typeOf(x) { |
| 13 | 13 | const T = @typeOf(x); |
| 14 | switch (T) { | |
| 14 | return switch (T) { | |
| 15 | 15 | f32 => @inlineCall(tanh32, x), |
| 16 | 16 | f64 => @inlineCall(tanh64, x), |
| 17 | 17 | else => @compileError("tanh not implemented for " ++ @typeName(T)), |
| 18 | } | |
| 18 | }; | |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | // tanh(x) = (exp(x) - exp(-x)) / (exp(x) + exp(-x)) |
| ... | ... | @@ -59,9 +59,9 @@ fn tanh32(x: f32) -> f32 { |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | if (u >> 31 != 0) { |
| 62 | -t | |
| 62 | return -t; | |
| 63 | 63 | } else { |
| 64 | t | |
| 64 | return t; | |
| 65 | 65 | } |
| 66 | 66 | } |
| 67 | 67 | |
| ... | ... | @@ -104,9 +104,9 @@ fn tanh64(x: f64) -> f64 { |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | if (u >> 63 != 0) { |
| 107 | -t | |
| 107 | return -t; | |
| 108 | 108 | } else { |
| 109 | t | |
| 109 | return t; | |
| 110 | 110 | } |
| 111 | 111 | } |
| 112 | 112 |
std/math/trunc.zig+6-6| ... | ... | @@ -9,11 +9,11 @@ const assert = @import("../debug.zig").assert; |
| 9 | 9 | |
| 10 | 10 | pub fn trunc(x: var) -> @typeOf(x) { |
| 11 | 11 | const T = @typeOf(x); |
| 12 | switch (T) { | |
| 12 | return switch (T) { | |
| 13 | 13 | f32 => @inlineCall(trunc32, x), |
| 14 | 14 | f64 => @inlineCall(trunc64, x), |
| 15 | 15 | else => @compileError("trunc not implemented for " ++ @typeName(T)), |
| 16 | } | |
| 16 | }; | |
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | fn trunc32(x: f32) -> f32 { |
| ... | ... | @@ -30,10 +30,10 @@ fn trunc32(x: f32) -> f32 { |
| 30 | 30 | |
| 31 | 31 | m = u32(@maxValue(u32)) >> u5(e); |
| 32 | 32 | if (u & m == 0) { |
| 33 | x | |
| 33 | return x; | |
| 34 | 34 | } else { |
| 35 | 35 | math.forceEval(x + 0x1p120); |
| 36 | @bitCast(f32, u & ~m) | |
| 36 | return @bitCast(f32, u & ~m); | |
| 37 | 37 | } |
| 38 | 38 | } |
| 39 | 39 | |
| ... | ... | @@ -51,10 +51,10 @@ fn trunc64(x: f64) -> f64 { |
| 51 | 51 | |
| 52 | 52 | m = u64(@maxValue(u64)) >> u6(e); |
| 53 | 53 | if (u & m == 0) { |
| 54 | x | |
| 54 | return x; | |
| 55 | 55 | } else { |
| 56 | 56 | math.forceEval(x + 0x1p120); |
| 57 | @bitCast(f64, u & ~m) | |
| 57 | return @bitCast(f64, u & ~m); | |
| 58 | 58 | } |
| 59 | 59 | } |
| 60 | 60 |
std/mem.zig+4-4| ... | ... | @@ -354,11 +354,11 @@ pub fn eql_slice_u8(a: []const u8, b: []const u8) -> bool { |
| 354 | 354 | /// split(" abc def ghi ", " ") |
| 355 | 355 | /// Will return slices for "abc", "def", "ghi", null, in that order. |
| 356 | 356 | pub fn split(buffer: []const u8, split_bytes: []const u8) -> SplitIterator { |
| 357 | SplitIterator { | |
| 357 | return SplitIterator { | |
| 358 | 358 | .index = 0, |
| 359 | 359 | .buffer = buffer, |
| 360 | 360 | .split_bytes = split_bytes, |
| 361 | } | |
| 361 | }; | |
| 362 | 362 | } |
| 363 | 363 | |
| 364 | 364 | test "mem.split" { |
| ... | ... | @@ -552,7 +552,7 @@ test "std.mem.reverse" { |
| 552 | 552 | var arr = []i32{ 5, 3, 1, 2, 4 }; |
| 553 | 553 | reverse(i32, arr[0..]); |
| 554 | 554 | |
| 555 | assert(eql(i32, arr, []i32{ 4, 2, 1, 3, 5 })) | |
| 555 | assert(eql(i32, arr, []i32{ 4, 2, 1, 3, 5 })); | |
| 556 | 556 | } |
| 557 | 557 | |
| 558 | 558 | /// In-place rotation of the values in an array ([0 1 2 3] becomes [1 2 3 0] if we rotate by 1) |
| ... | ... | @@ -567,5 +567,5 @@ test "std.mem.rotate" { |
| 567 | 567 | var arr = []i32{ 5, 3, 1, 2, 4 }; |
| 568 | 568 | rotate(i32, arr[0..], 2); |
| 569 | 569 | |
| 570 | assert(eql(i32, arr, []i32{ 1, 2, 4, 5, 3 })) | |
| 570 | assert(eql(i32, arr, []i32{ 1, 2, 4, 5, 3 })); | |
| 571 | 571 | } |
std/net.zig+11-11| ... | ... | @@ -72,7 +72,7 @@ pub fn lookup(hostname: []const u8, out_addrs: []Address) -> %[]Address { |
| 72 | 72 | //		if (family != AF_INET) |
| 73 | 73 | //			buf[cnt++] = (struct address){ .family = AF_INET6, .addr = { [15] = 1 } }; |
| 74 | 74 | // |
| 75 | unreachable // TODO | |
| 75 | unreachable; // TODO | |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | // TODO |
| ... | ... | @@ -84,7 +84,7 @@ pub fn lookup(hostname: []const u8, out_addrs: []Address) -> %[]Address { |
| 84 | 84 | // else => {}, |
| 85 | 85 | //}; |
| 86 | 86 | |
| 87 | unreachable // TODO | |
| 87 | unreachable; // TODO | |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | pub fn connectAddr(addr: &Address, port: u16) -> %Connection { |
| ... | ... | @@ -96,23 +96,23 @@ pub fn connectAddr(addr: &Address, port: u16) -> %Connection { |
| 96 | 96 | } |
| 97 | 97 | const socket_fd = i32(socket_ret); |
| 98 | 98 | |
| 99 | const connect_ret = if (addr.family == linux.AF_INET) { | |
| 99 | const connect_ret = if (addr.family == linux.AF_INET) x: { | |
| 100 | 100 | var os_addr: linux.sockaddr_in = undefined; |
| 101 | 101 | os_addr.family = addr.family; |
| 102 | 102 | os_addr.port = endian.swapIfLe(u16, port); |
| 103 | 103 | @memcpy((&u8)(&os_addr.addr), &addr.addr[0], 4); |
| 104 | 104 | @memset(&os_addr.zero[0], 0, @sizeOf(@typeOf(os_addr.zero))); |
| 105 | linux.connect(socket_fd, (&linux.sockaddr)(&os_addr), @sizeOf(linux.sockaddr_in)) | |
| 106 | } else if (addr.family == linux.AF_INET6) { | |
| 105 | break :x linux.connect(socket_fd, (&linux.sockaddr)(&os_addr), @sizeOf(linux.sockaddr_in)); | |
| 106 | } else if (addr.family == linux.AF_INET6) x: { | |
| 107 | 107 | var os_addr: linux.sockaddr_in6 = undefined; |
| 108 | 108 | os_addr.family = addr.family; |
| 109 | 109 | os_addr.port = endian.swapIfLe(u16, port); |
| 110 | 110 | os_addr.flowinfo = 0; |
| 111 | 111 | os_addr.scope_id = addr.scope_id; |
| 112 | 112 | @memcpy(&os_addr.addr[0], &addr.addr[0], 16); |
| 113 | linux.connect(socket_fd, (&linux.sockaddr)(&os_addr), @sizeOf(linux.sockaddr_in6)) | |
| 113 | break :x linux.connect(socket_fd, (&linux.sockaddr)(&os_addr), @sizeOf(linux.sockaddr_in6)); | |
| 114 | 114 | } else { |
| 115 | unreachable | |
| 115 | unreachable; | |
| 116 | 116 | }; |
| 117 | 117 | const connect_err = linux.getErrno(connect_ret); |
| 118 | 118 | if (connect_err > 0) { |
| ... | ... | @@ -165,13 +165,13 @@ pub fn parseIpLiteral(buf: []const u8) -> %Address { |
| 165 | 165 | fn hexDigit(c: u8) -> u8 { |
| 166 | 166 | // TODO use switch with range |
| 167 | 167 | if ('0' <= c and c <= '9') { |
| 168 | c - '0' | |
| 168 | return c - '0'; | |
| 169 | 169 | } else if ('A' <= c and c <= 'Z') { |
| 170 | c - 'A' + 10 | |
| 170 | return c - 'A' + 10; | |
| 171 | 171 | } else if ('a' <= c and c <= 'z') { |
| 172 | c - 'a' + 10 | |
| 172 | return c - 'a' + 10; | |
| 173 | 173 | } else { |
| 174 | @maxValue(u8) | |
| 174 | return @maxValue(u8); | |
| 175 | 175 | } |
| 176 | 176 | } |
| 177 | 177 |
std/os/child_process.zig+35-35| ... | ... | @@ -115,7 +115,7 @@ pub const ChildProcess = struct { |
| 115 | 115 | return self.spawnWindows(); |
| 116 | 116 | } else { |
| 117 | 117 | return self.spawnPosix(); |
| 118 | }; | |
| 118 | } | |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | 121 | pub fn spawnAndWait(self: &ChildProcess) -> %Term { |
| ... | ... | @@ -249,12 +249,12 @@ pub const ChildProcess = struct { |
| 249 | 249 | fn waitUnwrappedWindows(self: &ChildProcess) -> %void { |
| 250 | 250 | const result = os.windowsWaitSingle(self.handle, windows.INFINITE); |
| 251 | 251 | |
| 252 | self.term = (%Term)({ | |
| 252 | self.term = (%Term)(x: { | |
| 253 | 253 | var exit_code: windows.DWORD = undefined; |
| 254 | 254 | if (windows.GetExitCodeProcess(self.handle, &exit_code) == 0) { |
| 255 | Term { .Unknown = 0 } | |
| 255 | break :x Term { .Unknown = 0 }; | |
| 256 | 256 | } else { |
| 257 | Term { .Exited = @bitCast(i32, exit_code)} | |
| 257 | break :x Term { .Exited = @bitCast(i32, exit_code)}; | |
| 258 | 258 | } |
| 259 | 259 | }); |
| 260 | 260 | |
| ... | ... | @@ -300,7 +300,7 @@ pub const ChildProcess = struct { |
| 300 | 300 | defer { |
| 301 | 301 | os.close(self.err_pipe[0]); |
| 302 | 302 | os.close(self.err_pipe[1]); |
| 303 | }; | |
| 303 | } | |
| 304 | 304 | |
| 305 | 305 | // Write @maxValue(ErrInt) to the write end of the err_pipe. This is after |
| 306 | 306 | // waitpid, so this write is guaranteed to be after the child |
| ... | ... | @@ -319,15 +319,15 @@ pub const ChildProcess = struct { |
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | fn statusToTerm(status: i32) -> Term { |
| 322 | return if (posix.WIFEXITED(status)) { | |
| 322 | return if (posix.WIFEXITED(status)) | |
| 323 | 323 | Term { .Exited = posix.WEXITSTATUS(status) } |
| 324 | } else if (posix.WIFSIGNALED(status)) { | |
| 324 | else if (posix.WIFSIGNALED(status)) | |
| 325 | 325 | Term { .Signal = posix.WTERMSIG(status) } |
| 326 | } else if (posix.WIFSTOPPED(status)) { | |
| 326 | else if (posix.WIFSTOPPED(status)) | |
| 327 | 327 | Term { .Stopped = posix.WSTOPSIG(status) } |
| 328 | } else { | |
| 328 | else | |
| 329 | 329 | Term { .Unknown = status } |
| 330 | }; | |
| 330 | ; | |
| 331 | 331 | } |
| 332 | 332 | |
| 333 | 333 | fn spawnPosix(self: &ChildProcess) -> %void { |
| ... | ... | @@ -344,22 +344,22 @@ pub const ChildProcess = struct { |
| 344 | 344 | %defer if (self.stderr_behavior == StdIo.Pipe) { destroyPipe(stderr_pipe); }; |
| 345 | 345 | |
| 346 | 346 | const any_ignore = (self.stdin_behavior == StdIo.Ignore or self.stdout_behavior == StdIo.Ignore or self.stderr_behavior == StdIo.Ignore); |
| 347 | const dev_null_fd = if (any_ignore) { | |
| 347 | const dev_null_fd = if (any_ignore) | |
| 348 | 348 | %return os.posixOpen("/dev/null", posix.O_RDWR, 0, null) |
| 349 | } else { | |
| 349 | else | |
| 350 | 350 | undefined |
| 351 | }; | |
| 352 | defer { if (any_ignore) os.close(dev_null_fd); }; | |
| 351 | ; | |
| 352 | defer { if (any_ignore) os.close(dev_null_fd); } | |
| 353 | 353 | |
| 354 | 354 | var env_map_owned: BufMap = undefined; |
| 355 | 355 | var we_own_env_map: bool = undefined; |
| 356 | const env_map = if (self.env_map) |env_map| { | |
| 356 | const env_map = if (self.env_map) |env_map| x: { | |
| 357 | 357 | we_own_env_map = false; |
| 358 | env_map | |
| 359 | } else { | |
| 358 | break :x env_map; | |
| 359 | } else x: { | |
| 360 | 360 | we_own_env_map = true; |
| 361 | 361 | env_map_owned = %return os.getEnvMap(self.allocator); |
| 362 | &env_map_owned | |
| 362 | break :x &env_map_owned; | |
| 363 | 363 | }; |
| 364 | 364 | defer { if (we_own_env_map) env_map_owned.deinit(); } |
| 365 | 365 | |
| ... | ... | @@ -450,13 +450,13 @@ pub const ChildProcess = struct { |
| 450 | 450 | self.stdout_behavior == StdIo.Ignore or |
| 451 | 451 | self.stderr_behavior == StdIo.Ignore); |
| 452 | 452 | |
| 453 | const nul_handle = if (any_ignore) { | |
| 453 | const nul_handle = if (any_ignore) | |
| 454 | 454 | %return os.windowsOpen("NUL", windows.GENERIC_READ, windows.FILE_SHARE_READ, |
| 455 | 455 | windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL, null) |
| 456 | } else { | |
| 456 | else | |
| 457 | 457 | undefined |
| 458 | }; | |
| 459 | defer { if (any_ignore) os.close(nul_handle); }; | |
| 458 | ; | |
| 459 | defer { if (any_ignore) os.close(nul_handle); } | |
| 460 | 460 | if (any_ignore) { |
| 461 | 461 | %return windowsSetHandleInfo(nul_handle, windows.HANDLE_FLAG_INHERIT, 0); |
| 462 | 462 | } |
| ... | ... | @@ -542,30 +542,30 @@ pub const ChildProcess = struct { |
| 542 | 542 | }; |
| 543 | 543 | var piProcInfo: windows.PROCESS_INFORMATION = undefined; |
| 544 | 544 | |
| 545 | const cwd_slice = if (self.cwd) |cwd| { | |
| 545 | const cwd_slice = if (self.cwd) |cwd| | |
| 546 | 546 | %return cstr.addNullByte(self.allocator, cwd) |
| 547 | } else { | |
| 547 | else | |
| 548 | 548 | null |
| 549 | }; | |
| 549 | ; | |
| 550 | 550 | defer if (cwd_slice) |cwd| self.allocator.free(cwd); |
| 551 | 551 | const cwd_ptr = if (cwd_slice) |cwd| cwd.ptr else null; |
| 552 | 552 | |
| 553 | const maybe_envp_buf = if (self.env_map) |env_map| { | |
| 553 | const maybe_envp_buf = if (self.env_map) |env_map| | |
| 554 | 554 | %return os.createWindowsEnvBlock(self.allocator, env_map) |
| 555 | } else { | |
| 555 | else | |
| 556 | 556 | null |
| 557 | }; | |
| 557 | ; | |
| 558 | 558 | defer if (maybe_envp_buf) |envp_buf| self.allocator.free(envp_buf); |
| 559 | 559 | const envp_ptr = if (maybe_envp_buf) |envp_buf| envp_buf.ptr else null; |
| 560 | 560 | |
| 561 | 561 | // the cwd set in ChildProcess is in effect when choosing the executable path |
| 562 | 562 | // to match posix semantics |
| 563 | const app_name = if (self.cwd) |cwd| { | |
| 563 | const app_name = if (self.cwd) |cwd| x: { | |
| 564 | 564 | const resolved = %return os.path.resolve(self.allocator, cwd, self.argv[0]); |
| 565 | 565 | defer self.allocator.free(resolved); |
| 566 | %return cstr.addNullByte(self.allocator, resolved) | |
| 567 | } else { | |
| 568 | %return cstr.addNullByte(self.allocator, self.argv[0]) | |
| 566 | break :x %return cstr.addNullByte(self.allocator, resolved); | |
| 567 | } else x: { | |
| 568 | break :x %return cstr.addNullByte(self.allocator, self.argv[0]); | |
| 569 | 569 | }; |
| 570 | 570 | defer self.allocator.free(app_name); |
| 571 | 571 | |
| ... | ... | @@ -741,7 +741,7 @@ fn makePipe() -> %[2]i32 { |
| 741 | 741 | return switch (err) { |
| 742 | 742 | posix.EMFILE, posix.ENFILE => error.SystemResources, |
| 743 | 743 | else => os.unexpectedErrorPosix(err), |
| 744 | } | |
| 744 | }; | |
| 745 | 745 | } |
| 746 | 746 | return fds; |
| 747 | 747 | } |
| ... | ... | @@ -800,10 +800,10 @@ fn handleTerm(pid: i32, status: i32) { |
| 800 | 800 | } |
| 801 | 801 | } |
| 802 | 802 | |
| 803 | const sigchld_set = { | |
| 803 | const sigchld_set = x: { | |
| 804 | 804 | var signal_set = posix.empty_sigset; |
| 805 | 805 | posix.sigaddset(&signal_set, posix.SIGCHLD); |
| 806 | signal_set | |
| 806 | break :x signal_set; | |
| 807 | 807 | }; |
| 808 | 808 | |
| 809 | 809 | fn block_SIGCHLD() { |
std/os/darwin.zig+38-42| ... | ... | @@ -97,63 +97,63 @@ pub const SIGINFO = 29; /// information request |
| 97 | 97 | pub const SIGUSR1 = 30; /// user defined signal 1 |
| 98 | 98 | pub const SIGUSR2 = 31; /// user defined signal 2 |
| 99 | 99 | |
| 100 | fn wstatus(x: i32) -> i32 { x & 0o177 } | |
| 100 | fn wstatus(x: i32) -> i32 { return x & 0o177; } | |
| 101 | 101 | const wstopped = 0o177; |
| 102 | pub fn WEXITSTATUS(x: i32) -> i32 { x >> 8 } | |
| 103 | pub fn WTERMSIG(x: i32) -> i32 { wstatus(x) } | |
| 104 | pub fn WSTOPSIG(x: i32) -> i32 { x >> 8 } | |
| 105 | pub fn WIFEXITED(x: i32) -> bool { wstatus(x) == 0 } | |
| 106 | pub fn WIFSTOPPED(x: i32) -> bool { wstatus(x) == wstopped and WSTOPSIG(x) != 0x13 } | |
| 107 | pub fn WIFSIGNALED(x: i32) -> bool { wstatus(x) != wstopped and wstatus(x) != 0 } | |
| 102 | pub fn WEXITSTATUS(x: i32) -> i32 { return x >> 8; } | |
| 103 | pub fn WTERMSIG(x: i32) -> i32 { return wstatus(x); } | |
| 104 | pub fn WSTOPSIG(x: i32) -> i32 { return x >> 8; } | |
| 105 | pub fn WIFEXITED(x: i32) -> bool { return wstatus(x) == 0; } | |
| 106 | pub fn WIFSTOPPED(x: i32) -> bool { return wstatus(x) == wstopped and WSTOPSIG(x) != 0x13; } | |
| 107 | pub fn WIFSIGNALED(x: i32) -> bool { return wstatus(x) != wstopped and wstatus(x) != 0; } | |
| 108 | 108 | |
| 109 | 109 | /// Get the errno from a syscall return value, or 0 for no error. |
| 110 | 110 | pub fn getErrno(r: usize) -> usize { |
| 111 | 111 | const signed_r = @bitCast(isize, r); |
| 112 | if (signed_r > -4096 and signed_r < 0) usize(-signed_r) else 0 | |
| 112 | return if (signed_r > -4096 and signed_r < 0) usize(-signed_r) else 0; | |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | pub fn close(fd: i32) -> usize { |
| 116 | errnoWrap(c.close(fd)) | |
| 116 | return errnoWrap(c.close(fd)); | |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | pub fn abort() -> noreturn { |
| 120 | c.abort() | |
| 120 | return c.abort(); | |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | pub fn exit(code: i32) -> noreturn { |
| 124 | c.exit(code) | |
| 124 | return c.exit(code); | |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | pub fn isatty(fd: i32) -> bool { |
| 128 | c.isatty(fd) != 0 | |
| 128 | return c.isatty(fd) != 0; | |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | pub fn fstat(fd: i32, buf: &c.Stat) -> usize { |
| 132 | errnoWrap(c.@"fstat$INODE64"(fd, buf)) | |
| 132 | return errnoWrap(c.@"fstat$INODE64"(fd, buf)); | |
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | pub fn lseek(fd: i32, offset: isize, whence: c_int) -> usize { |
| 136 | errnoWrap(c.lseek(fd, offset, whence)) | |
| 136 | return errnoWrap(c.lseek(fd, offset, whence)); | |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | pub fn open(path: &const u8, flags: u32, mode: usize) -> usize { |
| 140 | errnoWrap(c.open(path, @bitCast(c_int, flags), mode)) | |
| 140 | return errnoWrap(c.open(path, @bitCast(c_int, flags), mode)); | |
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | pub fn raise(sig: i32) -> usize { |
| 144 | errnoWrap(c.raise(sig)) | |
| 144 | return errnoWrap(c.raise(sig)); | |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | pub fn read(fd: i32, buf: &u8, nbyte: usize) -> usize { |
| 148 | errnoWrap(c.read(fd, @ptrCast(&c_void, buf), nbyte)) | |
| 148 | return errnoWrap(c.read(fd, @ptrCast(&c_void, buf), nbyte)); | |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | pub fn stat(noalias path: &const u8, noalias buf: &stat) -> usize { |
| 152 | errnoWrap(c.stat(path, buf)) | |
| 152 | return errnoWrap(c.stat(path, buf)); | |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | pub fn write(fd: i32, buf: &const u8, nbyte: usize) -> usize { |
| 156 | errnoWrap(c.write(fd, @ptrCast(&const c_void, buf), nbyte)) | |
| 156 | return errnoWrap(c.write(fd, @ptrCast(&const c_void, buf), nbyte)); | |
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | pub fn mmap(address: ?&u8, length: usize, prot: usize, flags: usize, fd: i32, |
| ... | ... | @@ -166,79 +166,79 @@ pub fn mmap(address: ?&u8, length: usize, prot: usize, flags: usize, fd: i32, |
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | pub fn munmap(address: &u8, length: usize) -> usize { |
| 169 | errnoWrap(c.munmap(@ptrCast(&c_void, address), length)) | |
| 169 | return errnoWrap(c.munmap(@ptrCast(&c_void, address), length)); | |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | pub fn unlink(path: &const u8) -> usize { |
| 173 | errnoWrap(c.unlink(path)) | |
| 173 | return errnoWrap(c.unlink(path)); | |
| 174 | 174 | } |
| 175 | 175 | |
| 176 | 176 | pub fn getcwd(buf: &u8, size: usize) -> usize { |
| 177 | if (c.getcwd(buf, size) == null) @bitCast(usize, -isize(*c._errno())) else 0 | |
| 177 | return if (c.getcwd(buf, size) == null) @bitCast(usize, -isize(*c._errno())) else 0; | |
| 178 | 178 | } |
| 179 | 179 | |
| 180 | 180 | pub fn waitpid(pid: i32, status: &i32, options: u32) -> usize { |
| 181 | 181 | comptime assert(i32.bit_count == c_int.bit_count); |
| 182 | errnoWrap(c.waitpid(pid, @ptrCast(&c_int, status), @bitCast(c_int, options))) | |
| 182 | return errnoWrap(c.waitpid(pid, @ptrCast(&c_int, status), @bitCast(c_int, options))); | |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | pub fn fork() -> usize { |
| 186 | errnoWrap(c.fork()) | |
| 186 | return errnoWrap(c.fork()); | |
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | pub fn pipe(fds: &[2]i32) -> usize { |
| 190 | 190 | comptime assert(i32.bit_count == c_int.bit_count); |
| 191 | errnoWrap(c.pipe(@ptrCast(&c_int, fds))) | |
| 191 | return errnoWrap(c.pipe(@ptrCast(&c_int, fds))); | |
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | pub fn mkdir(path: &const u8, mode: u32) -> usize { |
| 195 | errnoWrap(c.mkdir(path, mode)) | |
| 195 | return errnoWrap(c.mkdir(path, mode)); | |
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | pub fn symlink(existing: &const u8, new: &const u8) -> usize { |
| 199 | errnoWrap(c.symlink(existing, new)) | |
| 199 | return errnoWrap(c.symlink(existing, new)); | |
| 200 | 200 | } |
| 201 | 201 | |
| 202 | 202 | pub fn rename(old: &const u8, new: &const u8) -> usize { |
| 203 | errnoWrap(c.rename(old, new)) | |
| 203 | return errnoWrap(c.rename(old, new)); | |
| 204 | 204 | } |
| 205 | 205 | |
| 206 | 206 | pub fn chdir(path: &const u8) -> usize { |
| 207 | errnoWrap(c.chdir(path)) | |
| 207 | return errnoWrap(c.chdir(path)); | |
| 208 | 208 | } |
| 209 | 209 | |
| 210 | 210 | pub fn execve(path: &const u8, argv: &const ?&const u8, envp: &const ?&const u8) |
| 211 | 211 | -> usize |
| 212 | 212 | { |
| 213 | errnoWrap(c.execve(path, argv, envp)) | |
| 213 | return errnoWrap(c.execve(path, argv, envp)); | |
| 214 | 214 | } |
| 215 | 215 | |
| 216 | 216 | pub fn dup2(old: i32, new: i32) -> usize { |
| 217 | errnoWrap(c.dup2(old, new)) | |
| 217 | return errnoWrap(c.dup2(old, new)); | |
| 218 | 218 | } |
| 219 | 219 | |
| 220 | 220 | pub fn readlink(noalias path: &const u8, noalias buf_ptr: &u8, buf_len: usize) -> usize { |
| 221 | errnoWrap(c.readlink(path, buf_ptr, buf_len)) | |
| 221 | return errnoWrap(c.readlink(path, buf_ptr, buf_len)); | |
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | pub fn nanosleep(req: &const timespec, rem: ?&timespec) -> usize { |
| 225 | errnoWrap(c.nanosleep(req, rem)) | |
| 225 | return errnoWrap(c.nanosleep(req, rem)); | |
| 226 | 226 | } |
| 227 | 227 | |
| 228 | 228 | pub fn realpath(noalias filename: &const u8, noalias resolved_name: &u8) -> usize { |
| 229 | if (c.realpath(filename, resolved_name) == null) @bitCast(usize, -isize(*c._errno())) else 0 | |
| 229 | return if (c.realpath(filename, resolved_name) == null) @bitCast(usize, -isize(*c._errno())) else 0; | |
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | pub fn setreuid(ruid: u32, euid: u32) -> usize { |
| 233 | errnoWrap(c.setreuid(ruid, euid)) | |
| 233 | return errnoWrap(c.setreuid(ruid, euid)); | |
| 234 | 234 | } |
| 235 | 235 | |
| 236 | 236 | pub fn setregid(rgid: u32, egid: u32) -> usize { |
| 237 | errnoWrap(c.setregid(rgid, egid)) | |
| 237 | return errnoWrap(c.setregid(rgid, egid)); | |
| 238 | 238 | } |
| 239 | 239 | |
| 240 | 240 | pub fn sigprocmask(flags: u32, noalias set: &const sigset_t, noalias oldset: ?&sigset_t) -> usize { |
| 241 | errnoWrap(c.sigprocmask(@bitCast(c_int, flags), set, oldset)) | |
| 241 | return errnoWrap(c.sigprocmask(@bitCast(c_int, flags), set, oldset)); | |
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | pub fn sigaction(sig: u5, noalias act: &const Sigaction, noalias oact: ?&Sigaction) -> usize { |
| ... | ... | @@ -285,9 +285,5 @@ pub fn sigaddset(set: &sigset_t, signo: u5) { |
| 285 | 285 | /// that the kernel represents it to libc. Errno was a mistake, let's make |
| 286 | 286 | /// it go away forever. |
| 287 | 287 | fn errnoWrap(value: isize) -> usize { |
| 288 | @bitCast(usize, if (value == -1) { | |
| 289 | -isize(*c._errno()) | |
| 290 | } else { | |
| 291 | value | |
| 292 | }) | |
| 288 | return @bitCast(usize, if (value == -1) -isize(*c._errno()) else value); | |
| 293 | 289 | } |
std/os/index.zig+9-10| ... | ... | @@ -84,7 +84,7 @@ pub fn getRandomBytes(buf: []u8) -> %void { |
| 84 | 84 | posix.EFAULT => unreachable, |
| 85 | 85 | posix.EINTR => continue, |
| 86 | 86 | else => unexpectedErrorPosix(err), |
| 87 | } | |
| 87 | }; | |
| 88 | 88 | } |
| 89 | 89 | return; |
| 90 | 90 | }, |
| ... | ... | @@ -151,18 +151,17 @@ pub coldcc fn exit(status: i32) -> noreturn { |
| 151 | 151 | } |
| 152 | 152 | switch (builtin.os) { |
| 153 | 153 | Os.linux, Os.darwin, Os.macosx, Os.ios => { |
| 154 | posix.exit(status) | |
| 154 | posix.exit(status); | |
| 155 | 155 | }, |
| 156 | 156 | Os.windows => { |
| 157 | 157 | // Map a possibly negative status code to a non-negative status for the systems default |
| 158 | 158 | // integer width. |
| 159 | const p_status = if (@sizeOf(c_uint) < @sizeOf(u32)) { | |
| 159 | const p_status = if (@sizeOf(c_uint) < @sizeOf(u32)) | |
| 160 | 160 | @truncate(c_uint, @bitCast(u32, status)) |
| 161 | } else { | |
| 162 | c_uint(@bitCast(u32, status)) | |
| 163 | }; | |
| 161 | else | |
| 162 | c_uint(@bitCast(u32, status)); | |
| 164 | 163 | |
| 165 | windows.ExitProcess(p_status) | |
| 164 | windows.ExitProcess(p_status); | |
| 166 | 165 | }, |
| 167 | 166 | else => @compileError("Unsupported OS"), |
| 168 | 167 | } |
| ... | ... | @@ -289,7 +288,7 @@ pub fn posixOpen(file_path: []const u8, flags: u32, perm: usize, allocator: ?&Al |
| 289 | 288 | posix.EPERM => error.AccessDenied, |
| 290 | 289 | posix.EEXIST => error.PathAlreadyExists, |
| 291 | 290 | else => unexpectedErrorPosix(err), |
| 292 | } | |
| 291 | }; | |
| 293 | 292 | } |
| 294 | 293 | return i32(result); |
| 295 | 294 | } |
| ... | ... | @@ -680,7 +679,7 @@ pub fn deleteFileWindows(allocator: &Allocator, file_path: []const u8) -> %void |
| 680 | 679 | windows.ERROR.ACCESS_DENIED => error.AccessDenied, |
| 681 | 680 | windows.ERROR.FILENAME_EXCED_RANGE, windows.ERROR.INVALID_PARAMETER => error.NameTooLong, |
| 682 | 681 | else => unexpectedErrorWindows(err), |
| 683 | } | |
| 682 | }; | |
| 684 | 683 | } |
| 685 | 684 | } |
| 686 | 685 | |
| ... | ... | @@ -1006,7 +1005,7 @@ pub const Dir = struct { |
| 1006 | 1005 | continue; |
| 1007 | 1006 | }, |
| 1008 | 1007 | else => return unexpectedErrorPosix(err), |
| 1009 | }; | |
| 1008 | } | |
| 1010 | 1009 | } |
| 1011 | 1010 | if (result == 0) |
| 1012 | 1011 | return null; |
std/os/linux.zig+68-68| ... | ... | @@ -367,14 +367,14 @@ pub const TFD_CLOEXEC = O_CLOEXEC; |
| 367 | 367 | pub const TFD_TIMER_ABSTIME = 1; |
| 368 | 368 | pub const TFD_TIMER_CANCEL_ON_SET = (1 << 1); |
| 369 | 369 | |
| 370 | fn unsigned(s: i32) -> u32 { @bitCast(u32, s) } | |
| 371 | fn signed(s: u32) -> i32 { @bitCast(i32, s) } | |
| 372 | pub fn WEXITSTATUS(s: i32) -> i32 { signed((unsigned(s) & 0xff00) >> 8) } | |
| 373 | pub fn WTERMSIG(s: i32) -> i32 { signed(unsigned(s) & 0x7f) } | |
| 374 | pub fn WSTOPSIG(s: i32) -> i32 { WEXITSTATUS(s) } | |
| 375 | pub fn WIFEXITED(s: i32) -> bool { WTERMSIG(s) == 0 } | |
| 376 | pub fn WIFSTOPPED(s: i32) -> bool { (u16)(((unsigned(s)&0xffff)*%0x10001)>>8) > 0x7f00 } | |
| 377 | pub fn WIFSIGNALED(s: i32) -> bool { (unsigned(s)&0xffff)-%1 < 0xff } | |
| 370 | fn unsigned(s: i32) -> u32 { return @bitCast(u32, s); } | |
| 371 | fn signed(s: u32) -> i32 { return @bitCast(i32, s); } | |
| 372 | pub fn WEXITSTATUS(s: i32) -> i32 { return signed((unsigned(s) & 0xff00) >> 8); } | |
| 373 | pub fn WTERMSIG(s: i32) -> i32 { return signed(unsigned(s) & 0x7f); } | |
| 374 | pub fn WSTOPSIG(s: i32) -> i32 { return WEXITSTATUS(s); } | |
| 375 | pub fn WIFEXITED(s: i32) -> bool { return WTERMSIG(s) == 0; } | |
| 376 | pub fn WIFSTOPPED(s: i32) -> bool { return (u16)(((unsigned(s)&0xffff)*%0x10001)>>8) > 0x7f00; } | |
| 377 | pub fn WIFSIGNALED(s: i32) -> bool { return (unsigned(s)&0xffff)-%1 < 0xff; } | |
| 378 | 378 | |
| 379 | 379 | |
| 380 | 380 | pub const winsize = extern struct { |
| ... | ... | @@ -387,31 +387,31 @@ pub const winsize = extern struct { |
| 387 | 387 | /// Get the errno from a syscall return value, or 0 for no error. |
| 388 | 388 | pub fn getErrno(r: usize) -> usize { |
| 389 | 389 | const signed_r = @bitCast(isize, r); |
| 390 | if (signed_r > -4096 and signed_r < 0) usize(-signed_r) else 0 | |
| 390 | return if (signed_r > -4096 and signed_r < 0) usize(-signed_r) else 0; | |
| 391 | 391 | } |
| 392 | 392 | |
| 393 | 393 | pub fn dup2(old: i32, new: i32) -> usize { |
| 394 | arch.syscall2(arch.SYS_dup2, usize(old), usize(new)) | |
| 394 | return arch.syscall2(arch.SYS_dup2, usize(old), usize(new)); | |
| 395 | 395 | } |
| 396 | 396 | |
| 397 | 397 | pub fn chdir(path: &const u8) -> usize { |
| 398 | arch.syscall1(arch.SYS_chdir, @ptrToInt(path)) | |
| 398 | return arch.syscall1(arch.SYS_chdir, @ptrToInt(path)); | |
| 399 | 399 | } |
| 400 | 400 | |
| 401 | 401 | pub fn execve(path: &const u8, argv: &const ?&const u8, envp: &const ?&const u8) -> usize { |
| 402 | arch.syscall3(arch.SYS_execve, @ptrToInt(path), @ptrToInt(argv), @ptrToInt(envp)) | |
| 402 | return arch.syscall3(arch.SYS_execve, @ptrToInt(path), @ptrToInt(argv), @ptrToInt(envp)); | |
| 403 | 403 | } |
| 404 | 404 | |
| 405 | 405 | pub fn fork() -> usize { |
| 406 | arch.syscall0(arch.SYS_fork) | |
| 406 | return arch.syscall0(arch.SYS_fork); | |
| 407 | 407 | } |
| 408 | 408 | |
| 409 | 409 | pub fn getcwd(buf: &u8, size: usize) -> usize { |
| 410 | arch.syscall2(arch.SYS_getcwd, @ptrToInt(buf), size) | |
| 410 | return arch.syscall2(arch.SYS_getcwd, @ptrToInt(buf), size); | |
| 411 | 411 | } |
| 412 | 412 | |
| 413 | 413 | pub fn getdents(fd: i32, dirp: &u8, count: usize) -> usize { |
| 414 | arch.syscall3(arch.SYS_getdents, usize(fd), @ptrToInt(dirp), count) | |
| 414 | return arch.syscall3(arch.SYS_getdents, usize(fd), @ptrToInt(dirp), count); | |
| 415 | 415 | } |
| 416 | 416 | |
| 417 | 417 | pub fn isatty(fd: i32) -> bool { |
| ... | ... | @@ -420,123 +420,123 @@ pub fn isatty(fd: i32) -> bool { |
| 420 | 420 | } |
| 421 | 421 | |
| 422 | 422 | pub fn readlink(noalias path: &const u8, noalias buf_ptr: &u8, buf_len: usize) -> usize { |
| 423 | arch.syscall3(arch.SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len) | |
| 423 | return arch.syscall3(arch.SYS_readlink, @ptrToInt(path), @ptrToInt(buf_ptr), buf_len); | |
| 424 | 424 | } |
| 425 | 425 | |
| 426 | 426 | pub fn mkdir(path: &const u8, mode: u32) -> usize { |
| 427 | arch.syscall2(arch.SYS_mkdir, @ptrToInt(path), mode) | |
| 427 | return arch.syscall2(arch.SYS_mkdir, @ptrToInt(path), mode); | |
| 428 | 428 | } |
| 429 | 429 | |
| 430 | 430 | pub fn mmap(address: ?&u8, length: usize, prot: usize, flags: usize, fd: i32, offset: isize) |
| 431 | 431 | -> usize |
| 432 | 432 | { |
| 433 | arch.syscall6(arch.SYS_mmap, @ptrToInt(address), length, prot, flags, usize(fd), | |
| 434 | @bitCast(usize, offset)) | |
| 433 | return arch.syscall6(arch.SYS_mmap, @ptrToInt(address), length, prot, flags, usize(fd), | |
| 434 | @bitCast(usize, offset)); | |
| 435 | 435 | } |
| 436 | 436 | |
| 437 | 437 | pub fn munmap(address: &u8, length: usize) -> usize { |
| 438 | arch.syscall2(arch.SYS_munmap, @ptrToInt(address), length) | |
| 438 | return arch.syscall2(arch.SYS_munmap, @ptrToInt(address), length); | |
| 439 | 439 | } |
| 440 | 440 | |
| 441 | 441 | pub fn read(fd: i32, buf: &u8, count: usize) -> usize { |
| 442 | arch.syscall3(arch.SYS_read, usize(fd), @ptrToInt(buf), count) | |
| 442 | return arch.syscall3(arch.SYS_read, usize(fd), @ptrToInt(buf), count); | |
| 443 | 443 | } |
| 444 | 444 | |
| 445 | 445 | pub fn rmdir(path: &const u8) -> usize { |
| 446 | arch.syscall1(arch.SYS_rmdir, @ptrToInt(path)) | |
| 446 | return arch.syscall1(arch.SYS_rmdir, @ptrToInt(path)); | |
| 447 | 447 | } |
| 448 | 448 | |
| 449 | 449 | pub fn symlink(existing: &const u8, new: &const u8) -> usize { |
| 450 | arch.syscall2(arch.SYS_symlink, @ptrToInt(existing), @ptrToInt(new)) | |
| 450 | return arch.syscall2(arch.SYS_symlink, @ptrToInt(existing), @ptrToInt(new)); | |
| 451 | 451 | } |
| 452 | 452 | |
| 453 | 453 | pub fn pread(fd: i32, buf: &u8, count: usize, offset: usize) -> usize { |
| 454 | arch.syscall4(arch.SYS_pread, usize(fd), @ptrToInt(buf), count, offset) | |
| 454 | return arch.syscall4(arch.SYS_pread, usize(fd), @ptrToInt(buf), count, offset); | |
| 455 | 455 | } |
| 456 | 456 | |
| 457 | 457 | pub fn pipe(fd: &[2]i32) -> usize { |
| 458 | pipe2(fd, 0) | |
| 458 | return pipe2(fd, 0); | |
| 459 | 459 | } |
| 460 | 460 | |
| 461 | 461 | pub fn pipe2(fd: &[2]i32, flags: usize) -> usize { |
| 462 | arch.syscall2(arch.SYS_pipe2, @ptrToInt(fd), flags) | |
| 462 | return arch.syscall2(arch.SYS_pipe2, @ptrToInt(fd), flags); | |
| 463 | 463 | } |
| 464 | 464 | |
| 465 | 465 | pub fn write(fd: i32, buf: &const u8, count: usize) -> usize { |
| 466 | arch.syscall3(arch.SYS_write, usize(fd), @ptrToInt(buf), count) | |
| 466 | return arch.syscall3(arch.SYS_write, usize(fd), @ptrToInt(buf), count); | |
| 467 | 467 | } |
| 468 | 468 | |
| 469 | 469 | pub fn pwrite(fd: i32, buf: &const u8, count: usize, offset: usize) -> usize { |
| 470 | arch.syscall4(arch.SYS_pwrite, usize(fd), @ptrToInt(buf), count, offset) | |
| 470 | return arch.syscall4(arch.SYS_pwrite, usize(fd), @ptrToInt(buf), count, offset); | |
| 471 | 471 | } |
| 472 | 472 | |
| 473 | 473 | pub fn rename(old: &const u8, new: &const u8) -> usize { |
| 474 | arch.syscall2(arch.SYS_rename, @ptrToInt(old), @ptrToInt(new)) | |
| 474 | return arch.syscall2(arch.SYS_rename, @ptrToInt(old), @ptrToInt(new)); | |
| 475 | 475 | } |
| 476 | 476 | |
| 477 | 477 | pub fn open(path: &const u8, flags: u32, perm: usize) -> usize { |
| 478 | arch.syscall3(arch.SYS_open, @ptrToInt(path), flags, perm) | |
| 478 | return arch.syscall3(arch.SYS_open, @ptrToInt(path), flags, perm); | |
| 479 | 479 | } |
| 480 | 480 | |
| 481 | 481 | pub fn create(path: &const u8, perm: usize) -> usize { |
| 482 | arch.syscall2(arch.SYS_creat, @ptrToInt(path), perm) | |
| 482 | return arch.syscall2(arch.SYS_creat, @ptrToInt(path), perm); | |
| 483 | 483 | } |
| 484 | 484 | |
| 485 | 485 | pub fn openat(dirfd: i32, path: &const u8, flags: usize, mode: usize) -> usize { |
| 486 | arch.syscall4(arch.SYS_openat, usize(dirfd), @ptrToInt(path), flags, mode) | |
| 486 | return arch.syscall4(arch.SYS_openat, usize(dirfd), @ptrToInt(path), flags, mode); | |
| 487 | 487 | } |
| 488 | 488 | |
| 489 | 489 | pub fn close(fd: i32) -> usize { |
| 490 | arch.syscall1(arch.SYS_close, usize(fd)) | |
| 490 | return arch.syscall1(arch.SYS_close, usize(fd)); | |
| 491 | 491 | } |
| 492 | 492 | |
| 493 | 493 | pub fn lseek(fd: i32, offset: isize, ref_pos: usize) -> usize { |
| 494 | arch.syscall3(arch.SYS_lseek, usize(fd), @bitCast(usize, offset), ref_pos) | |
| 494 | return arch.syscall3(arch.SYS_lseek, usize(fd), @bitCast(usize, offset), ref_pos); | |
| 495 | 495 | } |
| 496 | 496 | |
| 497 | 497 | pub fn exit(status: i32) -> noreturn { |
| 498 | 498 | _ = arch.syscall1(arch.SYS_exit, @bitCast(usize, isize(status))); |
| 499 | unreachable | |
| 499 | unreachable; | |
| 500 | 500 | } |
| 501 | 501 | |
| 502 | 502 | pub fn getrandom(buf: &u8, count: usize, flags: u32) -> usize { |
| 503 | arch.syscall3(arch.SYS_getrandom, @ptrToInt(buf), count, usize(flags)) | |
| 503 | return arch.syscall3(arch.SYS_getrandom, @ptrToInt(buf), count, usize(flags)); | |
| 504 | 504 | } |
| 505 | 505 | |
| 506 | 506 | pub fn kill(pid: i32, sig: i32) -> usize { |
| 507 | arch.syscall2(arch.SYS_kill, @bitCast(usize, isize(pid)), usize(sig)) | |
| 507 | return arch.syscall2(arch.SYS_kill, @bitCast(usize, isize(pid)), usize(sig)); | |
| 508 | 508 | } |
| 509 | 509 | |
| 510 | 510 | pub fn unlink(path: &const u8) -> usize { |
| 511 | arch.syscall1(arch.SYS_unlink, @ptrToInt(path)) | |
| 511 | return arch.syscall1(arch.SYS_unlink, @ptrToInt(path)); | |
| 512 | 512 | } |
| 513 | 513 | |
| 514 | 514 | pub fn waitpid(pid: i32, status: &i32, options: i32) -> usize { |
| 515 | arch.syscall4(arch.SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0) | |
| 515 | return arch.syscall4(arch.SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0); | |
| 516 | 516 | } |
| 517 | 517 | |
| 518 | 518 | pub fn nanosleep(req: &const timespec, rem: ?&timespec) -> usize { |
| 519 | arch.syscall2(arch.SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem)) | |
| 519 | return arch.syscall2(arch.SYS_nanosleep, @ptrToInt(req), @ptrToInt(rem)); | |
| 520 | 520 | } |
| 521 | 521 | |
| 522 | 522 | pub fn setuid(uid: u32) -> usize { |
| 523 | arch.syscall1(arch.SYS_setuid, uid) | |
| 523 | return arch.syscall1(arch.SYS_setuid, uid); | |
| 524 | 524 | } |
| 525 | 525 | |
| 526 | 526 | pub fn setgid(gid: u32) -> usize { |
| 527 | arch.syscall1(arch.SYS_setgid, gid) | |
| 527 | return arch.syscall1(arch.SYS_setgid, gid); | |
| 528 | 528 | } |
| 529 | 529 | |
| 530 | 530 | pub fn setreuid(ruid: u32, euid: u32) -> usize { |
| 531 | arch.syscall2(arch.SYS_setreuid, ruid, euid) | |
| 531 | return arch.syscall2(arch.SYS_setreuid, ruid, euid); | |
| 532 | 532 | } |
| 533 | 533 | |
| 534 | 534 | pub fn setregid(rgid: u32, egid: u32) -> usize { |
| 535 | arch.syscall2(arch.SYS_setregid, rgid, egid) | |
| 535 | return arch.syscall2(arch.SYS_setregid, rgid, egid); | |
| 536 | 536 | } |
| 537 | 537 | |
| 538 | 538 | pub fn sigprocmask(flags: u32, noalias set: &const sigset_t, noalias oldset: ?&sigset_t) -> usize { |
| 539 | arch.syscall4(arch.SYS_rt_sigprocmask, flags, @ptrToInt(set), @ptrToInt(oldset), NSIG/8) | |
| 539 | return arch.syscall4(arch.SYS_rt_sigprocmask, flags, @ptrToInt(set), @ptrToInt(oldset), NSIG/8); | |
| 540 | 540 | } |
| 541 | 541 | |
| 542 | 542 | pub fn sigaction(sig: u6, noalias act: &const Sigaction, noalias oact: ?&Sigaction) -> usize { |
| ... | ... | @@ -652,69 +652,69 @@ pub const iovec = extern struct { |
| 652 | 652 | }; |
| 653 | 653 | |
| 654 | 654 | pub fn getsockname(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) -> usize { |
| 655 | arch.syscall3(arch.SYS_getsockname, usize(fd), @ptrToInt(addr), @ptrToInt(len)) | |
| 655 | return arch.syscall3(arch.SYS_getsockname, usize(fd), @ptrToInt(addr), @ptrToInt(len)); | |
| 656 | 656 | } |
| 657 | 657 | |
| 658 | 658 | pub fn getpeername(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) -> usize { |
| 659 | arch.syscall3(arch.SYS_getpeername, usize(fd), @ptrToInt(addr), @ptrToInt(len)) | |
| 659 | return arch.syscall3(arch.SYS_getpeername, usize(fd), @ptrToInt(addr), @ptrToInt(len)); | |
| 660 | 660 | } |
| 661 | 661 | |
| 662 | 662 | pub fn socket(domain: i32, socket_type: i32, protocol: i32) -> usize { |
| 663 | arch.syscall3(arch.SYS_socket, usize(domain), usize(socket_type), usize(protocol)) | |
| 663 | return arch.syscall3(arch.SYS_socket, usize(domain), usize(socket_type), usize(protocol)); | |
| 664 | 664 | } |
| 665 | 665 | |
| 666 | 666 | pub fn setsockopt(fd: i32, level: i32, optname: i32, optval: &const u8, optlen: socklen_t) -> usize { |
| 667 | arch.syscall5(arch.SYS_setsockopt, usize(fd), usize(level), usize(optname), usize(optval), @ptrToInt(optlen)) | |
| 667 | return arch.syscall5(arch.SYS_setsockopt, usize(fd), usize(level), usize(optname), usize(optval), @ptrToInt(optlen)); | |
| 668 | 668 | } |
| 669 | 669 | |
| 670 | 670 | pub fn getsockopt(fd: i32, level: i32, optname: i32, noalias optval: &u8, noalias optlen: &socklen_t) -> usize { |
| 671 | arch.syscall5(arch.SYS_getsockopt, usize(fd), usize(level), usize(optname), @ptrToInt(optval), @ptrToInt(optlen)) | |
| 671 | return arch.syscall5(arch.SYS_getsockopt, usize(fd), usize(level), usize(optname), @ptrToInt(optval), @ptrToInt(optlen)); | |
| 672 | 672 | } |
| 673 | 673 | |
| 674 | 674 | pub fn sendmsg(fd: i32, msg: &const arch.msghdr, flags: u32) -> usize { |
| 675 | arch.syscall3(arch.SYS_sendmsg, usize(fd), @ptrToInt(msg), flags) | |
| 675 | return arch.syscall3(arch.SYS_sendmsg, usize(fd), @ptrToInt(msg), flags); | |
| 676 | 676 | } |
| 677 | 677 | |
| 678 | 678 | pub fn connect(fd: i32, addr: &const sockaddr, len: socklen_t) -> usize { |
| 679 | arch.syscall3(arch.SYS_connect, usize(fd), @ptrToInt(addr), usize(len)) | |
| 679 | return arch.syscall3(arch.SYS_connect, usize(fd), @ptrToInt(addr), usize(len)); | |
| 680 | 680 | } |
| 681 | 681 | |
| 682 | 682 | pub fn recvmsg(fd: i32, msg: &arch.msghdr, flags: u32) -> usize { |
| 683 | arch.syscall3(arch.SYS_recvmsg, usize(fd), @ptrToInt(msg), flags) | |
| 683 | return arch.syscall3(arch.SYS_recvmsg, usize(fd), @ptrToInt(msg), flags); | |
| 684 | 684 | } |
| 685 | 685 | |
| 686 | 686 | pub fn recvfrom(fd: i32, noalias buf: &u8, len: usize, flags: u32, |
| 687 | 687 | noalias addr: ?&sockaddr, noalias alen: ?&socklen_t) -> usize |
| 688 | 688 | { |
| 689 | arch.syscall6(arch.SYS_recvfrom, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen)) | |
| 689 | return arch.syscall6(arch.SYS_recvfrom, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), @ptrToInt(alen)); | |
| 690 | 690 | } |
| 691 | 691 | |
| 692 | 692 | pub fn shutdown(fd: i32, how: i32) -> usize { |
| 693 | arch.syscall2(arch.SYS_shutdown, usize(fd), usize(how)) | |
| 693 | return arch.syscall2(arch.SYS_shutdown, usize(fd), usize(how)); | |
| 694 | 694 | } |
| 695 | 695 | |
| 696 | 696 | pub fn bind(fd: i32, addr: &const sockaddr, len: socklen_t) -> usize { |
| 697 | arch.syscall3(arch.SYS_bind, usize(fd), @ptrToInt(addr), usize(len)) | |
| 697 | return arch.syscall3(arch.SYS_bind, usize(fd), @ptrToInt(addr), usize(len)); | |
| 698 | 698 | } |
| 699 | 699 | |
| 700 | 700 | pub fn listen(fd: i32, backlog: i32) -> usize { |
| 701 | arch.syscall2(arch.SYS_listen, usize(fd), usize(backlog)) | |
| 701 | return arch.syscall2(arch.SYS_listen, usize(fd), usize(backlog)); | |
| 702 | 702 | } |
| 703 | 703 | |
| 704 | 704 | pub fn sendto(fd: i32, buf: &const u8, len: usize, flags: u32, addr: ?&const sockaddr, alen: socklen_t) -> usize { |
| 705 | arch.syscall6(arch.SYS_sendto, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), usize(alen)) | |
| 705 | return arch.syscall6(arch.SYS_sendto, usize(fd), @ptrToInt(buf), len, flags, @ptrToInt(addr), usize(alen)); | |
| 706 | 706 | } |
| 707 | 707 | |
| 708 | 708 | pub fn socketpair(domain: i32, socket_type: i32, protocol: i32, fd: [2]i32) -> usize { |
| 709 | arch.syscall4(arch.SYS_socketpair, usize(domain), usize(socket_type), usize(protocol), @ptrToInt(&fd[0])) | |
| 709 | return arch.syscall4(arch.SYS_socketpair, usize(domain), usize(socket_type), usize(protocol), @ptrToInt(&fd[0])); | |
| 710 | 710 | } |
| 711 | 711 | |
| 712 | 712 | pub fn accept(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t) -> usize { |
| 713 | accept4(fd, addr, len, 0) | |
| 713 | return accept4(fd, addr, len, 0); | |
| 714 | 714 | } |
| 715 | 715 | |
| 716 | 716 | pub fn accept4(fd: i32, noalias addr: &sockaddr, noalias len: &socklen_t, flags: u32) -> usize { |
| 717 | arch.syscall4(arch.SYS_accept4, usize(fd), @ptrToInt(addr), @ptrToInt(len), flags) | |
| 717 | return arch.syscall4(arch.SYS_accept4, usize(fd), @ptrToInt(addr), @ptrToInt(len), flags); | |
| 718 | 718 | } |
| 719 | 719 | |
| 720 | 720 | // error NameTooLong; |
| ... | ... | @@ -749,7 +749,7 @@ pub const Stat = arch.Stat; |
| 749 | 749 | pub const timespec = arch.timespec; |
| 750 | 750 | |
| 751 | 751 | pub fn fstat(fd: i32, stat_buf: &Stat) -> usize { |
| 752 | arch.syscall2(arch.SYS_fstat, usize(fd), @ptrToInt(stat_buf)) | |
| 752 | return arch.syscall2(arch.SYS_fstat, usize(fd), @ptrToInt(stat_buf)); | |
| 753 | 753 | } |
| 754 | 754 | |
| 755 | 755 | pub const epoll_data = u64; |
| ... | ... | @@ -760,19 +760,19 @@ pub const epoll_event = extern struct { |
| 760 | 760 | }; |
| 761 | 761 | |
| 762 | 762 | pub fn epoll_create() -> usize { |
| 763 | arch.syscall1(arch.SYS_epoll_create, usize(1)) | |
| 763 | return arch.syscall1(arch.SYS_epoll_create, usize(1)); | |
| 764 | 764 | } |
| 765 | 765 | |
| 766 | 766 | pub fn epoll_ctl(epoll_fd: i32, op: i32, fd: i32, ev: &epoll_event) -> usize { |
| 767 | arch.syscall4(arch.SYS_epoll_ctl, usize(epoll_fd), usize(op), usize(fd), @ptrToInt(ev)) | |
| 767 | return arch.syscall4(arch.SYS_epoll_ctl, usize(epoll_fd), usize(op), usize(fd), @ptrToInt(ev)); | |
| 768 | 768 | } |
| 769 | 769 | |
| 770 | 770 | pub fn epoll_wait(epoll_fd: i32, events: &epoll_event, maxevents: i32, timeout: i32) -> usize { |
| 771 | arch.syscall4(arch.SYS_epoll_wait, usize(epoll_fd), @ptrToInt(events), usize(maxevents), usize(timeout)) | |
| 771 | return arch.syscall4(arch.SYS_epoll_wait, usize(epoll_fd), @ptrToInt(events), usize(maxevents), usize(timeout)); | |
| 772 | 772 | } |
| 773 | 773 | |
| 774 | 774 | pub fn timerfd_create(clockid: i32, flags: u32) -> usize { |
| 775 | arch.syscall2(arch.SYS_timerfd_create, usize(clockid), usize(flags)) | |
| 775 | return arch.syscall2(arch.SYS_timerfd_create, usize(clockid), usize(flags)); | |
| 776 | 776 | } |
| 777 | 777 | |
| 778 | 778 | pub const itimerspec = extern struct { |
| ... | ... | @@ -781,11 +781,11 @@ pub const itimerspec = extern struct { |
| 781 | 781 | }; |
| 782 | 782 | |
| 783 | 783 | pub fn timerfd_gettime(fd: i32, curr_value: &itimerspec) -> usize { |
| 784 | arch.syscall2(arch.SYS_timerfd_gettime, usize(fd), @ptrToInt(curr_value)) | |
| 784 | return arch.syscall2(arch.SYS_timerfd_gettime, usize(fd), @ptrToInt(curr_value)); | |
| 785 | 785 | } |
| 786 | 786 | |
| 787 | 787 | pub fn timerfd_settime(fd: i32, flags: u32, new_value: &const itimerspec, old_value: ?&itimerspec) -> usize { |
| 788 | arch.syscall4(arch.SYS_timerfd_settime, usize(fd), usize(flags), @ptrToInt(new_value), @ptrToInt(old_value)) | |
| 788 | return arch.syscall4(arch.SYS_timerfd_settime, usize(fd), usize(flags), @ptrToInt(new_value), @ptrToInt(old_value)); | |
| 789 | 789 | } |
| 790 | 790 | |
| 791 | 791 | test "import linux_test" { |
std/os/linux_x86_64.zig+16-16| ... | ... | @@ -371,52 +371,52 @@ pub const F_GETOWN_EX = 16; |
| 371 | 371 | pub const F_GETOWNER_UIDS = 17; |
| 372 | 372 | |
| 373 | 373 | pub fn syscall0(number: usize) -> usize { |
| 374 | asm volatile ("syscall" | |
| 374 | return asm volatile ("syscall" | |
| 375 | 375 | : [ret] "={rax}" (-> usize) |
| 376 | 376 | : [number] "{rax}" (number) |
| 377 | : "rcx", "r11") | |
| 377 | : "rcx", "r11"); | |
| 378 | 378 | } |
| 379 | 379 | |
| 380 | 380 | pub fn syscall1(number: usize, arg1: usize) -> usize { |
| 381 | asm volatile ("syscall" | |
| 381 | return asm volatile ("syscall" | |
| 382 | 382 | : [ret] "={rax}" (-> usize) |
| 383 | 383 | : [number] "{rax}" (number), |
| 384 | 384 | [arg1] "{rdi}" (arg1) |
| 385 | : "rcx", "r11") | |
| 385 | : "rcx", "r11"); | |
| 386 | 386 | } |
| 387 | 387 | |
| 388 | 388 | pub fn syscall2(number: usize, arg1: usize, arg2: usize) -> usize { |
| 389 | asm volatile ("syscall" | |
| 389 | return asm volatile ("syscall" | |
| 390 | 390 | : [ret] "={rax}" (-> usize) |
| 391 | 391 | : [number] "{rax}" (number), |
| 392 | 392 | [arg1] "{rdi}" (arg1), |
| 393 | 393 | [arg2] "{rsi}" (arg2) |
| 394 | : "rcx", "r11") | |
| 394 | : "rcx", "r11"); | |
| 395 | 395 | } |
| 396 | 396 | |
| 397 | 397 | pub fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) -> usize { |
| 398 | asm volatile ("syscall" | |
| 398 | return asm volatile ("syscall" | |
| 399 | 399 | : [ret] "={rax}" (-> usize) |
| 400 | 400 | : [number] "{rax}" (number), |
| 401 | 401 | [arg1] "{rdi}" (arg1), |
| 402 | 402 | [arg2] "{rsi}" (arg2), |
| 403 | 403 | [arg3] "{rdx}" (arg3) |
| 404 | : "rcx", "r11") | |
| 404 | : "rcx", "r11"); | |
| 405 | 405 | } |
| 406 | 406 | |
| 407 | 407 | pub fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) -> usize { |
| 408 | asm volatile ("syscall" | |
| 408 | return asm volatile ("syscall" | |
| 409 | 409 | : [ret] "={rax}" (-> usize) |
| 410 | 410 | : [number] "{rax}" (number), |
| 411 | 411 | [arg1] "{rdi}" (arg1), |
| 412 | 412 | [arg2] "{rsi}" (arg2), |
| 413 | 413 | [arg3] "{rdx}" (arg3), |
| 414 | 414 | [arg4] "{r10}" (arg4) |
| 415 | : "rcx", "r11") | |
| 415 | : "rcx", "r11"); | |
| 416 | 416 | } |
| 417 | 417 | |
| 418 | 418 | pub fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) -> usize { |
| 419 | asm volatile ("syscall" | |
| 419 | return asm volatile ("syscall" | |
| 420 | 420 | : [ret] "={rax}" (-> usize) |
| 421 | 421 | : [number] "{rax}" (number), |
| 422 | 422 | [arg1] "{rdi}" (arg1), |
| ... | ... | @@ -424,13 +424,13 @@ pub fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usiz |
| 424 | 424 | [arg3] "{rdx}" (arg3), |
| 425 | 425 | [arg4] "{r10}" (arg4), |
| 426 | 426 | [arg5] "{r8}" (arg5) |
| 427 | : "rcx", "r11") | |
| 427 | : "rcx", "r11"); | |
| 428 | 428 | } |
| 429 | 429 | |
| 430 | 430 | pub fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize, |
| 431 | 431 | arg5: usize, arg6: usize) -> usize |
| 432 | 432 | { |
| 433 | asm volatile ("syscall" | |
| 433 | return asm volatile ("syscall" | |
| 434 | 434 | : [ret] "={rax}" (-> usize) |
| 435 | 435 | : [number] "{rax}" (number), |
| 436 | 436 | [arg1] "{rdi}" (arg1), |
| ... | ... | @@ -439,14 +439,14 @@ pub fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usiz |
| 439 | 439 | [arg4] "{r10}" (arg4), |
| 440 | 440 | [arg5] "{r8}" (arg5), |
| 441 | 441 | [arg6] "{r9}" (arg6) |
| 442 | : "rcx", "r11") | |
| 442 | : "rcx", "r11"); | |
| 443 | 443 | } |
| 444 | 444 | |
| 445 | 445 | pub nakedcc fn restore_rt() { |
| 446 | asm volatile ("syscall" | |
| 446 | return asm volatile ("syscall" | |
| 447 | 447 | : |
| 448 | 448 | : [number] "{rax}" (usize(SYS_rt_sigreturn)) |
| 449 | : "rcx", "r11") | |
| 449 | : "rcx", "r11"); | |
| 450 | 450 | } |
| 451 | 451 | |
| 452 | 452 |
std/os/path.zig+18-18| ... | ... | @@ -749,21 +749,19 @@ pub fn relativeWindows(allocator: &Allocator, from: []const u8, to: []const u8) |
| 749 | 749 | const resolved_to = %return resolveWindows(allocator, [][]const u8{to}); |
| 750 | 750 | defer if (clean_up_resolved_to) allocator.free(resolved_to); |
| 751 | 751 | |
| 752 | const result_is_to = if (drive(resolved_to)) |to_drive| { | |
| 753 | if (drive(resolved_from)) |from_drive| { | |
| 752 | const result_is_to = if (drive(resolved_to)) |to_drive| | |
| 753 | if (drive(resolved_from)) |from_drive| | |
| 754 | 754 | asciiUpper(from_drive[0]) != asciiUpper(to_drive[0]) |
| 755 | } else { | |
| 755 | else | |
| 756 | 756 | true |
| 757 | } | |
| 758 | } else if (networkShare(resolved_to)) |to_ns| { | |
| 759 | if (networkShare(resolved_from)) |from_ns| { | |
| 757 | else if (networkShare(resolved_to)) |to_ns| | |
| 758 | if (networkShare(resolved_from)) |from_ns| | |
| 760 | 759 | !networkShareServersEql(to_ns, from_ns) |
| 761 | } else { | |
| 760 | else | |
| 762 | 761 | true |
| 763 | } | |
| 764 | } else { | |
| 765 | unreachable | |
| 766 | }; | |
| 762 | else | |
| 763 | unreachable; | |
| 764 | ||
| 767 | 765 | if (result_is_to) { |
| 768 | 766 | clean_up_resolved_to = false; |
| 769 | 767 | return resolved_to; |
| ... | ... | @@ -964,14 +962,16 @@ pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 { |
| 964 | 962 | |
| 965 | 963 | // windows returns \\?\ prepended to the path |
| 966 | 964 | // we strip it because nobody wants \\?\ prepended to their path |
| 967 | const final_len = if (result > 4 and mem.startsWith(u8, buf, "\\\\?\\")) { | |
| 968 | var i: usize = 4; | |
| 969 | while (i < result) : (i += 1) { | |
| 970 | buf[i - 4] = buf[i]; | |
| 965 | const final_len = x: { | |
| 966 | if (result > 4 and mem.startsWith(u8, buf, "\\\\?\\")) { | |
| 967 | var i: usize = 4; | |
| 968 | while (i < result) : (i += 1) { | |
| 969 | buf[i - 4] = buf[i]; | |
| 970 | } | |
| 971 | break :x result - 4; | |
| 972 | } else { | |
| 973 | break :x result; | |
| 971 | 974 | } |
| 972 | result - 4 | |
| 973 | } else { | |
| 974 | result | |
| 975 | 975 | }; |
| 976 | 976 | |
| 977 | 977 | return allocator.shrink(u8, buf, final_len); |
std/os/windows/util.zig+2-2| ... | ... | @@ -122,7 +122,7 @@ pub fn windowsOpen(file_path: []const u8, desired_access: windows.DWORD, share_m |
| 122 | 122 | /// Caller must free result. |
| 123 | 123 | pub fn createWindowsEnvBlock(allocator: &mem.Allocator, env_map: &const BufMap) -> %[]u8 { |
| 124 | 124 | // count bytes needed |
| 125 | const bytes_needed = { | |
| 125 | const bytes_needed = x: { | |
| 126 | 126 | var bytes_needed: usize = 1; // 1 for the final null byte |
| 127 | 127 | var it = env_map.iterator(); |
| 128 | 128 | while (it.next()) |pair| { |
| ... | ... | @@ -130,7 +130,7 @@ pub fn createWindowsEnvBlock(allocator: &mem.Allocator, env_map: &const BufMap) |
| 130 | 130 | // +1 for null byte |
| 131 | 131 | bytes_needed += pair.key.len + pair.value.len + 2; |
| 132 | 132 | } |
| 133 | bytes_needed | |
| 133 | break :x bytes_needed; | |
| 134 | 134 | }; |
| 135 | 135 | const result = %return allocator.alloc(u8, bytes_needed); |
| 136 | 136 | %defer allocator.free(result); |
std/rand.zig+14-14| ... | ... | @@ -28,9 +28,9 @@ pub const Rand = struct { |
| 28 | 28 | |
| 29 | 29 | /// Initialize random state with the given seed. |
| 30 | 30 | pub fn init(seed: usize) -> Rand { |
| 31 | Rand { | |
| 31 | return Rand { | |
| 32 | 32 | .rng = Rng.init(seed), |
| 33 | } | |
| 33 | }; | |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | /// Get an integer or boolean with random bits. |
| ... | ... | @@ -78,13 +78,13 @@ pub const Rand = struct { |
| 78 | 78 | const end_uint = uint(end); |
| 79 | 79 | const total_range = math.absCast(start) + end_uint; |
| 80 | 80 | const value = r.range(uint, 0, total_range); |
| 81 | const result = if (value < end_uint) { | |
| 82 | T(value) | |
| 83 | } else if (value == end_uint) { | |
| 84 | start | |
| 85 | } else { | |
| 81 | const result = if (value < end_uint) x: { | |
| 82 | break :x T(value); | |
| 83 | } else if (value == end_uint) x: { | |
| 84 | break :x start; | |
| 85 | } else x: { | |
| 86 | 86 | // Can't overflow because the range is over signed ints |
| 87 | %%math.negateCast(value - end_uint) | |
| 87 | break :x %%math.negateCast(value - end_uint); | |
| 88 | 88 | }; |
| 89 | 89 | return result; |
| 90 | 90 | } else { |
| ... | ... | @@ -114,13 +114,13 @@ pub const Rand = struct { |
| 114 | 114 | // const rand_bits = r.rng.scalar(int) & mask; |
| 115 | 115 | // return @float_compose(T, false, 0, rand_bits) - 1.0 |
| 116 | 116 | const int_type = @IntType(false, @sizeOf(T) * 8); |
| 117 | const precision = if (T == f32) { | |
| 117 | const precision = if (T == f32) | |
| 118 | 118 | 16777216 |
| 119 | } else if (T == f64) { | |
| 119 | else if (T == f64) | |
| 120 | 120 | 9007199254740992 |
| 121 | } else { | |
| 121 | else | |
| 122 | 122 | @compileError("unknown floating point type") |
| 123 | }; | |
| 123 | ; | |
| 124 | 124 | return T(r.range(int_type, 0, precision)) / T(precision); |
| 125 | 125 | } |
| 126 | 126 | }; |
| ... | ... | @@ -133,7 +133,7 @@ fn MersenneTwister( |
| 133 | 133 | comptime t: math.Log2Int(int), comptime c: int, |
| 134 | 134 | comptime l: math.Log2Int(int), comptime f: int) -> type |
| 135 | 135 | { |
| 136 | struct { | |
| 136 | return struct { | |
| 137 | 137 | const Self = this; |
| 138 | 138 | |
| 139 | 139 | array: [n]int, |
| ... | ... | @@ -189,7 +189,7 @@ fn MersenneTwister( |
| 189 | 189 | |
| 190 | 190 | return x; |
| 191 | 191 | } |
| 192 | } | |
| 192 | }; | |
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | test "rand float 32" { |
std/sort.zig+4-4| ... | ... | @@ -355,7 +355,7 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons |
| 355 | 355 | // these values will be pulled out to the start of A |
| 356 | 356 | last = A.start; |
| 357 | 357 | count = 1; |
| 358 | while (count < find) : ({last = index; count += 1}) { | |
| 358 | while (count < find) : ({last = index; count += 1;}) { | |
| 359 | 359 | index = findLastForward(T, items, items[last], Range.init(last + 1, A.end), lessThan, find - count); |
| 360 | 360 | if (index == A.end) break; |
| 361 | 361 | } |
| ... | ... | @@ -410,7 +410,7 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons |
| 410 | 410 | // these values will be pulled out to the end of B |
| 411 | 411 | last = B.end - 1; |
| 412 | 412 | count = 1; |
| 413 | while (count < find) : ({last = index - 1; count += 1}) { | |
| 413 | while (count < find) : ({last = index - 1; count += 1;}) { | |
| 414 | 414 | index = findFirstBackward(T, items, items[last], Range.init(B.start, last), lessThan, find - count); |
| 415 | 415 | if (index == B.start) break; |
| 416 | 416 | } |
| ... | ... | @@ -547,7 +547,7 @@ pub fn sort(comptime T: type, items: []T, lessThan: fn(lhs: &const T, rhs: &cons |
| 547 | 547 | // swap the first value of each A block with the value in buffer1 |
| 548 | 548 | var indexA = buffer1.start; |
| 549 | 549 | index = firstA.end; |
| 550 | while (index < blockA.end) : ({indexA += 1; index += block_size}) { | |
| 550 | while (index < blockA.end) : ({indexA += 1; index += block_size;}) { | |
| 551 | 551 | mem.swap(T, &items[indexA], &items[index]); |
| 552 | 552 | } |
| 553 | 553 | |
| ... | ... | @@ -1093,7 +1093,7 @@ test "another sort case" { |
| 1093 | 1093 | var arr = []i32{ 5, 3, 1, 2, 4 }; |
| 1094 | 1094 | sort(i32, arr[0..], i32asc); |
| 1095 | 1095 | |
| 1096 | assert(mem.eql(i32, arr, []i32{ 1, 2, 3, 4, 5 })) | |
| 1096 | assert(mem.eql(i32, arr, []i32{ 1, 2, 3, 4, 5 })); | |
| 1097 | 1097 | } |
| 1098 | 1098 | |
| 1099 | 1099 | test "sort fuzz testing" { |
std/special/build_runner.zig+6-10| ... | ... | @@ -45,21 +45,17 @@ pub fn main() -> %void { |
| 45 | 45 | |
| 46 | 46 | var stderr_file = io.getStdErr(); |
| 47 | 47 | var stderr_file_stream: io.FileOutStream = undefined; |
| 48 | var stderr_stream: %&io.OutStream = if (stderr_file) |*f| { | |
| 48 | var stderr_stream: %&io.OutStream = if (stderr_file) |*f| x: { | |
| 49 | 49 | stderr_file_stream = io.FileOutStream.init(f); |
| 50 | &stderr_file_stream.stream | |
| 51 | } else |err| { | |
| 52 | err | |
| 53 | }; | |
| 50 | break :x &stderr_file_stream.stream; | |
| 51 | } else |err| err; | |
| 54 | 52 | |
| 55 | 53 | var stdout_file = io.getStdOut(); |
| 56 | 54 | var stdout_file_stream: io.FileOutStream = undefined; |
| 57 | var stdout_stream: %&io.OutStream = if (stdout_file) |*f| { | |
| 55 | var stdout_stream: %&io.OutStream = if (stdout_file) |*f| x: { | |
| 58 | 56 | stdout_file_stream = io.FileOutStream.init(f); |
| 59 | &stdout_file_stream.stream | |
| 60 | } else |err| { | |
| 61 | err | |
| 62 | }; | |
| 57 | break :x &stdout_file_stream.stream; | |
| 58 | } else |err| err; | |
| 63 | 59 | |
| 64 | 60 | while (arg_it.next(allocator)) |err_or_arg| { |
| 65 | 61 | const arg = %return unwrapArg(err_or_arg); |
std/special/builtin.zig+9-9| ... | ... | @@ -46,15 +46,15 @@ extern fn __stack_chk_fail() -> noreturn { |
| 46 | 46 | |
| 47 | 47 | const math = @import("../math/index.zig"); |
| 48 | 48 | |
| 49 | export fn fmodf(x: f32, y: f32) -> f32 { generic_fmod(f32, x, y) } | |
| 50 | export fn fmod(x: f64, y: f64) -> f64 { generic_fmod(f64, x, y) } | |
| 49 | export fn fmodf(x: f32, y: f32) -> f32 { return generic_fmod(f32, x, y); } | |
| 50 | export fn fmod(x: f64, y: f64) -> f64 { return generic_fmod(f64, x, y); } | |
| 51 | 51 | |
| 52 | 52 | // TODO add intrinsics for these (and probably the double version too) |
| 53 | 53 | // and have the math stuff use the intrinsic. same as @mod and @rem |
| 54 | export fn floorf(x: f32) -> f32 { math.floor(x) } | |
| 55 | export fn ceilf(x: f32) -> f32 { math.ceil(x) } | |
| 56 | export fn floor(x: f64) -> f64 { math.floor(x) } | |
| 57 | export fn ceil(x: f64) -> f64 { math.ceil(x) } | |
| 54 | export fn floorf(x: f32) -> f32 { return math.floor(x); } | |
| 55 | export fn ceilf(x: f32) -> f32 { return math.ceil(x); } | |
| 56 | export fn floor(x: f64) -> f64 { return math.floor(x); } | |
| 57 | export fn ceil(x: f64) -> f64 { return math.ceil(x); } | |
| 58 | 58 | |
| 59 | 59 | fn generic_fmod(comptime T: type, x: T, y: T) -> T { |
| 60 | 60 | @setDebugSafety(this, false); |
| ... | ... | @@ -84,7 +84,7 @@ fn generic_fmod(comptime T: type, x: T, y: T) -> T { |
| 84 | 84 | // normalize x and y |
| 85 | 85 | if (ex == 0) { |
| 86 | 86 | i = ux << exp_bits; |
| 87 | while (i >> bits_minus_1 == 0) : ({ex -= 1; i <<= 1}) {} | |
| 87 | while (i >> bits_minus_1 == 0) : (b: {ex -= 1; break :b i <<= 1;}) {} | |
| 88 | 88 | ux <<= log2uint(@bitCast(u32, -ex + 1)); |
| 89 | 89 | } else { |
| 90 | 90 | ux &= @maxValue(uint) >> exp_bits; |
| ... | ... | @@ -92,7 +92,7 @@ fn generic_fmod(comptime T: type, x: T, y: T) -> T { |
| 92 | 92 | } |
| 93 | 93 | if (ey == 0) { |
| 94 | 94 | i = uy << exp_bits; |
| 95 | while (i >> bits_minus_1 == 0) : ({ey -= 1; i <<= 1}) {} | |
| 95 | while (i >> bits_minus_1 == 0) : (b: {ey -= 1; break :b i <<= 1;}) {} | |
| 96 | 96 | uy <<= log2uint(@bitCast(u32, -ey + 1)); |
| 97 | 97 | } else { |
| 98 | 98 | uy &= @maxValue(uint) >> exp_bits; |
| ... | ... | @@ -115,7 +115,7 @@ fn generic_fmod(comptime T: type, x: T, y: T) -> T { |
| 115 | 115 | return 0 * x; |
| 116 | 116 | ux = i; |
| 117 | 117 | } |
| 118 | while (ux >> digits == 0) : ({ux <<= 1; ex -= 1}) {} | |
| 118 | while (ux >> digits == 0) : (b: {ux <<= 1; break :b ex -= 1;}) {} | |
| 119 | 119 | |
| 120 | 120 | // scale result up |
| 121 | 121 | if (ex > 0) { |
std/special/compiler_rt/comparetf2.zig+18-22| ... | ... | @@ -38,27 +38,25 @@ pub extern fn __letf2(a: f128, b: f128) -> c_int { |
| 38 | 38 | |
| 39 | 39 | // If at least one of a and b is positive, we get the same result comparing |
| 40 | 40 | // a and b as signed integers as we would with a floating-point compare. |
| 41 | return if ((aInt & bInt) >= 0) { | |
| 42 | if (aInt < bInt) { | |
| 41 | return if ((aInt & bInt) >= 0) | |
| 42 | if (aInt < bInt) | |
| 43 | 43 | LE_LESS |
| 44 | } else if (aInt == bInt) { | |
| 44 | else if (aInt == bInt) | |
| 45 | 45 | LE_EQUAL |
| 46 | } else { | |
| 46 | else | |
| 47 | 47 | LE_GREATER |
| 48 | } | |
| 49 | } else { | |
| 48 | else | |
| 50 | 49 | // Otherwise, both are negative, so we need to flip the sense of the |
| 51 | 50 | // comparison to get the correct result. (This assumes a twos- or ones- |
| 52 | 51 | // complement integer representation; if integers are represented in a |
| 53 | 52 | // sign-magnitude representation, then this flip is incorrect). |
| 54 | if (aInt > bInt) { | |
| 53 | if (aInt > bInt) | |
| 55 | 54 | LE_LESS |
| 56 | } else if (aInt == bInt) { | |
| 55 | else if (aInt == bInt) | |
| 57 | 56 | LE_EQUAL |
| 58 | } else { | |
| 57 | else | |
| 59 | 58 | LE_GREATER |
| 60 | } | |
| 61 | }; | |
| 59 | ; | |
| 62 | 60 | } |
| 63 | 61 | |
| 64 | 62 | // TODO https://github.com/zig-lang/zig/issues/305 |
| ... | ... | @@ -78,23 +76,21 @@ pub extern fn __getf2(a: f128, b: f128) -> c_int { |
| 78 | 76 | |
| 79 | 77 | if (aAbs > infRep or bAbs > infRep) return GE_UNORDERED; |
| 80 | 78 | if ((aAbs | bAbs) == 0) return GE_EQUAL; |
| 81 | return if ((aInt & bInt) >= 0) { | |
| 82 | if (aInt < bInt) { | |
| 79 | return if ((aInt & bInt) >= 0) | |
| 80 | if (aInt < bInt) | |
| 83 | 81 | GE_LESS |
| 84 | } else if (aInt == bInt) { | |
| 82 | else if (aInt == bInt) | |
| 85 | 83 | GE_EQUAL |
| 86 | } else { | |
| 84 | else | |
| 87 | 85 | GE_GREATER |
| 88 | } | |
| 89 | } else { | |
| 90 | if (aInt > bInt) { | |
| 86 | else | |
| 87 | if (aInt > bInt) | |
| 91 | 88 | GE_LESS |
| 92 | } else if (aInt == bInt) { | |
| 89 | else if (aInt == bInt) | |
| 93 | 90 | GE_EQUAL |
| 94 | } else { | |
| 91 | else | |
| 95 | 92 | GE_GREATER |
| 96 | } | |
| 97 | }; | |
| 93 | ; | |
| 98 | 94 | } |
| 99 | 95 | |
| 100 | 96 | pub extern fn __unordtf2(a: f128, b: f128) -> c_int { |
test/cases/align.zig+9-9| ... | ... | @@ -10,7 +10,7 @@ test "global variable alignment" { |
| 10 | 10 | assert(@typeOf(slice) == []align(4) u8); |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | fn derp() align(@sizeOf(usize) * 2) -> i32 { 1234 } | |
| 13 | fn derp() align(@sizeOf(usize) * 2) -> i32 { return 1234; } | |
| 14 | 14 | fn noop1() align(1) {} |
| 15 | 15 | fn noop4() align(4) {} |
| 16 | 16 | |
| ... | ... | @@ -53,14 +53,14 @@ test "implicitly decreasing pointer alignment" { |
| 53 | 53 | assert(addUnaligned(&a, &b) == 7); |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | fn addUnaligned(a: &align(1) const u32, b: &align(1) const u32) -> u32 { *a + *b } | |
| 56 | fn addUnaligned(a: &align(1) const u32, b: &align(1) const u32) -> u32 { return *a + *b; } | |
| 57 | 57 | |
| 58 | 58 | test "implicitly decreasing slice alignment" { |
| 59 | 59 | const a: u32 align(4) = 3; |
| 60 | 60 | const b: u32 align(8) = 4; |
| 61 | 61 | assert(addUnalignedSlice((&a)[0..1], (&b)[0..1]) == 7); |
| 62 | 62 | } |
| 63 | fn addUnalignedSlice(a: []align(1) const u32, b: []align(1) const u32) -> u32 { a[0] + b[0] } | |
| 63 | fn addUnalignedSlice(a: []align(1) const u32, b: []align(1) const u32) -> u32 { return a[0] + b[0]; } | |
| 64 | 64 | |
| 65 | 65 | test "specifying alignment allows pointer cast" { |
| 66 | 66 | testBytesAlign(0x33); |
| ... | ... | @@ -115,20 +115,20 @@ fn testImplicitlyDecreaseFnAlign(ptr: fn () align(1) -> i32, answer: i32) { |
| 115 | 115 | assert(ptr() == answer); |
| 116 | 116 | } |
| 117 | 117 | |
| 118 | fn alignedSmall() align(8) -> i32 { 1234 } | |
| 119 | fn alignedBig() align(16) -> i32 { 5678 } | |
| 118 | fn alignedSmall() align(8) -> i32 { return 1234; } | |
| 119 | fn alignedBig() align(16) -> i32 { return 5678; } | |
| 120 | 120 | |
| 121 | 121 | |
| 122 | 122 | test "@alignCast functions" { |
| 123 | 123 | assert(fnExpectsOnly1(simple4) == 0x19); |
| 124 | 124 | } |
| 125 | 125 | fn fnExpectsOnly1(ptr: fn()align(1) -> i32) -> i32 { |
| 126 | fnExpects4(@alignCast(4, ptr)) | |
| 126 | return fnExpects4(@alignCast(4, ptr)); | |
| 127 | 127 | } |
| 128 | 128 | fn fnExpects4(ptr: fn()align(4) -> i32) -> i32 { |
| 129 | ptr() | |
| 129 | return ptr(); | |
| 130 | 130 | } |
| 131 | fn simple4() align(4) -> i32 { 0x19 } | |
| 131 | fn simple4() align(4) -> i32 { return 0x19; } | |
| 132 | 132 | |
| 133 | 133 | |
| 134 | 134 | test "generic function with align param" { |
| ... | ... | @@ -137,7 +137,7 @@ test "generic function with align param" { |
| 137 | 137 | assert(whyWouldYouEverDoThis(8) == 0x1); |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | fn whyWouldYouEverDoThis(comptime align_bytes: u8) align(align_bytes) -> u8 { 0x1 } | |
| 140 | fn whyWouldYouEverDoThis(comptime align_bytes: u8) align(align_bytes) -> u8 { return 0x1; } | |
| 141 | 141 | |
| 142 | 142 | |
| 143 | 143 | test "@ptrCast preserves alignment of bigger source" { |
test/cases/array.zig+2-2| ... | ... | @@ -22,7 +22,7 @@ test "arrays" { |
| 22 | 22 | assert(getArrayLen(array) == 5); |
| 23 | 23 | } |
| 24 | 24 | fn getArrayLen(a: []const u32) -> usize { |
| 25 | a.len | |
| 25 | return a.len; | |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | test "void arrays" { |
| ... | ... | @@ -41,7 +41,7 @@ test "array literal" { |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | test "array dot len const expr" { |
| 44 | assert(comptime {some_array.len == 4}); | |
| 44 | assert(comptime x: {break :x some_array.len == 4;}); | |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | const ArrayDotLenConstExpr = struct { |
test/cases/bitcast.zig+2-2| ... | ... | @@ -10,5 +10,5 @@ fn testBitCast_i32_u32() { |
| 10 | 10 | assert(conv2(@maxValue(u32)) == -1); |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | fn conv(x: i32) -> u32 { @bitCast(u32, x) } | |
| 14 | fn conv2(x: u32) -> i32 { @bitCast(i32, x) } | |
| 13 | fn conv(x: i32) -> u32 { return @bitCast(u32, x); } | |
| 14 | fn conv2(x: u32) -> i32 { return @bitCast(i32, x); } |
test/cases/bool.zig+1-1| ... | ... | @@ -22,7 +22,7 @@ test "bool cmp" { |
| 22 | 22 | assert(testBoolCmp(true, false) == false); |
| 23 | 23 | } |
| 24 | 24 | fn testBoolCmp(a: bool, b: bool) -> bool { |
| 25 | a == b | |
| 25 | return a == b; | |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | const global_f = false; |
test/cases/cast.zig+7-7| ... | ... | @@ -50,7 +50,7 @@ test "peer resolve arrays of different size to const slice" { |
| 50 | 50 | comptime assert(mem.eql(u8, boolToStr(false), "false")); |
| 51 | 51 | } |
| 52 | 52 | fn boolToStr(b: bool) -> []const u8 { |
| 53 | if (b) "true" else "false" | |
| 53 | return if (b) "true" else "false"; | |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | |
| ... | ... | @@ -239,17 +239,17 @@ test "peer type resolution: error and [N]T" { |
| 239 | 239 | |
| 240 | 240 | error BadValue; |
| 241 | 241 | fn testPeerErrorAndArray(x: u8) -> %[]const u8 { |
| 242 | switch (x) { | |
| 242 | return switch (x) { | |
| 243 | 243 | 0x00 => "OK", |
| 244 | 244 | else => error.BadValue, |
| 245 | } | |
| 245 | }; | |
| 246 | 246 | } |
| 247 | 247 | fn testPeerErrorAndArray2(x: u8) -> %[]const u8 { |
| 248 | switch (x) { | |
| 248 | return switch (x) { | |
| 249 | 249 | 0x00 => "OK", |
| 250 | 250 | 0x01 => "OKK", |
| 251 | 251 | else => error.BadValue, |
| 252 | } | |
| 252 | }; | |
| 253 | 253 | } |
| 254 | 254 | |
| 255 | 255 | test "explicit cast float number literal to integer if no fraction component" { |
| ... | ... | @@ -269,11 +269,11 @@ fn testCast128() { |
| 269 | 269 | } |
| 270 | 270 | |
| 271 | 271 | fn cast128Int(x: f128) -> u128 { |
| 272 | @bitCast(u128, x) | |
| 272 | return @bitCast(u128, x); | |
| 273 | 273 | } |
| 274 | 274 | |
| 275 | 275 | fn cast128Float(x: u128) -> f128 { |
| 276 | @bitCast(f128, x) | |
| 276 | return @bitCast(f128, x); | |
| 277 | 277 | } |
| 278 | 278 | |
| 279 | 279 | test "const slice widen cast" { |
test/cases/defer.zig+6-6| ... | ... | @@ -7,9 +7,9 @@ error FalseNotAllowed; |
| 7 | 7 | |
| 8 | 8 | fn runSomeErrorDefers(x: bool) -> %bool { |
| 9 | 9 | index = 0; |
| 10 | defer {result[index] = 'a'; index += 1;}; | |
| 11 | %defer {result[index] = 'b'; index += 1;}; | |
| 12 | defer {result[index] = 'c'; index += 1;}; | |
| 10 | defer {result[index] = 'a'; index += 1;} | |
| 11 | %defer {result[index] = 'b'; index += 1;} | |
| 12 | defer {result[index] = 'c'; index += 1;} | |
| 13 | 13 | return if (x) x else error.FalseNotAllowed; |
| 14 | 14 | } |
| 15 | 15 | |
| ... | ... | @@ -18,9 +18,9 @@ test "mixing normal and error defers" { |
| 18 | 18 | assert(result[0] == 'c'); |
| 19 | 19 | assert(result[1] == 'a'); |
| 20 | 20 | |
| 21 | const ok = runSomeErrorDefers(false) %% |err| { | |
| 21 | const ok = runSomeErrorDefers(false) %% |err| x: { | |
| 22 | 22 | assert(err == error.FalseNotAllowed); |
| 23 | true | |
| 23 | break :x true; | |
| 24 | 24 | }; |
| 25 | 25 | assert(ok); |
| 26 | 26 | assert(result[0] == 'c'); |
| ... | ... | @@ -41,5 +41,5 @@ fn testBreakContInDefer(x: usize) { |
| 41 | 41 | if (i == 5) break; |
| 42 | 42 | } |
| 43 | 43 | assert(i == 5); |
| 44 | }; | |
| 44 | } | |
| 45 | 45 | } |
test/cases/enum.zig+1-1| ... | ... | @@ -41,7 +41,7 @@ const Bar = enum { |
| 41 | 41 | }; |
| 42 | 42 | |
| 43 | 43 | fn returnAnInt(x: i32) -> Foo { |
| 44 | Foo { .One = x } | |
| 44 | return Foo { .One = x }; | |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 |
test/cases/enum_with_members.zig+3-3| ... | ... | @@ -8,9 +8,9 @@ const ET = union(enum) { |
| 8 | 8 | |
| 9 | 9 | pub fn print(a: &const ET, buf: []u8) -> %usize { |
| 10 | 10 | return switch (*a) { |
| 11 | ET.SINT => |x| { fmt.formatIntBuf(buf, x, 10, false, 0) }, | |
| 12 | ET.UINT => |x| { fmt.formatIntBuf(buf, x, 10, false, 0) }, | |
| 13 | } | |
| 11 | ET.SINT => |x| fmt.formatIntBuf(buf, x, 10, false, 0), | |
| 12 | ET.UINT => |x| fmt.formatIntBuf(buf, x, 10, false, 0), | |
| 13 | }; | |
| 14 | 14 | } |
| 15 | 15 | }; |
| 16 | 16 |
test/cases/error.zig+5-9| ... | ... | @@ -3,7 +3,7 @@ const mem = @import("std").mem; |
| 3 | 3 | |
| 4 | 4 | pub fn foo() -> %i32 { |
| 5 | 5 | const x = %return bar(); |
| 6 | return x + 1 | |
| 6 | return x + 1; | |
| 7 | 7 | } |
| 8 | 8 | |
| 9 | 9 | pub fn bar() -> %i32 { |
| ... | ... | @@ -21,7 +21,7 @@ test "error wrapping" { |
| 21 | 21 | |
| 22 | 22 | error ItBroke; |
| 23 | 23 | fn gimmeItBroke() -> []const u8 { |
| 24 | @errorName(error.ItBroke) | |
| 24 | return @errorName(error.ItBroke); | |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | test "@errorName" { |
| ... | ... | @@ -48,7 +48,7 @@ error AnError; |
| 48 | 48 | error AnError; |
| 49 | 49 | error SecondError; |
| 50 | 50 | fn shouldBeNotEqual(a: error, b: error) { |
| 51 | if (a == b) unreachable | |
| 51 | if (a == b) unreachable; | |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | |
| ... | ... | @@ -60,11 +60,7 @@ test "error binary operator" { |
| 60 | 60 | } |
| 61 | 61 | error ItBroke; |
| 62 | 62 | fn errBinaryOperatorG(x: bool) -> %isize { |
| 63 | if (x) { | |
| 64 | error.ItBroke | |
| 65 | } else { | |
| 66 | isize(10) | |
| 67 | } | |
| 63 | return if (x) error.ItBroke else isize(10); | |
| 68 | 64 | } |
| 69 | 65 | |
| 70 | 66 | |
| ... | ... | @@ -72,7 +68,7 @@ test "unwrap simple value from error" { |
| 72 | 68 | const i = %%unwrapSimpleValueFromErrorDo(); |
| 73 | 69 | assert(i == 13); |
| 74 | 70 | } |
| 75 | fn unwrapSimpleValueFromErrorDo() -> %isize { 13 } | |
| 71 | fn unwrapSimpleValueFromErrorDo() -> %isize { return 13; } | |
| 76 | 72 | |
| 77 | 73 | |
| 78 | 74 | test "error return in assignment" { |
test/cases/eval.zig+13-13| ... | ... | @@ -44,7 +44,7 @@ test "static function evaluation" { |
| 44 | 44 | assert(statically_added_number == 3); |
| 45 | 45 | } |
| 46 | 46 | const statically_added_number = staticAdd(1, 2); |
| 47 | fn staticAdd(a: i32, b: i32) -> i32 { a + b } | |
| 47 | fn staticAdd(a: i32, b: i32) -> i32 { return a + b; } | |
| 48 | 48 | |
| 49 | 49 | |
| 50 | 50 | test "const expr eval on single expr blocks" { |
| ... | ... | @@ -54,10 +54,10 @@ test "const expr eval on single expr blocks" { |
| 54 | 54 | fn constExprEvalOnSingleExprBlocksFn(x: i32, b: bool) -> i32 { |
| 55 | 55 | const literal = 3; |
| 56 | 56 | |
| 57 | const result = if (b) { | |
| 58 | literal | |
| 59 | } else { | |
| 60 | x | |
| 57 | const result = if (b) b: { | |
| 58 | break :b literal; | |
| 59 | } else b: { | |
| 60 | break :b x; | |
| 61 | 61 | }; |
| 62 | 62 | |
| 63 | 63 | return result; |
| ... | ... | @@ -94,9 +94,9 @@ pub const Vec3 = struct { |
| 94 | 94 | data: [3]f32, |
| 95 | 95 | }; |
| 96 | 96 | pub fn vec3(x: f32, y: f32, z: f32) -> Vec3 { |
| 97 | Vec3 { | |
| 97 | return Vec3 { | |
| 98 | 98 | .data = []f32 { x, y, z, }, |
| 99 | } | |
| 99 | }; | |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | 102 | |
| ... | ... | @@ -176,7 +176,7 @@ fn max(comptime T: type, a: T, b: T) -> T { |
| 176 | 176 | } |
| 177 | 177 | } |
| 178 | 178 | fn letsTryToCompareBools(a: bool, b: bool) -> bool { |
| 179 | max(bool, a, b) | |
| 179 | return max(bool, a, b); | |
| 180 | 180 | } |
| 181 | 181 | test "inlined block and runtime block phi" { |
| 182 | 182 | assert(letsTryToCompareBools(true, true)); |
| ... | ... | @@ -202,9 +202,9 @@ const cmd_fns = []CmdFn{ |
| 202 | 202 | CmdFn {.name = "two", .func = two}, |
| 203 | 203 | CmdFn {.name = "three", .func = three}, |
| 204 | 204 | }; |
| 205 | fn one(value: i32) -> i32 { value + 1 } | |
| 206 | fn two(value: i32) -> i32 { value + 2 } | |
| 207 | fn three(value: i32) -> i32 { value + 3 } | |
| 205 | fn one(value: i32) -> i32 { return value + 1; } | |
| 206 | fn two(value: i32) -> i32 { return value + 2; } | |
| 207 | fn three(value: i32) -> i32 { return value + 3; } | |
| 208 | 208 | |
| 209 | 209 | fn performFn(comptime prefix_char: u8, start_value: i32) -> i32 { |
| 210 | 210 | var result: i32 = start_value; |
| ... | ... | @@ -317,12 +317,12 @@ test "create global array with for loop" { |
| 317 | 317 | assert(global_array[9] == 9 * 9); |
| 318 | 318 | } |
| 319 | 319 | |
| 320 | const global_array = { | |
| 320 | const global_array = x: { | |
| 321 | 321 | var result: [10]usize = undefined; |
| 322 | 322 | for (result) |*item, index| { |
| 323 | 323 | *item = index * index; |
| 324 | 324 | } |
| 325 | result | |
| 325 | break :x result; | |
| 326 | 326 | }; |
| 327 | 327 | |
| 328 | 328 | test "compile-time downcast when the bits fit" { |
test/cases/fn.zig+10-10| ... | ... | @@ -4,7 +4,7 @@ test "params" { |
| 4 | 4 | assert(testParamsAdd(22, 11) == 33); |
| 5 | 5 | } |
| 6 | 6 | fn testParamsAdd(a: i32, b: i32) -> i32 { |
| 7 | a + b | |
| 7 | return a + b; | |
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | |
| ... | ... | @@ -22,7 +22,7 @@ test "void parameters" { |
| 22 | 22 | } |
| 23 | 23 | fn voidFun(a: i32, b: void, c: i32, d: void) { |
| 24 | 24 | const v = b; |
| 25 | const vv: void = if (a == 1) {v} else {}; | |
| 25 | const vv: void = if (a == 1) v else {}; | |
| 26 | 26 | assert(a + c == 3); |
| 27 | 27 | return vv; |
| 28 | 28 | } |
| ... | ... | @@ -45,9 +45,9 @@ test "separate block scopes" { |
| 45 | 45 | assert(no_conflict == 5); |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | const c = { | |
| 48 | const c = x: { | |
| 49 | 49 | const no_conflict = i32(10); |
| 50 | no_conflict | |
| 50 | break :x no_conflict; | |
| 51 | 51 | }; |
| 52 | 52 | assert(c == 10); |
| 53 | 53 | } |
| ... | ... | @@ -73,7 +73,7 @@ test "implicit cast function unreachable return" { |
| 73 | 73 | fn wantsFnWithVoid(f: fn()) { } |
| 74 | 74 | |
| 75 | 75 | fn fnWithUnreachable() -> noreturn { |
| 76 | unreachable | |
| 76 | unreachable; | |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | |
| ... | ... | @@ -83,14 +83,14 @@ test "function pointers" { |
| 83 | 83 | assert(f() == u32(i) + 5); |
| 84 | 84 | } |
| 85 | 85 | } |
| 86 | fn fn1() -> u32 {5} | |
| 87 | fn fn2() -> u32 {6} | |
| 88 | fn fn3() -> u32 {7} | |
| 89 | fn fn4() -> u32 {8} | |
| 86 | fn fn1() -> u32 {return 5;} | |
| 87 | fn fn2() -> u32 {return 6;} | |
| 88 | fn fn3() -> u32 {return 7;} | |
| 89 | fn fn4() -> u32 {return 8;} | |
| 90 | 90 | |
| 91 | 91 | |
| 92 | 92 | test "inline function call" { |
| 93 | 93 | assert(@inlineCall(add, 3, 9) == 12); |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | fn add(a: i32, b: i32) -> i32 { a + b } | |
| 96 | fn add(a: i32, b: i32) -> i32 { return a + b; } |
test/cases/for.zig+1-1| ... | ... | @@ -12,7 +12,7 @@ test "continue in for loop" { |
| 12 | 12 | } |
| 13 | 13 | break; |
| 14 | 14 | } |
| 15 | if (sum != 6) unreachable | |
| 15 | if (sum != 6) unreachable; | |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | test "for loop with pointer elem var" { |
test/cases/generics.zig+19-19| ... | ... | @@ -11,7 +11,7 @@ fn max(comptime T: type, a: T, b: T) -> T { |
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | fn add(comptime a: i32, b: i32) -> i32 { |
| 14 | return (comptime {a}) + b; | |
| 14 | return (comptime a) + b; | |
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | const the_max = max(u32, 1234, 5678); |
| ... | ... | @@ -20,15 +20,15 @@ test "compile time generic eval" { |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | fn gimmeTheBigOne(a: u32, b: u32) -> u32 { |
| 23 | max(u32, a, b) | |
| 23 | return max(u32, a, b); | |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | fn shouldCallSameInstance(a: u32, b: u32) -> u32 { |
| 27 | max(u32, a, b) | |
| 27 | return max(u32, a, b); | |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | fn sameButWithFloats(a: f64, b: f64) -> f64 { |
| 31 | max(f64, a, b) | |
| 31 | return max(f64, a, b); | |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | test "fn with comptime args" { |
| ... | ... | @@ -49,28 +49,28 @@ comptime { |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | fn max_var(a: var, b: var) -> @typeOf(a + b) { |
| 52 | if (a > b) a else b | |
| 52 | return if (a > b) a else b; | |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | fn max_i32(a: i32, b: i32) -> i32 { |
| 56 | max_var(a, b) | |
| 56 | return max_var(a, b); | |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | fn max_f64(a: f64, b: f64) -> f64 { |
| 60 | max_var(a, b) | |
| 60 | return max_var(a, b); | |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | |
| 64 | 64 | pub fn List(comptime T: type) -> type { |
| 65 | SmallList(T, 8) | |
| 65 | return SmallList(T, 8); | |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | pub fn SmallList(comptime T: type, comptime STATIC_SIZE: usize) -> type { |
| 69 | struct { | |
| 69 | return struct { | |
| 70 | 70 | items: []T, |
| 71 | 71 | length: usize, |
| 72 | 72 | prealloc_items: [STATIC_SIZE]T, |
| 73 | } | |
| 73 | }; | |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | test "function with return type type" { |
| ... | ... | @@ -91,20 +91,20 @@ test "generic struct" { |
| 91 | 91 | assert(b1.getVal()); |
| 92 | 92 | } |
| 93 | 93 | fn GenNode(comptime T: type) -> type { |
| 94 | struct { | |
| 94 | return struct { | |
| 95 | 95 | value: T, |
| 96 | 96 | next: ?&GenNode(T), |
| 97 | fn getVal(n: &const GenNode(T)) -> T { n.value } | |
| 98 | } | |
| 97 | fn getVal(n: &const GenNode(T)) -> T { return n.value; } | |
| 98 | }; | |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | test "const decls in struct" { |
| 102 | 102 | assert(GenericDataThing(3).count_plus_one == 4); |
| 103 | 103 | } |
| 104 | 104 | fn GenericDataThing(comptime count: isize) -> type { |
| 105 | struct { | |
| 105 | return struct { | |
| 106 | 106 | const count_plus_one = count + 1; |
| 107 | } | |
| 107 | }; | |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | |
| ... | ... | @@ -120,16 +120,16 @@ test "generic fn with implicit cast" { |
| 120 | 120 | assert(getFirstByte(u8, []u8 {13}) == 13); |
| 121 | 121 | assert(getFirstByte(u16, []u16 {0, 13}) == 0); |
| 122 | 122 | } |
| 123 | fn getByte(ptr: ?&const u8) -> u8 {*??ptr} | |
| 123 | fn getByte(ptr: ?&const u8) -> u8 {return *??ptr;} | |
| 124 | 124 | fn getFirstByte(comptime T: type, mem: []const T) -> u8 { |
| 125 | getByte(@ptrCast(&const u8, &mem[0])) | |
| 125 | return getByte(@ptrCast(&const u8, &mem[0])); | |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | |
| 129 | 129 | const foos = []fn(var) -> bool { foo1, foo2 }; |
| 130 | 130 | |
| 131 | fn foo1(arg: var) -> bool { arg } | |
| 132 | fn foo2(arg: var) -> bool { !arg } | |
| 131 | fn foo1(arg: var) -> bool { return arg; } | |
| 132 | fn foo2(arg: var) -> bool { return !arg; } | |
| 133 | 133 | |
| 134 | 134 | test "array of generic fns" { |
| 135 | 135 | assert(foos[0](true)); |
test/cases/if.zig+3-3| ... | ... | @@ -29,10 +29,10 @@ test "else if expression" { |
| 29 | 29 | } |
| 30 | 30 | fn elseIfExpressionF(c: u8) -> u8 { |
| 31 | 31 | if (c == 0) { |
| 32 | 0 | |
| 32 | return 0; | |
| 33 | 33 | } else if (c == 1) { |
| 34 | 1 | |
| 34 | return 1; | |
| 35 | 35 | } else { |
| 36 | u8(2) | |
| 36 | return u8(2); | |
| 37 | 37 | } |
| 38 | 38 | } |
test/cases/import/a_namespace.zig+1-1| ... | ... | @@ -1 +1 @@ |
| 1 | pub fn foo() -> i32 { 1234 } | |
| 1 | pub fn foo() -> i32 { return 1234; } |
test/cases/ir_block_deps.zig+2-2| ... | ... | @@ -8,10 +8,10 @@ fn foo(id: u64) -> %i32 { |
| 8 | 8 | return %return getErrInt(); |
| 9 | 9 | }, |
| 10 | 10 | else => error.ItBroke, |
| 11 | } | |
| 11 | }; | |
| 12 | 12 | } |
| 13 | 13 | |
| 14 | fn getErrInt() -> %i32 { 0 } | |
| 14 | fn getErrInt() -> %i32 { return 0; } | |
| 15 | 15 | |
| 16 | 16 | error ItBroke; |
| 17 | 17 |
test/cases/math.zig+11-11| ... | ... | @@ -28,16 +28,16 @@ fn testDivision() { |
| 28 | 28 | assert(divTrunc(f32, -5.0, 3.0) == -1.0); |
| 29 | 29 | } |
| 30 | 30 | fn div(comptime T: type, a: T, b: T) -> T { |
| 31 | a / b | |
| 31 | return a / b; | |
| 32 | 32 | } |
| 33 | 33 | fn divExact(comptime T: type, a: T, b: T) -> T { |
| 34 | @divExact(a, b) | |
| 34 | return @divExact(a, b); | |
| 35 | 35 | } |
| 36 | 36 | fn divFloor(comptime T: type, a: T, b: T) -> T { |
| 37 | @divFloor(a, b) | |
| 37 | return @divFloor(a, b); | |
| 38 | 38 | } |
| 39 | 39 | fn divTrunc(comptime T: type, a: T, b: T) -> T { |
| 40 | @divTrunc(a, b) | |
| 40 | return @divTrunc(a, b); | |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | test "@addWithOverflow" { |
| ... | ... | @@ -71,7 +71,7 @@ fn testClz() { |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | fn clz(x: var) -> usize { |
| 74 | @clz(x) | |
| 74 | return @clz(x); | |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | test "@ctz" { |
| ... | ... | @@ -86,7 +86,7 @@ fn testCtz() { |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | fn ctz(x: var) -> usize { |
| 89 | @ctz(x) | |
| 89 | return @ctz(x); | |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | test "assignment operators" { |
| ... | ... | @@ -180,10 +180,10 @@ fn test_u64_div() { |
| 180 | 180 | assert(result.remainder == 100663296); |
| 181 | 181 | } |
| 182 | 182 | fn divWithResult(a: u64, b: u64) -> DivResult { |
| 183 | DivResult { | |
| 183 | return DivResult { | |
| 184 | 184 | .quotient = a / b, |
| 185 | 185 | .remainder = a % b, |
| 186 | } | |
| 186 | }; | |
| 187 | 187 | } |
| 188 | 188 | const DivResult = struct { |
| 189 | 189 | quotient: u64, |
| ... | ... | @@ -191,8 +191,8 @@ const DivResult = struct { |
| 191 | 191 | }; |
| 192 | 192 | |
| 193 | 193 | test "binary not" { |
| 194 | assert(comptime {~u16(0b1010101010101010) == 0b0101010101010101}); | |
| 195 | assert(comptime {~u64(2147483647) == 18446744071562067968}); | |
| 194 | assert(comptime x: {break :x ~u16(0b1010101010101010) == 0b0101010101010101;}); | |
| 195 | assert(comptime x: {break :x ~u64(2147483647) == 18446744071562067968;}); | |
| 196 | 196 | testBinaryNot(0b1010101010101010); |
| 197 | 197 | } |
| 198 | 198 | |
| ... | ... | @@ -331,7 +331,7 @@ test "f128" { |
| 331 | 331 | comptime test_f128(); |
| 332 | 332 | } |
| 333 | 333 | |
| 334 | fn make_f128(x: f128) -> f128 { x } | |
| 334 | fn make_f128(x: f128) -> f128 { return x; } | |
| 335 | 335 | |
| 336 | 336 | fn test_f128() { |
| 337 | 337 | assert(@sizeOf(f128) == 16); |
test/cases/misc.zig+15-15| ... | ... | @@ -110,17 +110,17 @@ fn testShortCircuit(f: bool, t: bool) { |
| 110 | 110 | var hit_3 = f; |
| 111 | 111 | var hit_4 = f; |
| 112 | 112 | |
| 113 | if (t or {assert(f); f}) { | |
| 113 | if (t or x: {assert(f); break :x f;}) { | |
| 114 | 114 | hit_1 = t; |
| 115 | 115 | } |
| 116 | if (f or { hit_2 = t; f }) { | |
| 116 | if (f or x: { hit_2 = t; break :x f; }) { | |
| 117 | 117 | assert(f); |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | if (t and { hit_3 = t; f }) { | |
| 120 | if (t and x: { hit_3 = t; break :x f; }) { | |
| 121 | 121 | assert(f); |
| 122 | 122 | } |
| 123 | if (f and {assert(f); f}) { | |
| 123 | if (f and x: {assert(f); break :x f;}) { | |
| 124 | 124 | assert(f); |
| 125 | 125 | } else { |
| 126 | 126 | hit_4 = t; |
| ... | ... | @@ -135,11 +135,11 @@ test "truncate" { |
| 135 | 135 | assert(testTruncate(0x10fd) == 0xfd); |
| 136 | 136 | } |
| 137 | 137 | fn testTruncate(x: u32) -> u8 { |
| 138 | @truncate(u8, x) | |
| 138 | return @truncate(u8, x); | |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | 141 | fn first4KeysOfHomeRow() -> []const u8 { |
| 142 | "aoeu" | |
| 142 | return "aoeu"; | |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | test "return string from function" { |
| ... | ... | @@ -167,7 +167,7 @@ test "memcpy and memset intrinsics" { |
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | test "builtin static eval" { |
| 170 | const x : i32 = comptime {1 + 2 + 3}; | |
| 170 | const x : i32 = comptime x: {break :x 1 + 2 + 3;}; | |
| 171 | 171 | assert(x == comptime 6); |
| 172 | 172 | } |
| 173 | 173 | |
| ... | ... | @@ -190,7 +190,7 @@ test "slicing" { |
| 190 | 190 | |
| 191 | 191 | test "constant equal function pointers" { |
| 192 | 192 | const alias = emptyFn; |
| 193 | assert(comptime {emptyFn == alias}); | |
| 193 | assert(comptime x: {break :x emptyFn == alias;}); | |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | fn emptyFn() {} |
| ... | ... | @@ -280,14 +280,14 @@ test "cast small unsigned to larger signed" { |
| 280 | 280 | assert(castSmallUnsignedToLargerSigned1(200) == i16(200)); |
| 281 | 281 | assert(castSmallUnsignedToLargerSigned2(9999) == i64(9999)); |
| 282 | 282 | } |
| 283 | fn castSmallUnsignedToLargerSigned1(x: u8) -> i16 { x } | |
| 284 | fn castSmallUnsignedToLargerSigned2(x: u16) -> i64 { x } | |
| 283 | fn castSmallUnsignedToLargerSigned1(x: u8) -> i16 { return x; } | |
| 284 | fn castSmallUnsignedToLargerSigned2(x: u16) -> i64 { return x; } | |
| 285 | 285 | |
| 286 | 286 | |
| 287 | 287 | test "implicit cast after unreachable" { |
| 288 | 288 | assert(outer() == 1234); |
| 289 | 289 | } |
| 290 | fn inner() -> i32 { 1234 } | |
| 290 | fn inner() -> i32 { return 1234; } | |
| 291 | 291 | fn outer() -> i64 { |
| 292 | 292 | return inner(); |
| 293 | 293 | } |
| ... | ... | @@ -310,8 +310,8 @@ test "call result of if else expression" { |
| 310 | 310 | fn f2(x: bool) -> []const u8 { |
| 311 | 311 | return (if (x) fA else fB)(); |
| 312 | 312 | } |
| 313 | fn fA() -> []const u8 { "a" } | |
| 314 | fn fB() -> []const u8 { "b" } | |
| 313 | fn fA() -> []const u8 { return "a"; } | |
| 314 | fn fB() -> []const u8 { return "b"; } | |
| 315 | 315 | |
| 316 | 316 | |
| 317 | 317 | test "const expression eval handling of variables" { |
| ... | ... | @@ -379,7 +379,7 @@ test "pointer comparison" { |
| 379 | 379 | assert(ptrEql(b, b)); |
| 380 | 380 | } |
| 381 | 381 | fn ptrEql(a: &const []const u8, b: &const []const u8) -> bool { |
| 382 | a == b | |
| 382 | return a == b; | |
| 383 | 383 | } |
| 384 | 384 | |
| 385 | 385 | |
| ... | ... | @@ -483,7 +483,7 @@ test "@typeId" { |
| 483 | 483 | assert(@typeId(AUnion) == Tid.Union); |
| 484 | 484 | assert(@typeId(fn()) == Tid.Fn); |
| 485 | 485 | assert(@typeId(@typeOf(builtin)) == Tid.Namespace); |
| 486 | assert(@typeId(@typeOf({this})) == Tid.Block); | |
| 486 | assert(@typeId(@typeOf(x: {break :x this;})) == Tid.Block); | |
| 487 | 487 | // TODO bound fn |
| 488 | 488 | // TODO arg tuple |
| 489 | 489 | // TODO opaque |
test/cases/reflection.zig+1-1| ... | ... | @@ -22,7 +22,7 @@ test "reflection: function return type, var args, and param types" { |
| 22 | 22 | } |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | fn dummy(a: bool, b: i32, c: f32) -> i32 { 1234 } | |
| 25 | fn dummy(a: bool, b: i32, c: f32) -> i32 { return 1234; } | |
| 26 | 26 | fn dummy_varargs(args: ...) {} |
| 27 | 27 | |
| 28 | 28 | test "reflection: struct member types and names" { |
test/cases/struct.zig+9-9| ... | ... | @@ -2,7 +2,7 @@ const assert = @import("std").debug.assert; |
| 2 | 2 | const builtin = @import("builtin"); |
| 3 | 3 | |
| 4 | 4 | const StructWithNoFields = struct { |
| 5 | fn add(a: i32, b: i32) -> i32 { a + b } | |
| 5 | fn add(a: i32, b: i32) -> i32 { return a + b; } | |
| 6 | 6 | }; |
| 7 | 7 | const empty_global_instance = StructWithNoFields {}; |
| 8 | 8 | |
| ... | ... | @@ -109,7 +109,7 @@ const Foo = struct { |
| 109 | 109 | ptr: fn() -> i32, |
| 110 | 110 | }; |
| 111 | 111 | |
| 112 | fn aFunc() -> i32 { 13 } | |
| 112 | fn aFunc() -> i32 { return 13; } | |
| 113 | 113 | |
| 114 | 114 | fn callStructField(foo: &const Foo) -> i32 { |
| 115 | 115 | return foo.ptr(); |
| ... | ... | @@ -124,7 +124,7 @@ test "store member function in variable" { |
| 124 | 124 | } |
| 125 | 125 | const MemberFnTestFoo = struct { |
| 126 | 126 | x: i32, |
| 127 | fn member(foo: &const MemberFnTestFoo) -> i32 { foo.x } | |
| 127 | fn member(foo: &const MemberFnTestFoo) -> i32 { return foo.x; } | |
| 128 | 128 | }; |
| 129 | 129 | |
| 130 | 130 | |
| ... | ... | @@ -141,7 +141,7 @@ test "member functions" { |
| 141 | 141 | const MemberFnRand = struct { |
| 142 | 142 | seed: u32, |
| 143 | 143 | pub fn getSeed(r: &const MemberFnRand) -> u32 { |
| 144 | r.seed | |
| 144 | return r.seed; | |
| 145 | 145 | } |
| 146 | 146 | }; |
| 147 | 147 | |
| ... | ... | @@ -154,10 +154,10 @@ const Bar = struct { |
| 154 | 154 | y: i32, |
| 155 | 155 | }; |
| 156 | 156 | fn makeBar(x: i32, y: i32) -> Bar { |
| 157 | Bar { | |
| 157 | return Bar { | |
| 158 | 158 | .x = x, |
| 159 | 159 | .y = y, |
| 160 | } | |
| 160 | }; | |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | test "empty struct method call" { |
| ... | ... | @@ -166,7 +166,7 @@ test "empty struct method call" { |
| 166 | 166 | } |
| 167 | 167 | const EmptyStruct = struct { |
| 168 | 168 | fn method(es: &const EmptyStruct) -> i32 { |
| 169 | 1234 | |
| 169 | return 1234; | |
| 170 | 170 | } |
| 171 | 171 | }; |
| 172 | 172 | |
| ... | ... | @@ -176,14 +176,14 @@ test "return empty struct from fn" { |
| 176 | 176 | } |
| 177 | 177 | const EmptyStruct2 = struct {}; |
| 178 | 178 | fn testReturnEmptyStructFromFn() -> EmptyStruct2 { |
| 179 | EmptyStruct2 {} | |
| 179 | return EmptyStruct2 {}; | |
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | test "pass slice of empty struct to fn" { |
| 183 | 183 | assert(testPassSliceOfEmptyStructToFn([]EmptyStruct2{ EmptyStruct2{} }) == 1); |
| 184 | 184 | } |
| 185 | 185 | fn testPassSliceOfEmptyStructToFn(slice: []const EmptyStruct2) -> usize { |
| 186 | slice.len | |
| 186 | return slice.len; | |
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | const APackedStruct = packed struct { |
test/cases/switch.zig+9-9| ... | ... | @@ -21,12 +21,12 @@ test "switch with all ranges" { |
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | fn testSwitchWithAllRanges(x: u32, y: u32) -> u32 { |
| 24 | switch (x) { | |
| 24 | return switch (x) { | |
| 25 | 25 | 0 ... 100 => 1, |
| 26 | 26 | 101 ... 200 => 2, |
| 27 | 27 | 201 ... 300 => 3, |
| 28 | 28 | else => y, |
| 29 | } | |
| 29 | }; | |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | test "implicit comptime switch" { |
| ... | ... | @@ -132,7 +132,7 @@ test "switch with multiple expressions" { |
| 132 | 132 | assert(x == 2); |
| 133 | 133 | } |
| 134 | 134 | fn returnsFive() -> i32 { |
| 135 | 5 | |
| 135 | return 5; | |
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | |
| ... | ... | @@ -161,10 +161,10 @@ test "switch on type" { |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | fn trueIfBoolFalseOtherwise(comptime T: type) -> bool { |
| 164 | switch (T) { | |
| 164 | return switch (T) { | |
| 165 | 165 | bool => true, |
| 166 | 166 | else => false, |
| 167 | } | |
| 167 | }; | |
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | test "switch handles all cases of number" { |
| ... | ... | @@ -186,22 +186,22 @@ fn testSwitchHandleAllCases() { |
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | fn testSwitchHandleAllCasesExhaustive(x: u2) -> u2 { |
| 189 | switch (x) { | |
| 189 | return switch (x) { | |
| 190 | 190 | 0 => u2(3), |
| 191 | 191 | 1 => 2, |
| 192 | 192 | 2 => 1, |
| 193 | 193 | 3 => 0, |
| 194 | } | |
| 194 | }; | |
| 195 | 195 | } |
| 196 | 196 | |
| 197 | 197 | fn testSwitchHandleAllCasesRange(x: u8) -> u8 { |
| 198 | switch (x) { | |
| 198 | return switch (x) { | |
| 199 | 199 | 0 ... 100 => u8(0), |
| 200 | 200 | 101 ... 200 => 1, |
| 201 | 201 | 201, 203 => 2, |
| 202 | 202 | 202 => 4, |
| 203 | 203 | 204 ... 255 => 3, |
| 204 | } | |
| 204 | }; | |
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | test "switch all prongs unreachable" { |
test/cases/switch_prong_err_enum.zig+1-1| ... | ... | @@ -18,7 +18,7 @@ fn doThing(form_id: u64) -> %FormValue { |
| 18 | 18 | return switch (form_id) { |
| 19 | 19 | 17 => FormValue { .Address = %return readOnce() }, |
| 20 | 20 | else => error.InvalidDebugInfo, |
| 21 | } | |
| 21 | }; | |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | test "switch prong returns error enum" { |
test/cases/switch_prong_implicit_cast.zig+2-2| ... | ... | @@ -8,11 +8,11 @@ const FormValue = union(enum) { |
| 8 | 8 | error Whatever; |
| 9 | 9 | |
| 10 | 10 | fn foo(id: u64) -> %FormValue { |
| 11 | switch (id) { | |
| 11 | return switch (id) { | |
| 12 | 12 | 2 => FormValue { .Two = true }, |
| 13 | 13 | 1 => FormValue { .One = {} }, |
| 14 | 14 | else => return error.Whatever, |
| 15 | } | |
| 15 | }; | |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | test "switch prong implicit cast" { |
test/cases/this.zig+4-8| ... | ... | @@ -3,7 +3,7 @@ const assert = @import("std").debug.assert; |
| 3 | 3 | const module = this; |
| 4 | 4 | |
| 5 | 5 | fn Point(comptime T: type) -> type { |
| 6 | struct { | |
| 6 | return struct { | |
| 7 | 7 | const Self = this; |
| 8 | 8 | x: T, |
| 9 | 9 | y: T, |
| ... | ... | @@ -12,20 +12,16 @@ fn Point(comptime T: type) -> type { |
| 12 | 12 | self.x += 1; |
| 13 | 13 | self.y += 1; |
| 14 | 14 | } |
| 15 | } | |
| 15 | }; | |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | fn add(x: i32, y: i32) -> i32 { |
| 19 | x + y | |
| 19 | return x + y; | |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | fn factorial(x: i32) -> i32 { |
| 23 | 23 | const selfFn = this; |
| 24 | if (x == 0) { | |
| 25 | 1 | |
| 26 | } else { | |
| 27 | x * selfFn(x - 1) | |
| 28 | } | |
| 24 | return if (x == 0) 1 else x * selfFn(x - 1); | |
| 29 | 25 | } |
| 30 | 26 | |
| 31 | 27 | test "this refer to module call private fn" { |
test/cases/try.zig+5-13| ... | ... | @@ -7,9 +7,9 @@ test "try on error union" { |
| 7 | 7 | } |
| 8 | 8 | |
| 9 | 9 | fn tryOnErrorUnionImpl() { |
| 10 | const x = if (returnsTen()) |val| { | |
| 10 | const x = if (returnsTen()) |val| | |
| 11 | 11 | val + 1 |
| 12 | } else |err| switch (err) { | |
| 12 | else |err| switch (err) { | |
| 13 | 13 | error.ItBroke, error.NoMem => 1, |
| 14 | 14 | error.CrappedOut => i32(2), |
| 15 | 15 | else => unreachable, |
| ... | ... | @@ -21,22 +21,14 @@ error ItBroke; |
| 21 | 21 | error NoMem; |
| 22 | 22 | error CrappedOut; |
| 23 | 23 | fn returnsTen() -> %i32 { |
| 24 | 10 | |
| 24 | return 10; | |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | test "try without vars" { |
| 28 | const result1 = if (failIfTrue(true)) { | |
| 29 | 1 | |
| 30 | } else |_| { | |
| 31 | i32(2) | |
| 32 | }; | |
| 28 | const result1 = if (failIfTrue(true)) 1 else |_| i32(2); | |
| 33 | 29 | assert(result1 == 2); |
| 34 | 30 | |
| 35 | const result2 = if (failIfTrue(false)) { | |
| 36 | 1 | |
| 37 | } else |_| { | |
| 38 | i32(2) | |
| 39 | }; | |
| 31 | const result2 = if (failIfTrue(false)) 1 else |_| i32(2); | |
| 40 | 32 | assert(result2 == 1); |
| 41 | 33 | } |
| 42 | 34 |
test/cases/var_args.zig+2-2| ... | ... | @@ -58,8 +58,8 @@ fn extraFn(extra: u32, args: ...) -> usize { |
| 58 | 58 | |
| 59 | 59 | const foos = []fn(...) -> bool { foo1, foo2 }; |
| 60 | 60 | |
| 61 | fn foo1(args: ...) -> bool { true } | |
| 62 | fn foo2(args: ...) -> bool { false } | |
| 61 | fn foo1(args: ...) -> bool { return true; } | |
| 62 | fn foo2(args: ...) -> bool { return false; } | |
| 63 | 63 | |
| 64 | 64 | test "array of var args functions" { |
| 65 | 65 | assert(foos[0]()); |
test/cases/while.zig+18-30| ... | ... | @@ -118,73 +118,61 @@ test "while with error union condition" { |
| 118 | 118 | var numbers_left: i32 = undefined; |
| 119 | 119 | error OutOfNumbers; |
| 120 | 120 | fn getNumberOrErr() -> %i32 { |
| 121 | return if (numbers_left == 0) { | |
| 121 | return if (numbers_left == 0) | |
| 122 | 122 | error.OutOfNumbers |
| 123 | } else { | |
| 123 | else x: { | |
| 124 | 124 | numbers_left -= 1; |
| 125 | numbers_left | |
| 125 | break :x numbers_left; | |
| 126 | 126 | }; |
| 127 | 127 | } |
| 128 | 128 | fn getNumberOrNull() -> ?i32 { |
| 129 | return if (numbers_left == 0) { | |
| 129 | return if (numbers_left == 0) | |
| 130 | 130 | null |
| 131 | } else { | |
| 131 | else x: { | |
| 132 | 132 | numbers_left -= 1; |
| 133 | numbers_left | |
| 133 | break :x numbers_left; | |
| 134 | 134 | }; |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | test "while on nullable with else result follow else prong" { |
| 138 | 138 | const result = while (returnNull()) |value| { |
| 139 | 139 | break value; |
| 140 | } else { | |
| 141 | i32(2) | |
| 142 | }; | |
| 140 | } else i32(2); | |
| 143 | 141 | assert(result == 2); |
| 144 | 142 | } |
| 145 | 143 | |
| 146 | 144 | test "while on nullable with else result follow break prong" { |
| 147 | 145 | const result = while (returnMaybe(10)) |value| { |
| 148 | 146 | break value; |
| 149 | } else { | |
| 150 | i32(2) | |
| 151 | }; | |
| 147 | } else i32(2); | |
| 152 | 148 | assert(result == 10); |
| 153 | 149 | } |
| 154 | 150 | |
| 155 | 151 | test "while on error union with else result follow else prong" { |
| 156 | 152 | const result = while (returnError()) |value| { |
| 157 | 153 | break value; |
| 158 | } else |err| { | |
| 159 | i32(2) | |
| 160 | }; | |
| 154 | } else |err| i32(2); | |
| 161 | 155 | assert(result == 2); |
| 162 | 156 | } |
| 163 | 157 | |
| 164 | 158 | test "while on error union with else result follow break prong" { |
| 165 | 159 | const result = while (returnSuccess(10)) |value| { |
| 166 | 160 | break value; |
| 167 | } else |err| { | |
| 168 | i32(2) | |
| 169 | }; | |
| 161 | } else |err| i32(2); | |
| 170 | 162 | assert(result == 10); |
| 171 | 163 | } |
| 172 | 164 | |
| 173 | 165 | test "while on bool with else result follow else prong" { |
| 174 | 166 | const result = while (returnFalse()) { |
| 175 | 167 | break i32(10); |
| 176 | } else { | |
| 177 | i32(2) | |
| 178 | }; | |
| 168 | } else i32(2); | |
| 179 | 169 | assert(result == 2); |
| 180 | 170 | } |
| 181 | 171 | |
| 182 | 172 | test "while on bool with else result follow break prong" { |
| 183 | 173 | const result = while (returnTrue()) { |
| 184 | 174 | break i32(10); |
| 185 | } else { | |
| 186 | i32(2) | |
| 187 | }; | |
| 175 | } else i32(2); | |
| 188 | 176 | assert(result == 10); |
| 189 | 177 | } |
| 190 | 178 | |
| ... | ... | @@ -215,10 +203,10 @@ fn testContinueOuter() { |
| 215 | 203 | } |
| 216 | 204 | } |
| 217 | 205 | |
| 218 | fn returnNull() -> ?i32 { null } | |
| 219 | fn returnMaybe(x: i32) -> ?i32 { x } | |
| 206 | fn returnNull() -> ?i32 { return null; } | |
| 207 | fn returnMaybe(x: i32) -> ?i32 { return x; } | |
| 220 | 208 | error YouWantedAnError; |
| 221 | fn returnError() -> %i32 { error.YouWantedAnError } | |
| 222 | fn returnSuccess(x: i32) -> %i32 { x } | |
| 223 | fn returnFalse() -> bool { false } | |
| 224 | fn returnTrue() -> bool { true } | |
| 209 | fn returnError() -> %i32 { return error.YouWantedAnError; } | |
| 210 | fn returnSuccess(x: i32) -> %i32 { return x; } | |
| 211 | fn returnFalse() -> bool { return false; } | |
| 212 | fn returnTrue() -> bool { return true; } |
test/compare_output.zig+17-17| ... | ... | @@ -10,7 +10,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 10 | 10 | \\} |
| 11 | 11 | , "Hello, world!" ++ os.line_sep); |
| 12 | 12 | |
| 13 | cases.addCase({ | |
| 13 | cases.addCase(x: { | |
| 14 | 14 | var tc = cases.create("multiple files with private function", |
| 15 | 15 | \\use @import("std").io; |
| 16 | 16 | \\use @import("foo.zig"); |
| ... | ... | @@ -41,10 +41,10 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 41 | 41 | \\} |
| 42 | 42 | ); |
| 43 | 43 | |
| 44 | tc | |
| 44 | break :x tc; | |
| 45 | 45 | }); |
| 46 | 46 | |
| 47 | cases.addCase({ | |
| 47 | cases.addCase(x: { | |
| 48 | 48 | var tc = cases.create("import segregation", |
| 49 | 49 | \\use @import("foo.zig"); |
| 50 | 50 | \\use @import("bar.zig"); |
| ... | ... | @@ -82,10 +82,10 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 82 | 82 | \\} |
| 83 | 83 | ); |
| 84 | 84 | |
| 85 | tc | |
| 85 | break :x tc; | |
| 86 | 86 | }); |
| 87 | 87 | |
| 88 | cases.addCase({ | |
| 88 | cases.addCase(x: { | |
| 89 | 89 | var tc = cases.create("two files use import each other", |
| 90 | 90 | \\use @import("a.zig"); |
| 91 | 91 | \\ |
| ... | ... | @@ -112,7 +112,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 112 | 112 | \\pub const b_text = a_text; |
| 113 | 113 | ); |
| 114 | 114 | |
| 115 | tc | |
| 115 | break :x tc; | |
| 116 | 116 | }); |
| 117 | 117 | |
| 118 | 118 | cases.add("hello world without libc", |
| ... | ... | @@ -286,11 +286,11 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 286 | 286 | \\ const a_int = @ptrCast(&align(1) i32, a ?? unreachable); |
| 287 | 287 | \\ const b_int = @ptrCast(&align(1) i32, b ?? unreachable); |
| 288 | 288 | \\ if (*a_int < *b_int) { |
| 289 | \\ -1 | |
| 289 | \\ return -1; | |
| 290 | 290 | \\ } else if (*a_int > *b_int) { |
| 291 | \\ 1 | |
| 291 | \\ return 1; | |
| 292 | 292 | \\ } else { |
| 293 | \\ c_int(0) | |
| 293 | \\ return 0; | |
| 294 | 294 | \\ } |
| 295 | 295 | \\} |
| 296 | 296 | \\ |
| ... | ... | @@ -342,13 +342,13 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 342 | 342 | \\const Foo = struct { |
| 343 | 343 | \\ field1: Bar, |
| 344 | 344 | \\ |
| 345 | \\ fn method(a: &const Foo) -> bool { true } | |
| 345 | \\ fn method(a: &const Foo) -> bool { return true; } | |
| 346 | 346 | \\}; |
| 347 | 347 | \\ |
| 348 | 348 | \\const Bar = struct { |
| 349 | 349 | \\ field2: i32, |
| 350 | 350 | \\ |
| 351 | \\ fn method(b: &const Bar) -> bool { true } | |
| 351 | \\ fn method(b: &const Bar) -> bool { return true; } | |
| 352 | 352 | \\}; |
| 353 | 353 | \\ |
| 354 | 354 | \\pub fn main() -> %void { |
| ... | ... | @@ -429,7 +429,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 429 | 429 | \\fn its_gonna_pass() -> %void { } |
| 430 | 430 | , "before\nafter\ndefer3\ndefer1\n"); |
| 431 | 431 | |
| 432 | cases.addCase({ | |
| 432 | cases.addCase(x: { | |
| 433 | 433 | var tc = cases.create("@embedFile", |
| 434 | 434 | \\const foo_txt = @embedFile("foo.txt"); |
| 435 | 435 | \\const io = @import("std").io; |
| ... | ... | @@ -442,10 +442,10 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 442 | 442 | |
| 443 | 443 | tc.addSourceFile("foo.txt", "1234\nabcd\n"); |
| 444 | 444 | |
| 445 | tc | |
| 445 | break :x tc; | |
| 446 | 446 | }); |
| 447 | 447 | |
| 448 | cases.addCase({ | |
| 448 | cases.addCase(x: { | |
| 449 | 449 | var tc = cases.create("parsing args", |
| 450 | 450 | \\const std = @import("std"); |
| 451 | 451 | \\const io = std.io; |
| ... | ... | @@ -483,10 +483,10 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 483 | 483 | "last arg", |
| 484 | 484 | }); |
| 485 | 485 | |
| 486 | tc | |
| 486 | break :x tc; | |
| 487 | 487 | }); |
| 488 | 488 | |
| 489 | cases.addCase({ | |
| 489 | cases.addCase(x: { | |
| 490 | 490 | var tc = cases.create("parsing args new API", |
| 491 | 491 | \\const std = @import("std"); |
| 492 | 492 | \\const io = std.io; |
| ... | ... | @@ -524,6 +524,6 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 524 | 524 | "last arg", |
| 525 | 525 | }); |
| 526 | 526 | |
| 527 | tc | |
| 527 | break :x tc; | |
| 528 | 528 | }); |
| 529 | 529 | } |
test/compile_errors.zig+179-178| ... | ... | @@ -9,7 +9,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 9 | 9 | \\ } |
| 10 | 10 | \\ } |
| 11 | 11 | \\} |
| 12 | , ".tmp_source.zig:4:13: error: labeled loop not found: 'outer'"); | |
| 12 | , ".tmp_source.zig:4:13: error: label not found: 'outer'"); | |
| 13 | 13 | |
| 14 | 14 | cases.add("labeled continue not found", |
| 15 | 15 | \\export fn entry() { |
| ... | ... | @@ -39,7 +39,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 39 | 39 | \\ ({}) |
| 40 | 40 | \\ var bad = {}; |
| 41 | 41 | \\} |
| 42 | , ".tmp_source.zig:5:5: error: invalid token: 'var'"); | |
| 42 | , ".tmp_source.zig:5:5: error: expected token ';', found 'var'"); | |
| 43 | 43 | |
| 44 | 44 | cases.add("implicit semicolon - block expr", |
| 45 | 45 | \\export fn entry() { |
| ... | ... | @@ -48,7 +48,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 48 | 48 | \\ _ = {} |
| 49 | 49 | \\ var bad = {}; |
| 50 | 50 | \\} |
| 51 | , ".tmp_source.zig:5:5: error: invalid token: 'var'"); | |
| 51 | , ".tmp_source.zig:5:5: error: expected token ';', found 'var'"); | |
| 52 | 52 | |
| 53 | 53 | cases.add("implicit semicolon - comptime statement", |
| 54 | 54 | \\export fn entry() { |
| ... | ... | @@ -57,7 +57,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 57 | 57 | \\ comptime ({}) |
| 58 | 58 | \\ var bad = {}; |
| 59 | 59 | \\} |
| 60 | , ".tmp_source.zig:5:5: error: invalid token: 'var'"); | |
| 60 | , ".tmp_source.zig:5:5: error: expected token ';', found 'var'"); | |
| 61 | 61 | |
| 62 | 62 | cases.add("implicit semicolon - comptime expression", |
| 63 | 63 | \\export fn entry() { |
| ... | ... | @@ -66,7 +66,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 66 | 66 | \\ _ = comptime {} |
| 67 | 67 | \\ var bad = {}; |
| 68 | 68 | \\} |
| 69 | , ".tmp_source.zig:5:5: error: invalid token: 'var'"); | |
| 69 | , ".tmp_source.zig:5:5: error: expected token ';', found 'var'"); | |
| 70 | 70 | |
| 71 | 71 | cases.add("implicit semicolon - defer", |
| 72 | 72 | \\export fn entry() { |
| ... | ... | @@ -84,7 +84,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 84 | 84 | \\ if(true) ({}) |
| 85 | 85 | \\ var bad = {}; |
| 86 | 86 | \\} |
| 87 | , ".tmp_source.zig:5:5: error: invalid token: 'var'"); | |
| 87 | , ".tmp_source.zig:5:5: error: expected token ';', found 'var'"); | |
| 88 | 88 | |
| 89 | 89 | cases.add("implicit semicolon - if expression", |
| 90 | 90 | \\export fn entry() { |
| ... | ... | @@ -93,7 +93,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 93 | 93 | \\ _ = if(true) {} |
| 94 | 94 | \\ var bad = {}; |
| 95 | 95 | \\} |
| 96 | , ".tmp_source.zig:5:5: error: invalid token: 'var'"); | |
| 96 | , ".tmp_source.zig:5:5: error: expected token ';', found 'var'"); | |
| 97 | 97 | |
| 98 | 98 | cases.add("implicit semicolon - if-else statement", |
| 99 | 99 | \\export fn entry() { |
| ... | ... | @@ -102,7 +102,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 102 | 102 | \\ if(true) ({}) else ({}) |
| 103 | 103 | \\ var bad = {}; |
| 104 | 104 | \\} |
| 105 | , ".tmp_source.zig:5:5: error: invalid token: 'var'"); | |
| 105 | , ".tmp_source.zig:5:5: error: expected token ';', found 'var'"); | |
| 106 | 106 | |
| 107 | 107 | cases.add("implicit semicolon - if-else expression", |
| 108 | 108 | \\export fn entry() { |
| ... | ... | @@ -111,7 +111,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 111 | 111 | \\ _ = if(true) {} else {} |
| 112 | 112 | \\ var bad = {}; |
| 113 | 113 | \\} |
| 114 | , ".tmp_source.zig:5:5: error: invalid token: 'var'"); | |
| 114 | , ".tmp_source.zig:5:5: error: expected token ';', found 'var'"); | |
| 115 | 115 | |
| 116 | 116 | cases.add("implicit semicolon - if-else-if statement", |
| 117 | 117 | \\export fn entry() { |
| ... | ... | @@ -120,7 +120,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 120 | 120 | \\ if(true) ({}) else if(true) ({}) |
| 121 | 121 | \\ var bad = {}; |
| 122 | 122 | \\} |
| 123 | , ".tmp_source.zig:5:5: error: invalid token: 'var'"); | |
| 123 | , ".tmp_source.zig:5:5: error: expected token ';', found 'var'"); | |
| 124 | 124 | |
| 125 | 125 | cases.add("implicit semicolon - if-else-if expression", |
| 126 | 126 | \\export fn entry() { |
| ... | ... | @@ -129,7 +129,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 129 | 129 | \\ _ = if(true) {} else if(true) {} |
| 130 | 130 | \\ var bad = {}; |
| 131 | 131 | \\} |
| 132 | , ".tmp_source.zig:5:5: error: invalid token: 'var'"); | |
| 132 | , ".tmp_source.zig:5:5: error: expected token ';', found 'var'"); | |
| 133 | 133 | |
| 134 | 134 | cases.add("implicit semicolon - if-else-if-else statement", |
| 135 | 135 | \\export fn entry() { |
| ... | ... | @@ -138,7 +138,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 138 | 138 | \\ if(true) ({}) else if(true) ({}) else ({}) |
| 139 | 139 | \\ var bad = {}; |
| 140 | 140 | \\} |
| 141 | , ".tmp_source.zig:5:5: error: invalid token: 'var'"); | |
| 141 | , ".tmp_source.zig:5:5: error: expected token ';', found 'var'"); | |
| 142 | 142 | |
| 143 | 143 | cases.add("implicit semicolon - if-else-if-else expression", |
| 144 | 144 | \\export fn entry() { |
| ... | ... | @@ -147,7 +147,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 147 | 147 | \\ _ = if(true) {} else if(true) {} else {} |
| 148 | 148 | \\ var bad = {}; |
| 149 | 149 | \\} |
| 150 | , ".tmp_source.zig:5:5: error: invalid token: 'var'"); | |
| 150 | , ".tmp_source.zig:5:5: error: expected token ';', found 'var'"); | |
| 151 | 151 | |
| 152 | 152 | cases.add("implicit semicolon - test statement", |
| 153 | 153 | \\export fn entry() { |
| ... | ... | @@ -156,7 +156,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 156 | 156 | \\ if (foo()) |_| ({}) |
| 157 | 157 | \\ var bad = {}; |
| 158 | 158 | \\} |
| 159 | , ".tmp_source.zig:5:5: error: invalid token: 'var'"); | |
| 159 | , ".tmp_source.zig:5:5: error: expected token ';', found 'var'"); | |
| 160 | 160 | |
| 161 | 161 | cases.add("implicit semicolon - test expression", |
| 162 | 162 | \\export fn entry() { |
| ... | ... | @@ -165,7 +165,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 165 | 165 | \\ _ = if (foo()) |_| {} |
| 166 | 166 | \\ var bad = {}; |
| 167 | 167 | \\} |
| 168 | , ".tmp_source.zig:5:5: error: invalid token: 'var'"); | |
| 168 | , ".tmp_source.zig:5:5: error: expected token ';', found 'var'"); | |
| 169 | 169 | |
| 170 | 170 | cases.add("implicit semicolon - while statement", |
| 171 | 171 | \\export fn entry() { |
| ... | ... | @@ -174,7 +174,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 174 | 174 | \\ while(true) ({}) |
| 175 | 175 | \\ var bad = {}; |
| 176 | 176 | \\} |
| 177 | , ".tmp_source.zig:5:5: error: invalid token: 'var'"); | |
| 177 | , ".tmp_source.zig:5:5: error: expected token ';', found 'var'"); | |
| 178 | 178 | |
| 179 | 179 | cases.add("implicit semicolon - while expression", |
| 180 | 180 | \\export fn entry() { |
| ... | ... | @@ -183,7 +183,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 183 | 183 | \\ _ = while(true) {} |
| 184 | 184 | \\ var bad = {}; |
| 185 | 185 | \\} |
| 186 | , ".tmp_source.zig:5:5: error: invalid token: 'var'"); | |
| 186 | , ".tmp_source.zig:5:5: error: expected token ';', found 'var'"); | |
| 187 | 187 | |
| 188 | 188 | cases.add("implicit semicolon - while-continue statement", |
| 189 | 189 | \\export fn entry() { |
| ... | ... | @@ -192,7 +192,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 192 | 192 | \\ while(true):({}) ({}) |
| 193 | 193 | \\ var bad = {}; |
| 194 | 194 | \\} |
| 195 | , ".tmp_source.zig:5:5: error: invalid token: 'var'"); | |
| 195 | , ".tmp_source.zig:5:5: error: expected token ';', found 'var'"); | |
| 196 | 196 | |
| 197 | 197 | cases.add("implicit semicolon - while-continue expression", |
| 198 | 198 | \\export fn entry() { |
| ... | ... | @@ -201,7 +201,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 201 | 201 | \\ _ = while(true):({}) {} |
| 202 | 202 | \\ var bad = {}; |
| 203 | 203 | \\} |
| 204 | , ".tmp_source.zig:5:5: error: invalid token: 'var'"); | |
| 204 | , ".tmp_source.zig:5:5: error: expected token ';', found 'var'"); | |
| 205 | 205 | |
| 206 | 206 | cases.add("implicit semicolon - for statement", |
| 207 | 207 | \\export fn entry() { |
| ... | ... | @@ -210,7 +210,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 210 | 210 | \\ for(foo()) ({}) |
| 211 | 211 | \\ var bad = {}; |
| 212 | 212 | \\} |
| 213 | , ".tmp_source.zig:5:5: error: invalid token: 'var'"); | |
| 213 | , ".tmp_source.zig:5:5: error: expected token ';', found 'var'"); | |
| 214 | 214 | |
| 215 | 215 | cases.add("implicit semicolon - for expression", |
| 216 | 216 | \\export fn entry() { |
| ... | ... | @@ -219,7 +219,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 219 | 219 | \\ _ = for(foo()) {} |
| 220 | 220 | \\ var bad = {}; |
| 221 | 221 | \\} |
| 222 | , ".tmp_source.zig:5:5: error: invalid token: 'var'"); | |
| 222 | , ".tmp_source.zig:5:5: error: expected token ';', found 'var'"); | |
| 223 | 223 | |
| 224 | 224 | cases.add("multiple function definitions", |
| 225 | 225 | \\fn a() {} |
| ... | ... | @@ -276,12 +276,13 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 276 | 276 | |
| 277 | 277 | cases.add("undeclared identifier", |
| 278 | 278 | \\export fn a() { |
| 279 | \\ return | |
| 279 | 280 | \\ b + |
| 280 | \\ c | |
| 281 | \\ c; | |
| 281 | 282 | \\} |
| 282 | 283 | , |
| 283 | ".tmp_source.zig:2:5: error: use of undeclared identifier 'b'", | |
| 284 | ".tmp_source.zig:3:5: error: use of undeclared identifier 'c'"); | |
| 284 | ".tmp_source.zig:3:5: error: use of undeclared identifier 'b'", | |
| 285 | ".tmp_source.zig:4:5: error: use of undeclared identifier 'c'"); | |
| 285 | 286 | |
| 286 | 287 | cases.add("parameter redeclaration", |
| 287 | 288 | \\fn f(a : i32, a : i32) { |
| ... | ... | @@ -306,9 +307,9 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 306 | 307 | cases.add("variable has wrong type", |
| 307 | 308 | \\export fn f() -> i32 { |
| 308 | 309 | \\ const a = c"a"; |
| 309 | \\ a | |
| 310 | \\ return a; | |
| 310 | 311 | \\} |
| 311 | , ".tmp_source.zig:3:5: error: expected type 'i32', found '&const u8'"); | |
| 312 | , ".tmp_source.zig:3:12: error: expected type 'i32', found '&const u8'"); | |
| 312 | 313 | |
| 313 | 314 | cases.add("if condition is bool, not int", |
| 314 | 315 | \\export fn f() { |
| ... | ... | @@ -393,23 +394,23 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 393 | 394 | |
| 394 | 395 | cases.add("missing else clause", |
| 395 | 396 | \\fn f(b: bool) { |
| 396 | \\ const x : i32 = if (b) { 1 }; | |
| 397 | \\ const y = if (b) { i32(1) }; | |
| 397 | \\ const x : i32 = if (b) h: { break :h 1; }; | |
| 398 | \\ const y = if (b) h: { break :h i32(1); }; | |
| 398 | 399 | \\} |
| 399 | 400 | \\export fn entry() { f(true); } |
| 400 | , ".tmp_source.zig:2:30: error: integer value 1 cannot be implicitly casted to type 'void'", | |
| 401 | , ".tmp_source.zig:2:42: error: integer value 1 cannot be implicitly casted to type 'void'", | |
| 401 | 402 | ".tmp_source.zig:3:15: error: incompatible types: 'i32' and 'void'"); |
| 402 | 403 | |
| 403 | 404 | cases.add("direct struct loop", |
| 404 | 405 | \\const A = struct { a : A, }; |
| 405 | \\export fn entry() -> usize { @sizeOf(A) } | |
| 406 | \\export fn entry() -> usize { return @sizeOf(A); } | |
| 406 | 407 | , ".tmp_source.zig:1:11: error: struct 'A' contains itself"); |
| 407 | 408 | |
| 408 | 409 | cases.add("indirect struct loop", |
| 409 | 410 | \\const A = struct { b : B, }; |
| 410 | 411 | \\const B = struct { c : C, }; |
| 411 | 412 | \\const C = struct { a : A, }; |
| 412 | \\export fn entry() -> usize { @sizeOf(A) } | |
| 413 | \\export fn entry() -> usize { return @sizeOf(A); } | |
| 413 | 414 | , ".tmp_source.zig:1:11: error: struct 'A' contains itself"); |
| 414 | 415 | |
| 415 | 416 | cases.add("invalid struct field", |
| ... | ... | @@ -507,10 +508,10 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 507 | 508 | |
| 508 | 509 | cases.add("cast unreachable", |
| 509 | 510 | \\fn f() -> i32 { |
| 510 | \\ i32(return 1) | |
| 511 | \\ return i32(return 1); | |
| 511 | 512 | \\} |
| 512 | 513 | \\export fn entry() { _ = f(); } |
| 513 | , ".tmp_source.zig:2:8: error: unreachable code"); | |
| 514 | , ".tmp_source.zig:2:15: error: unreachable code"); | |
| 514 | 515 | |
| 515 | 516 | cases.add("invalid builtin fn", |
| 516 | 517 | \\fn f() -> @bogus(foo) { |
| ... | ... | @@ -533,7 +534,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 533 | 534 | |
| 534 | 535 | cases.add("struct init syntax for array", |
| 535 | 536 | \\const foo = []u16{.x = 1024,}; |
| 536 | \\export fn entry() -> usize { @sizeOf(@typeOf(foo)) } | |
| 537 | \\export fn entry() -> usize { return @sizeOf(@typeOf(foo)); } | |
| 537 | 538 | , ".tmp_source.zig:1:18: error: type '[]u16' does not support struct initialization syntax"); |
| 538 | 539 | |
| 539 | 540 | cases.add("type variables must be constant", |
| ... | ... | @@ -576,7 +577,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 576 | 577 | \\ } |
| 577 | 578 | \\} |
| 578 | 579 | \\ |
| 579 | \\export fn entry() -> usize { @sizeOf(@typeOf(f)) } | |
| 580 | \\export fn entry() -> usize { return @sizeOf(@typeOf(f)); } | |
| 580 | 581 | , ".tmp_source.zig:8:5: error: enumeration value 'Number.Four' not handled in switch"); |
| 581 | 582 | |
| 582 | 583 | cases.add("switch expression - duplicate enumeration prong", |
| ... | ... | @@ -596,7 +597,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 596 | 597 | \\ } |
| 597 | 598 | \\} |
| 598 | 599 | \\ |
| 599 | \\export fn entry() -> usize { @sizeOf(@typeOf(f)) } | |
| 600 | \\export fn entry() -> usize { return @sizeOf(@typeOf(f)); } | |
| 600 | 601 | , ".tmp_source.zig:13:15: error: duplicate switch value", |
| 601 | 602 | ".tmp_source.zig:10:15: note: other value is here"); |
| 602 | 603 | |
| ... | ... | @@ -618,7 +619,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 618 | 619 | \\ } |
| 619 | 620 | \\} |
| 620 | 621 | \\ |
| 621 | \\export fn entry() -> usize { @sizeOf(@typeOf(f)) } | |
| 622 | \\export fn entry() -> usize { return @sizeOf(@typeOf(f)); } | |
| 622 | 623 | , ".tmp_source.zig:13:15: error: duplicate switch value", |
| 623 | 624 | ".tmp_source.zig:10:15: note: other value is here"); |
| 624 | 625 | |
| ... | ... | @@ -641,20 +642,20 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 641 | 642 | \\ 0 => {}, |
| 642 | 643 | \\ } |
| 643 | 644 | \\} |
| 644 | \\export fn entry() -> usize { @sizeOf(@typeOf(foo)) } | |
| 645 | \\export fn entry() -> usize { return @sizeOf(@typeOf(foo)); } | |
| 645 | 646 | , |
| 646 | 647 | ".tmp_source.zig:2:5: error: switch must handle all possibilities"); |
| 647 | 648 | |
| 648 | 649 | cases.add("switch expression - duplicate or overlapping integer value", |
| 649 | 650 | \\fn foo(x: u8) -> u8 { |
| 650 | \\ switch (x) { | |
| 651 | \\ return switch (x) { | |
| 651 | 652 | \\ 0 ... 100 => u8(0), |
| 652 | 653 | \\ 101 ... 200 => 1, |
| 653 | 654 | \\ 201, 203 ... 207 => 2, |
| 654 | 655 | \\ 206 ... 255 => 3, |
| 655 | \\ } | |
| 656 | \\ }; | |
| 656 | 657 | \\} |
| 657 | \\export fn entry() -> usize { @sizeOf(@typeOf(foo)) } | |
| 658 | \\export fn entry() -> usize { return @sizeOf(@typeOf(foo)); } | |
| 658 | 659 | , |
| 659 | 660 | ".tmp_source.zig:6:9: error: duplicate switch value", |
| 660 | 661 | ".tmp_source.zig:5:14: note: previous value is here"); |
| ... | ... | @@ -666,14 +667,14 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 666 | 667 | \\ } |
| 667 | 668 | \\} |
| 668 | 669 | \\const y: u8 = 100; |
| 669 | \\export fn entry() -> usize { @sizeOf(@typeOf(foo)) } | |
| 670 | \\export fn entry() -> usize { return @sizeOf(@typeOf(foo)); } | |
| 670 | 671 | , |
| 671 | 672 | ".tmp_source.zig:2:5: error: else prong required when switching on type '&u8'"); |
| 672 | 673 | |
| 673 | 674 | cases.add("global variable initializer must be constant expression", |
| 674 | 675 | \\extern fn foo() -> i32; |
| 675 | 676 | \\const x = foo(); |
| 676 | \\export fn entry() -> i32 { x } | |
| 677 | \\export fn entry() -> i32 { return x; } | |
| 677 | 678 | , ".tmp_source.zig:2:11: error: unable to evaluate constant expression"); |
| 678 | 679 | |
| 679 | 680 | cases.add("array concatenation with wrong type", |
| ... | ... | @@ -681,38 +682,38 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 681 | 682 | \\const derp = usize(1234); |
| 682 | 683 | \\const a = derp ++ "foo"; |
| 683 | 684 | \\ |
| 684 | \\export fn entry() -> usize { @sizeOf(@typeOf(a)) } | |
| 685 | \\export fn entry() -> usize { return @sizeOf(@typeOf(a)); } | |
| 685 | 686 | , ".tmp_source.zig:3:11: error: expected array or C string literal, found 'usize'"); |
| 686 | 687 | |
| 687 | 688 | cases.add("non compile time array concatenation", |
| 688 | 689 | \\fn f() -> []u8 { |
| 689 | \\ s ++ "foo" | |
| 690 | \\ return s ++ "foo"; | |
| 690 | 691 | \\} |
| 691 | 692 | \\var s: [10]u8 = undefined; |
| 692 | \\export fn entry() -> usize { @sizeOf(@typeOf(f)) } | |
| 693 | , ".tmp_source.zig:2:5: error: unable to evaluate constant expression"); | |
| 693 | \\export fn entry() -> usize { return @sizeOf(@typeOf(f)); } | |
| 694 | , ".tmp_source.zig:2:12: error: unable to evaluate constant expression"); | |
| 694 | 695 | |
| 695 | 696 | cases.add("@cImport with bogus include", |
| 696 | 697 | \\const c = @cImport(@cInclude("bogus.h")); |
| 697 | \\export fn entry() -> usize { @sizeOf(@typeOf(c.bogo)) } | |
| 698 | \\export fn entry() -> usize { return @sizeOf(@typeOf(c.bogo)); } | |
| 698 | 699 | , ".tmp_source.zig:1:11: error: C import failed", |
| 699 | 700 | ".h:1:10: note: 'bogus.h' file not found"); |
| 700 | 701 | |
| 701 | 702 | cases.add("address of number literal", |
| 702 | 703 | \\const x = 3; |
| 703 | 704 | \\const y = &x; |
| 704 | \\fn foo() -> &const i32 { y } | |
| 705 | \\export fn entry() -> usize { @sizeOf(@typeOf(foo)) } | |
| 706 | , ".tmp_source.zig:3:26: error: expected type '&const i32', found '&const (integer literal)'"); | |
| 705 | \\fn foo() -> &const i32 { return y; } | |
| 706 | \\export fn entry() -> usize { return @sizeOf(@typeOf(foo)); } | |
| 707 | , ".tmp_source.zig:3:33: error: expected type '&const i32', found '&const (integer literal)'"); | |
| 707 | 708 | |
| 708 | 709 | cases.add("integer overflow error", |
| 709 | 710 | \\const x : u8 = 300; |
| 710 | \\export fn entry() -> usize { @sizeOf(@typeOf(x)) } | |
| 711 | \\export fn entry() -> usize { return @sizeOf(@typeOf(x)); } | |
| 711 | 712 | , ".tmp_source.zig:1:16: error: integer value 300 cannot be implicitly casted to type 'u8'"); |
| 712 | 713 | |
| 713 | 714 | cases.add("incompatible number literals", |
| 714 | 715 | \\const x = 2 == 2.0; |
| 715 | \\export fn entry() -> usize { @sizeOf(@typeOf(x)) } | |
| 716 | \\export fn entry() -> usize { return @sizeOf(@typeOf(x)); } | |
| 716 | 717 | , ".tmp_source.zig:1:11: error: integer value 2 cannot be implicitly casted to type '(float literal)'"); |
| 717 | 718 | |
| 718 | 719 | cases.add("missing function call param", |
| ... | ... | @@ -738,32 +739,32 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 738 | 739 | \\ const result = members[index](); |
| 739 | 740 | \\} |
| 740 | 741 | \\ |
| 741 | \\export fn entry() -> usize { @sizeOf(@typeOf(f)) } | |
| 742 | \\export fn entry() -> usize { return @sizeOf(@typeOf(f)); } | |
| 742 | 743 | , ".tmp_source.zig:20:34: error: expected 1 arguments, found 0"); |
| 743 | 744 | |
| 744 | 745 | cases.add("missing function name and param name", |
| 745 | 746 | \\fn () {} |
| 746 | 747 | \\fn f(i32) {} |
| 747 | \\export fn entry() -> usize { @sizeOf(@typeOf(f)) } | |
| 748 | \\export fn entry() -> usize { return @sizeOf(@typeOf(f)); } | |
| 748 | 749 | , |
| 749 | 750 | ".tmp_source.zig:1:1: error: missing function name", |
| 750 | 751 | ".tmp_source.zig:2:6: error: missing parameter name"); |
| 751 | 752 | |
| 752 | 753 | cases.add("wrong function type", |
| 753 | 754 | \\const fns = []fn(){ a, b, c }; |
| 754 | \\fn a() -> i32 {0} | |
| 755 | \\fn b() -> i32 {1} | |
| 756 | \\fn c() -> i32 {2} | |
| 757 | \\export fn entry() -> usize { @sizeOf(@typeOf(fns)) } | |
| 755 | \\fn a() -> i32 {return 0;} | |
| 756 | \\fn b() -> i32 {return 1;} | |
| 757 | \\fn c() -> i32 {return 2;} | |
| 758 | \\export fn entry() -> usize { return @sizeOf(@typeOf(fns)); } | |
| 758 | 759 | , ".tmp_source.zig:1:21: error: expected type 'fn()', found 'fn() -> i32'"); |
| 759 | 760 | |
| 760 | 761 | cases.add("extern function pointer mismatch", |
| 761 | 762 | \\const fns = [](fn(i32)->i32){ a, b, c }; |
| 762 | \\pub fn a(x: i32) -> i32 {x + 0} | |
| 763 | \\pub fn b(x: i32) -> i32 {x + 1} | |
| 764 | \\export fn c(x: i32) -> i32 {x + 2} | |
| 763 | \\pub fn a(x: i32) -> i32 {return x + 0;} | |
| 764 | \\pub fn b(x: i32) -> i32 {return x + 1;} | |
| 765 | \\export fn c(x: i32) -> i32 {return x + 2;} | |
| 765 | 766 | \\ |
| 766 | \\export fn entry() -> usize { @sizeOf(@typeOf(fns)) } | |
| 767 | \\export fn entry() -> usize { return @sizeOf(@typeOf(fns)); } | |
| 767 | 768 | , ".tmp_source.zig:1:37: error: expected type 'fn(i32) -> i32', found 'extern fn(i32) -> i32'"); |
| 768 | 769 | |
| 769 | 770 | |
| ... | ... | @@ -771,14 +772,14 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 771 | 772 | \\const x : f64 = 1.0; |
| 772 | 773 | \\const y : f32 = x; |
| 773 | 774 | \\ |
| 774 | \\export fn entry() -> usize { @sizeOf(@typeOf(y)) } | |
| 775 | \\export fn entry() -> usize { return @sizeOf(@typeOf(y)); } | |
| 775 | 776 | , ".tmp_source.zig:2:17: error: expected type 'f32', found 'f64'"); |
| 776 | 777 | |
| 777 | 778 | |
| 778 | 779 | cases.add("colliding invalid top level functions", |
| 779 | 780 | \\fn func() -> bogus {} |
| 780 | 781 | \\fn func() -> bogus {} |
| 781 | \\export fn entry() -> usize { @sizeOf(@typeOf(func)) } | |
| 782 | \\export fn entry() -> usize { return @sizeOf(@typeOf(func)); } | |
| 782 | 783 | , |
| 783 | 784 | ".tmp_source.zig:2:1: error: redefinition of 'func'", |
| 784 | 785 | ".tmp_source.zig:1:14: error: use of undeclared identifier 'bogus'"); |
| ... | ... | @@ -786,7 +787,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 786 | 787 | |
| 787 | 788 | cases.add("bogus compile var", |
| 788 | 789 | \\const x = @import("builtin").bogus; |
| 789 | \\export fn entry() -> usize { @sizeOf(@typeOf(x)) } | |
| 790 | \\export fn entry() -> usize { return @sizeOf(@typeOf(x)); } | |
| 790 | 791 | , ".tmp_source.zig:1:29: error: no member named 'bogus' in '"); |
| 791 | 792 | |
| 792 | 793 | |
| ... | ... | @@ -795,11 +796,11 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 795 | 796 | \\ y: [get()]u8, |
| 796 | 797 | \\}; |
| 797 | 798 | \\var global_var: usize = 1; |
| 798 | \\fn get() -> usize { global_var } | |
| 799 | \\fn get() -> usize { return global_var; } | |
| 799 | 800 | \\ |
| 800 | \\export fn entry() -> usize { @sizeOf(@typeOf(Foo)) } | |
| 801 | \\export fn entry() -> usize { return @sizeOf(@typeOf(Foo)); } | |
| 801 | 802 | , |
| 802 | ".tmp_source.zig:5:21: error: unable to evaluate constant expression", | |
| 803 | ".tmp_source.zig:5:28: error: unable to evaluate constant expression", | |
| 803 | 804 | ".tmp_source.zig:2:12: note: called from here", |
| 804 | 805 | ".tmp_source.zig:2:8: note: called from here"); |
| 805 | 806 | |
| ... | ... | @@ -810,7 +811,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 810 | 811 | \\}; |
| 811 | 812 | \\const x = Foo {.field = 1} + Foo {.field = 2}; |
| 812 | 813 | \\ |
| 813 | \\export fn entry() -> usize { @sizeOf(@typeOf(x)) } | |
| 814 | \\export fn entry() -> usize { return @sizeOf(@typeOf(x)); } | |
| 814 | 815 | , ".tmp_source.zig:4:28: error: invalid operands to binary expression: 'Foo' and 'Foo'"); |
| 815 | 816 | |
| 816 | 817 | |
| ... | ... | @@ -820,10 +821,10 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 820 | 821 | \\const int_x = u32(1) / u32(0); |
| 821 | 822 | \\const float_x = f32(1.0) / f32(0.0); |
| 822 | 823 | \\ |
| 823 | \\export fn entry1() -> usize { @sizeOf(@typeOf(lit_int_x)) } | |
| 824 | \\export fn entry2() -> usize { @sizeOf(@typeOf(lit_float_x)) } | |
| 825 | \\export fn entry3() -> usize { @sizeOf(@typeOf(int_x)) } | |
| 826 | \\export fn entry4() -> usize { @sizeOf(@typeOf(float_x)) } | |
| 824 | \\export fn entry1() -> usize { return @sizeOf(@typeOf(lit_int_x)); } | |
| 825 | \\export fn entry2() -> usize { return @sizeOf(@typeOf(lit_float_x)); } | |
| 826 | \\export fn entry3() -> usize { return @sizeOf(@typeOf(int_x)); } | |
| 827 | \\export fn entry4() -> usize { return @sizeOf(@typeOf(float_x)); } | |
| 827 | 828 | , |
| 828 | 829 | ".tmp_source.zig:1:21: error: division by zero is undefined", |
| 829 | 830 | ".tmp_source.zig:2:25: error: division by zero is undefined", |
| ... | ... | @@ -835,14 +836,14 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 835 | 836 | \\const foo = "a |
| 836 | 837 | \\b"; |
| 837 | 838 | \\ |
| 838 | \\export fn entry() -> usize { @sizeOf(@typeOf(foo)) } | |
| 839 | \\export fn entry() -> usize { return @sizeOf(@typeOf(foo)); } | |
| 839 | 840 | , ".tmp_source.zig:1:13: error: newline not allowed in string literal"); |
| 840 | 841 | |
| 841 | 842 | cases.add("invalid comparison for function pointers", |
| 842 | 843 | \\fn foo() {} |
| 843 | 844 | \\const invalid = foo > foo; |
| 844 | 845 | \\ |
| 845 | \\export fn entry() -> usize { @sizeOf(@typeOf(invalid)) } | |
| 846 | \\export fn entry() -> usize { return @sizeOf(@typeOf(invalid)); } | |
| 846 | 847 | , ".tmp_source.zig:2:21: error: operator not allowed for type 'fn()'"); |
| 847 | 848 | |
| 848 | 849 | cases.add("generic function instance with non-constant expression", |
| ... | ... | @@ -851,13 +852,13 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 851 | 852 | \\ return foo(a, b); |
| 852 | 853 | \\} |
| 853 | 854 | \\ |
| 854 | \\export fn entry() -> usize { @sizeOf(@typeOf(test1)) } | |
| 855 | \\export fn entry() -> usize { return @sizeOf(@typeOf(test1)); } | |
| 855 | 856 | , ".tmp_source.zig:3:16: error: unable to evaluate constant expression"); |
| 856 | 857 | |
| 857 | 858 | cases.add("assign null to non-nullable pointer", |
| 858 | 859 | \\const a: &u8 = null; |
| 859 | 860 | \\ |
| 860 | \\export fn entry() -> usize { @sizeOf(@typeOf(a)) } | |
| 861 | \\export fn entry() -> usize { return @sizeOf(@typeOf(a)); } | |
| 861 | 862 | , ".tmp_source.zig:1:16: error: expected type '&u8', found '(null)'"); |
| 862 | 863 | |
| 863 | 864 | cases.add("indexing an array of size zero", |
| ... | ... | @@ -870,18 +871,18 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 870 | 871 | cases.add("compile time division by zero", |
| 871 | 872 | \\const y = foo(0); |
| 872 | 873 | \\fn foo(x: u32) -> u32 { |
| 873 | \\ 1 / x | |
| 874 | \\ return 1 / x; | |
| 874 | 875 | \\} |
| 875 | 876 | \\ |
| 876 | \\export fn entry() -> usize { @sizeOf(@typeOf(y)) } | |
| 877 | \\export fn entry() -> usize { return @sizeOf(@typeOf(y)); } | |
| 877 | 878 | , |
| 878 | ".tmp_source.zig:3:7: error: division by zero is undefined", | |
| 879 | ".tmp_source.zig:3:14: error: division by zero is undefined", | |
| 879 | 880 | ".tmp_source.zig:1:14: note: called from here"); |
| 880 | 881 | |
| 881 | 882 | cases.add("branch on undefined value", |
| 882 | 883 | \\const x = if (undefined) true else false; |
| 883 | 884 | \\ |
| 884 | \\export fn entry() -> usize { @sizeOf(@typeOf(x)) } | |
| 885 | \\export fn entry() -> usize { return @sizeOf(@typeOf(x)); } | |
| 885 | 886 | , ".tmp_source.zig:1:15: error: use of undefined value"); |
| 886 | 887 | |
| 887 | 888 | |
| ... | ... | @@ -891,7 +892,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 891 | 892 | \\ return fibbonaci(x - 1) + fibbonaci(x - 2); |
| 892 | 893 | \\} |
| 893 | 894 | \\ |
| 894 | \\export fn entry() -> usize { @sizeOf(@typeOf(seventh_fib_number)) } | |
| 895 | \\export fn entry() -> usize { return @sizeOf(@typeOf(seventh_fib_number)); } | |
| 895 | 896 | , |
| 896 | 897 | ".tmp_source.zig:3:21: error: evaluation exceeded 1000 backwards branches", |
| 897 | 898 | ".tmp_source.zig:3:21: note: called from here"); |
| ... | ... | @@ -899,7 +900,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 899 | 900 | cases.add("@embedFile with bogus file", |
| 900 | 901 | \\const resource = @embedFile("bogus.txt"); |
| 901 | 902 | \\ |
| 902 | \\export fn entry() -> usize { @sizeOf(@typeOf(resource)) } | |
| 903 | \\export fn entry() -> usize { return @sizeOf(@typeOf(resource)); } | |
| 903 | 904 | , ".tmp_source.zig:1:29: error: unable to find '", "bogus.txt'"); |
| 904 | 905 | |
| 905 | 906 | cases.add("non-const expression in struct literal outside function", |
| ... | ... | @@ -909,7 +910,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 909 | 910 | \\const a = Foo {.x = get_it()}; |
| 910 | 911 | \\extern fn get_it() -> i32; |
| 911 | 912 | \\ |
| 912 | \\export fn entry() -> usize { @sizeOf(@typeOf(a)) } | |
| 913 | \\export fn entry() -> usize { return @sizeOf(@typeOf(a)); } | |
| 913 | 914 | , ".tmp_source.zig:4:21: error: unable to evaluate constant expression"); |
| 914 | 915 | |
| 915 | 916 | cases.add("non-const expression function call with struct return value outside function", |
| ... | ... | @@ -919,11 +920,11 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 919 | 920 | \\const a = get_it(); |
| 920 | 921 | \\fn get_it() -> Foo { |
| 921 | 922 | \\ global_side_effect = true; |
| 922 | \\ Foo {.x = 13} | |
| 923 | \\ return Foo {.x = 13}; | |
| 923 | 924 | \\} |
| 924 | 925 | \\var global_side_effect = false; |
| 925 | 926 | \\ |
| 926 | \\export fn entry() -> usize { @sizeOf(@typeOf(a)) } | |
| 927 | \\export fn entry() -> usize { return @sizeOf(@typeOf(a)); } | |
| 927 | 928 | , |
| 928 | 929 | ".tmp_source.zig:6:24: error: unable to evaluate constant expression", |
| 929 | 930 | ".tmp_source.zig:4:17: note: called from here"); |
| ... | ... | @@ -939,21 +940,21 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 939 | 940 | |
| 940 | 941 | cases.add("illegal comparison of types", |
| 941 | 942 | \\fn bad_eql_1(a: []u8, b: []u8) -> bool { |
| 942 | \\ a == b | |
| 943 | \\ return a == b; | |
| 943 | 944 | \\} |
| 944 | 945 | \\const EnumWithData = union(enum) { |
| 945 | 946 | \\ One: void, |
| 946 | 947 | \\ Two: i32, |
| 947 | 948 | \\}; |
| 948 | 949 | \\fn bad_eql_2(a: &const EnumWithData, b: &const EnumWithData) -> bool { |
| 949 | \\ *a == *b | |
| 950 | \\ return *a == *b; | |
| 950 | 951 | \\} |
| 951 | 952 | \\ |
| 952 | \\export fn entry1() -> usize { @sizeOf(@typeOf(bad_eql_1)) } | |
| 953 | \\export fn entry2() -> usize { @sizeOf(@typeOf(bad_eql_2)) } | |
| 953 | \\export fn entry1() -> usize { return @sizeOf(@typeOf(bad_eql_1)); } | |
| 954 | \\export fn entry2() -> usize { return @sizeOf(@typeOf(bad_eql_2)); } | |
| 954 | 955 | , |
| 955 | ".tmp_source.zig:2:7: error: operator not allowed for type '[]u8'", | |
| 956 | ".tmp_source.zig:9:8: error: operator not allowed for type 'EnumWithData'"); | |
| 956 | ".tmp_source.zig:2:14: error: operator not allowed for type '[]u8'", | |
| 957 | ".tmp_source.zig:9:15: error: operator not allowed for type 'EnumWithData'"); | |
| 957 | 958 | |
| 958 | 959 | cases.add("non-const switch number literal", |
| 959 | 960 | \\export fn foo() { |
| ... | ... | @@ -964,7 +965,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 964 | 965 | \\ }; |
| 965 | 966 | \\} |
| 966 | 967 | \\fn bar() -> i32 { |
| 967 | \\ 2 | |
| 968 | \\ return 2; | |
| 968 | 969 | \\} |
| 969 | 970 | , ".tmp_source.zig:2:15: error: unable to infer expression type"); |
| 970 | 971 | |
| ... | ... | @@ -987,56 +988,56 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 987 | 988 | cases.add("negation overflow in function evaluation", |
| 988 | 989 | \\const y = neg(-128); |
| 989 | 990 | \\fn neg(x: i8) -> i8 { |
| 990 | \\ -x | |
| 991 | \\ return -x; | |
| 991 | 992 | \\} |
| 992 | 993 | \\ |
| 993 | \\export fn entry() -> usize { @sizeOf(@typeOf(y)) } | |
| 994 | \\export fn entry() -> usize { return @sizeOf(@typeOf(y)); } | |
| 994 | 995 | , |
| 995 | ".tmp_source.zig:3:5: error: negation caused overflow", | |
| 996 | ".tmp_source.zig:3:12: error: negation caused overflow", | |
| 996 | 997 | ".tmp_source.zig:1:14: note: called from here"); |
| 997 | 998 | |
| 998 | 999 | cases.add("add overflow in function evaluation", |
| 999 | 1000 | \\const y = add(65530, 10); |
| 1000 | 1001 | \\fn add(a: u16, b: u16) -> u16 { |
| 1001 | \\ a + b | |
| 1002 | \\ return a + b; | |
| 1002 | 1003 | \\} |
| 1003 | 1004 | \\ |
| 1004 | \\export fn entry() -> usize { @sizeOf(@typeOf(y)) } | |
| 1005 | \\export fn entry() -> usize { return @sizeOf(@typeOf(y)); } | |
| 1005 | 1006 | , |
| 1006 | ".tmp_source.zig:3:7: error: operation caused overflow", | |
| 1007 | ".tmp_source.zig:3:14: error: operation caused overflow", | |
| 1007 | 1008 | ".tmp_source.zig:1:14: note: called from here"); |
| 1008 | 1009 | |
| 1009 | 1010 | |
| 1010 | 1011 | cases.add("sub overflow in function evaluation", |
| 1011 | 1012 | \\const y = sub(10, 20); |
| 1012 | 1013 | \\fn sub(a: u16, b: u16) -> u16 { |
| 1013 | \\ a - b | |
| 1014 | \\ return a - b; | |
| 1014 | 1015 | \\} |
| 1015 | 1016 | \\ |
| 1016 | \\export fn entry() -> usize { @sizeOf(@typeOf(y)) } | |
| 1017 | \\export fn entry() -> usize { return @sizeOf(@typeOf(y)); } | |
| 1017 | 1018 | , |
| 1018 | ".tmp_source.zig:3:7: error: operation caused overflow", | |
| 1019 | ".tmp_source.zig:3:14: error: operation caused overflow", | |
| 1019 | 1020 | ".tmp_source.zig:1:14: note: called from here"); |
| 1020 | 1021 | |
| 1021 | 1022 | cases.add("mul overflow in function evaluation", |
| 1022 | 1023 | \\const y = mul(300, 6000); |
| 1023 | 1024 | \\fn mul(a: u16, b: u16) -> u16 { |
| 1024 | \\ a * b | |
| 1025 | \\ return a * b; | |
| 1025 | 1026 | \\} |
| 1026 | 1027 | \\ |
| 1027 | \\export fn entry() -> usize { @sizeOf(@typeOf(y)) } | |
| 1028 | \\export fn entry() -> usize { return @sizeOf(@typeOf(y)); } | |
| 1028 | 1029 | , |
| 1029 | ".tmp_source.zig:3:7: error: operation caused overflow", | |
| 1030 | ".tmp_source.zig:3:14: error: operation caused overflow", | |
| 1030 | 1031 | ".tmp_source.zig:1:14: note: called from here"); |
| 1031 | 1032 | |
| 1032 | 1033 | cases.add("truncate sign mismatch", |
| 1033 | 1034 | \\fn f() -> i8 { |
| 1034 | 1035 | \\ const x: u32 = 10; |
| 1035 | \\ @truncate(i8, x) | |
| 1036 | \\ return @truncate(i8, x); | |
| 1036 | 1037 | \\} |
| 1037 | 1038 | \\ |
| 1038 | \\export fn entry() -> usize { @sizeOf(@typeOf(f)) } | |
| 1039 | , ".tmp_source.zig:3:19: error: expected signed integer type, found 'u32'"); | |
| 1039 | \\export fn entry() -> usize { return @sizeOf(@typeOf(f)); } | |
| 1040 | , ".tmp_source.zig:3:26: error: expected signed integer type, found 'u32'"); | |
| 1040 | 1041 | |
| 1041 | 1042 | cases.add("%return in function with non error return type", |
| 1042 | 1043 | \\export fn f() { |
| ... | ... | @@ -1067,16 +1068,16 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1067 | 1068 | |
| 1068 | 1069 | cases.add("export function with comptime parameter", |
| 1069 | 1070 | \\export fn foo(comptime x: i32, y: i32) -> i32{ |
| 1070 | \\ x + y | |
| 1071 | \\ return x + y; | |
| 1071 | 1072 | \\} |
| 1072 | 1073 | , ".tmp_source.zig:1:15: error: comptime parameter not allowed in function with calling convention 'ccc'"); |
| 1073 | 1074 | |
| 1074 | 1075 | cases.add("extern function with comptime parameter", |
| 1075 | 1076 | \\extern fn foo(comptime x: i32, y: i32) -> i32; |
| 1076 | 1077 | \\fn f() -> i32 { |
| 1077 | \\ foo(1, 2) | |
| 1078 | \\ return foo(1, 2); | |
| 1078 | 1079 | \\} |
| 1079 | \\export fn entry() -> usize { @sizeOf(@typeOf(f)) } | |
| 1080 | \\export fn entry() -> usize { return @sizeOf(@typeOf(f)); } | |
| 1080 | 1081 | , ".tmp_source.zig:1:15: error: comptime parameter not allowed in function with calling convention 'ccc'"); |
| 1081 | 1082 | |
| 1082 | 1083 | cases.add("convert fixed size array to slice with invalid size", |
| ... | ... | @@ -1090,15 +1091,15 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1090 | 1091 | \\var a: u32 = 0; |
| 1091 | 1092 | \\pub fn List(comptime T: type) -> type { |
| 1092 | 1093 | \\ a += 1; |
| 1093 | \\ SmallList(T, 8) | |
| 1094 | \\ return SmallList(T, 8); | |
| 1094 | 1095 | \\} |
| 1095 | 1096 | \\ |
| 1096 | 1097 | \\pub fn SmallList(comptime T: type, comptime STATIC_SIZE: usize) -> type { |
| 1097 | \\ struct { | |
| 1098 | \\ return struct { | |
| 1098 | 1099 | \\ items: []T, |
| 1099 | 1100 | \\ length: usize, |
| 1100 | 1101 | \\ prealloc_items: [STATIC_SIZE]T, |
| 1101 | \\ } | |
| 1102 | \\ }; | |
| 1102 | 1103 | \\} |
| 1103 | 1104 | \\ |
| 1104 | 1105 | \\export fn function_with_return_type_type() { |
| ... | ... | @@ -1113,7 +1114,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1113 | 1114 | \\fn f(m: []const u8) { |
| 1114 | 1115 | \\ m.copy(u8, self[0..], m); |
| 1115 | 1116 | \\} |
| 1116 | \\export fn entry() -> usize { @sizeOf(@typeOf(f)) } | |
| 1117 | \\export fn entry() -> usize { return @sizeOf(@typeOf(f)); } | |
| 1117 | 1118 | , ".tmp_source.zig:3:6: error: no member named 'copy' in '[]const u8'"); |
| 1118 | 1119 | |
| 1119 | 1120 | cases.add("wrong number of arguments for method fn call", |
| ... | ... | @@ -1124,7 +1125,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1124 | 1125 | \\ |
| 1125 | 1126 | \\ foo.method(1, 2); |
| 1126 | 1127 | \\} |
| 1127 | \\export fn entry() -> usize { @sizeOf(@typeOf(f)) } | |
| 1128 | \\export fn entry() -> usize { return @sizeOf(@typeOf(f)); } | |
| 1128 | 1129 | , ".tmp_source.zig:6:15: error: expected 2 arguments, found 3"); |
| 1129 | 1130 | |
| 1130 | 1131 | cases.add("assign through constant pointer", |
| ... | ... | @@ -1149,7 +1150,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1149 | 1150 | \\fn foo(blah: []u8) { |
| 1150 | 1151 | \\ for (blah) { } |
| 1151 | 1152 | \\} |
| 1152 | \\export fn entry() -> usize { @sizeOf(@typeOf(foo)) } | |
| 1153 | \\export fn entry() -> usize { return @sizeOf(@typeOf(foo)); } | |
| 1153 | 1154 | , ".tmp_source.zig:2:5: error: for loop expression missing element parameter"); |
| 1154 | 1155 | |
| 1155 | 1156 | cases.add("misspelled type with pointer only reference", |
| ... | ... | @@ -1182,7 +1183,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1182 | 1183 | \\ var jd = JsonNode {.kind = JsonType.JSONArray , .jobject = JsonOA.JSONArray {jll} }; |
| 1183 | 1184 | \\} |
| 1184 | 1185 | \\ |
| 1185 | \\export fn entry() -> usize { @sizeOf(@typeOf(foo)) } | |
| 1186 | \\export fn entry() -> usize { return @sizeOf(@typeOf(foo)); } | |
| 1186 | 1187 | , ".tmp_source.zig:5:16: error: use of undeclared identifier 'JsonList'"); |
| 1187 | 1188 | |
| 1188 | 1189 | cases.add("method call with first arg type primitive", |
| ... | ... | @@ -1190,9 +1191,9 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1190 | 1191 | \\ x: i32, |
| 1191 | 1192 | \\ |
| 1192 | 1193 | \\ fn init(x: i32) -> Foo { |
| 1193 | \\ Foo { | |
| 1194 | \\ return Foo { | |
| 1194 | 1195 | \\ .x = x, |
| 1195 | \\ } | |
| 1196 | \\ }; | |
| 1196 | 1197 | \\ } |
| 1197 | 1198 | \\}; |
| 1198 | 1199 | \\ |
| ... | ... | @@ -1209,10 +1210,10 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1209 | 1210 | \\ allocator: &Allocator, |
| 1210 | 1211 | \\ |
| 1211 | 1212 | \\ pub fn init(allocator: &Allocator) -> List { |
| 1212 | \\ List { | |
| 1213 | \\ return List { | |
| 1213 | 1214 | \\ .len = 0, |
| 1214 | 1215 | \\ .allocator = allocator, |
| 1215 | \\ } | |
| 1216 | \\ }; | |
| 1216 | 1217 | \\ } |
| 1217 | 1218 | \\}; |
| 1218 | 1219 | \\ |
| ... | ... | @@ -1235,10 +1236,10 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1235 | 1236 | \\const TINY_QUANTUM_SIZE = 1 << TINY_QUANTUM_SHIFT; |
| 1236 | 1237 | \\var block_aligned_stuff: usize = (4 + TINY_QUANTUM_SIZE) & ~(TINY_QUANTUM_SIZE - 1); |
| 1237 | 1238 | \\ |
| 1238 | \\export fn entry() -> usize { @sizeOf(@typeOf(block_aligned_stuff)) } | |
| 1239 | \\export fn entry() -> usize { return @sizeOf(@typeOf(block_aligned_stuff)); } | |
| 1239 | 1240 | , ".tmp_source.zig:3:60: error: unable to perform binary not operation on type '(integer literal)'"); |
| 1240 | 1241 | |
| 1241 | cases.addCase({ | |
| 1242 | cases.addCase(x: { | |
| 1242 | 1243 | const tc = cases.create("multiple files with private function error", |
| 1243 | 1244 | \\const foo = @import("foo.zig"); |
| 1244 | 1245 | \\ |
| ... | ... | @@ -1253,14 +1254,14 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1253 | 1254 | \\fn privateFunction() { } |
| 1254 | 1255 | ); |
| 1255 | 1256 | |
| 1256 | tc | |
| 1257 | break :x tc; | |
| 1257 | 1258 | }); |
| 1258 | 1259 | |
| 1259 | 1260 | cases.add("container init with non-type", |
| 1260 | 1261 | \\const zero: i32 = 0; |
| 1261 | 1262 | \\const a = zero{1}; |
| 1262 | 1263 | \\ |
| 1263 | \\export fn entry() -> usize { @sizeOf(@typeOf(a)) } | |
| 1264 | \\export fn entry() -> usize { return @sizeOf(@typeOf(a)); } | |
| 1264 | 1265 | , ".tmp_source.zig:2:11: error: expected type, found 'i32'"); |
| 1265 | 1266 | |
| 1266 | 1267 | cases.add("assign to constant field", |
| ... | ... | @@ -1288,22 +1289,22 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1288 | 1289 | \\ return 0; |
| 1289 | 1290 | \\} |
| 1290 | 1291 | \\ |
| 1291 | \\export fn entry() -> usize { @sizeOf(@typeOf(testTrickyDefer)) } | |
| 1292 | \\export fn entry() -> usize { return @sizeOf(@typeOf(testTrickyDefer)); } | |
| 1292 | 1293 | , ".tmp_source.zig:4:11: error: cannot return from defer expression"); |
| 1293 | 1294 | |
| 1294 | 1295 | cases.add("attempt to access var args out of bounds", |
| 1295 | 1296 | \\fn add(args: ...) -> i32 { |
| 1296 | \\ args[0] + args[1] | |
| 1297 | \\ return args[0] + args[1]; | |
| 1297 | 1298 | \\} |
| 1298 | 1299 | \\ |
| 1299 | 1300 | \\fn foo() -> i32 { |
| 1300 | \\ add(i32(1234)) | |
| 1301 | \\ return add(i32(1234)); | |
| 1301 | 1302 | \\} |
| 1302 | 1303 | \\ |
| 1303 | \\export fn entry() -> usize { @sizeOf(@typeOf(foo)) } | |
| 1304 | \\export fn entry() -> usize { return @sizeOf(@typeOf(foo)); } | |
| 1304 | 1305 | , |
| 1305 | ".tmp_source.zig:2:19: error: index 1 outside argument list of size 1", | |
| 1306 | ".tmp_source.zig:6:8: note: called from here"); | |
| 1306 | ".tmp_source.zig:2:26: error: index 1 outside argument list of size 1", | |
| 1307 | ".tmp_source.zig:6:15: note: called from here"); | |
| 1307 | 1308 | |
| 1308 | 1309 | cases.add("pass integer literal to var args", |
| 1309 | 1310 | \\fn add(args: ...) -> i32 { |
| ... | ... | @@ -1315,11 +1316,11 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1315 | 1316 | \\} |
| 1316 | 1317 | \\ |
| 1317 | 1318 | \\fn bar() -> i32 { |
| 1318 | \\ add(1, 2, 3, 4) | |
| 1319 | \\ return add(1, 2, 3, 4); | |
| 1319 | 1320 | \\} |
| 1320 | 1321 | \\ |
| 1321 | \\export fn entry() -> usize { @sizeOf(@typeOf(bar)) } | |
| 1322 | , ".tmp_source.zig:10:9: error: parameter of type '(integer literal)' requires comptime"); | |
| 1322 | \\export fn entry() -> usize { return @sizeOf(@typeOf(bar)); } | |
| 1323 | , ".tmp_source.zig:10:16: error: parameter of type '(integer literal)' requires comptime"); | |
| 1323 | 1324 | |
| 1324 | 1325 | cases.add("assign too big number to u16", |
| 1325 | 1326 | \\export fn foo() { |
| ... | ... | @@ -1329,12 +1330,12 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1329 | 1330 | |
| 1330 | 1331 | cases.add("global variable alignment non power of 2", |
| 1331 | 1332 | \\const some_data: [100]u8 align(3) = undefined; |
| 1332 | \\export fn entry() -> usize { @sizeOf(@typeOf(some_data)) } | |
| 1333 | \\export fn entry() -> usize { return @sizeOf(@typeOf(some_data)); } | |
| 1333 | 1334 | , ".tmp_source.zig:1:32: error: alignment value 3 is not a power of 2"); |
| 1334 | 1335 | |
| 1335 | 1336 | cases.add("function alignment non power of 2", |
| 1336 | 1337 | \\extern fn foo() align(3); |
| 1337 | \\export fn entry() { foo() } | |
| 1338 | \\export fn entry() { return foo(); } | |
| 1338 | 1339 | , ".tmp_source.zig:1:23: error: alignment value 3 is not a power of 2"); |
| 1339 | 1340 | |
| 1340 | 1341 | cases.add("compile log", |
| ... | ... | @@ -1369,7 +1370,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1369 | 1370 | \\ return *x; |
| 1370 | 1371 | \\} |
| 1371 | 1372 | \\ |
| 1372 | \\export fn entry() -> usize { @sizeOf(@typeOf(foo)) } | |
| 1373 | \\export fn entry() -> usize { return @sizeOf(@typeOf(foo)); } | |
| 1373 | 1374 | , ".tmp_source.zig:8:26: error: expected type '&const u3', found '&align(1:3:6) const u3'"); |
| 1374 | 1375 | |
| 1375 | 1376 | cases.add("referring to a struct that is invalid", |
| ... | ... | @@ -1405,14 +1406,14 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1405 | 1406 | \\export fn foo() { |
| 1406 | 1407 | \\ bar(); |
| 1407 | 1408 | \\} |
| 1408 | \\fn bar() -> i32 { 0 } | |
| 1409 | \\fn bar() -> i32 { return 0; } | |
| 1409 | 1410 | , ".tmp_source.zig:2:8: error: expression value is ignored"); |
| 1410 | 1411 | |
| 1411 | 1412 | cases.add("ignored assert-err-ok return value", |
| 1412 | 1413 | \\export fn foo() { |
| 1413 | 1414 | \\ %%bar(); |
| 1414 | 1415 | \\} |
| 1415 | \\fn bar() -> %i32 { 0 } | |
| 1416 | \\fn bar() -> %i32 { return 0; } | |
| 1416 | 1417 | , ".tmp_source.zig:2:5: error: expression value is ignored"); |
| 1417 | 1418 | |
| 1418 | 1419 | cases.add("ignored statement value", |
| ... | ... | @@ -1439,11 +1440,11 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1439 | 1440 | \\} |
| 1440 | 1441 | , ".tmp_source.zig:2:12: error: expression value is ignored"); |
| 1441 | 1442 | |
| 1442 | cases.add("ignored defered statement value", | |
| 1443 | cases.add("ignored defered function call", | |
| 1443 | 1444 | \\export fn foo() { |
| 1444 | 1445 | \\ defer bar(); |
| 1445 | 1446 | \\} |
| 1446 | \\fn bar() -> %i32 { 0 } | |
| 1447 | \\fn bar() -> %i32 { return 0; } | |
| 1447 | 1448 | , ".tmp_source.zig:2:14: error: expression value is ignored"); |
| 1448 | 1449 | |
| 1449 | 1450 | cases.add("dereference an array", |
| ... | ... | @@ -1454,7 +1455,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1454 | 1455 | \\ return (*out)[0..1]; |
| 1455 | 1456 | \\} |
| 1456 | 1457 | \\ |
| 1457 | \\export fn entry() -> usize { @sizeOf(@typeOf(pass)) } | |
| 1458 | \\export fn entry() -> usize { return @sizeOf(@typeOf(pass)); } | |
| 1458 | 1459 | , ".tmp_source.zig:4:5: error: attempt to dereference non pointer type '[10]u8'"); |
| 1459 | 1460 | |
| 1460 | 1461 | cases.add("pass const ptr to mutable ptr fn", |
| ... | ... | @@ -1467,10 +1468,10 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1467 | 1468 | \\ return true; |
| 1468 | 1469 | \\} |
| 1469 | 1470 | \\ |
| 1470 | \\export fn entry() -> usize { @sizeOf(@typeOf(foo)) } | |
| 1471 | \\export fn entry() -> usize { return @sizeOf(@typeOf(foo)); } | |
| 1471 | 1472 | , ".tmp_source.zig:4:19: error: expected type '&[]const u8', found '&const []const u8'"); |
| 1472 | 1473 | |
| 1473 | cases.addCase({ | |
| 1474 | cases.addCase(x: { | |
| 1474 | 1475 | const tc = cases.create("export collision", |
| 1475 | 1476 | \\const foo = @import("foo.zig"); |
| 1476 | 1477 | \\ |
| ... | ... | @@ -1486,13 +1487,13 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1486 | 1487 | \\pub const baz = 1234; |
| 1487 | 1488 | ); |
| 1488 | 1489 | |
| 1489 | tc | |
| 1490 | break :x tc; | |
| 1490 | 1491 | }); |
| 1491 | 1492 | |
| 1492 | 1493 | cases.add("pass non-copyable type by value to function", |
| 1493 | 1494 | \\const Point = struct { x: i32, y: i32, }; |
| 1494 | 1495 | \\fn foo(p: Point) { } |
| 1495 | \\export fn entry() -> usize { @sizeOf(@typeOf(foo)) } | |
| 1496 | \\export fn entry() -> usize { return @sizeOf(@typeOf(foo)); } | |
| 1496 | 1497 | , ".tmp_source.zig:2:11: error: type 'Point' is not copyable; cannot pass by value"); |
| 1497 | 1498 | |
| 1498 | 1499 | cases.add("implicit cast from array to mutable slice", |
| ... | ... | @@ -1515,7 +1516,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1515 | 1516 | \\fn foo(e: error) -> u2 { |
| 1516 | 1517 | \\ return u2(e); |
| 1517 | 1518 | \\} |
| 1518 | \\export fn entry() -> usize { @sizeOf(@typeOf(foo)) } | |
| 1519 | \\export fn entry() -> usize { return @sizeOf(@typeOf(foo)); } | |
| 1519 | 1520 | , ".tmp_source.zig:4:14: error: too many error values to fit in 'u2'"); |
| 1520 | 1521 | |
| 1521 | 1522 | cases.add("asm at compile time", |
| ... | ... | @@ -1665,17 +1666,17 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1665 | 1666 | |
| 1666 | 1667 | cases.add("inner struct member shadowing outer struct member", |
| 1667 | 1668 | \\fn A() -> type { |
| 1668 | \\ struct { | |
| 1669 | \\ return struct { | |
| 1669 | 1670 | \\ b: B(), |
| 1670 | 1671 | \\ |
| 1671 | 1672 | \\ const Self = this; |
| 1672 | 1673 | \\ |
| 1673 | 1674 | \\ fn B() -> type { |
| 1674 | \\ struct { | |
| 1675 | \\ return struct { | |
| 1675 | 1676 | \\ const Self = this; |
| 1676 | \\ } | |
| 1677 | \\ }; | |
| 1677 | 1678 | \\ } |
| 1678 | \\ } | |
| 1679 | \\ }; | |
| 1679 | 1680 | \\} |
| 1680 | 1681 | \\comptime { |
| 1681 | 1682 | \\ assert(A().B().Self != A().Self); |
| ... | ... | @@ -1691,7 +1692,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1691 | 1692 | \\export fn foo() { |
| 1692 | 1693 | \\ while (bar()) {} |
| 1693 | 1694 | \\} |
| 1694 | \\fn bar() -> ?i32 { 1 } | |
| 1695 | \\fn bar() -> ?i32 { return 1; } | |
| 1695 | 1696 | , |
| 1696 | 1697 | ".tmp_source.zig:2:15: error: expected type 'bool', found '?i32'"); |
| 1697 | 1698 | |
| ... | ... | @@ -1699,7 +1700,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1699 | 1700 | \\export fn foo() { |
| 1700 | 1701 | \\ while (bar()) {} |
| 1701 | 1702 | \\} |
| 1702 | \\fn bar() -> %i32 { 1 } | |
| 1703 | \\fn bar() -> %i32 { return 1; } | |
| 1703 | 1704 | , |
| 1704 | 1705 | ".tmp_source.zig:2:15: error: expected type 'bool', found '%i32'"); |
| 1705 | 1706 | |
| ... | ... | @@ -1707,7 +1708,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1707 | 1708 | \\export fn foo() { |
| 1708 | 1709 | \\ while (bar()) |x| {} |
| 1709 | 1710 | \\} |
| 1710 | \\fn bar() -> bool { true } | |
| 1711 | \\fn bar() -> bool { return true; } | |
| 1711 | 1712 | , |
| 1712 | 1713 | ".tmp_source.zig:2:15: error: expected nullable type, found 'bool'"); |
| 1713 | 1714 | |
| ... | ... | @@ -1715,7 +1716,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1715 | 1716 | \\export fn foo() { |
| 1716 | 1717 | \\ while (bar()) |x| {} |
| 1717 | 1718 | \\} |
| 1718 | \\fn bar() -> %i32 { 1 } | |
| 1719 | \\fn bar() -> %i32 { return 1; } | |
| 1719 | 1720 | , |
| 1720 | 1721 | ".tmp_source.zig:2:15: error: expected nullable type, found '%i32'"); |
| 1721 | 1722 | |
| ... | ... | @@ -1723,7 +1724,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1723 | 1724 | \\export fn foo() { |
| 1724 | 1725 | \\ while (bar()) |x| {} else |err| {} |
| 1725 | 1726 | \\} |
| 1726 | \\fn bar() -> bool { true } | |
| 1727 | \\fn bar() -> bool { return true; } | |
| 1727 | 1728 | , |
| 1728 | 1729 | ".tmp_source.zig:2:15: error: expected error union type, found 'bool'"); |
| 1729 | 1730 | |
| ... | ... | @@ -1731,7 +1732,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1731 | 1732 | \\export fn foo() { |
| 1732 | 1733 | \\ while (bar()) |x| {} else |err| {} |
| 1733 | 1734 | \\} |
| 1734 | \\fn bar() -> ?i32 { 1 } | |
| 1735 | \\fn bar() -> ?i32 { return 1; } | |
| 1735 | 1736 | , |
| 1736 | 1737 | ".tmp_source.zig:2:15: error: expected error union type, found '?i32'"); |
| 1737 | 1738 | |
| ... | ... | @@ -1762,17 +1763,17 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1762 | 1763 | |
| 1763 | 1764 | cases.add("signed integer division", |
| 1764 | 1765 | \\export fn foo(a: i32, b: i32) -> i32 { |
| 1765 | \\ a / b | |
| 1766 | \\ return a / b; | |
| 1766 | 1767 | \\} |
| 1767 | 1768 | , |
| 1768 | ".tmp_source.zig:2:7: error: division with 'i32' and 'i32': signed integers must use @divTrunc, @divFloor, or @divExact"); | |
| 1769 | ".tmp_source.zig:2:14: error: division with 'i32' and 'i32': signed integers must use @divTrunc, @divFloor, or @divExact"); | |
| 1769 | 1770 | |
| 1770 | 1771 | cases.add("signed integer remainder division", |
| 1771 | 1772 | \\export fn foo(a: i32, b: i32) -> i32 { |
| 1772 | \\ a % b | |
| 1773 | \\ return a % b; | |
| 1773 | 1774 | \\} |
| 1774 | 1775 | , |
| 1775 | ".tmp_source.zig:2:7: error: remainder division with 'i32' and 'i32': signed integers and floats must use @rem or @mod"); | |
| 1776 | ".tmp_source.zig:2:14: error: remainder division with 'i32' and 'i32': signed integers and floats must use @rem or @mod"); | |
| 1776 | 1777 | |
| 1777 | 1778 | cases.add("cast negative value to unsigned integer", |
| 1778 | 1779 | \\comptime { |
| ... | ... | @@ -1922,17 +1923,17 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 1922 | 1923 | |
| 1923 | 1924 | cases.add("explicit cast float literal to integer when there is a fraction component", |
| 1924 | 1925 | \\export fn entry() -> i32 { |
| 1925 | \\ i32(12.34) | |
| 1926 | \\ return i32(12.34); | |
| 1926 | 1927 | \\} |
| 1927 | 1928 | , |
| 1928 | ".tmp_source.zig:2:9: error: fractional component prevents float value 12.340000 from being casted to type 'i32'"); | |
| 1929 | ".tmp_source.zig:2:16: error: fractional component prevents float value 12.340000 from being casted to type 'i32'"); | |
| 1929 | 1930 | |
| 1930 | 1931 | cases.add("non pointer given to @ptrToInt", |
| 1931 | 1932 | \\export fn entry(x: i32) -> usize { |
| 1932 | \\ @ptrToInt(x) | |
| 1933 | \\ return @ptrToInt(x); | |
| 1933 | 1934 | \\} |
| 1934 | 1935 | , |
| 1935 | ".tmp_source.zig:2:15: error: expected pointer, found 'i32'"); | |
| 1936 | ".tmp_source.zig:2:22: error: expected pointer, found 'i32'"); | |
| 1936 | 1937 | |
| 1937 | 1938 | cases.add("@shlExact shifts out 1 bits", |
| 1938 | 1939 | \\comptime { |
| ... | ... | @@ -2028,7 +2029,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 2028 | 2029 | |
| 2029 | 2030 | cases.add("@alignCast expects pointer or slice", |
| 2030 | 2031 | \\export fn entry() { |
| 2031 | \\ @alignCast(4, u32(3)) | |
| 2032 | \\ @alignCast(4, u32(3)); | |
| 2032 | 2033 | \\} |
| 2033 | 2034 | , |
| 2034 | 2035 | ".tmp_source.zig:2:22: error: expected pointer or slice, found 'u32'"); |
| ... | ... | @@ -2040,7 +2041,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 2040 | 2041 | \\fn testImplicitlyDecreaseFnAlign(ptr: fn () align(8) -> i32, answer: i32) { |
| 2041 | 2042 | \\ if (ptr() != answer) unreachable; |
| 2042 | 2043 | \\} |
| 2043 | \\fn alignedSmall() align(4) -> i32 { 1234 } | |
| 2044 | \\fn alignedSmall() align(4) -> i32 { return 1234; } | |
| 2044 | 2045 | , |
| 2045 | 2046 | ".tmp_source.zig:2:35: error: expected type 'fn() align(8) -> i32', found 'fn() align(4) -> i32'"); |
| 2046 | 2047 | |
| ... | ... | @@ -2206,17 +2207,17 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 2206 | 2207 | \\const Mode = @import("builtin").Mode; |
| 2207 | 2208 | \\ |
| 2208 | 2209 | \\fn Free(comptime filename: []const u8) -> TestCase { |
| 2209 | \\ TestCase { | |
| 2210 | \\ return TestCase { | |
| 2210 | 2211 | \\ .filename = filename, |
| 2211 | 2212 | \\ .problem_type = ProblemType.Free, |
| 2212 | \\ } | |
| 2213 | \\ }; | |
| 2213 | 2214 | \\} |
| 2214 | 2215 | \\ |
| 2215 | 2216 | \\fn LibC(comptime filename: []const u8) -> TestCase { |
| 2216 | \\ TestCase { | |
| 2217 | \\ return TestCase { | |
| 2217 | 2218 | \\ .filename = filename, |
| 2218 | 2219 | \\ .problem_type = ProblemType.LinkLibC, |
| 2219 | \\ } | |
| 2220 | \\ }; | |
| 2220 | 2221 | \\} |
| 2221 | 2222 | \\ |
| 2222 | 2223 | \\const TestCase = struct { |
| ... | ... | @@ -2374,9 +2375,9 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 2374 | 2375 | \\pub fn MemoryPool(comptime T: type) -> type { |
| 2375 | 2376 | \\ const free_list_t = @compileError("aoeu"); |
| 2376 | 2377 | \\ |
| 2377 | \\ struct { | |
| 2378 | \\ return struct { | |
| 2378 | 2379 | \\ free_list: free_list_t, |
| 2379 | \\ } | |
| 2380 | \\ }; | |
| 2380 | 2381 | \\} |
| 2381 | 2382 | \\ |
| 2382 | 2383 | \\export fn entry() { |
| ... | ... | @@ -2651,7 +2652,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 2651 | 2652 | \\ C: bool, |
| 2652 | 2653 | \\}; |
| 2653 | 2654 | \\export fn entry() { |
| 2654 | \\ var a = Payload { .A = { 1234 } }; | |
| 2655 | \\ var a = Payload { .A = 1234 }; | |
| 2655 | 2656 | \\} |
| 2656 | 2657 | , |
| 2657 | 2658 | ".tmp_source.zig:6:29: error: extern union does not support enum tag type"); |
| ... | ... | @@ -2668,7 +2669,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 2668 | 2669 | \\ C: bool, |
| 2669 | 2670 | \\}; |
| 2670 | 2671 | \\export fn entry() { |
| 2671 | \\ var a = Payload { .A = { 1234 } }; | |
| 2672 | \\ var a = Payload { .A = 1234 }; | |
| 2672 | 2673 | \\} |
| 2673 | 2674 | , |
| 2674 | 2675 | ".tmp_source.zig:6:29: error: packed union does not support enum tag type"); |
| ... | ... | @@ -2680,7 +2681,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { |
| 2680 | 2681 | \\ C: bool, |
| 2681 | 2682 | \\}; |
| 2682 | 2683 | \\export fn entry() { |
| 2683 | \\ const a = Payload { .A = { 1234 } }; | |
| 2684 | \\ const a = Payload { .A = 1234 }; | |
| 2684 | 2685 | \\ foo(a); |
| 2685 | 2686 | \\} |
| 2686 | 2687 | \\fn foo(a: &const Payload) { |
test/debug_safety.zig+15-15| ... | ... | @@ -19,7 +19,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 19 | 19 | \\ baz(bar(a)); |
| 20 | 20 | \\} |
| 21 | 21 | \\fn bar(a: []const i32) -> i32 { |
| 22 | \\ a[4] | |
| 22 | \\ return a[4]; | |
| 23 | 23 | \\} |
| 24 | 24 | \\fn baz(a: i32) { } |
| 25 | 25 | ); |
| ... | ... | @@ -34,7 +34,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 34 | 34 | \\ if (x == 0) return error.Whatever; |
| 35 | 35 | \\} |
| 36 | 36 | \\fn add(a: u16, b: u16) -> u16 { |
| 37 | \\ a + b | |
| 37 | \\ return a + b; | |
| 38 | 38 | \\} |
| 39 | 39 | ); |
| 40 | 40 | |
| ... | ... | @@ -48,7 +48,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 48 | 48 | \\ if (x == 0) return error.Whatever; |
| 49 | 49 | \\} |
| 50 | 50 | \\fn sub(a: u16, b: u16) -> u16 { |
| 51 | \\ a - b | |
| 51 | \\ return a - b; | |
| 52 | 52 | \\} |
| 53 | 53 | ); |
| 54 | 54 | |
| ... | ... | @@ -62,7 +62,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 62 | 62 | \\ if (x == 0) return error.Whatever; |
| 63 | 63 | \\} |
| 64 | 64 | \\fn mul(a: u16, b: u16) -> u16 { |
| 65 | \\ a * b | |
| 65 | \\ return a * b; | |
| 66 | 66 | \\} |
| 67 | 67 | ); |
| 68 | 68 | |
| ... | ... | @@ -76,7 +76,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 76 | 76 | \\ if (x == 32767) return error.Whatever; |
| 77 | 77 | \\} |
| 78 | 78 | \\fn neg(a: i16) -> i16 { |
| 79 | \\ -a | |
| 79 | \\ return -a; | |
| 80 | 80 | \\} |
| 81 | 81 | ); |
| 82 | 82 | |
| ... | ... | @@ -90,7 +90,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 90 | 90 | \\ if (x == 32767) return error.Whatever; |
| 91 | 91 | \\} |
| 92 | 92 | \\fn div(a: i16, b: i16) -> i16 { |
| 93 | \\ @divTrunc(a, b) | |
| 93 | \\ return @divTrunc(a, b); | |
| 94 | 94 | \\} |
| 95 | 95 | ); |
| 96 | 96 | |
| ... | ... | @@ -104,7 +104,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 104 | 104 | \\ if (x == 0) return error.Whatever; |
| 105 | 105 | \\} |
| 106 | 106 | \\fn shl(a: i16, b: u4) -> i16 { |
| 107 | \\ @shlExact(a, b) | |
| 107 | \\ return @shlExact(a, b); | |
| 108 | 108 | \\} |
| 109 | 109 | ); |
| 110 | 110 | |
| ... | ... | @@ -118,7 +118,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 118 | 118 | \\ if (x == 0) return error.Whatever; |
| 119 | 119 | \\} |
| 120 | 120 | \\fn shl(a: u16, b: u4) -> u16 { |
| 121 | \\ @shlExact(a, b) | |
| 121 | \\ return @shlExact(a, b); | |
| 122 | 122 | \\} |
| 123 | 123 | ); |
| 124 | 124 | |
| ... | ... | @@ -132,7 +132,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 132 | 132 | \\ if (x == 0) return error.Whatever; |
| 133 | 133 | \\} |
| 134 | 134 | \\fn shr(a: i16, b: u4) -> i16 { |
| 135 | \\ @shrExact(a, b) | |
| 135 | \\ return @shrExact(a, b); | |
| 136 | 136 | \\} |
| 137 | 137 | ); |
| 138 | 138 | |
| ... | ... | @@ -146,7 +146,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 146 | 146 | \\ if (x == 0) return error.Whatever; |
| 147 | 147 | \\} |
| 148 | 148 | \\fn shr(a: u16, b: u4) -> u16 { |
| 149 | \\ @shrExact(a, b) | |
| 149 | \\ return @shrExact(a, b); | |
| 150 | 150 | \\} |
| 151 | 151 | ); |
| 152 | 152 | |
| ... | ... | @@ -159,7 +159,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 159 | 159 | \\ const x = div0(999, 0); |
| 160 | 160 | \\} |
| 161 | 161 | \\fn div0(a: i32, b: i32) -> i32 { |
| 162 | \\ @divTrunc(a, b) | |
| 162 | \\ return @divTrunc(a, b); | |
| 163 | 163 | \\} |
| 164 | 164 | ); |
| 165 | 165 | |
| ... | ... | @@ -173,7 +173,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 173 | 173 | \\ if (x == 0) return error.Whatever; |
| 174 | 174 | \\} |
| 175 | 175 | \\fn divExact(a: i32, b: i32) -> i32 { |
| 176 | \\ @divExact(a, b) | |
| 176 | \\ return @divExact(a, b); | |
| 177 | 177 | \\} |
| 178 | 178 | ); |
| 179 | 179 | |
| ... | ... | @@ -187,7 +187,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 187 | 187 | \\ if (x.len == 0) return error.Whatever; |
| 188 | 188 | \\} |
| 189 | 189 | \\fn widenSlice(slice: []align(1) const u8) -> []align(1) const i32 { |
| 190 | \\ ([]align(1) const i32)(slice) | |
| 190 | \\ return ([]align(1) const i32)(slice); | |
| 191 | 191 | \\} |
| 192 | 192 | ); |
| 193 | 193 | |
| ... | ... | @@ -201,7 +201,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 201 | 201 | \\ if (x == 0) return error.Whatever; |
| 202 | 202 | \\} |
| 203 | 203 | \\fn shorten_cast(x: i32) -> i8 { |
| 204 | \\ i8(x) | |
| 204 | \\ return i8(x); | |
| 205 | 205 | \\} |
| 206 | 206 | ); |
| 207 | 207 | |
| ... | ... | @@ -215,7 +215,7 @@ pub fn addCases(cases: &tests.CompareOutputContext) { |
| 215 | 215 | \\ if (x == 0) return error.Whatever; |
| 216 | 216 | \\} |
| 217 | 217 | \\fn unsigned_cast(x: i32) -> u32 { |
| 218 | \\ u32(x) | |
| 218 | \\ return u32(x); | |
| 219 | 219 | \\} |
| 220 | 220 | ); |
| 221 | 221 |
test/standalone/pkg_import/pkg.zig+1-1| ... | ... | @@ -1 +1 @@ |
| 1 | pub fn add(a: i32, b: i32) -> i32 { a + b } | |
| 1 | pub fn add(a: i32, b: i32) -> i32 { return a + b; } |
test/tests.zig+3-3| ... | ... | @@ -284,7 +284,7 @@ pub const CompareOutputContext = struct { |
| 284 | 284 | warn("Process {} terminated unexpectedly\n", full_exe_path); |
| 285 | 285 | return error.TestFailed; |
| 286 | 286 | }, |
| 287 | }; | |
| 287 | } | |
| 288 | 288 | |
| 289 | 289 | |
| 290 | 290 | if (!mem.eql(u8, self.expected_output, stdout.toSliceConst())) { |
| ... | ... | @@ -615,7 +615,7 @@ pub const CompileErrorContext = struct { |
| 615 | 615 | warn("Process {} terminated unexpectedly\n", b.zig_exe); |
| 616 | 616 | return error.TestFailed; |
| 617 | 617 | }, |
| 618 | }; | |
| 618 | } | |
| 619 | 619 | |
| 620 | 620 | |
| 621 | 621 | const stdout = stdout_buf.toSliceConst(); |
| ... | ... | @@ -891,7 +891,7 @@ pub const TranslateCContext = struct { |
| 891 | 891 | warn("Compilation terminated unexpectedly\n"); |
| 892 | 892 | return error.TestFailed; |
| 893 | 893 | }, |
| 894 | }; | |
| 894 | } | |
| 895 | 895 | |
| 896 | 896 | const stdout = stdout_buf.toSliceConst(); |
| 897 | 897 | const stderr = stderr_buf.toSliceConst(); |
test/translate_c.zig+55-55| ... | ... | @@ -203,13 +203,13 @@ pub fn addCases(cases: &tests.TranslateCContext) { |
| 203 | 203 | \\pub extern var fn_ptr: ?extern fn(); |
| 204 | 204 | , |
| 205 | 205 | \\pub inline fn foo() { |
| 206 | \\ (??fn_ptr)() | |
| 206 | \\ return (??fn_ptr)(); | |
| 207 | 207 | \\} |
| 208 | 208 | , |
| 209 | 209 | \\pub extern var fn_ptr2: ?extern fn(c_int, f32) -> u8; |
| 210 | 210 | , |
| 211 | 211 | \\pub inline fn bar(arg0: c_int, arg1: f32) -> u8 { |
| 212 | \\ (??fn_ptr2)(arg0, arg1) | |
| 212 | \\ return (??fn_ptr2)(arg0, arg1); | |
| 213 | 213 | \\} |
| 214 | 214 | ); |
| 215 | 215 | |
| ... | ... | @@ -475,10 +475,10 @@ pub fn addCases(cases: &tests.TranslateCContext) { |
| 475 | 475 | \\pub export fn max(a: c_int) { |
| 476 | 476 | \\ var b: c_int; |
| 477 | 477 | \\ var c: c_int; |
| 478 | \\ c = { | |
| 478 | \\ c = x: { | |
| 479 | 479 | \\ const _tmp = a; |
| 480 | 480 | \\ b = _tmp; |
| 481 | \\ _tmp | |
| 481 | \\ break :x _tmp; | |
| 482 | 482 | \\ }; |
| 483 | 483 | \\} |
| 484 | 484 | ); |
| ... | ... | @@ -613,9 +613,9 @@ pub fn addCases(cases: &tests.TranslateCContext) { |
| 613 | 613 | \\} |
| 614 | 614 | , |
| 615 | 615 | \\pub export fn foo() -> c_int { |
| 616 | \\ return { | |
| 616 | \\ return x: { | |
| 617 | 617 | \\ _ = 1; |
| 618 | \\ 2 | |
| 618 | \\ break :x 2; | |
| 619 | 619 | \\ }; |
| 620 | 620 | \\} |
| 621 | 621 | ); |
| ... | ... | @@ -645,45 +645,45 @@ pub fn addCases(cases: &tests.TranslateCContext) { |
| 645 | 645 | , |
| 646 | 646 | \\pub export fn foo() { |
| 647 | 647 | \\ var a: c_int = 0; |
| 648 | \\ a += { | |
| 648 | \\ a += x: { | |
| 649 | 649 | \\ const _ref = &a; |
| 650 | 650 | \\ (*_ref) = ((*_ref) + 1); |
| 651 | \\ *_ref | |
| 651 | \\ break :x *_ref; | |
| 652 | 652 | \\ }; |
| 653 | \\ a -= { | |
| 653 | \\ a -= x: { | |
| 654 | 654 | \\ const _ref = &a; |
| 655 | 655 | \\ (*_ref) = ((*_ref) - 1); |
| 656 | \\ *_ref | |
| 656 | \\ break :x *_ref; | |
| 657 | 657 | \\ }; |
| 658 | \\ a *= { | |
| 658 | \\ a *= x: { | |
| 659 | 659 | \\ const _ref = &a; |
| 660 | 660 | \\ (*_ref) = ((*_ref) * 1); |
| 661 | \\ *_ref | |
| 661 | \\ break :x *_ref; | |
| 662 | 662 | \\ }; |
| 663 | \\ a &= { | |
| 663 | \\ a &= x: { | |
| 664 | 664 | \\ const _ref = &a; |
| 665 | 665 | \\ (*_ref) = ((*_ref) & 1); |
| 666 | \\ *_ref | |
| 666 | \\ break :x *_ref; | |
| 667 | 667 | \\ }; |
| 668 | \\ a |= { | |
| 668 | \\ a |= x: { | |
| 669 | 669 | \\ const _ref = &a; |
| 670 | 670 | \\ (*_ref) = ((*_ref) | 1); |
| 671 | \\ *_ref | |
| 671 | \\ break :x *_ref; | |
| 672 | 672 | \\ }; |
| 673 | \\ a ^= { | |
| 673 | \\ a ^= x: { | |
| 674 | 674 | \\ const _ref = &a; |
| 675 | 675 | \\ (*_ref) = ((*_ref) ^ 1); |
| 676 | \\ *_ref | |
| 676 | \\ break :x *_ref; | |
| 677 | 677 | \\ }; |
| 678 | \\ a >>= @import("std").math.Log2Int(c_int)({ | |
| 678 | \\ a >>= @import("std").math.Log2Int(c_int)(x: { | |
| 679 | 679 | \\ const _ref = &a; |
| 680 | 680 | \\ (*_ref) = ((*_ref) >> @import("std").math.Log2Int(c_int)(1)); |
| 681 | \\ *_ref | |
| 681 | \\ break :x *_ref; | |
| 682 | 682 | \\ }); |
| 683 | \\ a <<= @import("std").math.Log2Int(c_int)({ | |
| 683 | \\ a <<= @import("std").math.Log2Int(c_int)(x: { | |
| 684 | 684 | \\ const _ref = &a; |
| 685 | 685 | \\ (*_ref) = ((*_ref) << @import("std").math.Log2Int(c_int)(1)); |
| 686 | \\ *_ref | |
| 686 | \\ break :x *_ref; | |
| 687 | 687 | \\ }); |
| 688 | 688 | \\} |
| 689 | 689 | ); |
| ... | ... | @@ -703,45 +703,45 @@ pub fn addCases(cases: &tests.TranslateCContext) { |
| 703 | 703 | , |
| 704 | 704 | \\pub export fn foo() { |
| 705 | 705 | \\ var a: c_uint = c_uint(0); |
| 706 | \\ a +%= { | |
| 706 | \\ a +%= x: { | |
| 707 | 707 | \\ const _ref = &a; |
| 708 | 708 | \\ (*_ref) = ((*_ref) +% c_uint(1)); |
| 709 | \\ *_ref | |
| 709 | \\ break :x *_ref; | |
| 710 | 710 | \\ }; |
| 711 | \\ a -%= { | |
| 711 | \\ a -%= x: { | |
| 712 | 712 | \\ const _ref = &a; |
| 713 | 713 | \\ (*_ref) = ((*_ref) -% c_uint(1)); |
| 714 | \\ *_ref | |
| 714 | \\ break :x *_ref; | |
| 715 | 715 | \\ }; |
| 716 | \\ a *%= { | |
| 716 | \\ a *%= x: { | |
| 717 | 717 | \\ const _ref = &a; |
| 718 | 718 | \\ (*_ref) = ((*_ref) *% c_uint(1)); |
| 719 | \\ *_ref | |
| 719 | \\ break :x *_ref; | |
| 720 | 720 | \\ }; |
| 721 | \\ a &= { | |
| 721 | \\ a &= x: { | |
| 722 | 722 | \\ const _ref = &a; |
| 723 | 723 | \\ (*_ref) = ((*_ref) & c_uint(1)); |
| 724 | \\ *_ref | |
| 724 | \\ break :x *_ref; | |
| 725 | 725 | \\ }; |
| 726 | \\ a |= { | |
| 726 | \\ a |= x: { | |
| 727 | 727 | \\ const _ref = &a; |
| 728 | 728 | \\ (*_ref) = ((*_ref) | c_uint(1)); |
| 729 | \\ *_ref | |
| 729 | \\ break :x *_ref; | |
| 730 | 730 | \\ }; |
| 731 | \\ a ^= { | |
| 731 | \\ a ^= x: { | |
| 732 | 732 | \\ const _ref = &a; |
| 733 | 733 | \\ (*_ref) = ((*_ref) ^ c_uint(1)); |
| 734 | \\ *_ref | |
| 734 | \\ break :x *_ref; | |
| 735 | 735 | \\ }; |
| 736 | \\ a >>= @import("std").math.Log2Int(c_uint)({ | |
| 736 | \\ a >>= @import("std").math.Log2Int(c_uint)(x: { | |
| 737 | 737 | \\ const _ref = &a; |
| 738 | 738 | \\ (*_ref) = ((*_ref) >> @import("std").math.Log2Int(c_uint)(1)); |
| 739 | \\ *_ref | |
| 739 | \\ break :x *_ref; | |
| 740 | 740 | \\ }); |
| 741 | \\ a <<= @import("std").math.Log2Int(c_uint)({ | |
| 741 | \\ a <<= @import("std").math.Log2Int(c_uint)(x: { | |
| 742 | 742 | \\ const _ref = &a; |
| 743 | 743 | \\ (*_ref) = ((*_ref) << @import("std").math.Log2Int(c_uint)(1)); |
| 744 | \\ *_ref | |
| 744 | \\ break :x *_ref; | |
| 745 | 745 | \\ }); |
| 746 | 746 | \\} |
| 747 | 747 | ); |
| ... | ... | @@ -778,29 +778,29 @@ pub fn addCases(cases: &tests.TranslateCContext) { |
| 778 | 778 | \\ i -= 1; |
| 779 | 779 | \\ u +%= 1; |
| 780 | 780 | \\ u -%= 1; |
| 781 | \\ i = { | |
| 781 | \\ i = x: { | |
| 782 | 782 | \\ const _ref = &i; |
| 783 | 783 | \\ const _tmp = *_ref; |
| 784 | 784 | \\ (*_ref) += 1; |
| 785 | \\ _tmp | |
| 785 | \\ break :x _tmp; | |
| 786 | 786 | \\ }; |
| 787 | \\ i = { | |
| 787 | \\ i = x: { | |
| 788 | 788 | \\ const _ref = &i; |
| 789 | 789 | \\ const _tmp = *_ref; |
| 790 | 790 | \\ (*_ref) -= 1; |
| 791 | \\ _tmp | |
| 791 | \\ break :x _tmp; | |
| 792 | 792 | \\ }; |
| 793 | \\ u = { | |
| 793 | \\ u = x: { | |
| 794 | 794 | \\ const _ref = &u; |
| 795 | 795 | \\ const _tmp = *_ref; |
| 796 | 796 | \\ (*_ref) +%= 1; |
| 797 | \\ _tmp | |
| 797 | \\ break :x _tmp; | |
| 798 | 798 | \\ }; |
| 799 | \\ u = { | |
| 799 | \\ u = x: { | |
| 800 | 800 | \\ const _ref = &u; |
| 801 | 801 | \\ const _tmp = *_ref; |
| 802 | 802 | \\ (*_ref) -%= 1; |
| 803 | \\ _tmp | |
| 803 | \\ break :x _tmp; | |
| 804 | 804 | \\ }; |
| 805 | 805 | \\} |
| 806 | 806 | ); |
| ... | ... | @@ -826,25 +826,25 @@ pub fn addCases(cases: &tests.TranslateCContext) { |
| 826 | 826 | \\ i -= 1; |
| 827 | 827 | \\ u +%= 1; |
| 828 | 828 | \\ u -%= 1; |
| 829 | \\ i = { | |
| 829 | \\ i = x: { | |
| 830 | 830 | \\ const _ref = &i; |
| 831 | 831 | \\ (*_ref) += 1; |
| 832 | \\ *_ref | |
| 832 | \\ break :x *_ref; | |
| 833 | 833 | \\ }; |
| 834 | \\ i = { | |
| 834 | \\ i = x: { | |
| 835 | 835 | \\ const _ref = &i; |
| 836 | 836 | \\ (*_ref) -= 1; |
| 837 | \\ *_ref | |
| 837 | \\ break :x *_ref; | |
| 838 | 838 | \\ }; |
| 839 | \\ u = { | |
| 839 | \\ u = x: { | |
| 840 | 840 | \\ const _ref = &u; |
| 841 | 841 | \\ (*_ref) +%= 1; |
| 842 | \\ *_ref | |
| 842 | \\ break :x *_ref; | |
| 843 | 843 | \\ }; |
| 844 | \\ u = { | |
| 844 | \\ u = x: { | |
| 845 | 845 | \\ const _ref = &u; |
| 846 | 846 | \\ (*_ref) -%= 1; |
| 847 | \\ *_ref | |
| 847 | \\ break :x *_ref; | |
| 848 | 848 | \\ }; |
| 849 | 849 | \\} |
| 850 | 850 | ); |
| ... | ... | @@ -1037,7 +1037,7 @@ pub fn addCases(cases: &tests.TranslateCContext) { |
| 1037 | 1037 | \\pub const glClearPFN = PFNGLCLEARPROC; |
| 1038 | 1038 | , |
| 1039 | 1039 | \\pub inline fn glClearUnion(arg0: GLbitfield) { |
| 1040 | \\ (??glProcs.gl.Clear)(arg0) | |
| 1040 | \\ return (??glProcs.gl.Clear)(arg0); | |
| 1041 | 1041 | \\} |
| 1042 | 1042 | , |
| 1043 | 1043 | \\pub const OpenGLProcs = union_OpenGLProcs; |