authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-30 20:35:54-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-30 20:35:54-04:00
loga35b366eb64272c6d4646aedc035a837ed0c3cb0
treedcde18b655e59df2b24f6804eb069f3f43393b28
parent76ab1d2b6c9eedd861920ae6b6f8ee06aa482159

[breaking] delete ptr deref prefix op

start using zig-fmt-pointer-reform branch build of zig fmt to fix code to use the new syntax all of test/cases/* are processed, but there are more left to be done - all the std lib used by the behavior tests

49 files changed, 1694 insertions(+), 880 deletions(-)

src/all_types.hpp-1
......@@ -614,7 +614,6 @@ enum PrefixOp {
614614 PrefixOpBinNot,
615615 PrefixOpNegation,
616616 PrefixOpNegationWrap,
617 PrefixOpDereference,
618617 PrefixOpMaybe,
619618 PrefixOpUnwrapMaybe,
620619};
src/ast_render.cpp-1
......@@ -66,7 +66,6 @@ static const char *prefix_op_str(PrefixOp prefix_op) {
6666 case PrefixOpNegationWrap: return "-%";
6767 case PrefixOpBoolNot: return "!";
6868 case PrefixOpBinNot: return "~";
69 case PrefixOpDereference: return "*";
7069 case PrefixOpMaybe: return "?";
7170 case PrefixOpUnwrapMaybe: return "??";
7271 }
src/ir.cpp-2
......@@ -4696,8 +4696,6 @@ static IrInstruction *ir_gen_prefix_op_expr(IrBuilder *irb, Scope *scope, AstNod
46964696 return ir_lval_wrap(irb, scope, ir_gen_prefix_op_id(irb, scope, node, IrUnOpNegation), lval);
46974697 case PrefixOpNegationWrap:
46984698 return ir_lval_wrap(irb, scope, ir_gen_prefix_op_id(irb, scope, node, IrUnOpNegationWrap), lval);
4699 case PrefixOpDereference:
4700 return ir_gen_prefix_op_id_lval(irb, scope, node, IrUnOpDereference, lval);
47014699 case PrefixOpMaybe:
47024700 return ir_lval_wrap(irb, scope, ir_gen_prefix_op_id(irb, scope, node, IrUnOpMaybe), lval);
47034701 case PrefixOpUnwrapMaybe:
src/parser.cpp+1-12
......@@ -1165,10 +1165,8 @@ static PrefixOp tok_to_prefix_op(Token *token) {
11651165 case TokenIdDash: return PrefixOpNegation;
11661166 case TokenIdMinusPercent: return PrefixOpNegationWrap;
11671167 case TokenIdTilde: return PrefixOpBinNot;
1168 case TokenIdStar: return PrefixOpDereference;
11691168 case TokenIdMaybe: return PrefixOpMaybe;
11701169 case TokenIdDoubleQuestion: return PrefixOpUnwrapMaybe;
1171 case TokenIdStarStar: return PrefixOpDereference;
11721170 default: return PrefixOpInvalid;
11731171 }
11741172}
......@@ -1214,7 +1212,7 @@ static AstNode *ast_parse_addr_of(ParseContext *pc, size_t *token_index) {
12141212
12151213/*
12161214PrefixOpExpression = PrefixOp ErrorSetExpr | SuffixOpExpression
1217PrefixOp = "!" | "-" | "~" | "*" | ("&" option("align" "(" Expression option(":" Integer ":" Integer) ")" ) option("const") option("volatile")) | "?" | "??" | "-%" | "try" | "await"
1215PrefixOp = "!" | "-" | "~" | ("*" option("align" "(" Expression option(":" Integer ":" Integer) ")" ) option("const") option("volatile")) | "?" | "??" | "-%" | "try" | "await"
12181216*/
12191217static AstNode *ast_parse_prefix_op_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
12201218 Token *token = &pc->tokens->at(*token_index);
......@@ -1237,15 +1235,6 @@ static AstNode *ast_parse_prefix_op_expr(ParseContext *pc, size_t *token_index,
12371235
12381236 AstNode *node = ast_create_node(pc, NodeTypePrefixOpExpr, token);
12391237 AstNode *parent_node = node;
1240 if (token->id == TokenIdStarStar) {
1241 // pretend that we got 2 star tokens
1242
1243 parent_node = ast_create_node(pc, NodeTypePrefixOpExpr, token);
1244 parent_node->data.prefix_op_expr.primary_expr = node;
1245 parent_node->data.prefix_op_expr.prefix_op = PrefixOpDereference;
1246
1247 node->column += 1;
1248 }
12491238
12501239 AstNode *prefix_op_expr = ast_parse_error_set_expr(pc, token_index, true);
12511240 node->data.prefix_op_expr.primary_expr = prefix_op_expr;
src/translate_c.cpp+53-30
......@@ -247,6 +247,12 @@ static AstNode *trans_create_node_field_access_str(Context *c, AstNode *containe
247247 return trans_create_node_field_access(c, container, buf_create_from_str(field_name));
248248}
249249
250static AstNode *trans_create_node_ptr_deref(Context *c, AstNode *child_node) {
251 AstNode *node = trans_create_node(c, NodeTypePtrDeref);
252 node->data.ptr_deref_expr.target = child_node;
253 return node;
254}
255
250256static AstNode *trans_create_node_prefix_op(Context *c, PrefixOp op, AstNode *child_node) {
251257 AstNode *node = trans_create_node(c, NodeTypePrefixOpExpr);
252258 node->data.prefix_op_expr.prefix_op = op;
......@@ -1412,8 +1418,7 @@ static AstNode *trans_create_compound_assign_shift(Context *c, ResultUsed result
14121418 AstNode *operation_type_cast = trans_c_cast(c, rhs_location,
14131419 stmt->getComputationLHSType(),
14141420 stmt->getLHS()->getType(),
1415 trans_create_node_prefix_op(c, PrefixOpDereference,
1416 trans_create_node_symbol(c, tmp_var_name)));
1421 trans_create_node_ptr_deref(c, trans_create_node_symbol(c, tmp_var_name)));
14171422
14181423 // result_type(... >> u5(rhs))
14191424 AstNode *result_type_cast = trans_c_cast(c, rhs_location,
......@@ -1426,7 +1431,7 @@ static AstNode *trans_create_compound_assign_shift(Context *c, ResultUsed result
14261431
14271432 // *_ref = ...
14281433 AstNode *assign_statement = trans_create_node_bin_op(c,
1429 trans_create_node_prefix_op(c, PrefixOpDereference,
1434 trans_create_node_ptr_deref(c,
14301435 trans_create_node_symbol(c, tmp_var_name)),
14311436 BinOpTypeAssign, result_type_cast);
14321437
......@@ -1436,7 +1441,7 @@ static AstNode *trans_create_compound_assign_shift(Context *c, ResultUsed result
14361441 // break :x *_ref
14371442 child_scope->node->data.block.statements.append(
14381443 trans_create_node_break(c, label_name,
1439 trans_create_node_prefix_op(c, PrefixOpDereference,
1444 trans_create_node_ptr_deref(c,
14401445 trans_create_node_symbol(c, tmp_var_name))));
14411446 }
14421447
......@@ -1483,11 +1488,11 @@ static AstNode *trans_create_compound_assign(Context *c, ResultUsed result_used,
14831488 if (rhs == nullptr) return nullptr;
14841489
14851490 AstNode *assign_statement = trans_create_node_bin_op(c,
1486 trans_create_node_prefix_op(c, PrefixOpDereference,
1491 trans_create_node_ptr_deref(c,
14871492 trans_create_node_symbol(c, tmp_var_name)),
14881493 BinOpTypeAssign,
14891494 trans_create_node_bin_op(c,
1490 trans_create_node_prefix_op(c, PrefixOpDereference,
1495 trans_create_node_ptr_deref(c,
14911496 trans_create_node_symbol(c, tmp_var_name)),
14921497 bin_op,
14931498 rhs));
......@@ -1496,7 +1501,7 @@ static AstNode *trans_create_compound_assign(Context *c, ResultUsed result_used,
14961501 // break :x *_ref
14971502 child_scope->node->data.block.statements.append(
14981503 trans_create_node_break(c, label_name,
1499 trans_create_node_prefix_op(c, PrefixOpDereference,
1504 trans_create_node_ptr_deref(c,
15001505 trans_create_node_symbol(c, tmp_var_name))));
15011506
15021507 return child_scope->node;
......@@ -1817,13 +1822,13 @@ static AstNode *trans_create_post_crement(Context *c, ResultUsed result_used, Tr
18171822 // const _tmp = *_ref;
18181823 Buf* tmp_var_name = buf_create_from_str("_tmp");
18191824 AstNode *tmp_var_decl = trans_create_node_var_decl_local(c, true, tmp_var_name, nullptr,
1820 trans_create_node_prefix_op(c, PrefixOpDereference,
1825 trans_create_node_ptr_deref(c,
18211826 trans_create_node_symbol(c, ref_var_name)));
18221827 child_scope->node->data.block.statements.append(tmp_var_decl);
18231828
18241829 // *_ref += 1;
18251830 AstNode *assign_statement = trans_create_node_bin_op(c,
1826 trans_create_node_prefix_op(c, PrefixOpDereference,
1831 trans_create_node_ptr_deref(c,
18271832 trans_create_node_symbol(c, ref_var_name)),
18281833 assign_op,
18291834 trans_create_node_unsigned(c, 1));
......@@ -1871,14 +1876,14 @@ static AstNode *trans_create_pre_crement(Context *c, ResultUsed result_used, Tra
18711876
18721877 // *_ref += 1;
18731878 AstNode *assign_statement = trans_create_node_bin_op(c,
1874 trans_create_node_prefix_op(c, PrefixOpDereference,
1879 trans_create_node_ptr_deref(c,
18751880 trans_create_node_symbol(c, ref_var_name)),
18761881 assign_op,
18771882 trans_create_node_unsigned(c, 1));
18781883 child_scope->node->data.block.statements.append(assign_statement);
18791884
18801885 // break :x *_ref
1881 AstNode *deref_expr = trans_create_node_prefix_op(c, PrefixOpDereference,
1886 AstNode *deref_expr = trans_create_node_ptr_deref(c,
18821887 trans_create_node_symbol(c, ref_var_name));
18831888 child_scope->node->data.block.statements.append(trans_create_node_break(c, label_name, deref_expr));
18841889
......@@ -1923,7 +1928,7 @@ static AstNode *trans_unary_operator(Context *c, ResultUsed result_used, TransSc
19231928 if (is_fn_ptr)
19241929 return value_node;
19251930 AstNode *unwrapped = trans_create_node_prefix_op(c, PrefixOpUnwrapMaybe, value_node);
1926 return trans_create_node_prefix_op(c, PrefixOpDereference, unwrapped);
1931 return trans_create_node_ptr_deref(c, unwrapped);
19271932 }
19281933 case UO_Plus:
19291934 emit_warning(c, stmt->getLocStart(), "TODO handle C translation UO_Plus");
......@@ -4469,27 +4474,45 @@ static AstNode *parse_ctok_suffix_op_expr(Context *c, CTokenize *ctok, size_t *t
44694474 }
44704475}
44714476
4472static PrefixOp ctok_to_prefix_op(CTok *token) {
4473 switch (token->id) {
4474 case CTokIdBang: return PrefixOpBoolNot;
4475 case CTokIdMinus: return PrefixOpNegation;
4476 case CTokIdTilde: return PrefixOpBinNot;
4477 case CTokIdAsterisk: return PrefixOpDereference;
4478 default: return PrefixOpInvalid;
4479 }
4480}
44814477static AstNode *parse_ctok_prefix_op_expr(Context *c, CTokenize *ctok, size_t *tok_i) {
44824478 CTok *op_tok = &ctok->tokens.at(*tok_i);
4483 PrefixOp prefix_op = ctok_to_prefix_op(op_tok);
4484 if (prefix_op == PrefixOpInvalid) {
4485 return parse_ctok_suffix_op_expr(c, ctok, tok_i);
4486 }
4487 *tok_i += 1;
44884479
4489 AstNode *prefix_op_expr = parse_ctok_prefix_op_expr(c, ctok, tok_i);
4490 if (prefix_op_expr == nullptr)
4491 return nullptr;
4492 return trans_create_node_prefix_op(c, prefix_op, prefix_op_expr);
4480 switch (op_tok->id) {
4481 case CTokIdBang:
4482 {
4483 *tok_i += 1;
4484 AstNode *prefix_op_expr = parse_ctok_prefix_op_expr(c, ctok, tok_i);
4485 if (prefix_op_expr == nullptr)
4486 return nullptr;
4487 return trans_create_node_prefix_op(c, PrefixOpBoolNot, prefix_op_expr);
4488 }
4489 case CTokIdMinus:
4490 {
4491 *tok_i += 1;
4492 AstNode *prefix_op_expr = parse_ctok_prefix_op_expr(c, ctok, tok_i);
4493 if (prefix_op_expr == nullptr)
4494 return nullptr;
4495 return trans_create_node_prefix_op(c, PrefixOpNegation, prefix_op_expr);
4496 }
4497 case CTokIdTilde:
4498 {
4499 *tok_i += 1;
4500 AstNode *prefix_op_expr = parse_ctok_prefix_op_expr(c, ctok, tok_i);
4501 if (prefix_op_expr == nullptr)
4502 return nullptr;
4503 return trans_create_node_prefix_op(c, PrefixOpBinNot, prefix_op_expr);
4504 }
4505 case CTokIdAsterisk:
4506 {
4507 *tok_i += 1;
4508 AstNode *prefix_op_expr = parse_ctok_prefix_op_expr(c, ctok, tok_i);
4509 if (prefix_op_expr == nullptr)
4510 return nullptr;
4511 return trans_create_node_ptr_deref(c, prefix_op_expr);
4512 }
4513 default:
4514 return parse_ctok_suffix_op_expr(c, ctok, tok_i);
4515 }
44934516}
44944517
44954518static void process_macro(Context *c, CTokenize *ctok, Buf *name, const char *char_ptr) {
std/debug/index.zig+98-135
......@@ -104,9 +104,7 @@ pub fn panic(comptime format: []const u8, args: ...) noreturn {
104104
105105var panicking: u8 = 0; // TODO make this a bool
106106
107pub fn panicExtra(trace: ?&const builtin.StackTrace, first_trace_addr: ?usize,
108 comptime format: []const u8, args: ...) noreturn
109{
107pub fn panicExtra(trace: ?&const builtin.StackTrace, first_trace_addr: ?usize, comptime format: []const u8, args: ...) noreturn {
110108 @setCold(true);
111109
112110 if (@atomicRmw(u8, &panicking, builtin.AtomicRmwOp.Xchg, 1, builtin.AtomicOrder.SeqCst) == 1) {
......@@ -132,9 +130,7 @@ const WHITE = "\x1b[37;1m";
132130const DIM = "\x1b[2m";
133131const RESET = "\x1b[0m";
134132
135pub fn writeStackTrace(stack_trace: &const builtin.StackTrace, out_stream: var, allocator: &mem.Allocator,
136 debug_info: &ElfStackTrace, tty_color: bool) !void
137{
133pub fn writeStackTrace(stack_trace: &const builtin.StackTrace, out_stream: var, allocator: &mem.Allocator, debug_info: &ElfStackTrace, tty_color: bool) !void {
138134 var frame_index: usize = undefined;
139135 var frames_left: usize = undefined;
140136 if (stack_trace.index < stack_trace.instruction_addresses.len) {
......@@ -154,9 +150,7 @@ pub fn writeStackTrace(stack_trace: &const builtin.StackTrace, out_stream: var,
154150 }
155151}
156152
157pub fn writeCurrentStackTrace(out_stream: var, allocator: &mem.Allocator,
158 debug_info: &ElfStackTrace, tty_color: bool, start_addr: ?usize) !void
159{
153pub fn writeCurrentStackTrace(out_stream: var, allocator: &mem.Allocator, debug_info: &ElfStackTrace, tty_color: bool, start_addr: ?usize) !void {
160154 const AddressState = union(enum) {
161155 NotLookingForStartAddress,
162156 LookingForStartAddress: usize,
......@@ -166,14 +160,14 @@ pub fn writeCurrentStackTrace(out_stream: var, allocator: &mem.Allocator,
166160 // else AddressState.NotLookingForStartAddress;
167161 var addr_state: AddressState = undefined;
168162 if (start_addr) |addr| {
169 addr_state = AddressState { .LookingForStartAddress = addr };
163 addr_state = AddressState{ .LookingForStartAddress = addr };
170164 } else {
171165 addr_state = AddressState.NotLookingForStartAddress;
172166 }
173167
174168 var fp = @ptrToInt(@frameAddress());
175 while (fp != 0) : (fp = *@intToPtr(&const usize, fp)) {
176 const return_address = *@intToPtr(&const usize, fp + @sizeOf(usize));
169 while (fp != 0) : (fp = @intToPtr(&const usize, fp).*) {
170 const return_address = @intToPtr(&const usize, fp + @sizeOf(usize)).*;
177171
178172 switch (addr_state) {
179173 AddressState.NotLookingForStartAddress => {},
......@@ -200,32 +194,32 @@ fn printSourceAtAddress(debug_info: &ElfStackTrace, out_stream: var, address: us
200194 // in practice because the compiler dumps everything in a single
201195 // object file. Future improvement: use external dSYM data when
202196 // available.
203 const unknown = macho.Symbol { .name = "???", .address = address };
197 const unknown = macho.Symbol{
198 .name = "???",
199 .address = address,
200 };
204201 const symbol = debug_info.symbol_table.search(address) ?? &unknown;
205 try out_stream.print(WHITE ++ "{}" ++ RESET ++ ": " ++
206 DIM ++ ptr_hex ++ " in ??? (???)" ++ RESET ++ "\n",
207 symbol.name, address);
202 try out_stream.print(WHITE ++ "{}" ++ RESET ++ ": " ++ DIM ++ ptr_hex ++ " in ??? (???)" ++ RESET ++ "\n", symbol.name, address);
208203 },
209204 else => {
210205 const compile_unit = findCompileUnit(debug_info, address) catch {
211 try out_stream.print("???:?:?: " ++ DIM ++ ptr_hex ++ " in ??? (???)" ++ RESET ++ "\n ???\n\n",
212 address);
206 try out_stream.print("???:?:?: " ++ DIM ++ ptr_hex ++ " in ??? (???)" ++ RESET ++ "\n ???\n\n", address);
213207 return;
214208 };
215209 const compile_unit_name = try compile_unit.die.getAttrString(debug_info, DW.AT_name);
216210 if (getLineNumberInfo(debug_info, compile_unit, address - 1)) |line_info| {
217211 defer line_info.deinit();
218 try out_stream.print(WHITE ++ "{}:{}:{}" ++ RESET ++ ": " ++
219 DIM ++ ptr_hex ++ " in ??? ({})" ++ RESET ++ "\n",
220 line_info.file_name, line_info.line, line_info.column,
221 address, compile_unit_name);
212 try out_stream.print(WHITE ++ "{}:{}:{}" ++ RESET ++ ": " ++ DIM ++ ptr_hex ++ " in ??? ({})" ++ RESET ++ "\n", line_info.file_name, line_info.line, line_info.column, address, compile_unit_name);
222213 if (printLineFromFile(debug_info.allocator(), out_stream, line_info)) {
223214 if (line_info.column == 0) {
224215 try out_stream.write("\n");
225216 } else {
226 {var col_i: usize = 1; while (col_i < line_info.column) : (col_i += 1) {
227 try out_stream.writeByte(' ');
228 }}
217 {
218 var col_i: usize = 1;
219 while (col_i < line_info.column) : (col_i += 1) {
220 try out_stream.writeByte(' ');
221 }
222 }
229223 try out_stream.write(GREEN ++ "^" ++ RESET ++ "\n");
230224 }
231225 } else |err| switch (err) {
......@@ -233,7 +227,8 @@ fn printSourceAtAddress(debug_info: &ElfStackTrace, out_stream: var, address: us
233227 else => return err,
234228 }
235229 } else |err| switch (err) {
236 error.MissingDebugInfo, error.InvalidDebugInfo => {
230 error.MissingDebugInfo,
231 error.InvalidDebugInfo => {
237232 try out_stream.print(ptr_hex ++ " in ??? ({})\n", address, compile_unit_name);
238233 },
239234 else => return err,
......@@ -247,7 +242,7 @@ pub fn openSelfDebugInfo(allocator: &mem.Allocator) !&ElfStackTrace {
247242 builtin.ObjectFormat.elf => {
248243 const st = try allocator.create(ElfStackTrace);
249244 errdefer allocator.destroy(st);
250 *st = ElfStackTrace {
245 st.* = ElfStackTrace{
251246 .self_exe_file = undefined,
252247 .elf = undefined,
253248 .debug_info = undefined,
......@@ -279,9 +274,7 @@ pub fn openSelfDebugInfo(allocator: &mem.Allocator) !&ElfStackTrace {
279274 const st = try allocator.create(ElfStackTrace);
280275 errdefer allocator.destroy(st);
281276
282 *st = ElfStackTrace {
283 .symbol_table = try macho.loadSymbols(allocator, &io.FileInStream.init(&exe_file)),
284 };
277 st.* = ElfStackTrace{ .symbol_table = try macho.loadSymbols(allocator, &io.FileInStream.init(&exe_file)) };
285278
286279 return st;
287280 },
......@@ -325,8 +318,7 @@ fn printLineFromFile(allocator: &mem.Allocator, out_stream: var, line_info: &con
325318 }
326319 }
327320
328 if (amt_read < buf.len)
329 return error.EndOfFile;
321 if (amt_read < buf.len) return error.EndOfFile;
330322 }
331323}
332324
......@@ -418,10 +410,8 @@ const Constant = struct {
418410 signed: bool,
419411
420412 fn asUnsignedLe(self: &const Constant) !u64 {
421 if (self.payload.len > @sizeOf(u64))
422 return error.InvalidDebugInfo;
423 if (self.signed)
424 return error.InvalidDebugInfo;
413 if (self.payload.len > @sizeOf(u64)) return error.InvalidDebugInfo;
414 if (self.signed) return error.InvalidDebugInfo;
425415 return mem.readInt(self.payload, u64, builtin.Endian.Little);
426416 }
427417};
......@@ -438,15 +428,14 @@ const Die = struct {
438428
439429 fn getAttr(self: &const Die, id: u64) ?&const FormValue {
440430 for (self.attrs.toSliceConst()) |*attr| {
441 if (attr.id == id)
442 return &attr.value;
431 if (attr.id == id) return &attr.value;
443432 }
444433 return null;
445434 }
446435
447436 fn getAttrAddr(self: &const Die, id: u64) !u64 {
448437 const form_value = self.getAttr(id) ?? return error.MissingDebugInfo;
449 return switch (*form_value) {
438 return switch (form_value.*) {
450439 FormValue.Address => |value| value,
451440 else => error.InvalidDebugInfo,
452441 };
......@@ -454,7 +443,7 @@ const Die = struct {
454443
455444 fn getAttrSecOffset(self: &const Die, id: u64) !u64 {
456445 const form_value = self.getAttr(id) ?? return error.MissingDebugInfo;
457 return switch (*form_value) {
446 return switch (form_value.*) {
458447 FormValue.Const => |value| value.asUnsignedLe(),
459448 FormValue.SecOffset => |value| value,
460449 else => error.InvalidDebugInfo,
......@@ -463,7 +452,7 @@ const Die = struct {
463452
464453 fn getAttrUnsignedLe(self: &const Die, id: u64) !u64 {
465454 const form_value = self.getAttr(id) ?? return error.MissingDebugInfo;
466 return switch (*form_value) {
455 return switch (form_value.*) {
467456 FormValue.Const => |value| value.asUnsignedLe(),
468457 else => error.InvalidDebugInfo,
469458 };
......@@ -471,7 +460,7 @@ const Die = struct {
471460
472461 fn getAttrString(self: &const Die, st: &ElfStackTrace, id: u64) ![]u8 {
473462 const form_value = self.getAttr(id) ?? return error.MissingDebugInfo;
474 return switch (*form_value) {
463 return switch (form_value.*) {
475464 FormValue.String => |value| value,
476465 FormValue.StrPtr => |offset| getString(st, offset),
477466 else => error.InvalidDebugInfo,
......@@ -518,10 +507,8 @@ const LineNumberProgram = struct {
518507 prev_basic_block: bool,
519508 prev_end_sequence: bool,
520509
521 pub fn init(is_stmt: bool, include_dirs: []const []const u8,
522 file_entries: &ArrayList(FileEntry), target_address: usize) LineNumberProgram
523 {
524 return LineNumberProgram {
510 pub fn init(is_stmt: bool, include_dirs: []const []const u8, file_entries: &ArrayList(FileEntry), target_address: usize) LineNumberProgram {
511 return LineNumberProgram{
525512 .address = 0,
526513 .file = 1,
527514 .line = 1,
......@@ -548,14 +535,16 @@ const LineNumberProgram = struct {
548535 return error.MissingDebugInfo;
549536 } else if (self.prev_file - 1 >= self.file_entries.len) {
550537 return error.InvalidDebugInfo;
551 } else &self.file_entries.items[self.prev_file - 1];
538 } else
539 &self.file_entries.items[self.prev_file - 1];
552540
553541 const dir_name = if (file_entry.dir_index >= self.include_dirs.len) {
554542 return error.InvalidDebugInfo;
555 } else self.include_dirs[file_entry.dir_index];
543 } else
544 self.include_dirs[file_entry.dir_index];
556545 const file_name = try os.path.join(self.file_entries.allocator, dir_name, file_entry.file_name);
557546 errdefer self.file_entries.allocator.free(file_name);
558 return LineInfo {
547 return LineInfo{
559548 .line = if (self.prev_line >= 0) usize(self.prev_line) else 0,
560549 .column = self.prev_column,
561550 .file_name = file_name,
......@@ -578,8 +567,7 @@ fn readStringRaw(allocator: &mem.Allocator, in_stream: var) ![]u8 {
578567 var buf = ArrayList(u8).init(allocator);
579568 while (true) {
580569 const byte = try in_stream.readByte();
581 if (byte == 0)
582 break;
570 if (byte == 0) break;
583571 try buf.append(byte);
584572 }
585573 return buf.toSlice();
......@@ -600,7 +588,7 @@ fn readAllocBytes(allocator: &mem.Allocator, in_stream: var, size: usize) ![]u8
600588
601589fn parseFormValueBlockLen(allocator: &mem.Allocator, in_stream: var, size: usize) !FormValue {
602590 const buf = try readAllocBytes(allocator, in_stream, size);
603 return FormValue { .Block = buf };
591 return FormValue{ .Block = buf };
604592}
605593
606594fn parseFormValueBlock(allocator: &mem.Allocator, in_stream: var, size: usize) !FormValue {
......@@ -609,26 +597,23 @@ fn parseFormValueBlock(allocator: &mem.Allocator, in_stream: var, size: usize) !
609597}
610598
611599fn parseFormValueConstant(allocator: &mem.Allocator, in_stream: var, signed: bool, size: usize) !FormValue {
612 return FormValue { .Const = Constant {
600 return FormValue{ .Const = Constant{
613601 .signed = signed,
614602 .payload = try readAllocBytes(allocator, in_stream, size),
615 }};
603 } };
616604}
617605
618606fn parseFormValueDwarfOffsetSize(in_stream: var, is_64: bool) !u64 {
619 return if (is_64) try in_stream.readIntLe(u64)
620 else u64(try in_stream.readIntLe(u32)) ;
607 return if (is_64) try in_stream.readIntLe(u64) else u64(try in_stream.readIntLe(u32));
621608}
622609
623610fn parseFormValueTargetAddrSize(in_stream: var) !u64 {
624 return if (@sizeOf(usize) == 4) u64(try in_stream.readIntLe(u32))
625 else if (@sizeOf(usize) == 8) try in_stream.readIntLe(u64)
626 else unreachable;
611 return if (@sizeOf(usize) == 4) u64(try in_stream.readIntLe(u32)) else if (@sizeOf(usize) == 8) try in_stream.readIntLe(u64) else unreachable;
627612}
628613
629614fn parseFormValueRefLen(allocator: &mem.Allocator, in_stream: var, size: usize) !FormValue {
630615 const buf = try readAllocBytes(allocator, in_stream, size);
631 return FormValue { .Ref = buf };
616 return FormValue{ .Ref = buf };
632617}
633618
634619fn parseFormValueRef(allocator: &mem.Allocator, in_stream: var, comptime T: type) !FormValue {
......@@ -646,11 +631,9 @@ const ParseFormValueError = error {
646631 OutOfMemory,
647632};
648633
649fn parseFormValue(allocator: &mem.Allocator, in_stream: var, form_id: u64, is_64: bool)
650 ParseFormValueError!FormValue
651{
634fn parseFormValue(allocator: &mem.Allocator, in_stream: var, form_id: u64, is_64: bool) ParseFormValueError!FormValue {
652635 return switch (form_id) {
653 DW.FORM_addr => FormValue { .Address = try parseFormValueTargetAddrSize(in_stream) },
636 DW.FORM_addr => FormValue{ .Address = try parseFormValueTargetAddrSize(in_stream) },
654637 DW.FORM_block1 => parseFormValueBlock(allocator, in_stream, 1),
655638 DW.FORM_block2 => parseFormValueBlock(allocator, in_stream, 2),
656639 DW.FORM_block4 => parseFormValueBlock(allocator, in_stream, 4),
......@@ -662,7 +645,8 @@ fn parseFormValue(allocator: &mem.Allocator, in_stream: var, form_id: u64, is_64
662645 DW.FORM_data2 => parseFormValueConstant(allocator, in_stream, false, 2),
663646 DW.FORM_data4 => parseFormValueConstant(allocator, in_stream, false, 4),
664647 DW.FORM_data8 => parseFormValueConstant(allocator, in_stream, false, 8),
665 DW.FORM_udata, DW.FORM_sdata => {
648 DW.FORM_udata,
649 DW.FORM_sdata => {
666650 const block_len = try readULeb128(in_stream);
667651 const signed = form_id == DW.FORM_sdata;
668652 return parseFormValueConstant(allocator, in_stream, signed, block_len);
......@@ -670,11 +654,11 @@ fn parseFormValue(allocator: &mem.Allocator, in_stream: var, form_id: u64, is_64
670654 DW.FORM_exprloc => {
671655 const size = try readULeb128(in_stream);
672656 const buf = try readAllocBytes(allocator, in_stream, size);
673 return FormValue { .ExprLoc = buf };
657 return FormValue{ .ExprLoc = buf };
674658 },
675 DW.FORM_flag => FormValue { .Flag = (try in_stream.readByte()) != 0 },
676 DW.FORM_flag_present => FormValue { .Flag = true },
677 DW.FORM_sec_offset => FormValue { .SecOffset = try parseFormValueDwarfOffsetSize(in_stream, is_64) },
659 DW.FORM_flag => FormValue{ .Flag = (try in_stream.readByte()) != 0 },
660 DW.FORM_flag_present => FormValue{ .Flag = true },
661 DW.FORM_sec_offset => FormValue{ .SecOffset = try parseFormValueDwarfOffsetSize(in_stream, is_64) },
678662
679663 DW.FORM_ref1 => parseFormValueRef(allocator, in_stream, u8),
680664 DW.FORM_ref2 => parseFormValueRef(allocator, in_stream, u16),
......@@ -685,11 +669,11 @@ fn parseFormValue(allocator: &mem.Allocator, in_stream: var, form_id: u64, is_64
685669 return parseFormValueRefLen(allocator, in_stream, ref_len);
686670 },
687671
688 DW.FORM_ref_addr => FormValue { .RefAddr = try parseFormValueDwarfOffsetSize(in_stream, is_64) },
689 DW.FORM_ref_sig8 => FormValue { .RefSig8 = try in_stream.readIntLe(u64) },
672 DW.FORM_ref_addr => FormValue{ .RefAddr = try parseFormValueDwarfOffsetSize(in_stream, is_64) },
673 DW.FORM_ref_sig8 => FormValue{ .RefSig8 = try in_stream.readIntLe(u64) },
690674
691 DW.FORM_string => FormValue { .String = try readStringRaw(allocator, in_stream) },
692 DW.FORM_strp => FormValue { .StrPtr = try parseFormValueDwarfOffsetSize(in_stream, is_64) },
675 DW.FORM_string => FormValue{ .String = try readStringRaw(allocator, in_stream) },
676 DW.FORM_strp => FormValue{ .StrPtr = try parseFormValueDwarfOffsetSize(in_stream, is_64) },
693677 DW.FORM_indirect => {
694678 const child_form_id = try readULeb128(in_stream);
695679 return parseFormValue(allocator, in_stream, child_form_id, is_64);
......@@ -705,9 +689,8 @@ fn parseAbbrevTable(st: &ElfStackTrace) !AbbrevTable {
705689 var result = AbbrevTable.init(st.allocator());
706690 while (true) {
707691 const abbrev_code = try readULeb128(in_stream);
708 if (abbrev_code == 0)
709 return result;
710 try result.append(AbbrevTableEntry {
692 if (abbrev_code == 0) return result;
693 try result.append(AbbrevTableEntry{
711694 .abbrev_code = abbrev_code,
712695 .tag_id = try readULeb128(in_stream),
713696 .has_children = (try in_stream.readByte()) == DW.CHILDREN_yes,
......@@ -718,9 +701,8 @@ fn parseAbbrevTable(st: &ElfStackTrace) !AbbrevTable {
718701 while (true) {
719702 const attr_id = try readULeb128(in_stream);
720703 const form_id = try readULeb128(in_stream);
721 if (attr_id == 0 and form_id == 0)
722 break;
723 try attrs.append(AbbrevAttr {
704 if (attr_id == 0 and form_id == 0) break;
705 try attrs.append(AbbrevAttr{
724706 .attr_id = attr_id,
725707 .form_id = form_id,
726708 });
......@@ -737,7 +719,7 @@ fn getAbbrevTable(st: &ElfStackTrace, abbrev_offset: u64) !&const AbbrevTable {
737719 }
738720 }
739721 try st.self_exe_file.seekTo(st.debug_abbrev.offset + abbrev_offset);
740 try st.abbrev_table_list.append(AbbrevTableHeader {
722 try st.abbrev_table_list.append(AbbrevTableHeader{
741723 .offset = abbrev_offset,
742724 .table = try parseAbbrevTable(st),
743725 });
......@@ -746,8 +728,7 @@ fn getAbbrevTable(st: &ElfStackTrace, abbrev_offset: u64) !&const AbbrevTable {
746728
747729fn getAbbrevTableEntry(abbrev_table: &const AbbrevTable, abbrev_code: u64) ?&const AbbrevTableEntry {
748730 for (abbrev_table.toSliceConst()) |*table_entry| {
749 if (table_entry.abbrev_code == abbrev_code)
750 return table_entry;
731 if (table_entry.abbrev_code == abbrev_code) return table_entry;
751732 }
752733 return null;
753734}
......@@ -759,14 +740,14 @@ fn parseDie(st: &ElfStackTrace, abbrev_table: &const AbbrevTable, is_64: bool) !
759740 const abbrev_code = try readULeb128(in_stream);
760741 const table_entry = getAbbrevTableEntry(abbrev_table, abbrev_code) ?? return error.InvalidDebugInfo;
761742
762 var result = Die {
743 var result = Die{
763744 .tag_id = table_entry.tag_id,
764745 .has_children = table_entry.has_children,
765746 .attrs = ArrayList(Die.Attr).init(st.allocator()),
766747 };
767748 try result.attrs.resize(table_entry.attrs.len);
768749 for (table_entry.attrs.toSliceConst()) |attr, i| {
769 result.attrs.items[i] = Die.Attr {
750 result.attrs.items[i] = Die.Attr{
770751 .id = attr.attr_id,
771752 .value = try parseFormValue(st.allocator(), in_stream, attr.form_id, is_64),
772753 };
......@@ -790,8 +771,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
790771
791772 var is_64: bool = undefined;
792773 const unit_length = try readInitialLength(@typeOf(in_stream.readFn).ReturnType.ErrorSet, in_stream, &is_64);
793 if (unit_length == 0)
794 return error.MissingDebugInfo;
774 if (unit_length == 0) return error.MissingDebugInfo;
795775 const next_offset = unit_length + (if (is_64) usize(12) else usize(4));
796776
797777 if (compile_unit.index != this_index) {
......@@ -803,8 +783,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
803783 // TODO support 3 and 5
804784 if (version != 2 and version != 4) return error.InvalidDebugInfo;
805785
806 const prologue_length = if (is_64) try in_stream.readInt(st.elf.endian, u64)
807 else try in_stream.readInt(st.elf.endian, u32);
786 const prologue_length = if (is_64) try in_stream.readInt(st.elf.endian, u64) else try in_stream.readInt(st.elf.endian, u32);
808787 const prog_start_offset = (try in_file.getPos()) + prologue_length;
809788
810789 const minimum_instruction_length = try in_stream.readByte();
......@@ -819,38 +798,37 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
819798 const line_base = try in_stream.readByteSigned();
820799
821800 const line_range = try in_stream.readByte();
822 if (line_range == 0)
823 return error.InvalidDebugInfo;
801 if (line_range == 0) return error.InvalidDebugInfo;
824802
825803 const opcode_base = try in_stream.readByte();
826804
827805 const standard_opcode_lengths = try st.allocator().alloc(u8, opcode_base - 1);
828806
829 {var i: usize = 0; while (i < opcode_base - 1) : (i += 1) {
830 standard_opcode_lengths[i] = try in_stream.readByte();
831 }}
807 {
808 var i: usize = 0;
809 while (i < opcode_base - 1) : (i += 1) {
810 standard_opcode_lengths[i] = try in_stream.readByte();
811 }
812 }
832813
833814 var include_directories = ArrayList([]u8).init(st.allocator());
834815 try include_directories.append(compile_unit_cwd);
835816 while (true) {
836817 const dir = try st.readString();
837 if (dir.len == 0)
838 break;
818 if (dir.len == 0) break;
839819 try include_directories.append(dir);
840820 }
841821
842822 var file_entries = ArrayList(FileEntry).init(st.allocator());
843 var prog = LineNumberProgram.init(default_is_stmt, include_directories.toSliceConst(),
844 &file_entries, target_address);
823 var prog = LineNumberProgram.init(default_is_stmt, include_directories.toSliceConst(), &file_entries, target_address);
845824
846825 while (true) {
847826 const file_name = try st.readString();
848 if (file_name.len == 0)
849 break;
827 if (file_name.len == 0) break;
850828 const dir_index = try readULeb128(in_stream);
851829 const mtime = try readULeb128(in_stream);
852830 const len_bytes = try readULeb128(in_stream);
853 try file_entries.append(FileEntry {
831 try file_entries.append(FileEntry{
854832 .file_name = file_name,
855833 .dir_index = dir_index,
856834 .mtime = mtime,
......@@ -866,8 +844,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
866844 var sub_op: u8 = undefined; // TODO move this to the correct scope and fix the compiler crash
867845 if (opcode == DW.LNS_extended_op) {
868846 const op_size = try readULeb128(in_stream);
869 if (op_size < 1)
870 return error.InvalidDebugInfo;
847 if (op_size < 1) return error.InvalidDebugInfo;
871848 sub_op = try in_stream.readByte();
872849 switch (sub_op) {
873850 DW.LNE_end_sequence => {
......@@ -884,7 +861,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
884861 const dir_index = try readULeb128(in_stream);
885862 const mtime = try readULeb128(in_stream);
886863 const len_bytes = try readULeb128(in_stream);
887 try file_entries.append(FileEntry {
864 try file_entries.append(FileEntry{
888865 .file_name = file_name,
889866 .dir_index = dir_index,
890867 .mtime = mtime,
......@@ -941,11 +918,9 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe
941918 const arg = try in_stream.readInt(st.elf.endian, u16);
942919 prog.address += arg;
943920 },
944 DW.LNS_set_prologue_end => {
945 },
921 DW.LNS_set_prologue_end => {},
946922 else => {
947 if (opcode - 1 >= standard_opcode_lengths.len)
948 return error.InvalidDebugInfo;
923 if (opcode - 1 >= standard_opcode_lengths.len) return error.InvalidDebugInfo;
949924 const len_bytes = standard_opcode_lengths[opcode - 1];
950925 try in_file.seekForward(len_bytes);
951926 },
......@@ -972,16 +947,13 @@ fn scanAllCompileUnits(st: &ElfStackTrace) !void {
972947
973948 var is_64: bool = undefined;
974949 const unit_length = try readInitialLength(@typeOf(in_stream.readFn).ReturnType.ErrorSet, in_stream, &is_64);
975 if (unit_length == 0)
976 return;
950 if (unit_length == 0) return;
977951 const next_offset = unit_length + (if (is_64) usize(12) else usize(4));
978952
979953 const version = try in_stream.readInt(st.elf.endian, u16);
980954 if (version < 2 or version > 5) return error.InvalidDebugInfo;
981955
982 const debug_abbrev_offset =
983 if (is_64) try in_stream.readInt(st.elf.endian, u64)
984 else try in_stream.readInt(st.elf.endian, u32);
956 const debug_abbrev_offset = if (is_64) try in_stream.readInt(st.elf.endian, u64) else try in_stream.readInt(st.elf.endian, u32);
985957
986958 const address_size = try in_stream.readByte();
987959 if (address_size != @sizeOf(usize)) return error.InvalidDebugInfo;
......@@ -992,15 +964,14 @@ fn scanAllCompileUnits(st: &ElfStackTrace) !void {
992964 try st.self_exe_file.seekTo(compile_unit_pos);
993965
994966 const compile_unit_die = try st.allocator().create(Die);
995 *compile_unit_die = try parseDie(st, abbrev_table, is_64);
967 compile_unit_die.* = try parseDie(st, abbrev_table, is_64);
996968
997 if (compile_unit_die.tag_id != DW.TAG_compile_unit)
998 return error.InvalidDebugInfo;
969 if (compile_unit_die.tag_id != DW.TAG_compile_unit) return error.InvalidDebugInfo;
999970
1000971 const pc_range = x: {
1001972 if (compile_unit_die.getAttrAddr(DW.AT_low_pc)) |low_pc| {
1002973 if (compile_unit_die.getAttr(DW.AT_high_pc)) |high_pc_value| {
1003 const pc_end = switch (*high_pc_value) {
974 const pc_end = switch (high_pc_value.*) {
1004975 FormValue.Address => |value| value,
1005976 FormValue.Const => |value| b: {
1006977 const offset = try value.asUnsignedLe();
......@@ -1008,7 +979,7 @@ fn scanAllCompileUnits(st: &ElfStackTrace) !void {
1008979 },
1009980 else => return error.InvalidDebugInfo,
1010981 };
1011 break :x PcRange {
982 break :x PcRange{
1012983 .start = low_pc,
1013984 .end = pc_end,
1014985 };
......@@ -1016,13 +987,12 @@ fn scanAllCompileUnits(st: &ElfStackTrace) !void {
1016987 break :x null;
1017988 }
1018989 } else |err| {
1019 if (err != error.MissingDebugInfo)
1020 return err;
990 if (err != error.MissingDebugInfo) return err;
1021991 break :x null;
1022992 }
1023993 };
1024994
1025 try st.compile_unit_list.append(CompileUnit {
995 try st.compile_unit_list.append(CompileUnit{
1026996 .version = version,
1027997 .is_64 = is_64,
1028998 .pc_range = pc_range,
......@@ -1040,8 +1010,7 @@ fn findCompileUnit(st: &ElfStackTrace, target_address: u64) !&const CompileUnit
10401010 const in_stream = &in_file_stream.stream;
10411011 for (st.compile_unit_list.toSlice()) |*compile_unit| {
10421012 if (compile_unit.pc_range) |range| {
1043 if (target_address >= range.start and target_address < range.end)
1044 return compile_unit;
1013 if (target_address >= range.start and target_address < range.end) return compile_unit;
10451014 }
10461015 if (compile_unit.die.getAttrSecOffset(DW.AT_ranges)) |ranges_offset| {
10471016 var base_address: usize = 0;
......@@ -1063,8 +1032,7 @@ fn findCompileUnit(st: &ElfStackTrace, target_address: u64) !&const CompileUnit
10631032 }
10641033 }
10651034 } else |err| {
1066 if (err != error.MissingDebugInfo)
1067 return err;
1035 if (err != error.MissingDebugInfo) return err;
10681036 continue;
10691037 }
10701038 }
......@@ -1073,8 +1041,8 @@ fn findCompileUnit(st: &ElfStackTrace, target_address: u64) !&const CompileUnit
10731041
10741042fn readInitialLength(comptime E: type, in_stream: &io.InStream(E), is_64: &bool) !u64 {
10751043 const first_32_bits = try in_stream.readIntLe(u32);
1076 *is_64 = (first_32_bits == 0xffffffff);
1077 if (*is_64) {
1044 is_64.* = (first_32_bits == 0xffffffff);
1045 if (is_64.*) {
10781046 return in_stream.readIntLe(u64);
10791047 } else {
10801048 if (first_32_bits >= 0xfffffff0) return error.InvalidDebugInfo;
......@@ -1091,13 +1059,11 @@ fn readULeb128(in_stream: var) !u64 {
10911059
10921060 var operand: u64 = undefined;
10931061
1094 if (@shlWithOverflow(u64, byte & 0b01111111, u6(shift), &operand))
1095 return error.InvalidDebugInfo;
1062 if (@shlWithOverflow(u64, byte & 0b01111111, u6(shift), &operand)) return error.InvalidDebugInfo;
10961063
10971064 result |= operand;
10981065
1099 if ((byte & 0b10000000) == 0)
1100 return result;
1066 if ((byte & 0b10000000) == 0) return result;
11011067
11021068 shift += 7;
11031069 }
......@@ -1112,15 +1078,13 @@ fn readILeb128(in_stream: var) !i64 {
11121078
11131079 var operand: i64 = undefined;
11141080
1115 if (@shlWithOverflow(i64, byte & 0b01111111, u6(shift), &operand))
1116 return error.InvalidDebugInfo;
1081 if (@shlWithOverflow(i64, byte & 0b01111111, u6(shift), &operand)) return error.InvalidDebugInfo;
11171082
11181083 result |= operand;
11191084 shift += 7;
11201085
11211086 if ((byte & 0b10000000) == 0) {
1122 if (shift < @sizeOf(i64) * 8 and (byte & 0b01000000) != 0)
1123 result |= -(i64(1) << u6(shift));
1087 if (shift < @sizeOf(i64) * 8 and (byte & 0b01000000) != 0) result |= -(i64(1) << u6(shift));
11241088 return result;
11251089 }
11261090 }
......@@ -1131,7 +1095,6 @@ pub const global_allocator = &global_fixed_allocator.allocator;
11311095var global_fixed_allocator = std.heap.FixedBufferAllocator.init(global_allocator_mem[0..]);
11321096var global_allocator_mem: [100 * 1024]u8 = undefined;
11331097
1134
11351098// TODO make thread safe
11361099var debug_info_allocator: ?&mem.Allocator = null;
11371100var debug_info_direct_allocator: std.heap.DirectAllocator = undefined;
std/math/index.zig+29-46
......@@ -47,12 +47,12 @@ pub fn forceEval(value: var) void {
4747 f32 => {
4848 var x: f32 = undefined;
4949 const p = @ptrCast(&volatile f32, &x);
50 *p = x;
50 p.* = x;
5151 },
5252 f64 => {
5353 var x: f64 = undefined;
5454 const p = @ptrCast(&volatile f64, &x);
55 *p = x;
55 p.* = x;
5656 },
5757 else => {
5858 @compileError("forceEval not implemented for " ++ @typeName(T));
......@@ -179,7 +179,6 @@ test "math" {
179179 _ = @import("complex/index.zig");
180180}
181181
182
183182pub fn min(x: var, y: var) @typeOf(x + y) {
184183 return if (x < y) x else y;
185184}
......@@ -280,10 +279,10 @@ pub fn rotr(comptime T: type, x: T, r: var) T {
280279}
281280
282281test "math.rotr" {
283 assert(rotr(u8, 0b00000001, usize(0)) == 0b00000001);
284 assert(rotr(u8, 0b00000001, usize(9)) == 0b10000000);
285 assert(rotr(u8, 0b00000001, usize(8)) == 0b00000001);
286 assert(rotr(u8, 0b00000001, usize(4)) == 0b00010000);
282 assert(rotr(u8, 0b00000001, usize(0)) == 0b00000001);
283 assert(rotr(u8, 0b00000001, usize(9)) == 0b10000000);
284 assert(rotr(u8, 0b00000001, usize(8)) == 0b00000001);
285 assert(rotr(u8, 0b00000001, usize(4)) == 0b00010000);
287286 assert(rotr(u8, 0b00000001, isize(-1)) == 0b00000010);
288287}
289288
......@@ -299,14 +298,13 @@ pub fn rotl(comptime T: type, x: T, r: var) T {
299298}
300299
301300test "math.rotl" {
302 assert(rotl(u8, 0b00000001, usize(0)) == 0b00000001);
303 assert(rotl(u8, 0b00000001, usize(9)) == 0b00000010);
304 assert(rotl(u8, 0b00000001, usize(8)) == 0b00000001);
305 assert(rotl(u8, 0b00000001, usize(4)) == 0b00010000);
301 assert(rotl(u8, 0b00000001, usize(0)) == 0b00000001);
302 assert(rotl(u8, 0b00000001, usize(9)) == 0b00000010);
303 assert(rotl(u8, 0b00000001, usize(8)) == 0b00000001);
304 assert(rotl(u8, 0b00000001, usize(4)) == 0b00010000);
306305 assert(rotl(u8, 0b00000001, isize(-1)) == 0b10000000);
307306}
308307
309
310308pub fn Log2Int(comptime T: type) type {
311309 return @IntType(false, log2(T.bit_count));
312310}
......@@ -323,14 +321,14 @@ fn testOverflow() void {
323321 assert((shlExact(i32, 0b11, 4) catch unreachable) == 0b110000);
324322}
325323
326
327324pub fn absInt(x: var) !@typeOf(x) {
328325 const T = @typeOf(x);
329326 comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt
330327 comptime assert(T.is_signed); // must pass a signed integer to absInt
331 if (x == @minValue(@typeOf(x)))
328
329 if (x == @minValue(@typeOf(x))) {
332330 return error.Overflow;
333 {
331 } else {
334332 @setRuntimeSafety(false);
335333 return if (x < 0) -x else x;
336334 }
......@@ -349,10 +347,8 @@ pub const absFloat = @import("fabs.zig").fabs;
349347
350348pub fn divTrunc(comptime T: type, numerator: T, denominator: T) !T {
351349 @setRuntimeSafety(false);
352 if (denominator == 0)
353 return error.DivisionByZero;
354 if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1)
355 return error.Overflow;
350 if (denominator == 0) return error.DivisionByZero;
351 if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1) return error.Overflow;
356352 return @divTrunc(numerator, denominator);
357353}
358354
......@@ -372,10 +368,8 @@ fn testDivTrunc() void {
372368
373369pub fn divFloor(comptime T: type, numerator: T, denominator: T) !T {
374370 @setRuntimeSafety(false);
375 if (denominator == 0)
376 return error.DivisionByZero;
377 if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1)
378 return error.Overflow;
371 if (denominator == 0) return error.DivisionByZero;
372 if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1) return error.Overflow;
379373 return @divFloor(numerator, denominator);
380374}
381375
......@@ -395,13 +389,10 @@ fn testDivFloor() void {
395389
396390pub fn divExact(comptime T: type, numerator: T, denominator: T) !T {
397391 @setRuntimeSafety(false);
398 if (denominator == 0)
399 return error.DivisionByZero;
400 if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1)
401 return error.Overflow;
392 if (denominator == 0) return error.DivisionByZero;
393 if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == @minValue(T) and denominator == -1) return error.Overflow;
402394 const result = @divTrunc(numerator, denominator);
403 if (result * denominator != numerator)
404 return error.UnexpectedRemainder;
395 if (result * denominator != numerator) return error.UnexpectedRemainder;
405396 return result;
406397}
407398
......@@ -423,10 +414,8 @@ fn testDivExact() void {
423414
424415pub fn mod(comptime T: type, numerator: T, denominator: T) !T {
425416 @setRuntimeSafety(false);
426 if (denominator == 0)
427 return error.DivisionByZero;
428 if (denominator < 0)
429 return error.NegativeDenominator;
417 if (denominator == 0) return error.DivisionByZero;
418 if (denominator < 0) return error.NegativeDenominator;
430419 return @mod(numerator, denominator);
431420}
432421
......@@ -448,10 +437,8 @@ fn testMod() void {
448437
449438pub fn rem(comptime T: type, numerator: T, denominator: T) !T {
450439 @setRuntimeSafety(false);
451 if (denominator == 0)
452 return error.DivisionByZero;
453 if (denominator < 0)
454 return error.NegativeDenominator;
440 if (denominator == 0) return error.DivisionByZero;
441 if (denominator < 0) return error.NegativeDenominator;
455442 return @rem(numerator, denominator);
456443}
457444
......@@ -475,8 +462,7 @@ fn testRem() void {
475462/// Result is an unsigned integer.
476463pub fn absCast(x: var) @IntType(false, @typeOf(x).bit_count) {
477464 const uint = @IntType(false, @typeOf(x).bit_count);
478 if (x >= 0)
479 return uint(x);
465 if (x >= 0) return uint(x);
480466
481467 return uint(-(x + 1)) + 1;
482468}
......@@ -495,15 +481,12 @@ test "math.absCast" {
495481/// Returns the negation of the integer parameter.
496482/// Result is a signed integer.
497483pub fn negateCast(x: var) !@IntType(true, @typeOf(x).bit_count) {
498 if (@typeOf(x).is_signed)
499 return negate(x);
484 if (@typeOf(x).is_signed) return negate(x);
500485
501486 const int = @IntType(true, @typeOf(x).bit_count);
502 if (x > -@minValue(int))
503 return error.Overflow;
487 if (x > -@minValue(int)) return error.Overflow;
504488
505 if (x == -@minValue(int))
506 return @minValue(int);
489 if (x == -@minValue(int)) return @minValue(int);
507490
508491 return -int(x);
509492}
......@@ -546,7 +529,7 @@ pub fn floorPowerOfTwo(comptime T: type, value: T) T {
546529 var x = value;
547530
548531 comptime var i = 1;
549 inline while(T.bit_count > i) : (i *= 2) {
532 inline while (T.bit_count > i) : (i *= 2) {
550533 x |= (x >> i);
551534 }
552535
std/mem.zig+124-73
......@@ -6,14 +6,14 @@ const builtin = @import("builtin");
66const mem = this;
77
88pub const Allocator = struct {
9 const Error = error {OutOfMemory};
9 const Error = error{OutOfMemory};
1010
1111 /// Allocate byte_count bytes and return them in a slice, with the
1212 /// slice's pointer aligned at least to alignment bytes.
1313 /// The returned newly allocated memory is undefined.
1414 /// `alignment` is guaranteed to be >= 1
1515 /// `alignment` is guaranteed to be a power of 2
16 allocFn: fn (self: &Allocator, byte_count: usize, alignment: u29) Error![]u8,
16 allocFn: fn(self: &Allocator, byte_count: usize, alignment: u29) Error![]u8,
1717
1818 /// If `new_byte_count > old_mem.len`:
1919 /// * `old_mem.len` is the same as what was returned from allocFn or reallocFn.
......@@ -26,10 +26,10 @@ pub const Allocator = struct {
2626 /// The returned newly allocated memory is undefined.
2727 /// `alignment` is guaranteed to be >= 1
2828 /// `alignment` is guaranteed to be a power of 2
29 reallocFn: fn (self: &Allocator, old_mem: []u8, new_byte_count: usize, alignment: u29) Error![]u8,
29 reallocFn: fn(self: &Allocator, old_mem: []u8, new_byte_count: usize, alignment: u29) Error![]u8,
3030
3131 /// Guaranteed: `old_mem.len` is the same as what was returned from `allocFn` or `reallocFn`
32 freeFn: fn (self: &Allocator, old_mem: []u8) void,
32 freeFn: fn(self: &Allocator, old_mem: []u8) void,
3333
3434 fn create(self: &Allocator, comptime T: type) !&T {
3535 if (@sizeOf(T) == 0) return &{};
......@@ -47,7 +47,7 @@ pub const Allocator = struct {
4747 if (@sizeOf(T) == 0) return &{};
4848 const slice = try self.alloc(T, 1);
4949 const ptr = &slice[0];
50 *ptr = *init;
50 ptr.* = init.*;
5151 return ptr;
5252 }
5353
......@@ -59,9 +59,7 @@ pub const Allocator = struct {
5959 return self.alignedAlloc(T, @alignOf(T), n);
6060 }
6161
62 fn alignedAlloc(self: &Allocator, comptime T: type, comptime alignment: u29,
63 n: usize) ![]align(alignment) T
64 {
62 fn alignedAlloc(self: &Allocator, comptime T: type, comptime alignment: u29, n: usize) ![]align(alignment) T {
6563 if (n == 0) {
6664 return (&align(alignment) T)(undefined)[0..0];
6765 }
......@@ -70,7 +68,7 @@ pub const Allocator = struct {
7068 assert(byte_slice.len == byte_count);
7169 // This loop gets optimized out in ReleaseFast mode
7270 for (byte_slice) |*byte| {
73 *byte = undefined;
71 byte.* = undefined;
7472 }
7573 return ([]align(alignment) T)(@alignCast(alignment, byte_slice));
7674 }
......@@ -79,9 +77,7 @@ pub const Allocator = struct {
7977 return self.alignedRealloc(T, @alignOf(T), @alignCast(@alignOf(T), old_mem), n);
8078 }
8179
82 fn alignedRealloc(self: &Allocator, comptime T: type, comptime alignment: u29,
83 old_mem: []align(alignment) T, n: usize) ![]align(alignment) T
84 {
80 fn alignedRealloc(self: &Allocator, comptime T: type, comptime alignment: u29, old_mem: []align(alignment) T, n: usize) ![]align(alignment) T {
8581 if (old_mem.len == 0) {
8682 return self.alloc(T, n);
8783 }
......@@ -97,7 +93,7 @@ pub const Allocator = struct {
9793 if (n > old_mem.len) {
9894 // This loop gets optimized out in ReleaseFast mode
9995 for (byte_slice[old_byte_slice.len..]) |*byte| {
100 *byte = undefined;
96 byte.* = undefined;
10197 }
10298 }
10399 return ([]T)(@alignCast(alignment, byte_slice));
......@@ -110,9 +106,7 @@ pub const Allocator = struct {
110106 return self.alignedShrink(T, @alignOf(T), @alignCast(@alignOf(T), old_mem), n);
111107 }
112108
113 fn alignedShrink(self: &Allocator, comptime T: type, comptime alignment: u29,
114 old_mem: []align(alignment) T, n: usize) []align(alignment) T
115 {
109 fn alignedShrink(self: &Allocator, comptime T: type, comptime alignment: u29, old_mem: []align(alignment) T, n: usize) []align(alignment) T {
116110 if (n == 0) {
117111 self.free(old_mem);
118112 return old_mem[0..0];
......@@ -131,8 +125,7 @@ pub const Allocator = struct {
131125
132126 fn free(self: &Allocator, memory: var) void {
133127 const bytes = ([]const u8)(memory);
134 if (bytes.len == 0)
135 return;
128 if (bytes.len == 0) return;
136129 const non_const_ptr = @intToPtr(&u8, @ptrToInt(bytes.ptr));
137130 self.freeFn(self, non_const_ptr[0..bytes.len]);
138131 }
......@@ -146,11 +139,13 @@ pub fn copy(comptime T: type, dest: []T, source: []const T) void {
146139 // this and automatically omit safety checks for loops
147140 @setRuntimeSafety(false);
148141 assert(dest.len >= source.len);
149 for (source) |s, i| dest[i] = s;
142 for (source) |s, i|
143 dest[i] = s;
150144}
151145
152146pub fn set(comptime T: type, dest: []T, value: T) void {
153 for (dest) |*d| *d = value;
147 for (dest) |*d|
148 d.* = value;
154149}
155150
156151/// Returns true if lhs < rhs, false otherwise
......@@ -229,8 +224,7 @@ pub fn lastIndexOfScalar(comptime T: type, slice: []const T, value: T) ?usize {
229224 var i: usize = slice.len;
230225 while (i != 0) {
231226 i -= 1;
232 if (slice[i] == value)
233 return i;
227 if (slice[i] == value) return i;
234228 }
235229 return null;
236230}
......@@ -238,8 +232,7 @@ pub fn lastIndexOfScalar(comptime T: type, slice: []const T, value: T) ?usize {
238232pub fn indexOfScalarPos(comptime T: type, slice: []const T, start_index: usize, value: T) ?usize {
239233 var i: usize = start_index;
240234 while (i < slice.len) : (i += 1) {
241 if (slice[i] == value)
242 return i;
235 if (slice[i] == value) return i;
243236 }
244237 return null;
245238}
......@@ -253,8 +246,7 @@ pub fn lastIndexOfAny(comptime T: type, slice: []const T, values: []const T) ?us
253246 while (i != 0) {
254247 i -= 1;
255248 for (values) |value| {
256 if (slice[i] == value)
257 return i;
249 if (slice[i] == value) return i;
258250 }
259251 }
260252 return null;
......@@ -264,8 +256,7 @@ pub fn indexOfAnyPos(comptime T: type, slice: []const T, start_index: usize, val
264256 var i: usize = start_index;
265257 while (i < slice.len) : (i += 1) {
266258 for (values) |value| {
267 if (slice[i] == value)
268 return i;
259 if (slice[i] == value) return i;
269260 }
270261 }
271262 return null;
......@@ -279,28 +270,23 @@ pub fn indexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize
279270/// To start looking at a different index, slice the haystack first.
280271/// TODO is there even a better algorithm for this?
281272pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize {
282 if (needle.len > haystack.len)
283 return null;
273 if (needle.len > haystack.len) return null;
284274
285275 var i: usize = haystack.len - needle.len;
286276 while (true) : (i -= 1) {
287 if (mem.eql(T, haystack[i..i+needle.len], needle))
288 return i;
289 if (i == 0)
290 return null;
277 if (mem.eql(T, haystack[i..i + needle.len], needle)) return i;
278 if (i == 0) return null;
291279 }
292280}
293281
294282// TODO boyer-moore algorithm
295283pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize {
296 if (needle.len > haystack.len)
297 return null;
284 if (needle.len > haystack.len) return null;
298285
299286 var i: usize = start_index;
300287 const end = haystack.len - needle.len;
301288 while (i <= end) : (i += 1) {
302 if (eql(T, haystack[i .. i + needle.len], needle))
303 return i;
289 if (eql(T, haystack[i..i + needle.len], needle)) return i;
304290 }
305291 return null;
306292}
......@@ -355,9 +341,12 @@ pub fn readIntBE(comptime T: type, bytes: []const u8) T {
355341 }
356342 assert(bytes.len == @sizeOf(T));
357343 var result: T = 0;
358 {comptime var i = 0; inline while (i < @sizeOf(T)) : (i += 1) {
359 result = (result << 8) | T(bytes[i]);
360 }}
344 {
345 comptime var i = 0;
346 inline while (i < @sizeOf(T)) : (i += 1) {
347 result = (result << 8) | T(bytes[i]);
348 }
349 }
361350 return result;
362351}
363352
......@@ -369,9 +358,12 @@ pub fn readIntLE(comptime T: type, bytes: []const u8) T {
369358 }
370359 assert(bytes.len == @sizeOf(T));
371360 var result: T = 0;
372 {comptime var i = 0; inline while (i < @sizeOf(T)) : (i += 1) {
373 result |= T(bytes[i]) << i * 8;
374 }}
361 {
362 comptime var i = 0;
363 inline while (i < @sizeOf(T)) : (i += 1) {
364 result |= T(bytes[i]) << i * 8;
365 }
366 }
375367 return result;
376368}
377369
......@@ -393,7 +385,7 @@ pub fn writeInt(buf: []u8, value: var, endian: builtin.Endian) void {
393385 },
394386 builtin.Endian.Little => {
395387 for (buf) |*b| {
396 *b = @truncate(u8, bits);
388 b.* = @truncate(u8, bits);
397389 bits >>= 8;
398390 }
399391 },
......@@ -401,7 +393,6 @@ pub fn writeInt(buf: []u8, value: var, endian: builtin.Endian) void {
401393 assert(bits == 0);
402394}
403395
404
405396pub fn hash_slice_u8(k: []const u8) u32 {
406397 // FNV 32-bit hash
407398 var h: u32 = 2166136261;
......@@ -420,7 +411,7 @@ pub fn eql_slice_u8(a: []const u8, b: []const u8) bool {
420411/// split(" abc def ghi ", " ")
421412/// Will return slices for "abc", "def", "ghi", null, in that order.
422413pub fn split(buffer: []const u8, split_bytes: []const u8) SplitIterator {
423 return SplitIterator {
414 return SplitIterator{
424415 .index = 0,
425416 .buffer = buffer,
426417 .split_bytes = split_bytes,
......@@ -436,7 +427,7 @@ test "mem.split" {
436427}
437428
438429pub fn startsWith(comptime T: type, haystack: []const T, needle: []const T) bool {
439 return if (needle.len > haystack.len) false else eql(T, haystack[0 .. needle.len], needle);
430 return if (needle.len > haystack.len) false else eql(T, haystack[0..needle.len], needle);
440431}
441432
442433test "mem.startsWith" {
......@@ -445,10 +436,9 @@ test "mem.startsWith" {
445436}
446437
447438pub fn endsWith(comptime T: type, haystack: []const T, needle: []const T) bool {
448 return if (needle.len > haystack.len) false else eql(T, haystack[haystack.len - needle.len ..], needle);
439 return if (needle.len > haystack.len) false else eql(T, haystack[haystack.len - needle.len..], needle);
449440}
450441
451
452442test "mem.endsWith" {
453443 assert(endsWith(u8, "Needle in haystack", "haystack"));
454444 assert(!endsWith(u8, "Bob", "Bo"));
......@@ -542,29 +532,47 @@ test "testReadInt" {
542532}
543533fn testReadIntImpl() void {
544534 {
545 const bytes = []u8{ 0x12, 0x34, 0x56, 0x78 };
546 assert(readInt(bytes, u32, builtin.Endian.Big) == 0x12345678);
547 assert(readIntBE(u32, bytes) == 0x12345678);
548 assert(readIntBE(i32, bytes) == 0x12345678);
535 const bytes = []u8{
536 0x12,
537 0x34,
538 0x56,
539 0x78,
540 };
541 assert(readInt(bytes, u32, builtin.Endian.Big) == 0x12345678);
542 assert(readIntBE(u32, bytes) == 0x12345678);
543 assert(readIntBE(i32, bytes) == 0x12345678);
549544 assert(readInt(bytes, u32, builtin.Endian.Little) == 0x78563412);
550 assert(readIntLE(u32, bytes) == 0x78563412);
551 assert(readIntLE(i32, bytes) == 0x78563412);
545 assert(readIntLE(u32, bytes) == 0x78563412);
546 assert(readIntLE(i32, bytes) == 0x78563412);
552547 }
553548 {
554 const buf = []u8{0x00, 0x00, 0x12, 0x34};
549 const buf = []u8{
550 0x00,
551 0x00,
552 0x12,
553 0x34,
554 };
555555 const answer = readInt(buf, u64, builtin.Endian.Big);
556556 assert(answer == 0x00001234);
557557 }
558558 {
559 const buf = []u8{0x12, 0x34, 0x00, 0x00};
559 const buf = []u8{
560 0x12,
561 0x34,
562 0x00,
563 0x00,
564 };
560565 const answer = readInt(buf, u64, builtin.Endian.Little);
561566 assert(answer == 0x00003412);
562567 }
563568 {
564 const bytes = []u8{0xff, 0xfe};
565 assert(readIntBE(u16, bytes) == 0xfffe);
569 const bytes = []u8{
570 0xff,
571 0xfe,
572 };
573 assert(readIntBE(u16, bytes) == 0xfffe);
566574 assert(readIntBE(i16, bytes) == -0x0002);
567 assert(readIntLE(u16, bytes) == 0xfeff);
575 assert(readIntLE(u16, bytes) == 0xfeff);
568576 assert(readIntLE(i16, bytes) == -0x0101);
569577 }
570578}
......@@ -577,19 +585,38 @@ fn testWriteIntImpl() void {
577585 var bytes: [4]u8 = undefined;
578586
579587 writeInt(bytes[0..], u32(0x12345678), builtin.Endian.Big);
580 assert(eql(u8, bytes, []u8{ 0x12, 0x34, 0x56, 0x78 }));
588 assert(eql(u8, bytes, []u8{
589 0x12,
590 0x34,
591 0x56,
592 0x78,
593 }));
581594
582595 writeInt(bytes[0..], u32(0x78563412), builtin.Endian.Little);
583 assert(eql(u8, bytes, []u8{ 0x12, 0x34, 0x56, 0x78 }));
596 assert(eql(u8, bytes, []u8{
597 0x12,
598 0x34,
599 0x56,
600 0x78,
601 }));
584602
585603 writeInt(bytes[0..], u16(0x1234), builtin.Endian.Big);
586 assert(eql(u8, bytes, []u8{ 0x00, 0x00, 0x12, 0x34 }));
604 assert(eql(u8, bytes, []u8{
605 0x00,
606 0x00,
607 0x12,
608 0x34,
609 }));
587610
588611 writeInt(bytes[0..], u16(0x1234), builtin.Endian.Little);
589 assert(eql(u8, bytes, []u8{ 0x34, 0x12, 0x00, 0x00 }));
612 assert(eql(u8, bytes, []u8{
613 0x34,
614 0x12,
615 0x00,
616 0x00,
617 }));
590618}
591619
592
593620pub fn min(comptime T: type, slice: []const T) T {
594621 var best = slice[0];
595622 for (slice[1..]) |item| {
......@@ -615,9 +642,9 @@ test "mem.max" {
615642}
616643
617644pub fn swap(comptime T: type, a: &T, b: &T) void {
618 const tmp = *a;
619 *a = *b;
620 *b = tmp;
645 const tmp = a.*;
646 a.* = b.*;
647 b.* = tmp;
621648}
622649
623650/// In-place order reversal of a slice
......@@ -630,10 +657,22 @@ pub fn reverse(comptime T: type, items: []T) void {
630657}
631658
632659test "std.mem.reverse" {
633 var arr = []i32{ 5, 3, 1, 2, 4 };
660 var arr = []i32{
661 5,
662 3,
663 1,
664 2,
665 4,
666 };
634667 reverse(i32, arr[0..]);
635668
636 assert(eql(i32, arr, []i32{ 4, 2, 1, 3, 5 }));
669 assert(eql(i32, arr, []i32{
670 4,
671 2,
672 1,
673 3,
674 5,
675 }));
637676}
638677
639678/// In-place rotation of the values in an array ([0 1 2 3] becomes [1 2 3 0] if we rotate by 1)
......@@ -645,10 +684,22 @@ pub fn rotate(comptime T: type, items: []T, amount: usize) void {
645684}
646685
647686test "std.mem.rotate" {
648 var arr = []i32{ 5, 3, 1, 2, 4 };
687 var arr = []i32{
688 5,
689 3,
690 1,
691 2,
692 4,
693 };
649694 rotate(i32, arr[0..], 2);
650695
651 assert(eql(i32, arr, []i32{ 1, 2, 4, 5, 3 }));
696 assert(eql(i32, arr, []i32{
697 1,
698 2,
699 4,
700 5,
701 3,
702 }));
652703}
653704
654705// TODO: When https://github.com/zig-lang/zig/issues/649 is solved these can be done by
std/zig/parser.zig+7-2
......@@ -3705,7 +3705,9 @@ pub const Parser = struct {
37053705 },
37063706 ast.Node.Id.PrefixOp => {
37073707 const prefix_op_node = @fieldParentPtr(ast.Node.PrefixOp, "base", base);
3708 try stack.append(RenderState { .Expression = prefix_op_node.rhs });
3708 if (prefix_op_node.op != ast.Node.PrefixOp.Op.Deref) {
3709 try stack.append(RenderState { .Expression = prefix_op_node.rhs });
3710 }
37093711 switch (prefix_op_node.op) {
37103712 ast.Node.PrefixOp.Op.AddrOf => |addr_of_info| {
37113713 try stream.write("&");
......@@ -3742,7 +3744,10 @@ pub const Parser = struct {
37423744 },
37433745 ast.Node.PrefixOp.Op.BitNot => try stream.write("~"),
37443746 ast.Node.PrefixOp.Op.BoolNot => try stream.write("!"),
3745 ast.Node.PrefixOp.Op.Deref => try stream.write("*"),
3747 ast.Node.PrefixOp.Op.Deref => {
3748 try stack.append(RenderState { .Text = ".*" });
3749 try stack.append(RenderState { .Expression = prefix_op_node.rhs });
3750 },
37463751 ast.Node.PrefixOp.Op.Negation => try stream.write("-"),
37473752 ast.Node.PrefixOp.Op.NegationWrap => try stream.write("-%"),
37483753 ast.Node.PrefixOp.Op.Try => try stream.write("try "),
test/cases/align.zig+60-26
......@@ -10,7 +10,9 @@ test "global variable alignment" {
1010 assert(@typeOf(slice) == []align(4) u8);
1111}
1212
13fn derp() align(@sizeOf(usize) * 2) i32 { return 1234; }
13fn derp() align(@sizeOf(usize) * 2) i32 {
14 return 1234;
15}
1416fn noop1() align(1) void {}
1517fn noop4() align(4) void {}
1618
......@@ -22,7 +24,6 @@ test "function alignment" {
2224 noop4();
2325}
2426
25
2627var baz: packed struct {
2728 a: u32,
2829 b: u32,
......@@ -32,7 +33,6 @@ test "packed struct alignment" {
3233 assert(@typeOf(&baz.b) == &align(1) u32);
3334}
3435
35
3636const blah: packed struct {
3737 a: u3,
3838 b: u3,
......@@ -53,29 +53,43 @@ test "implicitly decreasing pointer alignment" {
5353 assert(addUnaligned(&a, &b) == 7);
5454}
5555
56fn addUnaligned(a: &align(1) const u32, b: &align(1) const u32) u32 { return *a + *b; }
56fn addUnaligned(a: &align(1) const u32, b: &align(1) const u32) u32 {
57 return a.* + b.*;
58}
5759
5860test "implicitly decreasing slice alignment" {
5961 const a: u32 align(4) = 3;
6062 const b: u32 align(8) = 4;
6163 assert(addUnalignedSlice((&a)[0..1], (&b)[0..1]) == 7);
6264}
63fn addUnalignedSlice(a: []align(1) const u32, b: []align(1) const u32) u32 { return a[0] + b[0]; }
65fn addUnalignedSlice(a: []align(1) const u32, b: []align(1) const u32) u32 {
66 return a[0] + b[0];
67}
6468
6569test "specifying alignment allows pointer cast" {
6670 testBytesAlign(0x33);
6771}
6872fn testBytesAlign(b: u8) void {
69 var bytes align(4) = []u8{b, b, b, b};
73 var bytes align(4) = []u8 {
74 b,
75 b,
76 b,
77 b,
78 };
7079 const ptr = @ptrCast(&u32, &bytes[0]);
71 assert(*ptr == 0x33333333);
80 assert(ptr.* == 0x33333333);
7281}
7382
7483test "specifying alignment allows slice cast" {
7584 testBytesAlignSlice(0x33);
7685}
7786fn testBytesAlignSlice(b: u8) void {
78 var bytes align(4) = []u8{b, b, b, b};
87 var bytes align(4) = []u8 {
88 b,
89 b,
90 b,
91 b,
92 };
7993 const slice = ([]u32)(bytes[0..]);
8094 assert(slice[0] == 0x33333333);
8195}
......@@ -89,11 +103,14 @@ fn expectsOnly1(x: &align(1) u32) void {
89103 expects4(@alignCast(4, x));
90104}
91105fn expects4(x: &align(4) u32) void {
92 *x += 1;
106 x.* += 1;
93107}
94108
95109test "@alignCast slices" {
96 var array align(4) = []u32{1, 1};
110 var array align(4) = []u32 {
111 1,
112 1,
113 };
97114 const slice = array[0..];
98115 sliceExpectsOnly1(slice);
99116 assert(slice[0] == 2);
......@@ -105,31 +122,34 @@ fn sliceExpects4(slice: []align(4) u32) void {
105122 slice[0] += 1;
106123}
107124
108
109125test "implicitly decreasing fn alignment" {
110126 testImplicitlyDecreaseFnAlign(alignedSmall, 1234);
111127 testImplicitlyDecreaseFnAlign(alignedBig, 5678);
112128}
113129
114fn testImplicitlyDecreaseFnAlign(ptr: fn () align(1) i32, answer: i32) void {
130fn testImplicitlyDecreaseFnAlign(ptr: fn() align(1) i32, answer: i32) void {
115131 assert(ptr() == answer);
116132}
117133
118fn alignedSmall() align(8) i32 { return 1234; }
119fn alignedBig() align(16) i32 { return 5678; }
120
134fn alignedSmall() align(8) i32 {
135 return 1234;
136}
137fn alignedBig() align(16) i32 {
138 return 5678;
139}
121140
122141test "@alignCast functions" {
123142 assert(fnExpectsOnly1(simple4) == 0x19);
124143}
125fn fnExpectsOnly1(ptr: fn()align(1) i32) i32 {
144fn fnExpectsOnly1(ptr: fn() align(1) i32) i32 {
126145 return fnExpects4(@alignCast(4, ptr));
127146}
128fn fnExpects4(ptr: fn()align(4) i32) i32 {
147fn fnExpects4(ptr: fn() align(4) i32) i32 {
129148 return ptr();
130149}
131fn simple4() align(4) i32 { return 0x19; }
132
150fn simple4() align(4) i32 {
151 return 0x19;
152}
133153
134154test "generic function with align param" {
135155 assert(whyWouldYouEverDoThis(1) == 0x1);
......@@ -137,8 +157,9 @@ test "generic function with align param" {
137157 assert(whyWouldYouEverDoThis(8) == 0x1);
138158}
139159
140fn whyWouldYouEverDoThis(comptime align_bytes: u8) align(align_bytes) u8 { return 0x1; }
141
160fn whyWouldYouEverDoThis(comptime align_bytes: u8) align(align_bytes) u8 {
161 return 0x1;
162}
142163
143164test "@ptrCast preserves alignment of bigger source" {
144165 var x: u32 align(16) = 1234;
......@@ -146,24 +167,38 @@ test "@ptrCast preserves alignment of bigger source" {
146167 assert(@typeOf(ptr) == &align(16) u8);
147168}
148169
149
150170test "compile-time known array index has best alignment possible" {
151171 // take full advantage of over-alignment
152 var array align(4) = []u8 {1, 2, 3, 4};
172 var array align(4) = []u8 {
173 1,
174 2,
175 3,
176 4,
177 };
153178 assert(@typeOf(&array[0]) == &align(4) u8);
154179 assert(@typeOf(&array[1]) == &u8);
155180 assert(@typeOf(&array[2]) == &align(2) u8);
156181 assert(@typeOf(&array[3]) == &u8);
157182
158183 // because align is too small but we still figure out to use 2
159 var bigger align(2) = []u64{1, 2, 3, 4};
184 var bigger align(2) = []u64 {
185 1,
186 2,
187 3,
188 4,
189 };
160190 assert(@typeOf(&bigger[0]) == &align(2) u64);
161191 assert(@typeOf(&bigger[1]) == &align(2) u64);
162192 assert(@typeOf(&bigger[2]) == &align(2) u64);
163193 assert(@typeOf(&bigger[3]) == &align(2) u64);
164194
165195 // because pointer is align 2 and u32 align % 2 == 0 we can assume align 2
166 var smaller align(2) = []u32{1, 2, 3, 4};
196 var smaller align(2) = []u32 {
197 1,
198 2,
199 3,
200 4,
201 };
167202 testIndex(&smaller[0], 0, &align(2) u32);
168203 testIndex(&smaller[0], 1, &align(2) u32);
169204 testIndex(&smaller[0], 2, &align(2) u32);
......@@ -182,7 +217,6 @@ fn testIndex2(ptr: &align(4) u8, index: usize, comptime T: type) void {
182217 assert(@typeOf(&ptr[index]) == T);
183218}
184219
185
186220test "alignstack" {
187221 assert(fnWithAlignedStack() == 1234);
188222}
test/cases/alignof.zig+5-1
......@@ -1,7 +1,11 @@
11const assert = @import("std").debug.assert;
22const builtin = @import("builtin");
33
4const Foo = struct { x: u32, y: u32, z: u32, };
4const Foo = struct {
5 x: u32,
6 y: u32,
7 z: u32,
8};
59
610test "@alignOf(T) before referencing T" {
711 comptime assert(@alignOf(Foo) != @maxValue(usize));
test/cases/array.zig+31-10
......@@ -2,9 +2,9 @@ const assert = @import("std").debug.assert;
22const mem = @import("std").mem;
33
44test "arrays" {
5 var array : [5]u32 = undefined;
5 var array: [5]u32 = undefined;
66
7 var i : u32 = 0;
7 var i: u32 = 0;
88 while (i < 5) {
99 array[i] = i + 1;
1010 i = array[i];
......@@ -34,24 +34,41 @@ test "void arrays" {
3434}
3535
3636test "array literal" {
37 const hex_mult = []u16{4096, 256, 16, 1};
37 const hex_mult = []u16 {
38 4096,
39 256,
40 16,
41 1,
42 };
3843
3944 assert(hex_mult.len == 4);
4045 assert(hex_mult[1] == 256);
4146}
4247
4348test "array dot len const expr" {
44 assert(comptime x: {break :x some_array.len == 4;});
49 assert(comptime x: {
50 break :x some_array.len == 4;
51 });
4552}
4653
4754const ArrayDotLenConstExpr = struct {
4855 y: [some_array.len]u8,
4956};
50const some_array = []u8 {0, 1, 2, 3};
51
57const some_array = []u8 {
58 0,
59 1,
60 2,
61 3,
62};
5263
5364test "nested arrays" {
54 const array_of_strings = [][]const u8 {"hello", "this", "is", "my", "thing"};
65 const array_of_strings = [][]const u8 {
66 "hello",
67 "this",
68 "is",
69 "my",
70 "thing",
71 };
5572 for (array_of_strings) |s, i| {
5673 if (i == 0) assert(mem.eql(u8, s, "hello"));
5774 if (i == 1) assert(mem.eql(u8, s, "this"));
......@@ -61,7 +78,6 @@ test "nested arrays" {
6178 }
6279}
6380
64
6581var s_array: [8]Sub = undefined;
6682const Sub = struct {
6783 b: u8,
......@@ -70,7 +86,9 @@ const Str = struct {
7086 a: []Sub,
7187};
7288test "set global var array via slice embedded in struct" {
73 var s = Str { .a = s_array[0..]};
89 var s = Str {
90 .a = s_array[0..],
91 };
7492
7593 s.a[0].b = 1;
7694 s.a[1].b = 2;
......@@ -82,7 +100,10 @@ test "set global var array via slice embedded in struct" {
82100}
83101
84102test "array literal with specified size" {
85 var array = [2]u8{1, 2};
103 var array = [2]u8 {
104 1,
105 2,
106 };
86107 assert(array[0] == 1);
87108 assert(array[1] == 2);
88109}
test/cases/bitcast.zig+6-2
......@@ -10,5 +10,9 @@ fn testBitCast_i32_u32() void {
1010 assert(conv2(@maxValue(u32)) == -1);
1111}
1212
13fn conv(x: i32) u32 { return @bitCast(u32, x); }
14fn conv2(x: u32) i32 { return @bitCast(i32, x); }
13fn conv(x: i32) u32 {
14 return @bitCast(u32, x);
15}
16fn conv2(x: u32) i32 {
17 return @bitCast(i32, x);
18}
test/cases/bugs/394.zig+14-3
......@@ -1,9 +1,20 @@
1const E = union(enum) { A: [9]u8, B: u64, };
2const S = struct { x: u8, y: E, };
1const E = union(enum) {
2 A: [9]u8,
3 B: u64,
4};
5const S = struct {
6 x: u8,
7 y: E,
8};
39
410const assert = @import("std").debug.assert;
511
612test "bug 394 fixed" {
7 const x = S { .x = 3, .y = E {.B = 1 } };
13 const x = S {
14 .x = 3,
15 .y = E {
16 .B = 1,
17 },
18 };
819 assert(x.x == 3);
920}
test/cases/bugs/655.zig+1-1
......@@ -8,5 +8,5 @@ test "function with &const parameter with type dereferenced by namespace" {
88}
99
1010fn foo(x: &const other_file.Integer) void {
11 std.debug.assert(*x == 1234);
11 std.debug.assert(x.* == 1234);
1212}
test/cases/bugs/656.zig+7-4
......@@ -14,12 +14,15 @@ test "nullable if after an if in a switch prong of a switch with 2 prongs in an
1414}
1515
1616fn foo(a: bool, b: bool) void {
17 var prefix_op = PrefixOp { .AddrOf = Value { .align_expr = 1234 } };
18 if (a) {
19 } else {
17 var prefix_op = PrefixOp {
18 .AddrOf = Value {
19 .align_expr = 1234,
20 },
21 };
22 if (a) {} else {
2023 switch (prefix_op) {
2124 PrefixOp.AddrOf => |addr_of_info| {
22 if (b) { }
25 if (b) {}
2326 if (addr_of_info.align_expr) |align_expr| {
2427 assert(align_expr == 1234);
2528 }
test/cases/bugs/828.zig+5-5
......@@ -1,10 +1,10 @@
11const CountBy = struct {
22 a: usize,
3
3
44 const One = CountBy {
55 .a = 1,
66 };
7
7
88 pub fn counter(self: &const CountBy) Counter {
99 return Counter {
1010 .i = 0,
......@@ -14,7 +14,7 @@ const CountBy = struct {
1414
1515const Counter = struct {
1616 i: usize,
17
17
1818 pub fn count(self: &Counter) bool {
1919 self.i += 1;
2020 return self.i <= 10;
......@@ -24,8 +24,8 @@ const Counter = struct {
2424fn constCount(comptime cb: &const CountBy, comptime unused: u32) void {
2525 comptime {
2626 var cnt = cb.counter();
27 if(cnt.i != 0) @compileError("Counter instance reused!");
28 while(cnt.count()){}
27 if (cnt.i != 0) @compileError("Counter instance reused!");
28 while (cnt.count()) {}
2929 }
3030}
3131
test/cases/bugs/920.zig+12-7
......@@ -12,8 +12,7 @@ const ZigTable = struct {
1212 zero_case: fn(&Random, f64) f64,
1313};
1414
15fn ZigTableGen(comptime is_symmetric: bool, comptime r: f64, comptime v: f64, comptime f: fn(f64) f64,
16 comptime f_inv: fn(f64) f64, comptime zero_case: fn(&Random, f64) f64) ZigTable {
15fn ZigTableGen(comptime is_symmetric: bool, comptime r: f64, comptime v: f64, comptime f: fn(f64) f64, comptime f_inv: fn(f64) f64, comptime zero_case: fn(&Random, f64) f64) ZigTable {
1716 var tables: ZigTable = undefined;
1817
1918 tables.is_symmetric = is_symmetric;
......@@ -26,12 +25,12 @@ fn ZigTableGen(comptime is_symmetric: bool, comptime r: f64, comptime v: f64, co
2625
2726 for (tables.x[2..256]) |*entry, i| {
2827 const last = tables.x[2 + i - 1];
29 *entry = f_inv(v / last + f(last));
28 entry.* = f_inv(v / last + f(last));
3029 }
3130 tables.x[256] = 0;
3231
3332 for (tables.f[0..]) |*entry, i| {
34 *entry = f(tables.x[i]);
33 entry.* = f(tables.x[i]);
3534 }
3635
3736 return tables;
......@@ -40,9 +39,15 @@ fn ZigTableGen(comptime is_symmetric: bool, comptime r: f64, comptime v: f64, co
4039const norm_r = 3.6541528853610088;
4140const norm_v = 0.00492867323399;
4241
43fn norm_f(x: f64) f64 { return math.exp(-x * x / 2.0); }
44fn norm_f_inv(y: f64) f64 { return math.sqrt(-2.0 * math.ln(y)); }
45fn norm_zero_case(random: &Random, u: f64) f64 { return 0.0; }
42fn norm_f(x: f64) f64 {
43 return math.exp(-x * x / 2.0);
44}
45fn norm_f_inv(y: f64) f64 {
46 return math.sqrt(-2.0 * math.ln(y));
47}
48fn norm_zero_case(random: &Random, u: f64) f64 {
49 return 0.0;
50}
4651
4752const NormalDist = blk: {
4853 @setEvalBranchQuota(30000);
test/cases/cast.zig+43-29
......@@ -14,10 +14,10 @@ test "integer literal to pointer cast" {
1414}
1515
1616test "pointer reinterpret const float to int" {
17 const float: f64 = 5.99999999999994648725e-01;
17 const float: f64 = 5.99999999999994648725e - 01;
1818 const float_ptr = &float;
1919 const int_ptr = @ptrCast(&const i32, float_ptr);
20 const int_val = *int_ptr;
20 const int_val = int_ptr.*;
2121 assert(int_val == 858993411);
2222}
2323
......@@ -29,25 +29,31 @@ test "implicitly cast a pointer to a const pointer of it" {
2929}
3030
3131fn funcWithConstPtrPtr(x: &const &i32) void {
32 **x += 1;
32 x.*.* += 1;
3333}
3434
3535test "implicitly cast a container to a const pointer of it" {
36 const z = Struct(void) { .x = void{} };
36 const z = Struct(void) {
37 .x = void{},
38 };
3739 assert(0 == @sizeOf(@typeOf(z)));
3840 assert(void{} == Struct(void).pointer(z).x);
3941 assert(void{} == Struct(void).pointer(&z).x);
4042 assert(void{} == Struct(void).maybePointer(z).x);
4143 assert(void{} == Struct(void).maybePointer(&z).x);
4244 assert(void{} == Struct(void).maybePointer(null).x);
43 const s = Struct(u8) { .x = 42 };
45 const s = Struct(u8) {
46 .x = 42,
47 };
4448 assert(0 != @sizeOf(@typeOf(s)));
4549 assert(42 == Struct(u8).pointer(s).x);
4650 assert(42 == Struct(u8).pointer(&s).x);
4751 assert(42 == Struct(u8).maybePointer(s).x);
4852 assert(42 == Struct(u8).maybePointer(&s).x);
4953 assert(0 == Struct(u8).maybePointer(null).x);
50 const u = Union { .x = 42 };
54 const u = Union {
55 .x = 42,
56 };
5157 assert(42 == Union.pointer(u).x);
5258 assert(42 == Union.pointer(&u).x);
5359 assert(42 == Union.maybePointer(u).x);
......@@ -67,12 +73,14 @@ fn Struct(comptime T: type) type {
6773 x: T,
6874
6975 fn pointer(self: &const Self) Self {
70 return *self;
76 return self.*;
7177 }
7278
7379 fn maybePointer(self: ?&const Self) Self {
74 const none = Self { .x = if (T == void) void{} else 0 };
75 return *(self ?? &none);
80 const none = Self {
81 .x = if (T == void) void{} else 0,
82 };
83 return (self ?? &none).*;
7684 }
7785 };
7886}
......@@ -81,12 +89,14 @@ const Union = union {
8189 x: u8,
8290
8391 fn pointer(self: &const Union) Union {
84 return *self;
92 return self.*;
8593 }
8694
8795 fn maybePointer(self: ?&const Union) Union {
88 const none = Union { .x = 0 };
89 return *(self ?? &none);
96 const none = Union {
97 .x = 0,
98 };
99 return (self ?? &none).*;
90100 }
91101};
92102
......@@ -95,11 +105,11 @@ const Enum = enum {
95105 Some,
96106
97107 fn pointer(self: &const Enum) Enum {
98 return *self;
108 return self.*;
99109 }
100110
101111 fn maybePointer(self: ?&const Enum) Enum {
102 return *(self ?? &Enum.None);
112 return (self ?? &Enum.None).*;
103113 }
104114};
105115
......@@ -108,19 +118,21 @@ test "implicitly cast indirect pointer to maybe-indirect pointer" {
108118 const Self = this;
109119 x: u8,
110120 fn constConst(p: &const &const Self) u8 {
111 return (*p).x;
121 return (p.*).x;
112122 }
113123 fn maybeConstConst(p: ?&const &const Self) u8 {
114 return (*??p).x;
124 return (??p.*).x;
115125 }
116126 fn constConstConst(p: &const &const &const Self) u8 {
117 return (**p).x;
127 return (p.*.*).x;
118128 }
119129 fn maybeConstConstConst(p: ?&const &const &const Self) u8 {
120 return (**??p).x;
130 return (??p.*.*).x;
121131 }
122132 };
123 const s = S { .x = 42 };
133 const s = S {
134 .x = 42,
135 };
124136 const p = &s;
125137 const q = &p;
126138 const r = &q;
......@@ -154,7 +166,6 @@ fn boolToStr(b: bool) []const u8 {
154166 return if (b) "true" else "false";
155167}
156168
157
158169test "peer resolve array and const slice" {
159170 testPeerResolveArrayConstSlice(true);
160171 comptime testPeerResolveArrayConstSlice(true);
......@@ -168,12 +179,12 @@ fn testPeerResolveArrayConstSlice(b: bool) void {
168179
169180test "integer literal to &const int" {
170181 const x: &const i32 = 3;
171 assert(*x == 3);
182 assert(x.* == 3);
172183}
173184
174185test "string literal to &const []const u8" {
175186 const x: &const []const u8 = "hello";
176 assert(mem.eql(u8, *x, "hello"));
187 assert(mem.eql(u8, x.*, "hello"));
177188}
178189
179190test "implicitly cast from T to error!?T" {
......@@ -191,7 +202,9 @@ fn castToMaybeTypeError(z: i32) void {
191202 const f = z;
192203 const g: error!?i32 = f;
193204
194 const a = A{ .a = z };
205 const a = A {
206 .a = z,
207 };
195208 const b: error!?A = a;
196209 assert((??(b catch unreachable)).a == 1);
197210}
......@@ -205,7 +218,6 @@ fn implicitIntLitToMaybe() void {
205218 const g: error!?i32 = 1;
206219}
207220
208
209221test "return null from fn() error!?&T" {
210222 const a = returnNullFromMaybeTypeErrorRef();
211223 const b = returnNullLitFromMaybeTypeErrorRef();
......@@ -235,7 +247,6 @@ fn peerTypeTAndMaybeT(c: bool, b: bool) ?usize {
235247 return usize(3);
236248}
237249
238
239250test "peer type resolution: [0]u8 and []const u8" {
240251 assert(peerTypeEmptyArrayAndSlice(true, "hi").len == 0);
241252 assert(peerTypeEmptyArrayAndSlice(false, "hi").len == 1);
......@@ -246,7 +257,7 @@ test "peer type resolution: [0]u8 and []const u8" {
246257}
247258fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) []const u8 {
248259 if (a) {
249 return []const u8 {};
260 return []const u8{};
250261 }
251262
252263 return slice[0..1];
......@@ -261,7 +272,6 @@ fn castToMaybeSlice() ?[]const u8 {
261272 return "hi";
262273}
263274
264
265275test "implicitly cast from [0]T to error![]T" {
266276 testCastZeroArrayToErrSliceMut();
267277 comptime testCastZeroArrayToErrSliceMut();
......@@ -329,7 +339,6 @@ fn foo(args: ...) void {
329339 assert(@typeOf(args[0]) == &const [5]u8);
330340}
331341
332
333342test "peer type resolution: error and [N]T" {
334343 // TODO: implicit error!T to error!U where T can implicitly cast to U
335344 //assert(mem.eql(u8, try testPeerErrorAndArray(0), "OK"));
......@@ -378,7 +387,12 @@ fn cast128Float(x: u128) f128 {
378387}
379388
380389test "const slice widen cast" {
381 const bytes align(4) = []u8{0x12, 0x12, 0x12, 0x12};
390 const bytes align(4) = []u8 {
391 0x12,
392 0x12,
393 0x12,
394 0x12,
395 };
382396
383397 const u32_value = ([]const u32)(bytes[0..])[0];
384398 assert(u32_value == 0x12121212);
test/cases/coroutines.zig+9-9
......@@ -36,7 +36,7 @@ async fn testAsyncSeq() void {
3636 suspend;
3737 seq('d');
3838}
39var points = []u8{0} ** "abcdefg".len;
39var points = []u8 {0} ** "abcdefg".len;
4040var index: usize = 0;
4141
4242fn seq(c: u8) void {
......@@ -94,7 +94,7 @@ async fn await_another() i32 {
9494 return 1234;
9595}
9696
97var await_points = []u8{0} ** "abcdefghi".len;
97var await_points = []u8 {0} ** "abcdefghi".len;
9898var await_seq_index: usize = 0;
9999
100100fn await_seq(c: u8) void {
......@@ -102,7 +102,6 @@ fn await_seq(c: u8) void {
102102 await_seq_index += 1;
103103}
104104
105
106105var early_final_result: i32 = 0;
107106
108107test "coroutine await early return" {
......@@ -126,7 +125,7 @@ async fn early_another() i32 {
126125 return 1234;
127126}
128127
129var early_points = []u8{0} ** "abcdef".len;
128var early_points = []u8 {0} ** "abcdef".len;
130129var early_seq_index: usize = 0;
131130
132131fn early_seq(c: u8) void {
......@@ -175,8 +174,8 @@ test "async fn pointer in a struct field" {
175174}
176175
177176async<&std.mem.Allocator> fn simpleAsyncFn2(y: &i32) void {
178 defer *y += 2;
179 *y += 1;
177 defer y.* += 2;
178 y.* += 1;
180179 suspend;
181180}
182181
......@@ -205,7 +204,8 @@ test "error return trace across suspend points - async return" {
205204 cancel p2;
206205}
207206
208fn nonFailing() promise->error!void {
207// TODO https://github.com/zig-lang/zig/issues/760
208fn nonFailing() (promise->error!void) {
209209 return async<std.debug.global_allocator> suspendThenFail() catch unreachable;
210210}
211211
......@@ -238,7 +238,7 @@ async fn testBreakFromSuspend(my_result: &i32) void {
238238 s: suspend |p| {
239239 break :s;
240240 }
241 *my_result += 1;
241 my_result.* += 1;
242242 suspend;
243 *my_result += 1;
243 my_result.* += 1;
244244}
test/cases/defer.zig+12-3
......@@ -5,9 +5,18 @@ var index: usize = undefined;
55
66fn runSomeErrorDefers(x: bool) !bool {
77 index = 0;
8 defer {result[index] = 'a'; index += 1;}
9 errdefer {result[index] = 'b'; index += 1;}
10 defer {result[index] = 'c'; index += 1;}
8 defer {
9 result[index] = 'a';
10 index += 1;
11 }
12 errdefer {
13 result[index] = 'b';
14 index += 1;
15 }
16 defer {
17 result[index] = 'c';
18 index += 1;
19 }
1120 return if (x) x else error.FalseNotAllowed;
1221}
1322
test/cases/enum.zig+548-58
......@@ -2,8 +2,15 @@ const assert = @import("std").debug.assert;
22const mem = @import("std").mem;
33
44test "enum type" {
5 const foo1 = Foo{ .One = 13};
6 const foo2 = Foo{. Two = Point { .x = 1234, .y = 5678, }};
5 const foo1 = Foo {
6 .One = 13,
7 };
8 const foo2 = Foo {
9 .Two = Point {
10 .x = 1234,
11 .y = 5678,
12 },
13 };
714 const bar = Bar.B;
815
916 assert(bar == Bar.B);
......@@ -41,26 +48,31 @@ const Bar = enum {
4148};
4249
4350fn returnAnInt(x: i32) Foo {
44 return Foo { .One = x };
51 return Foo {
52 .One = x,
53 };
4554}
4655
47
4856test "constant enum with payload" {
49 var empty = AnEnumWithPayload {.Empty = {}};
50 var full = AnEnumWithPayload {.Full = 13};
57 var empty = AnEnumWithPayload {
58 .Empty = {},
59 };
60 var full = AnEnumWithPayload {
61 .Full = 13,
62 };
5163 shouldBeEmpty(empty);
5264 shouldBeNotEmpty(full);
5365}
5466
5567fn shouldBeEmpty(x: &const AnEnumWithPayload) void {
56 switch (*x) {
68 switch (x.*) {
5769 AnEnumWithPayload.Empty => {},
5870 else => unreachable,
5971 }
6072}
6173
6274fn shouldBeNotEmpty(x: &const AnEnumWithPayload) void {
63 switch (*x) {
75 switch (x.*) {
6476 AnEnumWithPayload.Empty => unreachable,
6577 else => {},
6678 }
......@@ -71,8 +83,6 @@ const AnEnumWithPayload = union(enum) {
7183 Full: i32,
7284};
7385
74
75
7686const Number = enum {
7787 Zero,
7888 One,
......@@ -93,7 +103,6 @@ fn shouldEqual(n: Number, expected: u3) void {
93103 assert(u3(n) == expected);
94104}
95105
96
97106test "int to enum" {
98107 testIntToEnumEval(3);
99108}
......@@ -108,7 +117,6 @@ const IntToEnumNumber = enum {
108117 Four,
109118};
110119
111
112120test "@tagName" {
113121 assert(mem.eql(u8, testEnumTagNameBare(BareNumber.Three), "Three"));
114122 comptime assert(mem.eql(u8, testEnumTagNameBare(BareNumber.Three), "Three"));
......@@ -124,7 +132,6 @@ const BareNumber = enum {
124132 Three,
125133};
126134
127
128135test "enum alignment" {
129136 comptime {
130137 assert(@alignOf(AlignTestEnum) >= @alignOf([9]u8));
......@@ -137,47 +144,529 @@ const AlignTestEnum = union(enum) {
137144 B: u64,
138145};
139146
140const ValueCount1 = enum { I0 };
141const ValueCount2 = enum { I0, I1 };
147const ValueCount1 = enum {
148 I0,
149};
150const ValueCount2 = enum {
151 I0,
152 I1,
153};
142154const ValueCount256 = enum {
143 I0, I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11, I12, I13, I14, I15,
144 I16, I17, I18, I19, I20, I21, I22, I23, I24, I25, I26, I27, I28, I29, I30, I31,
145 I32, I33, I34, I35, I36, I37, I38, I39, I40, I41, I42, I43, I44, I45, I46, I47,
146 I48, I49, I50, I51, I52, I53, I54, I55, I56, I57, I58, I59, I60, I61, I62, I63,
147 I64, I65, I66, I67, I68, I69, I70, I71, I72, I73, I74, I75, I76, I77, I78, I79,
148 I80, I81, I82, I83, I84, I85, I86, I87, I88, I89, I90, I91, I92, I93, I94, I95,
149 I96, I97, I98, I99, I100, I101, I102, I103, I104, I105, I106, I107, I108, I109,
150 I110, I111, I112, I113, I114, I115, I116, I117, I118, I119, I120, I121, I122, I123,
151 I124, I125, I126, I127, I128, I129, I130, I131, I132, I133, I134, I135, I136, I137,
152 I138, I139, I140, I141, I142, I143, I144, I145, I146, I147, I148, I149, I150, I151,
153 I152, I153, I154, I155, I156, I157, I158, I159, I160, I161, I162, I163, I164, I165,
154 I166, I167, I168, I169, I170, I171, I172, I173, I174, I175, I176, I177, I178, I179,
155 I180, I181, I182, I183, I184, I185, I186, I187, I188, I189, I190, I191, I192, I193,
156 I194, I195, I196, I197, I198, I199, I200, I201, I202, I203, I204, I205, I206, I207,
157 I208, I209, I210, I211, I212, I213, I214, I215, I216, I217, I218, I219, I220, I221,
158 I222, I223, I224, I225, I226, I227, I228, I229, I230, I231, I232, I233, I234, I235,
159 I236, I237, I238, I239, I240, I241, I242, I243, I244, I245, I246, I247, I248, I249,
160 I250, I251, I252, I253, I254, I255
155 I0,
156 I1,
157 I2,
158 I3,
159 I4,
160 I5,
161 I6,
162 I7,
163 I8,
164 I9,
165 I10,
166 I11,
167 I12,
168 I13,
169 I14,
170 I15,
171 I16,
172 I17,
173 I18,
174 I19,
175 I20,
176 I21,
177 I22,
178 I23,
179 I24,
180 I25,
181 I26,
182 I27,
183 I28,
184 I29,
185 I30,
186 I31,
187 I32,
188 I33,
189 I34,
190 I35,
191 I36,
192 I37,
193 I38,
194 I39,
195 I40,
196 I41,
197 I42,
198 I43,
199 I44,
200 I45,
201 I46,
202 I47,
203 I48,
204 I49,
205 I50,
206 I51,
207 I52,
208 I53,
209 I54,
210 I55,
211 I56,
212 I57,
213 I58,
214 I59,
215 I60,
216 I61,
217 I62,
218 I63,
219 I64,
220 I65,
221 I66,
222 I67,
223 I68,
224 I69,
225 I70,
226 I71,
227 I72,
228 I73,
229 I74,
230 I75,
231 I76,
232 I77,
233 I78,
234 I79,
235 I80,
236 I81,
237 I82,
238 I83,
239 I84,
240 I85,
241 I86,
242 I87,
243 I88,
244 I89,
245 I90,
246 I91,
247 I92,
248 I93,
249 I94,
250 I95,
251 I96,
252 I97,
253 I98,
254 I99,
255 I100,
256 I101,
257 I102,
258 I103,
259 I104,
260 I105,
261 I106,
262 I107,
263 I108,
264 I109,
265 I110,
266 I111,
267 I112,
268 I113,
269 I114,
270 I115,
271 I116,
272 I117,
273 I118,
274 I119,
275 I120,
276 I121,
277 I122,
278 I123,
279 I124,
280 I125,
281 I126,
282 I127,
283 I128,
284 I129,
285 I130,
286 I131,
287 I132,
288 I133,
289 I134,
290 I135,
291 I136,
292 I137,
293 I138,
294 I139,
295 I140,
296 I141,
297 I142,
298 I143,
299 I144,
300 I145,
301 I146,
302 I147,
303 I148,
304 I149,
305 I150,
306 I151,
307 I152,
308 I153,
309 I154,
310 I155,
311 I156,
312 I157,
313 I158,
314 I159,
315 I160,
316 I161,
317 I162,
318 I163,
319 I164,
320 I165,
321 I166,
322 I167,
323 I168,
324 I169,
325 I170,
326 I171,
327 I172,
328 I173,
329 I174,
330 I175,
331 I176,
332 I177,
333 I178,
334 I179,
335 I180,
336 I181,
337 I182,
338 I183,
339 I184,
340 I185,
341 I186,
342 I187,
343 I188,
344 I189,
345 I190,
346 I191,
347 I192,
348 I193,
349 I194,
350 I195,
351 I196,
352 I197,
353 I198,
354 I199,
355 I200,
356 I201,
357 I202,
358 I203,
359 I204,
360 I205,
361 I206,
362 I207,
363 I208,
364 I209,
365 I210,
366 I211,
367 I212,
368 I213,
369 I214,
370 I215,
371 I216,
372 I217,
373 I218,
374 I219,
375 I220,
376 I221,
377 I222,
378 I223,
379 I224,
380 I225,
381 I226,
382 I227,
383 I228,
384 I229,
385 I230,
386 I231,
387 I232,
388 I233,
389 I234,
390 I235,
391 I236,
392 I237,
393 I238,
394 I239,
395 I240,
396 I241,
397 I242,
398 I243,
399 I244,
400 I245,
401 I246,
402 I247,
403 I248,
404 I249,
405 I250,
406 I251,
407 I252,
408 I253,
409 I254,
410 I255,
161411};
162412const ValueCount257 = enum {
163 I0, I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11, I12, I13, I14, I15,
164 I16, I17, I18, I19, I20, I21, I22, I23, I24, I25, I26, I27, I28, I29, I30, I31,
165 I32, I33, I34, I35, I36, I37, I38, I39, I40, I41, I42, I43, I44, I45, I46, I47,
166 I48, I49, I50, I51, I52, I53, I54, I55, I56, I57, I58, I59, I60, I61, I62, I63,
167 I64, I65, I66, I67, I68, I69, I70, I71, I72, I73, I74, I75, I76, I77, I78, I79,
168 I80, I81, I82, I83, I84, I85, I86, I87, I88, I89, I90, I91, I92, I93, I94, I95,
169 I96, I97, I98, I99, I100, I101, I102, I103, I104, I105, I106, I107, I108, I109,
170 I110, I111, I112, I113, I114, I115, I116, I117, I118, I119, I120, I121, I122, I123,
171 I124, I125, I126, I127, I128, I129, I130, I131, I132, I133, I134, I135, I136, I137,
172 I138, I139, I140, I141, I142, I143, I144, I145, I146, I147, I148, I149, I150, I151,
173 I152, I153, I154, I155, I156, I157, I158, I159, I160, I161, I162, I163, I164, I165,
174 I166, I167, I168, I169, I170, I171, I172, I173, I174, I175, I176, I177, I178, I179,
175 I180, I181, I182, I183, I184, I185, I186, I187, I188, I189, I190, I191, I192, I193,
176 I194, I195, I196, I197, I198, I199, I200, I201, I202, I203, I204, I205, I206, I207,
177 I208, I209, I210, I211, I212, I213, I214, I215, I216, I217, I218, I219, I220, I221,
178 I222, I223, I224, I225, I226, I227, I228, I229, I230, I231, I232, I233, I234, I235,
179 I236, I237, I238, I239, I240, I241, I242, I243, I244, I245, I246, I247, I248, I249,
180 I250, I251, I252, I253, I254, I255, I256
413 I0,
414 I1,
415 I2,
416 I3,
417 I4,
418 I5,
419 I6,
420 I7,
421 I8,
422 I9,
423 I10,
424 I11,
425 I12,
426 I13,
427 I14,
428 I15,
429 I16,
430 I17,
431 I18,
432 I19,
433 I20,
434 I21,
435 I22,
436 I23,
437 I24,
438 I25,
439 I26,
440 I27,
441 I28,
442 I29,
443 I30,
444 I31,
445 I32,
446 I33,
447 I34,
448 I35,
449 I36,
450 I37,
451 I38,
452 I39,
453 I40,
454 I41,
455 I42,
456 I43,
457 I44,
458 I45,
459 I46,
460 I47,
461 I48,
462 I49,
463 I50,
464 I51,
465 I52,
466 I53,
467 I54,
468 I55,
469 I56,
470 I57,
471 I58,
472 I59,
473 I60,
474 I61,
475 I62,
476 I63,
477 I64,
478 I65,
479 I66,
480 I67,
481 I68,
482 I69,
483 I70,
484 I71,
485 I72,
486 I73,
487 I74,
488 I75,
489 I76,
490 I77,
491 I78,
492 I79,
493 I80,
494 I81,
495 I82,
496 I83,
497 I84,
498 I85,
499 I86,
500 I87,
501 I88,
502 I89,
503 I90,
504 I91,
505 I92,
506 I93,
507 I94,
508 I95,
509 I96,
510 I97,
511 I98,
512 I99,
513 I100,
514 I101,
515 I102,
516 I103,
517 I104,
518 I105,
519 I106,
520 I107,
521 I108,
522 I109,
523 I110,
524 I111,
525 I112,
526 I113,
527 I114,
528 I115,
529 I116,
530 I117,
531 I118,
532 I119,
533 I120,
534 I121,
535 I122,
536 I123,
537 I124,
538 I125,
539 I126,
540 I127,
541 I128,
542 I129,
543 I130,
544 I131,
545 I132,
546 I133,
547 I134,
548 I135,
549 I136,
550 I137,
551 I138,
552 I139,
553 I140,
554 I141,
555 I142,
556 I143,
557 I144,
558 I145,
559 I146,
560 I147,
561 I148,
562 I149,
563 I150,
564 I151,
565 I152,
566 I153,
567 I154,
568 I155,
569 I156,
570 I157,
571 I158,
572 I159,
573 I160,
574 I161,
575 I162,
576 I163,
577 I164,
578 I165,
579 I166,
580 I167,
581 I168,
582 I169,
583 I170,
584 I171,
585 I172,
586 I173,
587 I174,
588 I175,
589 I176,
590 I177,
591 I178,
592 I179,
593 I180,
594 I181,
595 I182,
596 I183,
597 I184,
598 I185,
599 I186,
600 I187,
601 I188,
602 I189,
603 I190,
604 I191,
605 I192,
606 I193,
607 I194,
608 I195,
609 I196,
610 I197,
611 I198,
612 I199,
613 I200,
614 I201,
615 I202,
616 I203,
617 I204,
618 I205,
619 I206,
620 I207,
621 I208,
622 I209,
623 I210,
624 I211,
625 I212,
626 I213,
627 I214,
628 I215,
629 I216,
630 I217,
631 I218,
632 I219,
633 I220,
634 I221,
635 I222,
636 I223,
637 I224,
638 I225,
639 I226,
640 I227,
641 I228,
642 I229,
643 I230,
644 I231,
645 I232,
646 I233,
647 I234,
648 I235,
649 I236,
650 I237,
651 I238,
652 I239,
653 I240,
654 I241,
655 I242,
656 I243,
657 I244,
658 I245,
659 I246,
660 I247,
661 I248,
662 I249,
663 I250,
664 I251,
665 I252,
666 I253,
667 I254,
668 I255,
669 I256,
181670};
182671
183672test "enum sizes" {
......@@ -189,11 +678,11 @@ test "enum sizes" {
189678 }
190679}
191680
192const Small2 = enum (u2) {
681const Small2 = enum(u2) {
193682 One,
194683 Two,
195684};
196const Small = enum (u2) {
685const Small = enum(u2) {
197686 One,
198687 Two,
199688 Three,
......@@ -213,8 +702,7 @@ test "set enum tag type" {
213702 }
214703}
215704
216
217const A = enum (u3) {
705const A = enum(u3) {
218706 One,
219707 Two,
220708 Three,
......@@ -225,7 +713,7 @@ const A = enum (u3) {
225713 Four2,
226714};
227715
228const B = enum (u3) {
716const B = enum(u3) {
229717 One3,
230718 Two3,
231719 Three3,
......@@ -236,7 +724,7 @@ const B = enum (u3) {
236724 Four23,
237725};
238726
239const C = enum (u2) {
727const C = enum(u2) {
240728 One4,
241729 Two4,
242730 Three4,
......@@ -389,6 +877,8 @@ test "enum with tag values don't require parens" {
389877}
390878
391879test "enum with 1 field but explicit tag type should still have the tag type" {
392 const Enum = enum(u8) { B = 2 };
880 const Enum = enum(u8) {
881 B = 2,
882 };
393883 comptime @import("std").debug.assert(@sizeOf(Enum) == @sizeOf(u8));
394884}
test/cases/enum_with_members.zig+7-3
......@@ -7,7 +7,7 @@ const ET = union(enum) {
77 UINT: u32,
88
99 pub fn print(a: &const ET, buf: []u8) error!usize {
10 return switch (*a) {
10 return switch (a.*) {
1111 ET.SINT => |x| fmt.formatIntBuf(buf, x, 10, false, 0),
1212 ET.UINT => |x| fmt.formatIntBuf(buf, x, 10, false, 0),
1313 };
......@@ -15,8 +15,12 @@ const ET = union(enum) {
1515};
1616
1717test "enum with members" {
18 const a = ET { .SINT = -42 };
19 const b = ET { .UINT = 42 };
18 const a = ET {
19 .SINT = -42,
20 };
21 const b = ET {
22 .UINT = 42,
23 };
2024 var buf: [20]u8 = undefined;
2125
2226 assert((a.print(buf[0..]) catch unreachable) == 3);
test/cases/error.zig+30-26
......@@ -30,14 +30,12 @@ test "@errorName" {
3030 assert(mem.eql(u8, @errorName(error.ALongerErrorName), "ALongerErrorName"));
3131}
3232
33
3433test "error values" {
3534 const a = i32(error.err1);
3635 const b = i32(error.err2);
3736 assert(a != b);
3837}
3938
40
4139test "redefinition of error values allowed" {
4240 shouldBeNotEqual(error.AnError, error.SecondError);
4341}
......@@ -45,7 +43,6 @@ fn shouldBeNotEqual(a: error, b: error) void {
4543 if (a == b) unreachable;
4644}
4745
48
4946test "error binary operator" {
5047 const a = errBinaryOperatorG(true) catch 3;
5148 const b = errBinaryOperatorG(false) catch 3;
......@@ -56,20 +53,20 @@ fn errBinaryOperatorG(x: bool) error!isize {
5653 return if (x) error.ItBroke else isize(10);
5754}
5855
59
6056test "unwrap simple value from error" {
6157 const i = unwrapSimpleValueFromErrorDo() catch unreachable;
6258 assert(i == 13);
6359}
64fn unwrapSimpleValueFromErrorDo() error!isize { return 13; }
65
60fn unwrapSimpleValueFromErrorDo() error!isize {
61 return 13;
62}
6663
6764test "error return in assignment" {
6865 doErrReturnInAssignment() catch unreachable;
6966}
7067
7168fn doErrReturnInAssignment() error!void {
72 var x : i32 = undefined;
69 var x: i32 = undefined;
7370 x = try makeANonErr();
7471}
7572
......@@ -95,7 +92,10 @@ test "error set type " {
9592 comptime testErrorSetType();
9693}
9794
98const MyErrSet = error {OutOfMemory, FileNotFound};
95const MyErrSet = error {
96 OutOfMemory,
97 FileNotFound,
98};
9999
100100fn testErrorSetType() void {
101101 assert(@memberCount(MyErrSet) == 2);
......@@ -109,14 +109,19 @@ fn testErrorSetType() void {
109109 }
110110}
111111
112
113112test "explicit error set cast" {
114113 testExplicitErrorSetCast(Set1.A);
115114 comptime testExplicitErrorSetCast(Set1.A);
116115}
117116
118const Set1 = error{A, B};
119const Set2 = error{A, C};
117const Set1 = error {
118 A,
119 B,
120};
121const Set2 = error {
122 A,
123 C,
124};
120125
121126fn testExplicitErrorSetCast(set1: Set1) void {
122127 var x = Set2(set1);
......@@ -129,7 +134,8 @@ test "comptime test error for empty error set" {
129134 comptime testComptimeTestErrorEmptySet(1234);
130135}
131136
132const EmptyErrorSet = error {};
137const EmptyErrorSet = error {
138};
133139
134140fn testComptimeTestErrorEmptySet(x: EmptyErrorSet!i32) void {
135141 if (x) |v| assert(v == 1234) else |err| @compileError("bad");
......@@ -145,7 +151,9 @@ test "comptime err to int of error set with only 1 possible value" {
145151 testErrToIntWithOnePossibleValue(error.A, u32(error.A));
146152 comptime testErrToIntWithOnePossibleValue(error.A, u32(error.A));
147153}
148fn testErrToIntWithOnePossibleValue(x: error{A}, comptime value: u32) void {
154fn testErrToIntWithOnePossibleValue(x: error {
155 A,
156}, comptime value: u32) void {
149157 if (u32(x) != value) {
150158 @compileError("bad");
151159 }
......@@ -176,7 +184,6 @@ fn quux_1() !i32 {
176184 return error.C;
177185}
178186
179
180187test "error: fn returning empty error set can be passed as fn returning any error" {
181188 entry();
182189 comptime entry();
......@@ -186,24 +193,24 @@ fn entry() void {
186193 foo2(bar2);
187194}
188195
189fn foo2(f: fn()error!void) void {
196fn foo2(f: fn() error!void) void {
190197 const x = f();
191198}
192199
193fn bar2() (error{}!void) { }
194
200fn bar2() (error {
201}!void) {}
195202
196203test "error: Zero sized error set returned with value payload crash" {
197204 _ = foo3(0);
198205 _ = comptime foo3(0);
199206}
200207
201const Error = error{};
208const Error = error {
209};
202210fn foo3(b: usize) Error!usize {
203211 return b;
204212}
205213
206
207214test "error: Infer error set from literals" {
208215 _ = nullLiteral("n") catch |err| handleErrors(err);
209216 _ = floatLiteral("n") catch |err| handleErrors(err);
......@@ -215,29 +222,26 @@ test "error: Infer error set from literals" {
215222
216223fn handleErrors(err: var) noreturn {
217224 switch (err) {
218 error.T => {}
225 error.T => {},
219226 }
220227
221228 unreachable;
222229}
223230
224231fn nullLiteral(str: []const u8) !?i64 {
225 if (str[0] == 'n')
226 return null;
232 if (str[0] == 'n') return null;
227233
228234 return error.T;
229235}
230236
231237fn floatLiteral(str: []const u8) !?f64 {
232 if (str[0] == 'n')
233 return 1.0;
238 if (str[0] == 'n') return 1.0;
234239
235240 return error.T;
236241}
237242
238243fn intLiteral(str: []const u8) !?i64 {
239 if (str[0] == 'n')
240 return 1;
244 if (str[0] == 'n') return 1;
241245
242246 return error.T;
243247}
test/cases/eval.zig+88-55
......@@ -11,8 +11,6 @@ fn fibonacci(x: i32) i32 {
1111 return fibonacci(x - 1) + fibonacci(x - 2);
1212}
1313
14
15
1614fn unwrapAndAddOne(blah: ?i32) i32 {
1715 return ??blah + 1;
1816}
......@@ -40,13 +38,13 @@ test "inline variable gets result of const if" {
4038 assert(gimme1or2(false) == 2);
4139}
4240
43
4441test "static function evaluation" {
4542 assert(statically_added_number == 3);
4643}
4744const statically_added_number = staticAdd(1, 2);
48fn staticAdd(a: i32, b: i32) i32 { return a + b; }
49
45fn staticAdd(a: i32, b: i32) i32 {
46 return a + b;
47}
5048
5149test "const expr eval on single expr blocks" {
5250 assert(constExprEvalOnSingleExprBlocksFn(1, true) == 3);
......@@ -64,9 +62,6 @@ fn constExprEvalOnSingleExprBlocksFn(x: i32, b: bool) i32 {
6462 return result;
6563}
6664
67
68
69
7065test "statically initialized list" {
7166 assert(static_point_list[0].x == 1);
7267 assert(static_point_list[0].y == 2);
......@@ -77,7 +72,10 @@ const Point = struct {
7772 x: i32,
7873 y: i32,
7974};
80const static_point_list = []Point { makePoint(1, 2), makePoint(3, 4) };
75const static_point_list = []Point {
76 makePoint(1, 2),
77 makePoint(3, 4),
78};
8179fn makePoint(x: i32, y: i32) Point {
8280 return Point {
8381 .x = x,
......@@ -85,7 +83,6 @@ fn makePoint(x: i32, y: i32) Point {
8583 };
8684}
8785
88
8986test "static eval list init" {
9087 assert(static_vec3.data[2] == 1.0);
9188 assert(vec3(0.0, 0.0, 3.0).data[2] == 3.0);
......@@ -96,17 +93,19 @@ pub const Vec3 = struct {
9693};
9794pub fn vec3(x: f32, y: f32, z: f32) Vec3 {
9895 return Vec3 {
99 .data = []f32 { x, y, z, },
96 .data = []f32 {
97 x,
98 y,
99 z,
100 },
100101 };
101102}
102103
103
104104test "constant expressions" {
105 var array : [array_size]u8 = undefined;
105 var array: [array_size]u8 = undefined;
106106 assert(@sizeOf(@typeOf(array)) == 20);
107107}
108const array_size : u8 = 20;
109
108const array_size: u8 = 20;
110109
111110test "constant struct with negation" {
112111 assert(vertices[0].x == -0.6);
......@@ -119,12 +118,29 @@ const Vertex = struct {
119118 b: f32,
120119};
121120const vertices = []Vertex {
122 Vertex { .x = -0.6, .y = -0.4, .r = 1.0, .g = 0.0, .b = 0.0 },
123 Vertex { .x = 0.6, .y = -0.4, .r = 0.0, .g = 1.0, .b = 0.0 },
124 Vertex { .x = 0.0, .y = 0.6, .r = 0.0, .g = 0.0, .b = 1.0 },
121 Vertex {
122 .x = -0.6,
123 .y = -0.4,
124 .r = 1.0,
125 .g = 0.0,
126 .b = 0.0,
127 },
128 Vertex {
129 .x = 0.6,
130 .y = -0.4,
131 .r = 0.0,
132 .g = 1.0,
133 .b = 0.0,
134 },
135 Vertex {
136 .x = 0.0,
137 .y = 0.6,
138 .r = 0.0,
139 .g = 0.0,
140 .b = 1.0,
141 },
125142};
126143
127
128144test "statically initialized struct" {
129145 st_init_str_foo.x += 1;
130146 assert(st_init_str_foo.x == 14);
......@@ -133,15 +149,21 @@ const StInitStrFoo = struct {
133149 x: i32,
134150 y: bool,
135151};
136var st_init_str_foo = StInitStrFoo { .x = 13, .y = true, };
137
152var st_init_str_foo = StInitStrFoo {
153 .x = 13,
154 .y = true,
155};
138156
139157test "statically initalized array literal" {
140 const y : [4]u8 = st_init_arr_lit_x;
158 const y: [4]u8 = st_init_arr_lit_x;
141159 assert(y[3] == 4);
142160}
143const st_init_arr_lit_x = []u8{1,2,3,4};
144
161const st_init_arr_lit_x = []u8 {
162 1,
163 2,
164 3,
165 4,
166};
145167
146168test "const slice" {
147169 comptime {
......@@ -198,14 +220,29 @@ const CmdFn = struct {
198220 func: fn(i32) i32,
199221};
200222
201const cmd_fns = []CmdFn{
202 CmdFn {.name = "one", .func = one},
203 CmdFn {.name = "two", .func = two},
204 CmdFn {.name = "three", .func = three},
223const cmd_fns = []CmdFn {
224 CmdFn {
225 .name = "one",
226 .func = one,
227 },
228 CmdFn {
229 .name = "two",
230 .func = two,
231 },
232 CmdFn {
233 .name = "three",
234 .func = three,
235 },
205236};
206fn one(value: i32) i32 { return value + 1; }
207fn two(value: i32) i32 { return value + 2; }
208fn three(value: i32) i32 { return value + 3; }
237fn one(value: i32) i32 {
238 return value + 1;
239}
240fn two(value: i32) i32 {
241 return value + 2;
242}
243fn three(value: i32) i32 {
244 return value + 3;
245}
209246
210247fn performFn(comptime prefix_char: u8, start_value: i32) i32 {
211248 var result: i32 = start_value;
......@@ -229,7 +266,7 @@ test "eval @setRuntimeSafety at compile-time" {
229266 assert(result == 1234);
230267}
231268
232fn fnWithSetRuntimeSafety() i32{
269fn fnWithSetRuntimeSafety() i32 {
233270 @setRuntimeSafety(true);
234271 return 1234;
235272}
......@@ -244,7 +281,6 @@ fn fnWithFloatMode() f32 {
244281 return 1234.0;
245282}
246283
247
248284const SimpleStruct = struct {
249285 field: i32,
250286
......@@ -253,7 +289,9 @@ const SimpleStruct = struct {
253289 }
254290};
255291
256var simple_struct = SimpleStruct{ .field = 1234, };
292var simple_struct = SimpleStruct {
293 .field = 1234,
294};
257295
258296const bound_fn = simple_struct.method;
259297
......@@ -261,8 +299,6 @@ test "call method on bound fn referring to var instance" {
261299 assert(bound_fn() == 1237);
262300}
263301
264
265
266302test "ptr to local array argument at comptime" {
267303 comptime {
268304 var bytes: [10]u8 = undefined;
......@@ -277,7 +313,6 @@ fn modifySomeBytes(bytes: []u8) void {
277313 bytes[9] = 'b';
278314}
279315
280
281316test "comparisons 0 <= uint and 0 > uint should be comptime" {
282317 testCompTimeUIntComparisons(1234);
283318}
......@@ -296,8 +331,6 @@ fn testCompTimeUIntComparisons(x: u32) void {
296331 }
297332}
298333
299
300
301334test "const ptr to variable data changes at runtime" {
302335 assert(foo_ref.name[0] == 'a');
303336 foo_ref.name = "b";
......@@ -308,11 +341,11 @@ const Foo = struct {
308341 name: []const u8,
309342};
310343
311var foo_contents = Foo { .name = "a", };
344var foo_contents = Foo {
345 .name = "a",
346};
312347const foo_ref = &foo_contents;
313348
314
315
316349test "create global array with for loop" {
317350 assert(global_array[5] == 5 * 5);
318351 assert(global_array[9] == 9 * 9);
......@@ -321,7 +354,7 @@ test "create global array with for loop" {
321354const global_array = x: {
322355 var result: [10]usize = undefined;
323356 for (result) |*item, index| {
324 *item = index * index;
357 item.* = index * index;
325358 }
326359 break :x result;
327360};
......@@ -379,7 +412,7 @@ test "f128 at compile time is lossy" {
379412
380413pub fn TypeWithCompTimeSlice(comptime field_name: []const u8) type {
381414 return struct {
382 pub const Node = struct { };
415 pub const Node = struct {};
383416 };
384417}
385418
......@@ -401,10 +434,10 @@ fn copyWithPartialInline(s: []u32, b: []u8) void {
401434 comptime var i: usize = 0;
402435 inline while (i < 4) : (i += 1) {
403436 s[i] = 0;
404 s[i] |= u32(b[i*4+0]) << 24;
405 s[i] |= u32(b[i*4+1]) << 16;
406 s[i] |= u32(b[i*4+2]) << 8;
407 s[i] |= u32(b[i*4+3]) << 0;
437 s[i] |= u32(b[i * 4 + 0]) << 24;
438 s[i] |= u32(b[i * 4 + 1]) << 16;
439 s[i] |= u32(b[i * 4 + 2]) << 8;
440 s[i] |= u32(b[i * 4 + 3]) << 0;
408441 }
409442}
410443
......@@ -413,7 +446,7 @@ test "binary math operator in partially inlined function" {
413446 var b: [16]u8 = undefined;
414447
415448 for (b) |*r, i|
416 *r = u8(i + 1);
449 r.* = u8(i + 1);
417450
418451 copyWithPartialInline(s[0..], b[0..]);
419452 assert(s[0] == 0x1020304);
......@@ -422,7 +455,6 @@ test "binary math operator in partially inlined function" {
422455 assert(s[3] == 0xd0e0f10);
423456}
424457
425
426458test "comptime function with the same args is memoized" {
427459 comptime {
428460 assert(MakeType(i32) == MakeType(i32));
......@@ -447,12 +479,12 @@ test "comptime function with mutable pointer is not memoized" {
447479}
448480
449481fn increment(value: &i32) void {
450 *value += 1;
482 value.* += 1;
451483}
452484
453485fn generateTable(comptime T: type) [1010]T {
454 var res : [1010]T = undefined;
455 var i : usize = 0;
486 var res: [1010]T = undefined;
487 var i: usize = 0;
456488 while (i < 1010) : (i += 1) {
457489 res[i] = T(i);
458490 }
......@@ -496,9 +528,10 @@ const SingleFieldStruct = struct {
496528 }
497529};
498530test "const ptr to comptime mutable data is not memoized" {
499
500531 comptime {
501 var foo = SingleFieldStruct {.x = 1};
532 var foo = SingleFieldStruct {
533 .x = 1,
534 };
502535 assert(foo.read_x() == 1);
503536 foo.x = 2;
504537 assert(foo.read_x() == 2);
test/cases/fn.zig+26-18
......@@ -7,7 +7,6 @@ fn testParamsAdd(a: i32, b: i32) i32 {
77 return a + b;
88}
99
10
1110test "local variables" {
1211 testLocVars(2);
1312}
......@@ -16,7 +15,6 @@ fn testLocVars(b: i32) void {
1615 if (a + b != 3) unreachable;
1716}
1817
19
2018test "void parameters" {
2119 voidFun(1, void{}, 2, {});
2220}
......@@ -27,9 +25,8 @@ fn voidFun(a: i32, b: void, c: i32, d: void) void {
2725 return vv;
2826}
2927
30
3128test "mutable local variables" {
32 var zero : i32 = 0;
29 var zero: i32 = 0;
3330 assert(zero == 0);
3431
3532 var i = i32(0);
......@@ -41,7 +38,7 @@ test "mutable local variables" {
4138
4239test "separate block scopes" {
4340 {
44 const no_conflict : i32 = 5;
41 const no_conflict: i32 = 5;
4542 assert(no_conflict == 5);
4643 }
4744
......@@ -56,8 +53,7 @@ test "call function with empty string" {
5653 acceptsString("");
5754}
5855
59fn acceptsString(foo: []u8) void { }
60
56fn acceptsString(foo: []u8) void {}
6157
6258fn @"weird function name"() i32 {
6359 return 1234;
......@@ -70,31 +66,43 @@ test "implicit cast function unreachable return" {
7066 wantsFnWithVoid(fnWithUnreachable);
7167}
7268
73fn wantsFnWithVoid(f: fn() void) void { }
69fn wantsFnWithVoid(f: fn() void) void {}
7470
7571fn fnWithUnreachable() noreturn {
7672 unreachable;
7773}
7874
79
8075test "function pointers" {
81 const fns = []@typeOf(fn1) { fn1, fn2, fn3, fn4, };
76 const fns = []@typeOf(fn1) {
77 fn1,
78 fn2,
79 fn3,
80 fn4,
81 };
8282 for (fns) |f, i| {
8383 assert(f() == u32(i) + 5);
8484 }
8585}
86fn fn1() u32 {return 5;}
87fn fn2() u32 {return 6;}
88fn fn3() u32 {return 7;}
89fn fn4() u32 {return 8;}
90
86fn fn1() u32 {
87 return 5;
88}
89fn fn2() u32 {
90 return 6;
91}
92fn fn3() u32 {
93 return 7;
94}
95fn fn4() u32 {
96 return 8;
97}
9198
9299test "inline function call" {
93100 assert(@inlineCall(add, 3, 9) == 12);
94101}
95102
96fn add(a: i32, b: i32) i32 { return a + b; }
97
103fn add(a: i32, b: i32) i32 {
104 return a + b;
105}
98106
99107test "number literal as an argument" {
100108 numberLiteralArg(3);
......@@ -110,4 +118,4 @@ test "assign inline fn to const variable" {
110118 a();
111119}
112120
113inline fn inlineFn() void { }
121inline fn inlineFn() void {}
test/cases/for.zig+37-7
......@@ -3,8 +3,14 @@ const assert = std.debug.assert;
33const mem = std.mem;
44
55test "continue in for loop" {
6 const array = []i32 {1, 2, 3, 4, 5};
7 var sum : i32 = 0;
6 const array = []i32 {
7 1,
8 2,
9 3,
10 4,
11 5,
12 };
13 var sum: i32 = 0;
814 for (array) |x| {
915 sum += x;
1016 if (x < 3) {
......@@ -24,17 +30,39 @@ test "for loop with pointer elem var" {
2430}
2531fn mangleString(s: []u8) void {
2632 for (s) |*c| {
27 *c += 1;
33 c.* += 1;
2834 }
2935}
3036
3137test "basic for loop" {
32 const expected_result = []u8{9, 8, 7, 6, 0, 1, 2, 3, 9, 8, 7, 6, 0, 1, 2, 3 };
38 const expected_result = []u8 {
39 9,
40 8,
41 7,
42 6,
43 0,
44 1,
45 2,
46 3,
47 9,
48 8,
49 7,
50 6,
51 0,
52 1,
53 2,
54 3,
55 };
3356
3457 var buffer: [expected_result.len]u8 = undefined;
3558 var buf_index: usize = 0;
3659
37 const array = []u8 {9, 8, 7, 6};
60 const array = []u8 {
61 9,
62 8,
63 7,
64 6,
65 };
3866 for (array) |item| {
3967 buffer[buf_index] = item;
4068 buf_index += 1;
......@@ -65,7 +93,8 @@ fn testBreakOuter() void {
6593 var array = "aoeu";
6694 var count: usize = 0;
6795 outer: for (array) |_| {
68 for (array) |_2| { // TODO shouldn't get error for redeclaring "_"
96 // TODO shouldn't get error for redeclaring "_"
97 for (array) |_2| {
6998 count += 1;
7099 break :outer;
71100 }
......@@ -82,7 +111,8 @@ fn testContinueOuter() void {
82111 var array = "aoeu";
83112 var counter: usize = 0;
84113 outer: for (array) |_| {
85 for (array) |_2| { // TODO shouldn't get error for redeclaring "_"
114 // TODO shouldn't get error for redeclaring "_"
115 for (array) |_2| {
86116 counter += 1;
87117 continue :outer;
88118 }
test/cases/generics.zig+28-14
......@@ -37,7 +37,6 @@ test "fn with comptime args" {
3737 assert(sameButWithFloats(0.43, 0.49) == 0.49);
3838}
3939
40
4140test "var params" {
4241 assert(max_i32(12, 34) == 34);
4342 assert(max_f64(1.2, 3.4) == 3.4);
......@@ -60,7 +59,6 @@ fn max_f64(a: f64, b: f64) f64 {
6059 return max_var(a, b);
6160}
6261
63
6462pub fn List(comptime T: type) type {
6563 return SmallList(T, 8);
6664}
......@@ -82,10 +80,15 @@ test "function with return type type" {
8280 assert(list2.prealloc_items.len == 8);
8381}
8482
85
8683test "generic struct" {
87 var a1 = GenNode(i32) {.value = 13, .next = null,};
88 var b1 = GenNode(bool) {.value = true, .next = null,};
84 var a1 = GenNode(i32) {
85 .value = 13,
86 .next = null,
87 };
88 var b1 = GenNode(bool) {
89 .value = true,
90 .next = null,
91 };
8992 assert(a1.value == 13);
9093 assert(a1.value == a1.getVal());
9194 assert(b1.getVal());
......@@ -94,7 +97,9 @@ fn GenNode(comptime T: type) type {
9497 return struct {
9598 value: T,
9699 next: ?&GenNode(T),
97 fn getVal(n: &const GenNode(T)) T { return n.value; }
100 fn getVal(n: &const GenNode(T)) T {
101 return n.value;
102 }
98103 };
99104}
100105
......@@ -107,7 +112,6 @@ fn GenericDataThing(comptime count: isize) type {
107112 };
108113}
109114
110
111115test "use generic param in generic param" {
112116 assert(aGenericFn(i32, 3, 4) == 7);
113117}
......@@ -115,21 +119,31 @@ fn aGenericFn(comptime T: type, comptime a: T, b: T) T {
115119 return a + b;
116120}
117121
118
119122test "generic fn with implicit cast" {
120123 assert(getFirstByte(u8, []u8 {13}) == 13);
121 assert(getFirstByte(u16, []u16 {0, 13}) == 0);
124 assert(getFirstByte(u16, []u16 {
125 0,
126 13,
127 }) == 0);
128}
129fn getByte(ptr: ?&const u8) u8 {
130 return ??ptr.*;
122131}
123fn getByte(ptr: ?&const u8) u8 {return *??ptr;}
124132fn getFirstByte(comptime T: type, mem: []const T) u8 {
125133 return getByte(@ptrCast(&const u8, &mem[0]));
126134}
127135
136const foos = []fn(var) bool {
137 foo1,
138 foo2,
139};
128140
129const foos = []fn(var) bool { foo1, foo2 };
130
131fn foo1(arg: var) bool { return arg; }
132fn foo2(arg: var) bool { return !arg; }
141fn foo1(arg: var) bool {
142 return arg;
143}
144fn foo2(arg: var) bool {
145 return !arg;
146}
133147
134148test "array of generic fns" {
135149 assert(foos[0](true));
test/cases/if.zig-1
......@@ -23,7 +23,6 @@ fn firstEqlThird(a: i32, b: i32, c: i32) void {
2323 }
2424}
2525
26
2726test "else if expression" {
2827 assert(elseIfExpressionF(1) == 1);
2928}
test/cases/import/a_namespace.zig+3-1
......@@ -1 +1,3 @@
1pub fn foo() i32 { return 1234; }
1pub fn foo() i32 {
2 return 1234;
3}
test/cases/ir_block_deps.zig+3-1
......@@ -11,7 +11,9 @@ fn foo(id: u64) !i32 {
1111 };
1212}
1313
14fn getErrInt() error!i32 { return 0; }
14fn getErrInt() error!i32 {
15 return 0;
16}
1517
1618test "ir block deps" {
1719 assert((foo(1) catch unreachable) == 0);
test/cases/math.zig+40-52
......@@ -28,25 +28,12 @@ fn testDivision() void {
2828 assert(divTrunc(f32, -5.0, 3.0) == -1.0);
2929
3030 comptime {
31 assert(
32 1194735857077236777412821811143690633098347576 %
33 508740759824825164163191790951174292733114988 ==
34 177254337427586449086438229241342047632117600);
35 assert(@rem(-1194735857077236777412821811143690633098347576,
36 508740759824825164163191790951174292733114988) ==
37 -177254337427586449086438229241342047632117600);
38 assert(1194735857077236777412821811143690633098347576 /
39 508740759824825164163191790951174292733114988 ==
40 2);
41 assert(@divTrunc(-1194735857077236777412821811143690633098347576,
42 508740759824825164163191790951174292733114988) ==
43 -2);
44 assert(@divTrunc(1194735857077236777412821811143690633098347576,
45 -508740759824825164163191790951174292733114988) ==
46 -2);
47 assert(@divTrunc(-1194735857077236777412821811143690633098347576,
48 -508740759824825164163191790951174292733114988) ==
49 2);
31 assert(1194735857077236777412821811143690633098347576 % 508740759824825164163191790951174292733114988 == 177254337427586449086438229241342047632117600);
32 assert(@rem(-1194735857077236777412821811143690633098347576, 508740759824825164163191790951174292733114988) == -177254337427586449086438229241342047632117600);
33 assert(1194735857077236777412821811143690633098347576 / 508740759824825164163191790951174292733114988 == 2);
34 assert(@divTrunc(-1194735857077236777412821811143690633098347576, 508740759824825164163191790951174292733114988) == -2);
35 assert(@divTrunc(1194735857077236777412821811143690633098347576, -508740759824825164163191790951174292733114988) == -2);
36 assert(@divTrunc(-1194735857077236777412821811143690633098347576, -508740759824825164163191790951174292733114988) == 2);
5037 assert(4126227191251978491697987544882340798050766755606969681711 % 10 == 1);
5138 }
5239}
......@@ -114,18 +101,28 @@ fn ctz(x: var) usize {
114101
115102test "assignment operators" {
116103 var i: u32 = 0;
117 i += 5; assert(i == 5);
118 i -= 2; assert(i == 3);
119 i *= 20; assert(i == 60);
120 i /= 3; assert(i == 20);
121 i %= 11; assert(i == 9);
122 i <<= 1; assert(i == 18);
123 i >>= 2; assert(i == 4);
104 i += 5;
105 assert(i == 5);
106 i -= 2;
107 assert(i == 3);
108 i *= 20;
109 assert(i == 60);
110 i /= 3;
111 assert(i == 20);
112 i %= 11;
113 assert(i == 9);
114 i <<= 1;
115 assert(i == 18);
116 i >>= 2;
117 assert(i == 4);
124118 i = 6;
125 i &= 5; assert(i == 4);
126 i ^= 6; assert(i == 2);
119 i &= 5;
120 assert(i == 4);
121 i ^= 6;
122 assert(i == 2);
127123 i = 6;
128 i |= 3; assert(i == 7);
124 i |= 3;
125 assert(i == 7);
129126}
130127
131128test "three expr in a row" {
......@@ -138,7 +135,7 @@ fn testThreeExprInARow(f: bool, t: bool) void {
138135 assertFalse(1 | 2 | 4 != 7);
139136 assertFalse(3 ^ 6 ^ 8 != 13);
140137 assertFalse(7 & 14 & 28 != 4);
141 assertFalse(9 << 1 << 2 != 9 << 3);
138 assertFalse(9 << 1 << 2 != 9 << 3);
142139 assertFalse(90 >> 1 >> 2 != 90 >> 3);
143140 assertFalse(100 - 1 + 1000 != 1099);
144141 assertFalse(5 * 4 / 2 % 3 != 1);
......@@ -150,7 +147,6 @@ fn assertFalse(b: bool) void {
150147 assert(!b);
151148}
152149
153
154150test "const number literal" {
155151 const one = 1;
156152 const eleven = ten + one;
......@@ -159,8 +155,6 @@ test "const number literal" {
159155}
160156const ten = 10;
161157
162
163
164158test "unsigned wrapping" {
165159 testUnsignedWrappingEval(@maxValue(u32));
166160 comptime testUnsignedWrappingEval(@maxValue(u32));
......@@ -214,8 +208,12 @@ const DivResult = struct {
214208};
215209
216210test "binary not" {
217 assert(comptime x: {break :x ~u16(0b1010101010101010) == 0b0101010101010101;});
218 assert(comptime x: {break :x ~u64(2147483647) == 18446744071562067968;});
211 assert(comptime x: {
212 break :x ~u16(0b1010101010101010) == 0b0101010101010101;
213 });
214 assert(comptime x: {
215 break :x ~u64(2147483647) == 18446744071562067968;
216 });
219217 testBinaryNot(0b1010101010101010);
220218}
221219
......@@ -319,27 +317,15 @@ fn testShrExact(x: u8) void {
319317
320318test "big number addition" {
321319 comptime {
322 assert(
323 35361831660712422535336160538497375248 +
324 101752735581729509668353361206450473702 ==
325 137114567242441932203689521744947848950);
326 assert(
327 594491908217841670578297176641415611445982232488944558774612 +
328 390603545391089362063884922208143568023166603618446395589768 ==
329 985095453608931032642182098849559179469148836107390954364380);
320 assert(35361831660712422535336160538497375248 + 101752735581729509668353361206450473702 == 137114567242441932203689521744947848950);
321 assert(594491908217841670578297176641415611445982232488944558774612 + 390603545391089362063884922208143568023166603618446395589768 == 985095453608931032642182098849559179469148836107390954364380);
330322 }
331323}
332324
333325test "big number multiplication" {
334326 comptime {
335 assert(
336 45960427431263824329884196484953148229 *
337 128339149605334697009938835852565949723 ==
338 5898522172026096622534201617172456926982464453350084962781392314016180490567);
339 assert(
340 594491908217841670578297176641415611445982232488944558774612 *
341 390603545391089362063884922208143568023166603618446395589768 ==
342 232210647056203049913662402532976186578842425262306016094292237500303028346593132411865381225871291702600263463125370016);
327 assert(45960427431263824329884196484953148229 * 128339149605334697009938835852565949723 == 5898522172026096622534201617172456926982464453350084962781392314016180490567);
328 assert(594491908217841670578297176641415611445982232488944558774612 * 390603545391089362063884922208143568023166603618446395589768 == 232210647056203049913662402532976186578842425262306016094292237500303028346593132411865381225871291702600263463125370016);
343329 }
344330}
345331
......@@ -380,7 +366,9 @@ test "f128" {
380366 comptime test_f128();
381367}
382368
383fn make_f128(x: f128) f128 { return x; }
369fn make_f128(x: f128) f128 {
370 return x;
371}
384372
385373fn test_f128() void {
386374 assert(@sizeOf(f128) == 16);
test/cases/misc.zig+138-87
......@@ -4,6 +4,7 @@ const cstr = @import("std").cstr;
44const builtin = @import("builtin");
55
66// normal comment
7
78/// this is a documentation comment
89/// doc comment line 2
910fn emptyFunctionWithComments() void {}
......@@ -16,8 +17,7 @@ comptime {
1617 @export("disabledExternFn", disabledExternFn, builtin.GlobalLinkage.Internal);
1718}
1819
19extern fn disabledExternFn() void {
20}
20extern fn disabledExternFn() void {}
2121
2222test "call disabled extern fn" {
2323 disabledExternFn();
......@@ -110,17 +110,29 @@ fn testShortCircuit(f: bool, t: bool) void {
110110 var hit_3 = f;
111111 var hit_4 = f;
112112
113 if (t or x: {assert(f); break :x f;}) {
113 if (t or x: {
114 assert(f);
115 break :x f;
116 }) {
114117 hit_1 = t;
115118 }
116 if (f or x: { hit_2 = t; break :x f; }) {
119 if (f or x: {
120 hit_2 = t;
121 break :x f;
122 }) {
117123 assert(f);
118124 }
119125
120 if (t and x: { hit_3 = t; break :x f; }) {
126 if (t and x: {
127 hit_3 = t;
128 break :x f;
129 }) {
121130 assert(f);
122131 }
123 if (f and x: {assert(f); break :x f;}) {
132 if (f and x: {
133 assert(f);
134 break :x f;
135 }) {
124136 assert(f);
125137 } else {
126138 hit_4 = t;
......@@ -146,8 +158,8 @@ test "return string from function" {
146158 assert(mem.eql(u8, first4KeysOfHomeRow(), "aoeu"));
147159}
148160
149const g1 : i32 = 1233 + 1;
150var g2 : i32 = 0;
161const g1: i32 = 1233 + 1;
162var g2: i32 = 0;
151163
152164test "global variables" {
153165 assert(g2 == 0);
......@@ -155,10 +167,9 @@ test "global variables" {
155167 assert(g2 == 1234);
156168}
157169
158
159170test "memcpy and memset intrinsics" {
160 var foo : [20]u8 = undefined;
161 var bar : [20]u8 = undefined;
171 var foo: [20]u8 = undefined;
172 var bar: [20]u8 = undefined;
162173
163174 @memset(&foo[0], 'A', foo.len);
164175 @memcpy(&bar[0], &foo[0], bar.len);
......@@ -167,12 +178,14 @@ test "memcpy and memset intrinsics" {
167178}
168179
169180test "builtin static eval" {
170 const x : i32 = comptime x: {break :x 1 + 2 + 3;};
181 const x: i32 = comptime x: {
182 break :x 1 + 2 + 3;
183 };
171184 assert(x == comptime 6);
172185}
173186
174187test "slicing" {
175 var array : [20]i32 = undefined;
188 var array: [20]i32 = undefined;
176189
177190 array[5] = 1234;
178191
......@@ -187,15 +200,15 @@ test "slicing" {
187200 if (slice_rest.len != 10) unreachable;
188201}
189202
190
191203test "constant equal function pointers" {
192204 const alias = emptyFn;
193 assert(comptime x: {break :x emptyFn == alias;});
205 assert(comptime x: {
206 break :x emptyFn == alias;
207 });
194208}
195209
196210fn emptyFn() void {}
197211
198
199212test "hex escape" {
200213 assert(mem.eql(u8, "\x68\x65\x6c\x6c\x6f", "hello"));
201214}
......@@ -219,7 +232,7 @@ test "string escapes" {
219232}
220233
221234test "multiline string" {
222 const s1 =
235 const s1 =
223236 \\one
224237 \\two)
225238 \\three
......@@ -229,7 +242,7 @@ test "multiline string" {
229242}
230243
231244test "multiline C string" {
232 const s1 =
245 const s1 =
233246 c\\one
234247 c\\two)
235248 c\\three
......@@ -238,18 +251,16 @@ test "multiline C string" {
238251 assert(cstr.cmp(s1, s2) == 0);
239252}
240253
241
242254test "type equality" {
243255 assert(&const u8 != &u8);
244256}
245257
246
247258const global_a: i32 = 1234;
248259const global_b: &const i32 = &global_a;
249260const global_c: &const f32 = @ptrCast(&const f32, global_b);
250261test "compile time global reinterpret" {
251262 const d = @ptrCast(&const i32, global_c);
252 assert(*d == 1234);
263 assert(d.* == 1234);
253264}
254265
255266test "explicit cast maybe pointers" {
......@@ -261,12 +272,11 @@ test "generic malloc free" {
261272 const a = memAlloc(u8, 10) catch unreachable;
262273 memFree(u8, a);
263274}
264var some_mem : [100]u8 = undefined;
275var some_mem: [100]u8 = undefined;
265276fn memAlloc(comptime T: type, n: usize) error![]T {
266277 return @ptrCast(&T, &some_mem[0])[0..n];
267278}
268fn memFree(comptime T: type, memory: []T) void { }
269
279fn memFree(comptime T: type, memory: []T) void {}
270280
271281test "cast undefined" {
272282 const array: [100]u8 = undefined;
......@@ -275,32 +285,35 @@ test "cast undefined" {
275285}
276286fn testCastUndefined(x: []const u8) void {}
277287
278
279288test "cast small unsigned to larger signed" {
280289 assert(castSmallUnsignedToLargerSigned1(200) == i16(200));
281290 assert(castSmallUnsignedToLargerSigned2(9999) == i64(9999));
282291}
283fn castSmallUnsignedToLargerSigned1(x: u8) i16 { return x; }
284fn castSmallUnsignedToLargerSigned2(x: u16) i64 { return x; }
285
292fn castSmallUnsignedToLargerSigned1(x: u8) i16 {
293 return x;
294}
295fn castSmallUnsignedToLargerSigned2(x: u16) i64 {
296 return x;
297}
286298
287299test "implicit cast after unreachable" {
288300 assert(outer() == 1234);
289301}
290fn inner() i32 { return 1234; }
302fn inner() i32 {
303 return 1234;
304}
291305fn outer() i64 {
292306 return inner();
293307}
294308
295
296309test "pointer dereferencing" {
297310 var x = i32(3);
298311 const y = &x;
299312
300 *y += 1;
313 y.* += 1;
301314
302315 assert(x == 4);
303 assert(*y == 4);
316 assert(y.* == 4);
304317}
305318
306319test "call result of if else expression" {
......@@ -310,9 +323,12 @@ test "call result of if else expression" {
310323fn f2(x: bool) []const u8 {
311324 return (if (x) fA else fB)();
312325}
313fn fA() []const u8 { return "a"; }
314fn fB() []const u8 { return "b"; }
315
326fn fA() []const u8 {
327 return "a";
328}
329fn fB() []const u8 {
330 return "b";
331}
316332
317333test "const expression eval handling of variables" {
318334 var x = true;
......@@ -321,8 +337,6 @@ test "const expression eval handling of variables" {
321337 }
322338}
323339
324
325
326340test "constant enum initialization with differing sizes" {
327341 test3_1(test3_foo);
328342 test3_2(test3_bar);
......@@ -336,10 +350,17 @@ const Test3Point = struct {
336350 x: i32,
337351 y: i32,
338352};
339const test3_foo = Test3Foo { .Three = Test3Point {.x = 3, .y = 4}};
340const test3_bar = Test3Foo { .Two = 13};
353const test3_foo = Test3Foo {
354 .Three = Test3Point {
355 .x = 3,
356 .y = 4,
357 },
358};
359const test3_bar = Test3Foo {
360 .Two = 13,
361};
341362fn test3_1(f: &const Test3Foo) void {
342 switch (*f) {
363 switch (f.*) {
343364 Test3Foo.Three => |pt| {
344365 assert(pt.x == 3);
345366 assert(pt.y == 4);
......@@ -348,7 +369,7 @@ fn test3_1(f: &const Test3Foo) void {
348369 }
349370}
350371fn test3_2(f: &const Test3Foo) void {
351 switch (*f) {
372 switch (f.*) {
352373 Test3Foo.Two => |x| {
353374 assert(x == 13);
354375 },
......@@ -356,23 +377,19 @@ fn test3_2(f: &const Test3Foo) void {
356377 }
357378}
358379
359
360380test "character literals" {
361381 assert('\'' == single_quote);
362382}
363383const single_quote = '\'';
364384
365
366
367385test "take address of parameter" {
368386 testTakeAddressOfParameter(12.34);
369387}
370388fn testTakeAddressOfParameter(f: f32) void {
371389 const f_ptr = &f;
372 assert(*f_ptr == 12.34);
390 assert(f_ptr.* == 12.34);
373391}
374392
375
376393test "pointer comparison" {
377394 const a = ([]const u8)("a");
378395 const b = &a;
......@@ -382,23 +399,30 @@ fn ptrEql(a: &const []const u8, b: &const []const u8) bool {
382399 return a == b;
383400}
384401
385
386402test "C string concatenation" {
387403 const a = c"OK" ++ c" IT " ++ c"WORKED";
388404 const b = c"OK IT WORKED";
389405
390406 const len = cstr.len(b);
391407 const len_with_null = len + 1;
392 {var i: u32 = 0; while (i < len_with_null) : (i += 1) {
393 assert(a[i] == b[i]);
394 }}
408 {
409 var i: u32 = 0;
410 while (i < len_with_null) : (i += 1) {
411 assert(a[i] == b[i]);
412 }
413 }
395414 assert(a[len] == 0);
396415 assert(b[len] == 0);
397416}
398417
399418test "cast slice to u8 slice" {
400419 assert(@sizeOf(i32) == 4);
401 var big_thing_array = []i32{1, 2, 3, 4};
420 var big_thing_array = []i32 {
421 1,
422 2,
423 3,
424 4,
425 };
402426 const big_thing_slice: []i32 = big_thing_array[0..];
403427 const bytes = ([]u8)(big_thing_slice);
404428 assert(bytes.len == 4 * 4);
......@@ -421,25 +445,22 @@ test "pointer to void return type" {
421445}
422446fn testPointerToVoidReturnType() error!void {
423447 const a = testPointerToVoidReturnType2();
424 return *a;
448 return a.*;
425449}
426450const test_pointer_to_void_return_type_x = void{};
427451fn testPointerToVoidReturnType2() &const void {
428452 return &test_pointer_to_void_return_type_x;
429453}
430454
431
432455test "non const ptr to aliased type" {
433456 const int = i32;
434457 assert(?&int == ?&i32);
435458}
436459
437
438
439460test "array 2D const double ptr" {
440461 const rect_2d_vertexes = [][1]f32 {
441 []f32{1.0},
442 []f32{2.0},
462 []f32 {1.0},
463 []f32 {2.0},
443464 };
444465 testArray2DConstDoublePtr(&rect_2d_vertexes[0][0]);
445466}
......@@ -450,10 +471,21 @@ fn testArray2DConstDoublePtr(ptr: &const f32) void {
450471}
451472
452473const Tid = builtin.TypeId;
453const AStruct = struct { x: i32, };
454const AnEnum = enum { One, Two, };
455const AUnionEnum = union(enum) { One: i32, Two: void, };
456const AUnion = union { One: void, Two: void };
474const AStruct = struct {
475 x: i32,
476};
477const AnEnum = enum {
478 One,
479 Two,
480};
481const AUnionEnum = union(enum) {
482 One: i32,
483 Two: void,
484};
485const AUnion = union {
486 One: void,
487 Two: void,
488};
457489
458490test "@typeId" {
459491 comptime {
......@@ -481,9 +513,11 @@ test "@typeId" {
481513 assert(@typeId(@typeOf(AUnionEnum.One)) == Tid.Enum);
482514 assert(@typeId(AUnionEnum) == Tid.Union);
483515 assert(@typeId(AUnion) == Tid.Union);
484 assert(@typeId(fn()void) == Tid.Fn);
516 assert(@typeId(fn() void) == Tid.Fn);
485517 assert(@typeId(@typeOf(builtin)) == Tid.Namespace);
486 assert(@typeId(@typeOf(x: {break :x this;})) == Tid.Block);
518 assert(@typeId(@typeOf(x: {
519 break :x this;
520 })) == Tid.Block);
487521 // TODO bound fn
488522 // TODO arg tuple
489523 // TODO opaque
......@@ -499,8 +533,7 @@ test "@canImplicitCast" {
499533}
500534
501535test "@typeName" {
502 const Struct = struct {
503 };
536 const Struct = struct {};
504537 const Union = union {
505538 unused: u8,
506539 };
......@@ -525,14 +558,19 @@ fn TypeFromFn(comptime T: type) type {
525558test "volatile load and store" {
526559 var number: i32 = 1234;
527560 const ptr = (&volatile i32)(&number);
528 *ptr += 1;
529 assert(*ptr == 1235);
561 ptr.* += 1;
562 assert(ptr.* == 1235);
530563}
531564
532565test "slice string literal has type []const u8" {
533566 comptime {
534567 assert(@typeOf("aoeu"[0..]) == []const u8);
535 const array = []i32{1, 2, 3, 4};
568 const array = []i32 {
569 1,
570 2,
571 3,
572 4,
573 };
536574 assert(@typeOf(array[0..]) == []const i32);
537575 }
538576}
......@@ -544,12 +582,15 @@ const GDTEntry = struct {
544582 field: i32,
545583};
546584var gdt = []GDTEntry {
547 GDTEntry {.field = 1},
548 GDTEntry {.field = 2},
585 GDTEntry {
586 .field = 1,
587 },
588 GDTEntry {
589 .field = 2,
590 },
549591};
550592var global_ptr = &gdt[0];
551593
552
553594// can't really run this test but we can make sure it has no compile error
554595// and generates code
555596const vram = @intToPtr(&volatile u8, 0x20000000)[0..0x8000];
......@@ -584,7 +625,7 @@ test "comptime if inside runtime while which unconditionally breaks" {
584625}
585626fn testComptimeIfInsideRuntimeWhileWhichUnconditionallyBreaks(cond: bool) void {
586627 while (cond) {
587 if (false) { }
628 if (false) {}
588629 break;
589630 }
590631}
......@@ -607,7 +648,9 @@ fn testStructInFn() void {
607648 kind: BlockKind,
608649 };
609650
610 var block = Block { .kind = 1234 };
651 var block = Block {
652 .kind = 1234,
653 };
611654
612655 block.kind += 1;
613656
......@@ -617,7 +660,9 @@ fn testStructInFn() void {
617660fn fnThatClosesOverLocalConst() type {
618661 const c = 1;
619662 return struct {
620 fn g() i32 { return c; }
663 fn g() i32 {
664 return c;
665 }
621666 };
622667}
623668
......@@ -635,22 +680,29 @@ fn thisIsAColdFn() void {
635680 @setCold(true);
636681}
637682
638
639const PackedStruct = packed struct { a: u8, b: u8, };
640const PackedUnion = packed union { a: u8, b: u32, };
641const PackedEnum = packed enum { A, B, };
683const PackedStruct = packed struct {
684 a: u8,
685 b: u8,
686};
687const PackedUnion = packed union {
688 a: u8,
689 b: u32,
690};
691const PackedEnum = packed enum {
692 A,
693 B,
694};
642695
643696test "packed struct, enum, union parameters in extern function" {
644 testPackedStuff(
645 PackedStruct{.a = 1, .b = 2},
646 PackedUnion{.a = 1},
647 PackedEnum.A,
648 );
649}
650
651export fn testPackedStuff(a: &const PackedStruct, b: &const PackedUnion, c: PackedEnum) void {
697 testPackedStuff(PackedStruct {
698 .a = 1,
699 .b = 2,
700 }, PackedUnion {
701 .a = 1,
702 }, PackedEnum.A);
652703}
653704
705export fn testPackedStuff(a: &const PackedStruct, b: &const PackedUnion, c: PackedEnum) void {}
654706
655707test "slicing zero length array" {
656708 const s1 = ""[0..];
......@@ -661,7 +713,6 @@ test "slicing zero length array" {
661713 assert(mem.eql(u32, s2, []u32{}));
662714}
663715
664
665716const addr1 = @ptrCast(&const u8, emptyFn);
666717test "comptime cast fn to ptr" {
667718 const addr2 = @ptrCast(&const u8, emptyFn);
test/cases/namespace_depends_on_compile_var/index.zig+1-1
......@@ -8,7 +8,7 @@ test "namespace depends on compile var" {
88 assert(!some_namespace.a_bool);
99 }
1010}
11const some_namespace = switch(builtin.os) {
11const some_namespace = switch (builtin.os) {
1212 builtin.Os.linux => @import("a.zig"),
1313 else => @import("b.zig"),
1414};
test/cases/null.zig+12-15
......@@ -1,7 +1,7 @@
11const assert = @import("std").debug.assert;
22
33test "nullable type" {
4 const x : ?bool = true;
4 const x: ?bool = true;
55
66 if (x) |y| {
77 if (y) {
......@@ -13,13 +13,13 @@ test "nullable type" {
1313 unreachable;
1414 }
1515
16 const next_x : ?i32 = null;
16 const next_x: ?i32 = null;
1717
1818 const z = next_x ?? 1234;
1919
2020 assert(z == 1234);
2121
22 const final_x : ?i32 = 13;
22 const final_x: ?i32 = 13;
2323
2424 const num = final_x ?? unreachable;
2525
......@@ -30,19 +30,17 @@ test "test maybe object and get a pointer to the inner value" {
3030 var maybe_bool: ?bool = true;
3131
3232 if (maybe_bool) |*b| {
33 *b = false;
33 b.* = false;
3434 }
3535
3636 assert(??maybe_bool == false);
3737}
3838
39
4039test "rhs maybe unwrap return" {
4140 const x: ?bool = true;
4241 const y = x ?? return;
4342}
4443
45
4644test "maybe return" {
4745 maybeReturnImpl();
4846 comptime maybeReturnImpl();
......@@ -50,8 +48,7 @@ test "maybe return" {
5048
5149fn maybeReturnImpl() void {
5250 assert(??foo(1235));
53 if (foo(null) != null)
54 unreachable;
51 if (foo(null) != null) unreachable;
5552 assert(!??foo(1234));
5653}
5754
......@@ -60,12 +57,16 @@ fn foo(x: ?i32) ?bool {
6057 return value > 1234;
6158}
6259
63
6460test "if var maybe pointer" {
65 assert(shouldBeAPlus1(Particle {.a = 14, .b = 1, .c = 1, .d = 1}) == 15);
61 assert(shouldBeAPlus1(Particle {
62 .a = 14,
63 .b = 1,
64 .c = 1,
65 .d = 1,
66 }) == 15);
6667}
6768fn shouldBeAPlus1(p: &const Particle) u64 {
68 var maybe_particle: ?Particle = *p;
69 var maybe_particle: ?Particle = p.*;
6970 if (maybe_particle) |*particle| {
7071 particle.a += 1;
7172 }
......@@ -81,7 +82,6 @@ const Particle = struct {
8182 d: u64,
8283};
8384
84
8585test "null literal outside function" {
8686 const is_null = here_is_a_null_literal.context == null;
8787 assert(is_null);
......@@ -96,7 +96,6 @@ const here_is_a_null_literal = SillyStruct {
9696 .context = null,
9797};
9898
99
10099test "test null runtime" {
101100 testTestNullRuntime(null);
102101}
......@@ -123,8 +122,6 @@ fn bar(x: ?void) ?void {
123122 }
124123}
125124
126
127
128125const StructWithNullable = struct {
129126 field: ?i32,
130127};
test/cases/ref_var_in_if_after_if_2nd_switch_prong.zig+1-1
......@@ -23,7 +23,7 @@ fn foo(c: bool, k: Num, c2: bool, b: []const u8) void {
2323 if (c) {
2424 const output_path = b;
2525
26 if (c2) { }
26 if (c2) {}
2727
2828 a(output_path);
2929 }
test/cases/reflection.zig+3-2
......@@ -23,7 +23,9 @@ test "reflection: function return type, var args, and param types" {
2323 }
2424}
2525
26fn dummy(a: bool, b: i32, c: f32) i32 { return 1234; }
26fn dummy(a: bool, b: i32, c: f32) i32 {
27 return 1234;
28}
2729fn dummy_varargs(args: ...) void {}
2830
2931test "reflection: struct member types and names" {
......@@ -54,7 +56,6 @@ test "reflection: enum member types and names" {
5456 assert(mem.eql(u8, @memberName(Bar, 2), "Three"));
5557 assert(mem.eql(u8, @memberName(Bar, 3), "Four"));
5658 }
57
5859}
5960
6061test "reflection: @field" {
test/cases/slice.zig+6-2
......@@ -18,7 +18,11 @@ test "slice child property" {
1818}
1919
2020test "runtime safety lets us slice from len..len" {
21 var an_array = []u8{1, 2, 3};
21 var an_array = []u8 {
22 1,
23 2,
24 3,
25 };
2226 assert(mem.eql(u8, sliceFromLenToLen(an_array[0..], 3, 3), ""));
2327}
2428
......@@ -27,7 +31,7 @@ fn sliceFromLenToLen(a_slice: []u8, start: usize, end: usize) []u8 {
2731}
2832
2933test "implicitly cast array of size 0 to slice" {
30 var msg = []u8 {};
34 var msg = []u8{};
3135 assertLenIsZero(msg);
3236}
3337
test/cases/struct.zig+48-35
......@@ -2,9 +2,11 @@ const assert = @import("std").debug.assert;
22const builtin = @import("builtin");
33
44const StructWithNoFields = struct {
5 fn add(a: i32, b: i32) i32 { return a + b; }
5 fn add(a: i32, b: i32) i32 {
6 return a + b;
7 }
68};
7const empty_global_instance = StructWithNoFields {};
9const empty_global_instance = StructWithNoFields{};
810
911test "call struct static method" {
1012 const result = StructWithNoFields.add(3, 4);
......@@ -34,12 +36,11 @@ test "void struct fields" {
3436 assert(@sizeOf(VoidStructFieldsFoo) == 4);
3537}
3638const VoidStructFieldsFoo = struct {
37 a : void,
38 b : i32,
39 c : void,
39 a: void,
40 b: i32,
41 c: void,
4042};
4143
42
4344test "structs" {
4445 var foo: StructFoo = undefined;
4546 @memset(@ptrCast(&u8, &foo), 0, @sizeOf(StructFoo));
......@@ -50,9 +51,9 @@ test "structs" {
5051 assert(foo.c == 100);
5152}
5253const StructFoo = struct {
53 a : i32,
54 b : bool,
55 c : f32,
54 a: i32,
55 b: bool,
56 c: f32,
5657};
5758fn testFoo(foo: &const StructFoo) void {
5859 assert(foo.b);
......@@ -61,7 +62,6 @@ fn testMutation(foo: &StructFoo) void {
6162 foo.c = 100;
6263}
6364
64
6565const Node = struct {
6666 val: Val,
6767 next: &Node,
......@@ -72,10 +72,10 @@ const Val = struct {
7272};
7373
7474test "struct point to self" {
75 var root : Node = undefined;
75 var root: Node = undefined;
7676 root.val.x = 1;
7777
78 var node : Node = undefined;
78 var node: Node = undefined;
7979 node.next = &root;
8080 node.val.x = 2;
8181
......@@ -85,8 +85,8 @@ test "struct point to self" {
8585}
8686
8787test "struct byval assign" {
88 var foo1 : StructFoo = undefined;
89 var foo2 : StructFoo = undefined;
88 var foo1: StructFoo = undefined;
89 var foo2: StructFoo = undefined;
9090
9191 foo1.a = 1234;
9292 foo2.a = 0;
......@@ -96,46 +96,57 @@ test "struct byval assign" {
9696}
9797
9898fn structInitializer() void {
99 const val = Val { .x = 42 };
99 const val = Val {
100 .x = 42,
101 };
100102 assert(val.x == 42);
101103}
102104
103
104105test "fn call of struct field" {
105 assert(callStructField(Foo {.ptr = aFunc,}) == 13);
106 assert(callStructField(Foo {
107 .ptr = aFunc,
108 }) == 13);
106109}
107110
108111const Foo = struct {
109112 ptr: fn() i32,
110113};
111114
112fn aFunc() i32 { return 13; }
115fn aFunc() i32 {
116 return 13;
117}
113118
114119fn callStructField(foo: &const Foo) i32 {
115120 return foo.ptr();
116121}
117122
118
119123test "store member function in variable" {
120 const instance = MemberFnTestFoo { .x = 1234, };
124 const instance = MemberFnTestFoo {
125 .x = 1234,
126 };
121127 const memberFn = MemberFnTestFoo.member;
122128 const result = memberFn(instance);
123129 assert(result == 1234);
124130}
125131const MemberFnTestFoo = struct {
126132 x: i32,
127 fn member(foo: &const MemberFnTestFoo) i32 { return foo.x; }
133 fn member(foo: &const MemberFnTestFoo) i32 {
134 return foo.x;
135 }
128136};
129137
130
131138test "call member function directly" {
132 const instance = MemberFnTestFoo { .x = 1234, };
139 const instance = MemberFnTestFoo {
140 .x = 1234,
141 };
133142 const result = MemberFnTestFoo.member(instance);
134143 assert(result == 1234);
135144}
136145
137146test "member functions" {
138 const r = MemberFnRand {.seed = 1234};
147 const r = MemberFnRand {
148 .seed = 1234,
149 };
139150 assert(r.getSeed() == 1234);
140151}
141152const MemberFnRand = struct {
......@@ -170,17 +181,16 @@ const EmptyStruct = struct {
170181 }
171182};
172183
173
174184test "return empty struct from fn" {
175185 _ = testReturnEmptyStructFromFn();
176186}
177187const EmptyStruct2 = struct {};
178188fn testReturnEmptyStructFromFn() EmptyStruct2 {
179 return EmptyStruct2 {};
189 return EmptyStruct2{};
180190}
181191
182192test "pass slice of empty struct to fn" {
183 assert(testPassSliceOfEmptyStructToFn([]EmptyStruct2{ EmptyStruct2{} }) == 1);
193 assert(testPassSliceOfEmptyStructToFn([]EmptyStruct2 {EmptyStruct2{}}) == 1);
184194}
185195fn testPassSliceOfEmptyStructToFn(slice: []const EmptyStruct2) usize {
186196 return slice.len;
......@@ -201,7 +211,6 @@ test "packed struct" {
201211 assert(four == 4);
202212}
203213
204
205214const BitField1 = packed struct {
206215 a: u3,
207216 b: u3,
......@@ -301,7 +310,7 @@ test "packed array 24bits" {
301310 assert(@sizeOf(FooArray24Bits) == 2 + 2 * 3 + 2);
302311 }
303312
304 var bytes = []u8{0} ** (@sizeOf(FooArray24Bits) + 1);
313 var bytes = []u8 {0} ** (@sizeOf(FooArray24Bits) + 1);
305314 bytes[bytes.len - 1] = 0xaa;
306315 const ptr = &([]FooArray24Bits)(bytes[0..bytes.len - 1])[0];
307316 assert(ptr.a == 0);
......@@ -351,7 +360,7 @@ test "aligned array of packed struct" {
351360 assert(@sizeOf(FooArrayOfAligned) == 2 * 2);
352361 }
353362
354 var bytes = []u8{0xbb} ** @sizeOf(FooArrayOfAligned);
363 var bytes = []u8 {0xbb} ** @sizeOf(FooArrayOfAligned);
355364 const ptr = &([]FooArrayOfAligned)(bytes[0..bytes.len])[0];
356365
357366 assert(ptr.a[0].a == 0xbb);
......@@ -360,11 +369,15 @@ test "aligned array of packed struct" {
360369 assert(ptr.a[1].b == 0xbb);
361370}
362371
363
364
365372test "runtime struct initialization of bitfield" {
366 const s1 = Nibbles { .x = x1, .y = x1 };
367 const s2 = Nibbles { .x = u4(x2), .y = u4(x2) };
373 const s1 = Nibbles {
374 .x = x1,
375 .y = x1,
376 };
377 const s2 = Nibbles {
378 .x = u4(x2),
379 .y = u4(x2),
380 };
368381
369382 assert(s1.x == x1);
370383 assert(s1.y == x1);
......@@ -394,7 +407,7 @@ test "native bit field understands endianness" {
394407 var all: u64 = 0x7765443322221111;
395408 var bytes: [8]u8 = undefined;
396409 @memcpy(&bytes[0], @ptrCast(&u8, &all), 8);
397 var bitfields = *@ptrCast(&Bitfields, &bytes[0]);
410 var bitfields = @ptrCast(&Bitfields, &bytes[0]).*;
398411
399412 assert(bitfields.f1 == 0x1111);
400413 assert(bitfields.f2 == 0x2222);
test/cases/struct_contains_null_ptr_itself.zig-1
......@@ -19,4 +19,3 @@ pub const Node = struct {
1919pub const NodeLineComment = struct {
2020 base: Node,
2121};
22
test/cases/struct_contains_slice_of_itself.zig+1-1
......@@ -6,7 +6,7 @@ const Node = struct {
66};
77
88test "struct contains slice of itself" {
9 var other_nodes = []Node{
9 var other_nodes = []Node {
1010 Node {
1111 .payload = 31,
1212 .children = []Node{},
test/cases/switch.zig+33-16
......@@ -6,7 +6,10 @@ test "switch with numbers" {
66
77fn testSwitchWithNumbers(x: u32) void {
88 const result = switch (x) {
9 1, 2, 3, 4 ... 8 => false,
9 1,
10 2,
11 3,
12 4 ... 8 => false,
1013 13 => true,
1114 else => false,
1215 };
......@@ -34,8 +37,10 @@ test "implicit comptime switch" {
3437 const result = switch (x) {
3538 3 => 10,
3639 4 => 11,
37 5, 6 => 12,
38 7, 8 => 13,
40 5,
41 6 => 12,
42 7,
43 8 => 13,
3944 else => 14,
4045 };
4146
......@@ -61,7 +66,6 @@ fn nonConstSwitchOnEnum(fruit: Fruit) void {
6166 }
6267}
6368
64
6569test "switch statement" {
6670 nonConstSwitch(SwitchStatmentFoo.C);
6771}
......@@ -81,11 +85,16 @@ const SwitchStatmentFoo = enum {
8185 D,
8286};
8387
84
8588test "switch prong with variable" {
86 switchProngWithVarFn(SwitchProngWithVarEnum { .One = 13});
87 switchProngWithVarFn(SwitchProngWithVarEnum { .Two = 13.0});
88 switchProngWithVarFn(SwitchProngWithVarEnum { .Meh = {}});
89 switchProngWithVarFn(SwitchProngWithVarEnum {
90 .One = 13,
91 });
92 switchProngWithVarFn(SwitchProngWithVarEnum {
93 .Two = 13.0,
94 });
95 switchProngWithVarFn(SwitchProngWithVarEnum {
96 .Meh = {},
97 });
8998}
9099const SwitchProngWithVarEnum = union(enum) {
91100 One: i32,
......@@ -93,7 +102,7 @@ const SwitchProngWithVarEnum = union(enum) {
93102 Meh: void,
94103};
95104fn switchProngWithVarFn(a: &const SwitchProngWithVarEnum) void {
96 switch(*a) {
105 switch (a.*) {
97106 SwitchProngWithVarEnum.One => |x| {
98107 assert(x == 13);
99108 },
......@@ -112,9 +121,11 @@ test "switch on enum using pointer capture" {
112121}
113122
114123fn testSwitchEnumPtrCapture() void {
115 var value = SwitchProngWithVarEnum { .One = 1234 };
124 var value = SwitchProngWithVarEnum {
125 .One = 1234,
126 };
116127 switch (value) {
117 SwitchProngWithVarEnum.One => |*x| *x += 1,
128 SwitchProngWithVarEnum.One => |*x| x.* += 1,
118129 else => unreachable,
119130 }
120131 switch (value) {
......@@ -125,8 +136,12 @@ fn testSwitchEnumPtrCapture() void {
125136
126137test "switch with multiple expressions" {
127138 const x = switch (returnsFive()) {
128 1, 2, 3 => 1,
129 4, 5, 6 => 2,
139 1,
140 2,
141 3 => 1,
142 4,
143 5,
144 6 => 2,
130145 else => i32(3),
131146 };
132147 assert(x == 2);
......@@ -135,14 +150,15 @@ fn returnsFive() i32 {
135150 return 5;
136151}
137152
138
139153const Number = union(enum) {
140154 One: u64,
141155 Two: u8,
142156 Three: f32,
143157};
144158
145const number = Number { .Three = 1.23 };
159const number = Number {
160 .Three = 1.23,
161};
146162
147163fn returnsFalse() bool {
148164 switch (number) {
......@@ -198,7 +214,8 @@ fn testSwitchHandleAllCasesRange(x: u8) u8 {
198214 return switch (x) {
199215 0 ... 100 => u8(0),
200216 101 ... 200 => 1,
201 201, 203 => 2,
217 201,
218 203 => 2,
202219 202 => 4,
203220 204 ... 255 => 3,
204221 };
test/cases/switch_prong_err_enum.zig+6-2
......@@ -14,14 +14,18 @@ const FormValue = union(enum) {
1414
1515fn doThing(form_id: u64) error!FormValue {
1616 return switch (form_id) {
17 17 => FormValue { .Address = try readOnce() },
17 17 => FormValue {
18 .Address = try readOnce(),
19 },
1820 else => error.InvalidDebugInfo,
1921 };
2022}
2123
2224test "switch prong returns error enum" {
2325 switch (doThing(17) catch unreachable) {
24 FormValue.Address => |payload| { assert(payload == 1); },
26 FormValue.Address => |payload| {
27 assert(payload == 1);
28 },
2529 else => unreachable,
2630 }
2731 assert(read_count == 1);
test/cases/switch_prong_implicit_cast.zig+6-2
......@@ -7,8 +7,12 @@ const FormValue = union(enum) {
77
88fn foo(id: u64) !FormValue {
99 return switch (id) {
10 2 => FormValue { .Two = true },
11 1 => FormValue { .One = {} },
10 2 => FormValue {
11 .Two = true,
12 },
13 1 => FormValue {
14 .One = {},
15 },
1216 else => return error.Whatever,
1317 };
1418}
test/cases/try.zig+3-5
......@@ -3,14 +3,12 @@ const assert = @import("std").debug.assert;
33test "try on error union" {
44 tryOnErrorUnionImpl();
55 comptime tryOnErrorUnionImpl();
6
76}
87
98fn tryOnErrorUnionImpl() void {
10 const x = if (returnsTen()) |val|
11 val + 1
12 else |err| switch (err) {
13 error.ItBroke, error.NoMem => 1,
9 const x = if (returnsTen()) |val| val + 1 else |err| switch (err) {
10 error.ItBroke,
11 error.NoMem => 1,
1412 error.CrappedOut => i32(2),
1513 else => unreachable,
1614 };
test/cases/undefined.zig+2-2
......@@ -63,6 +63,6 @@ test "assign undefined to struct with method" {
6363}
6464
6565test "type name of undefined" {
66 const x = undefined;
67 assert(mem.eql(u8, @typeName(@typeOf(x)), "(undefined)"));
66 const x = undefined;
67 assert(mem.eql(u8, @typeName(@typeOf(x)), "(undefined)"));
6868}
test/cases/union.zig+50-37
......@@ -10,38 +10,41 @@ const Agg = struct {
1010 val2: Value,
1111};
1212
13const v1 = Value { .Int = 1234 };
14const v2 = Value { .Array = []u8{3} ** 9 };
13const v1 = Value{ .Int = 1234 };
14const v2 = Value{ .Array = []u8{3} ** 9 };
1515
16const err = (error!Agg)(Agg {
16const err = (error!Agg)(Agg{
1717 .val1 = v1,
1818 .val2 = v2,
1919});
2020
21const array = []Value { v1, v2, v1, v2};
22
21const array = []Value{
22 v1,
23 v2,
24 v1,
25 v2,
26};
2327
2428test "unions embedded in aggregate types" {
2529 switch (array[1]) {
2630 Value.Array => |arr| assert(arr[4] == 3),
2731 else => unreachable,
2832 }
29 switch((err catch unreachable).val1) {
33 switch ((err catch unreachable).val1) {
3034 Value.Int => |x| assert(x == 1234),
3135 else => unreachable,
3236 }
3337}
3438
35
3639const Foo = union {
3740 float: f64,
3841 int: i32,
3942};
4043
4144test "basic unions" {
42 var foo = Foo { .int = 1 };
45 var foo = Foo{ .int = 1 };
4346 assert(foo.int == 1);
44 foo = Foo {.float = 12.34};
47 foo = Foo{ .float = 12.34 };
4548 assert(foo.float == 12.34);
4649}
4750
......@@ -56,11 +59,11 @@ test "init union with runtime value" {
5659}
5760
5861fn setFloat(foo: &Foo, x: f64) void {
59 *foo = Foo { .float = x };
62 foo.* = Foo{ .float = x };
6063}
6164
6265fn setInt(foo: &Foo, x: i32) void {
63 *foo = Foo { .int = x };
66 foo.* = Foo{ .int = x };
6467}
6568
6669const FooExtern = extern union {
......@@ -69,13 +72,12 @@ const FooExtern = extern union {
6972};
7073
7174test "basic extern unions" {
72 var foo = FooExtern { .int = 1 };
75 var foo = FooExtern{ .int = 1 };
7376 assert(foo.int == 1);
7477 foo.float = 12.34;
7578 assert(foo.float == 12.34);
7679}
7780
78
7981const Letter = enum {
8082 A,
8183 B,
......@@ -93,12 +95,12 @@ test "union with specified enum tag" {
9395}
9496
9597fn doTest() void {
96 assert(bar(Payload {.A = 1234}) == -10);
98 assert(bar(Payload{ .A = 1234 }) == -10);
9799}
98100
99101fn bar(value: &const Payload) i32 {
100 assert(Letter(*value) == Letter.A);
101 return switch (*value) {
102 assert(Letter(value.*) == Letter.A);
103 return switch (value.*) {
102104 Payload.A => |x| return x - 1244,
103105 Payload.B => |x| if (x == 12.34) i32(20) else 21,
104106 Payload.C => |x| if (x) i32(30) else 31,
......@@ -131,13 +133,13 @@ const MultipleChoice2 = union(enum(u32)) {
131133
132134test "union(enum(u32)) with specified and unspecified tag values" {
133135 comptime assert(@TagType(@TagType(MultipleChoice2)) == u32);
134 testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2 {.C = 123});
135 comptime testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2 { .C = 123} );
136 testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 });
137 comptime testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 });
136138}
137139
138140fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: &const MultipleChoice2) void {
139 assert(u32(@TagType(MultipleChoice2)(*x)) == 60);
140 assert(1123 == switch (*x) {
141 assert(u32(@TagType(MultipleChoice2)(x.*)) == 60);
142 assert(1123 == switch (x.*) {
141143 MultipleChoice2.A => 1,
142144 MultipleChoice2.B => 2,
143145 MultipleChoice2.C => |v| i32(1000) + v,
......@@ -150,10 +152,9 @@ fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: &const MultipleChoice2) void
150152 });
151153}
152154
153
154155const ExternPtrOrInt = extern union {
155156 ptr: &u8,
156 int: u64
157 int: u64,
157158};
158159test "extern union size" {
159160 comptime assert(@sizeOf(ExternPtrOrInt) == 8);
......@@ -161,7 +162,7 @@ test "extern union size" {
161162
162163const PackedPtrOrInt = packed union {
163164 ptr: &u8,
164 int: u64
165 int: u64,
165166};
166167test "extern union size" {
167168 comptime assert(@sizeOf(PackedPtrOrInt) == 8);
......@@ -174,8 +175,16 @@ test "union with only 1 field which is void should be zero bits" {
174175 comptime assert(@sizeOf(ZeroBits) == 0);
175176}
176177
177const TheTag = enum {A, B, C};
178const TheUnion = union(TheTag) { A: i32, B: i32, C: i32 };
178const TheTag = enum {
179 A,
180 B,
181 C,
182};
183const TheUnion = union(TheTag) {
184 A: i32,
185 B: i32,
186 C: i32,
187};
179188test "union field access gives the enum values" {
180189 assert(TheUnion.A == TheTag.A);
181190 assert(TheUnion.B == TheTag.B);
......@@ -183,20 +192,28 @@ test "union field access gives the enum values" {
183192}
184193
185194test "cast union to tag type of union" {
186 testCastUnionToTagType(TheUnion {.B = 1234});
187 comptime testCastUnionToTagType(TheUnion {.B = 1234});
195 testCastUnionToTagType(TheUnion{ .B = 1234 });
196 comptime testCastUnionToTagType(TheUnion{ .B = 1234 });
188197}
189198
190199fn testCastUnionToTagType(x: &const TheUnion) void {
191 assert(TheTag(*x) == TheTag.B);
200 assert(TheTag(x.*) == TheTag.B);
192201}
193202
194203test "cast tag type of union to union" {
195204 var x: Value2 = Letter2.B;
196205 assert(Letter2(x) == Letter2.B);
197206}
198const Letter2 = enum { A, B, C };
199const Value2 = union(Letter2) { A: i32, B, C, };
207const Letter2 = enum {
208 A,
209 B,
210 C,
211};
212const Value2 = union(Letter2) {
213 A: i32,
214 B,
215 C,
216};
200217
201218test "implicit cast union to its tag type" {
202219 var x: Value2 = Letter2.B;
......@@ -217,19 +234,16 @@ const TheUnion2 = union(enum) {
217234};
218235
219236fn assertIsTheUnion2Item1(value: &const TheUnion2) void {
220 assert(*value == TheUnion2.Item1);
237 assert(value.* == TheUnion2.Item1);
221238}
222239
223
224240pub const PackThis = union(enum) {
225241 Invalid: bool,
226242 StringLiteral: u2,
227243};
228244
229245test "constant packed union" {
230 testConstPackedUnion([]PackThis {
231 PackThis { .StringLiteral = 1 },
232 });
246 testConstPackedUnion([]PackThis{PackThis{ .StringLiteral = 1 }});
233247}
234248
235249fn testConstPackedUnion(expected_tokens: []const PackThis) void {
......@@ -242,7 +256,7 @@ test "switch on union with only 1 field" {
242256 switch (r) {
243257 PartialInst.Compiled => {
244258 var z: PartialInstWithPayload = undefined;
245 z = PartialInstWithPayload { .Compiled = 1234 };
259 z = PartialInstWithPayload{ .Compiled = 1234 };
246260 switch (z) {
247261 PartialInstWithPayload.Compiled => |x| {
248262 assert(x == 1234);
......@@ -261,4 +275,3 @@ const PartialInst = union(enum) {
261275const PartialInstWithPayload = union(enum) {
262276 Compiled: i32,
263277};
264
test/cases/var_args.zig+16-9
......@@ -2,9 +2,12 @@ const assert = @import("std").debug.assert;
22
33fn add(args: ...) i32 {
44 var sum = i32(0);
5 {comptime var i: usize = 0; inline while (i < args.len) : (i += 1) {
6 sum += args[i];
7 }}
5 {
6 comptime var i: usize = 0;
7 inline while (i < args.len) : (i += 1) {
8 sum += args[i];
9 }
10 }
811 return sum;
912}
1013
......@@ -55,18 +58,23 @@ fn extraFn(extra: u32, args: ...) usize {
5558 return args.len;
5659}
5760
61const foos = []fn(...) bool {
62 foo1,
63 foo2,
64};
5865
59const foos = []fn(...) bool { foo1, foo2 };
60
61fn foo1(args: ...) bool { return true; }
62fn foo2(args: ...) bool { return false; }
66fn foo1(args: ...) bool {
67 return true;
68}
69fn foo2(args: ...) bool {
70 return false;
71}
6372
6473test "array of var args functions" {
6574 assert(foos[0]());
6675 assert(!foos[1]());
6776}
6877
69
7078test "pass array and slice of same array to var args should have same pointers" {
7179 const array = "hi";
7280 const slice: []const u8 = array;
......@@ -79,7 +87,6 @@ fn assertSlicePtrsEql(args: ...) void {
7987 assert(s1.ptr == s2.ptr);
8088}
8189
82
8390test "pass zero length array to var args param" {
8491 doNothingWithFirstArg("");
8592}
test/cases/while.zig+41-24
......@@ -1,7 +1,7 @@
11const assert = @import("std").debug.assert;
22
33test "while loop" {
4 var i : i32 = 0;
4 var i: i32 = 0;
55 while (i < 4) {
66 i += 1;
77 }
......@@ -35,7 +35,7 @@ test "continue and break" {
3535}
3636var continue_and_break_counter: i32 = 0;
3737fn runContinueAndBreakTest() void {
38 var i : i32 = 0;
38 var i: i32 = 0;
3939 while (true) {
4040 continue_and_break_counter += 2;
4141 i += 1;
......@@ -58,10 +58,13 @@ fn returnWithImplicitCastFromWhileLoopTest() error!void {
5858
5959test "while with continue expression" {
6060 var sum: i32 = 0;
61 {var i: i32 = 0; while (i < 10) : (i += 1) {
62 if (i == 5) continue;
63 sum += i;
64 }}
61 {
62 var i: i32 = 0;
63 while (i < 10) : (i += 1) {
64 if (i == 5) continue;
65 sum += i;
66 }
67 }
6568 assert(sum == 40);
6669}
6770
......@@ -117,17 +120,13 @@ test "while with error union condition" {
117120
118121var numbers_left: i32 = undefined;
119122fn getNumberOrErr() error!i32 {
120 return if (numbers_left == 0)
121 error.OutOfNumbers
122 else x: {
123 return if (numbers_left == 0) error.OutOfNumbers else x: {
123124 numbers_left -= 1;
124125 break :x numbers_left;
125126 };
126127}
127128fn getNumberOrNull() ?i32 {
128 return if (numbers_left == 0)
129 null
130 else x: {
129 return if (numbers_left == 0) null else x: {
131130 numbers_left -= 1;
132131 break :x numbers_left;
133132 };
......@@ -136,42 +135,48 @@ fn getNumberOrNull() ?i32 {
136135test "while on nullable with else result follow else prong" {
137136 const result = while (returnNull()) |value| {
138137 break value;
139 } else i32(2);
138 } else
139 i32(2);
140140 assert(result == 2);
141141}
142142
143143test "while on nullable with else result follow break prong" {
144144 const result = while (returnMaybe(10)) |value| {
145145 break value;
146 } else i32(2);
146 } else
147 i32(2);
147148 assert(result == 10);
148149}
149150
150151test "while on error union with else result follow else prong" {
151152 const result = while (returnError()) |value| {
152153 break value;
153 } else |err| i32(2);
154 } else|err|
155 i32(2);
154156 assert(result == 2);
155157}
156158
157159test "while on error union with else result follow break prong" {
158160 const result = while (returnSuccess(10)) |value| {
159161 break value;
160 } else |err| i32(2);
162 } else|err|
163 i32(2);
161164 assert(result == 10);
162165}
163166
164167test "while on bool with else result follow else prong" {
165168 const result = while (returnFalse()) {
166169 break i32(10);
167 } else i32(2);
170 } else
171 i32(2);
168172 assert(result == 2);
169173}
170174
171175test "while on bool with else result follow break prong" {
172176 const result = while (returnTrue()) {
173177 break i32(10);
174 } else i32(2);
178 } else
179 i32(2);
175180 assert(result == 10);
176181}
177182
......@@ -202,9 +207,21 @@ fn testContinueOuter() void {
202207 }
203208}
204209
205fn returnNull() ?i32 { return null; }
206fn returnMaybe(x: i32) ?i32 { return x; }
207fn returnError() error!i32 { return error.YouWantedAnError; }
208fn returnSuccess(x: i32) error!i32 { return x; }
209fn returnFalse() bool { return false; }
210fn returnTrue() bool { return true; }
210fn returnNull() ?i32 {
211 return null;
212}
213fn returnMaybe(x: i32) ?i32 {
214 return x;
215}
216fn returnError() error!i32 {
217 return error.YouWantedAnError;
218}
219fn returnSuccess(x: i32) error!i32 {
220 return x;
221}
222fn returnFalse() bool {
223 return false;
224}
225fn returnTrue() bool {
226 return true;
227}