diff --git a/doc/langref.md b/doc/langref.md index bfc2812b73..c81c9ede0e 100644 --- a/doc/langref.md +++ b/doc/langref.md @@ -79,8 +79,6 @@ SwitchProng = (list(SwitchItem, ",") | "else") "=>" option("|" option("*") Symbo SwitchItem = Expression | (Expression "..." Expression) -WhileExpression(body) = "while" "(" Expression option(";" Expression) ")" body - ForExpression(body) = "for" "(" Expression ")" option("|" option("*") Symbol option("," Symbol) "|") body BoolOrExpression = BoolAndExpression "or" BoolOrExpression | BoolAndExpression @@ -95,6 +93,8 @@ TryExpression(body) = "if" "(" Expression ")" option("|" option("*") Symbol "|") TestExpression(body) = "if" "(" Expression ")" option("|" option("*") Symbol "|") body option("else" BlockExpression(body)) +WhileExpression(body) = "while" "(" Expression ")" option("|" option("*") Symbol "|") option(":" "(" Expression ")") body option("else" option("|" Symbol "|") BlockExpression(body)) + BoolAndExpression = ComparisonExpression "and" BoolAndExpression | ComparisonExpression ComparisonExpression = BinaryOrExpression ComparisonOperator BinaryOrExpression | BinaryOrExpression diff --git a/example/cat/main.zig b/example/cat/main.zig index 3c581d8a50..e1429c78ba 100644 --- a/example/cat/main.zig +++ b/example/cat/main.zig @@ -7,7 +7,7 @@ pub fn main() -> %void { const exe = os.args.at(0); var catted_anything = false; var arg_i: usize = 1; - while (arg_i < os.args.count(); arg_i += 1) { + while (arg_i < os.args.count()) : (arg_i += 1) { const arg = os.args.at(arg_i); if (mem.eql(u8, arg, "-")) { catted_anything = true; diff --git a/src/all_types.hpp b/src/all_types.hpp index 6be77d19f0..dd8a8e7a67 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -609,8 +609,12 @@ struct AstNodeTestExpr { struct AstNodeWhileExpr { AstNode *condition; + Buf *var_symbol; + bool var_is_ptr; AstNode *continue_expr; AstNode *body; + AstNode *else_node; + Buf *err_symbol; bool is_inline; }; diff --git a/src/parser.cpp b/src/parser.cpp index 3898ddbb81..a8c26f3c0e 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -1580,7 +1580,7 @@ static AstNode *ast_parse_bool_or_expr(ParseContext *pc, size_t *token_index, bo } /* -WhileExpression(body) = option("inline") "while" "(" Expression option(";" Expression) ")" body +WhileExpression(body) = option("inline") "while" "(" Expression ")" option("|" option("*") Symbol "|") option(":" "(" Expression ")") body option("else" option("|" Symbol "|") BlockExpression(body)) */ static AstNode *ast_parse_while_expr(ParseContext *pc, size_t *token_index, bool mandatory) { Token *first_token = &pc->tokens->at(*token_index); @@ -1613,21 +1613,49 @@ static AstNode *ast_parse_while_expr(ParseContext *pc, size_t *token_index, bool ast_eat_token(pc, token_index, TokenIdLParen); node->data.while_expr.condition = ast_parse_expression(pc, token_index, true); + ast_eat_token(pc, token_index, TokenIdRParen); - Token *semi_or_rparen = &pc->tokens->at(*token_index); + Token *open_bar_tok = &pc->tokens->at(*token_index); + if (open_bar_tok->id == TokenIdBinOr) { + *token_index += 1; - if (semi_or_rparen->id == TokenIdRParen) { - *token_index += 1; - node->data.while_expr.body = ast_parse_block_or_expression(pc, token_index, true); - } else if (semi_or_rparen->id == TokenIdSemicolon) { - *token_index += 1; - node->data.while_expr.continue_expr = ast_parse_expression(pc, token_index, true); - ast_eat_token(pc, token_index, TokenIdRParen); - node->data.while_expr.body = ast_parse_block_or_expression(pc, token_index, true); - } else { - ast_invalid_token_error(pc, semi_or_rparen); + Token *star_tok = &pc->tokens->at(*token_index); + if (star_tok->id == TokenIdStar) { + *token_index += 1; + node->data.while_expr.var_is_ptr = true; + } + + Token *var_name_tok = ast_eat_token(pc, token_index, TokenIdSymbol); + node->data.while_expr.var_symbol = token_buf(var_name_tok); + ast_eat_token(pc, token_index, TokenIdBinOr); } + Token *colon_tok = &pc->tokens->at(*token_index); + if (colon_tok->id == TokenIdColon) { + *token_index += 1; + ast_eat_token(pc, token_index, TokenIdLParen); + node->data.while_expr.continue_expr = ast_parse_expression(pc, token_index, true); + ast_eat_token(pc, token_index, TokenIdRParen); + } + + node->data.while_expr.body = ast_parse_block_or_expression(pc, token_index, true); + + Token *else_tok = &pc->tokens->at(*token_index); + if (else_tok->id == TokenIdKeywordElse) { + *token_index += 1; + + Token *else_bar_tok = &pc->tokens->at(*token_index); + if (else_bar_tok->id == TokenIdBinOr) { + *token_index += 1; + + Token *err_name_tok = ast_eat_token(pc, token_index, TokenIdSymbol); + node->data.while_expr.err_symbol = token_buf(err_name_tok); + + ast_eat_token(pc, token_index, TokenIdBinOr); + } + + node->data.while_expr.body = ast_parse_block_or_expression(pc, token_index, true); + } return node; } diff --git a/std/base64.zig b/std/base64.zig index 8f4f2fac78..1bf8ecc2b9 100644 --- a/std/base64.zig +++ b/std/base64.zig @@ -17,7 +17,7 @@ pub fn encodeWithAlphabet(dest: []u8, source: []const u8, alphabet: []const u8) var i: usize = 0; var out_index: usize = 0; - while (i + 2 < source.len; i += 3) { + while (i + 2 < source.len) : (i += 3) { dest[out_index] = alphabet[(source[i] >> 2) & 0x3f]; out_index += 1; diff --git a/std/cstr.zig b/std/cstr.zig index 0934b9967f..d117392abf 100644 --- a/std/cstr.zig +++ b/std/cstr.zig @@ -3,13 +3,13 @@ const assert = debug.assert; pub fn len(ptr: &const u8) -> usize { var count: usize = 0; - while (ptr[count] != 0; count += 1) {} + while (ptr[count] != 0) : (count += 1) {} return count; } pub fn cmp(a: &const u8, b: &const u8) -> i8 { var index: usize = 0; - while (a[index] == b[index] and a[index] != 0; index += 1) {} + while (a[index] == b[index] and a[index] != 0) : (index += 1) {} if (a[index] > b[index]) { return 1; } else if (a[index] < b[index]) { diff --git a/std/debug.zig b/std/debug.zig index 8e5416e8ae..03b172e5d0 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -79,7 +79,7 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty var ignored_count: usize = 0; var fp = usize(@frameAddress()); - while (fp != 0; fp = *@intToPtr(&const usize, fp)) { + while (fp != 0) : (fp = *@intToPtr(&const usize, fp)) { if (ignored_count < ignore_frame_count) { ignored_count += 1; continue; @@ -108,7 +108,7 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty if (line_info.column == 0) { %return out_stream.write("\n"); } else { - {var col_i: usize = 1; while (col_i < line_info.column; col_i += 1) { + {var col_i: usize = 1; while (col_i < line_info.column) : (col_i += 1) { %return out_stream.writeByte(' '); }} %return out_stream.write(GREEN ++ "^" ++ RESET ++ "\n"); @@ -594,7 +594,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe var this_offset = st.debug_line.offset; var this_index: usize = 0; - while (this_offset < debug_line_end; this_index += 1) { + while (this_offset < debug_line_end) : (this_index += 1) { %return in_stream.seekTo(this_offset); var is_64: bool = undefined; @@ -628,7 +628,7 @@ fn getLineNumberInfo(st: &ElfStackTrace, compile_unit: &const CompileUnit, targe const standard_opcode_lengths = %return st.allocator().alloc(u8, opcode_base - 1); - {var i: usize = 0; while (i < opcode_base - 1; i += 1) { + {var i: usize = 0; while (i < opcode_base - 1) : (i += 1) { standard_opcode_lengths[i] = %return in_stream.readByte(); }} diff --git a/std/fmt.zig b/std/fmt.zig index fa01f936c2..3de5c0eb4c 100644 --- a/std/fmt.zig +++ b/std/fmt.zig @@ -200,7 +200,7 @@ pub fn formatBuf(buf: []const u8, width: usize, var leftover_padding = if (width > buf.len) (width - buf.len) else return true; const pad_byte: u8 = ' '; - while (leftover_padding > 0; leftover_padding -= 1) { + while (leftover_padding > 0) : (leftover_padding -= 1) { if (!output(context, (&pad_byte)[0...1])) return false; } diff --git a/std/hash_map.zig b/std/hash_map.zig index c39761eafb..7b2d0d60e1 100644 --- a/std/hash_map.zig +++ b/std/hash_map.zig @@ -43,7 +43,7 @@ pub fn HashMap(comptime K: type, comptime V: type, assert(it.initial_modification_count == it.hm.modification_count); // concurrent modification } if (it.count >= it.hm.size) return null; - while (it.index < it.hm.entries.len; it.index += 1) { + while (it.index < it.hm.entries.len) : (it.index += 1) { const entry = &it.hm.entries[it.index]; if (entry.used) { it.index += 1; @@ -112,7 +112,7 @@ pub fn HashMap(comptime K: type, comptime V: type, pub fn remove(hm: &Self, key: K) -> ?&Entry { hm.incrementModificationCount(); const start_index = hm.keyToIndex(key); - {var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index; roll_over += 1) { + {var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) { const index = (start_index + roll_over) % hm.entries.len; var entry = &hm.entries[index]; @@ -121,7 +121,7 @@ pub fn HashMap(comptime K: type, comptime V: type, if (!eql(entry.key, key)) continue; - while (roll_over < hm.entries.len; roll_over += 1) { + while (roll_over < hm.entries.len) : (roll_over += 1) { const next_index = (start_index + roll_over + 1) % hm.entries.len; const next_entry = &hm.entries[next_index]; if (!next_entry.used or next_entry.distance_from_start_index == 0) { @@ -169,7 +169,7 @@ pub fn HashMap(comptime K: type, comptime V: type, const start_index = hm.keyToIndex(key); var roll_over: usize = 0; var distance_from_start_index: usize = 0; - while (roll_over < hm.entries.len; {roll_over += 1; distance_from_start_index += 1}) { + while (roll_over < hm.entries.len) : ({roll_over += 1; distance_from_start_index += 1}) { const index = (start_index + roll_over) % hm.entries.len; const entry = &hm.entries[index]; @@ -215,7 +215,7 @@ pub fn HashMap(comptime K: type, comptime V: type, fn internalGet(hm: &Self, key: K) -> ?&Entry { const start_index = hm.keyToIndex(key); - {var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index; roll_over += 1) { + {var roll_over: usize = 0; while (roll_over <= hm.max_distance_from_start_index) : (roll_over += 1) { const index = (start_index + roll_over) % hm.entries.len; const entry = &hm.entries[index]; diff --git a/std/list.zig b/std/list.zig index 3052fb3243..54d32413ec 100644 --- a/std/list.zig +++ b/std/list.zig @@ -78,11 +78,11 @@ test "basic list test" { var list = List(i32).init(&debug.global_allocator); defer list.deinit(); - {var i: usize = 0; while (i < 10; i += 1) { + {var i: usize = 0; while (i < 10) : (i += 1) { %%list.append(i32(i + 1)); }} - {var i: usize = 0; while (i < 10; i += 1) { + {var i: usize = 0; while (i < 10) : (i += 1) { assert(list.items[i] == i32(i + 1)); }} diff --git a/std/mem.zig b/std/mem.zig index ad350fa942..591005ad98 100644 --- a/std/mem.zig +++ b/std/mem.zig @@ -123,7 +123,7 @@ pub fn set(comptime T: type, dest: []T, value: T) { pub fn cmp(comptime T: type, a: []const T, b: []const T) -> Cmp { const n = math.min(a.len, b.len); var i: usize = 0; - while (i < n; i += 1) { + while (i < n) : (i += 1) { if (a[i] == b[i]) continue; return if (a[i] > b[i]) Cmp.Greater else if (a[i] < b[i]) Cmp.Less else Cmp.Equal; } @@ -164,7 +164,7 @@ pub fn indexOf(comptime T: type, haystack: []const T, needle: []const T) -> ?usi var i: usize = 0; const end = haystack.len - needle.len; - while (i <= end; i += 1) { + while (i <= end) : (i += 1) { if (eql(T, haystack[i...i + needle.len], needle)) return i; } @@ -263,14 +263,14 @@ const SplitIterator = struct { pub fn next(self: &SplitIterator) -> ?[]const u8 { // move to beginning of token - while (self.index < self.s.len and self.s[self.index] == self.c; self.index += 1) {} + while (self.index < self.s.len and self.s[self.index] == self.c) : (self.index += 1) {} const start = self.index; if (start == self.s.len) { return null; } // move to end of token - while (self.index < self.s.len and self.s[self.index] != self.c; self.index += 1) {} + while (self.index < self.s.len and self.s[self.index] != self.c) : (self.index += 1) {} const end = self.index; return self.s[start...end]; @@ -280,7 +280,7 @@ const SplitIterator = struct { pub fn rest(self: &const SplitIterator) -> []const u8 { // move to beginning of token var index: usize = self.index; - while (index < self.s.len and self.s[index] == self.c; index += 1) {} + while (index < self.s.len and self.s[index] == self.c) : (index += 1) {} return self.s[index...]; } }; diff --git a/std/os/index.zig b/std/os/index.zig index 3162a81c7f..7b1e32f4c4 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -276,7 +276,7 @@ pub fn posixExecve(exe_path: []const u8, argv: []const []const u8, env_map: &con { var it = env_map.iterator(); var i: usize = 0; - while (true; i += 1) { + while (true) : (i += 1) { const pair = it.next() ?? break; const env_buf = %return allocator.alloc(u8, pair.key.len + pair.value.len + 2); @@ -354,11 +354,11 @@ pub fn getEnvMap(allocator: &Allocator) -> %BufMap { for (environ_raw) |ptr| { var line_i: usize = 0; - while (ptr[line_i] != 0 and ptr[line_i] != '='; line_i += 1) {} + while (ptr[line_i] != 0 and ptr[line_i] != '=') : (line_i += 1) {} const key = ptr[0...line_i]; var end_i: usize = line_i; - while (ptr[end_i] != 0; end_i += 1) {} + while (ptr[end_i] != 0) : (end_i += 1) {} const value = ptr[line_i + 1...end_i]; %return result.set(key, value); @@ -369,13 +369,13 @@ pub fn getEnvMap(allocator: &Allocator) -> %BufMap { pub fn getEnv(key: []const u8) -> ?[]const u8 { for (environ_raw) |ptr| { var line_i: usize = 0; - while (ptr[line_i] != 0 and ptr[line_i] != '='; line_i += 1) {} + while (ptr[line_i] != 0 and ptr[line_i] != '=') : (line_i += 1) {} const this_key = ptr[0...line_i]; if (!mem.eql(u8, key, this_key)) continue; var end_i: usize = line_i; - while (ptr[end_i] != 0; end_i += 1) {} + while (ptr[end_i] != 0) : (end_i += 1) {} const this_value = ptr[line_i + 1...end_i]; return this_value; diff --git a/std/os/path.zig b/std/os/path.zig index d4896b634a..5cc36cace3 100644 --- a/std/os/path.zig +++ b/std/os/path.zig @@ -25,7 +25,7 @@ pub fn join(allocator: &Allocator, paths: ...) -> %[]u8 { var total_paths_len: usize = paths.len; // 1 slash per path { comptime var path_i = 0; - inline while (path_i < paths.len; path_i += 1) { + inline while (path_i < paths.len) : (path_i += 1) { const arg = ([]const u8)(paths[path_i]); total_paths_len += arg.len; } @@ -74,7 +74,7 @@ pub fn isAbsolute(path: []const u8) -> bool { pub fn resolve(allocator: &Allocator, args: ...) -> %[]u8 { var paths: [args.len][]const u8 = undefined; comptime var arg_i = 0; - inline while (arg_i < args.len; arg_i += 1) { + inline while (arg_i < args.len) : (arg_i += 1) { paths[arg_i] = args[arg_i]; } return resolveSlice(allocator, paths); diff --git a/std/rand.zig b/std/rand.zig index 5e990d2ce6..7a4483fe7f 100644 --- a/std/rand.zig +++ b/std/rand.zig @@ -121,7 +121,7 @@ fn MersenneTwister( var prev_value = seed; mt.array[0] = prev_value; var i: usize = 1; - while (i < n; i += 1) { + while (i < n) : (i += 1) { prev_value = int(i) +% f *% (prev_value ^ (prev_value >> (int.bit_count - 2))); mt.array[i] = prev_value; } @@ -136,12 +136,12 @@ fn MersenneTwister( if (mt.index >= n) { var i: usize = 0; - while (i < n - m; i += 1) { + while (i < n - m) : (i += 1) { const x = (mt.array[i] & UM) | (mt.array[i + 1] & LM); mt.array[i] = mt.array[i + m] ^ (x >> 1) ^ mag01[x & 0x1]; } - while (i < n - 1; i += 1) { + while (i < n - 1) : (i += 1) { const x = (mt.array[i] & UM) | (mt.array[i + 1] & LM); mt.array[i] = mt.array[i + m - n] ^ (x >> 1) ^ mag01[x & 0x1]; @@ -168,7 +168,7 @@ fn MersenneTwister( test "rand float 32" { var r = Rand.init(42); var i: usize = 0; - while (i < 1000; i += 1) { + while (i < 1000) : (i += 1) { const val = r.float(f32); assert(val >= 0.0); assert(val < 1.0); diff --git a/std/special/bootstrap.zig b/std/special/bootstrap.zig index 269643ac4f..fbe0b651c3 100644 --- a/std/special/bootstrap.zig +++ b/std/special/bootstrap.zig @@ -42,7 +42,7 @@ fn callMain(argc: usize, argv: &&u8, envp: &?&u8) -> %void { std.os.args.raw = argv[0...argc]; var env_count: usize = 0; - while (envp[env_count] != null; env_count += 1) {} + while (envp[env_count] != null) : (env_count += 1) {} std.os.environ_raw = @ptrCast(&&u8, envp)[0...env_count]; std.debug.user_main_fn = root.main; diff --git a/std/special/build_runner.zig b/std/special/build_runner.zig index 8d9407098e..4c55b01ed8 100644 --- a/std/special/build_runner.zig +++ b/std/special/build_runner.zig @@ -55,7 +55,7 @@ pub fn main() -> %void { var prefix: ?[]const u8 = null; - while (arg_i < os.args.count(); arg_i += 1) { + while (arg_i < os.args.count()) : (arg_i += 1) { const arg = os.args.at(arg_i); if (mem.startsWith(u8, arg, "-D")) { const option_contents = arg[2...]; diff --git a/std/special/builtin.zig b/std/special/builtin.zig index 110b962630..f15f13c45f 100644 --- a/std/special/builtin.zig +++ b/std/special/builtin.zig @@ -10,7 +10,7 @@ export fn memset(dest: ?&u8, c: u8, n: usize) { @setDebugSafety(this, false); var index: usize = 0; - while (index != n; index += 1) + while (index != n) : (index += 1) (??dest)[index] = c; } @@ -18,7 +18,7 @@ export fn memcpy(noalias dest: ?&u8, noalias src: ?&const u8, n: usize) { @setDebugSafety(this, false); var index: usize = 0; - while (index != n; index += 1) + while (index != n) : (index += 1) (??dest)[index] = (??src)[index]; } diff --git a/std/special/compiler_rt.zig b/std/special/compiler_rt.zig index 30e7606244..e4fc338e82 100644 --- a/std/special/compiler_rt.zig +++ b/std/special/compiler_rt.zig @@ -296,7 +296,7 @@ export fn __udivsi3(n: su_int, d: su_int) -> su_int { var q: su_int = n << (n_uword_bits - sr); var r: su_int = n >> sr; var carry: su_int = 0; - while (sr > 0; sr -= 1) { + while (sr > 0) : (sr -= 1) { // r:q = ((r:q) << 1) | carry r = (r << 1) | (q >> (n_uword_bits - 1)); q = (q << 1) | carry; diff --git a/test/cases/const_slice_child.zig b/test/cases/const_slice_child.zig index 9d7a32be81..2862f80cf3 100644 --- a/test/cases/const_slice_child.zig +++ b/test/cases/const_slice_child.zig @@ -31,7 +31,7 @@ fn bar(argc: usize) { fn strlen(ptr: &const u8) -> usize { var count: usize = 0; - while (ptr[count] != 0; count += 1) {} + while (ptr[count] != 0) : (count += 1) {} return count; } diff --git a/test/cases/eval.zig b/test/cases/eval.zig index 677feff48d..87c8101ed0 100644 --- a/test/cases/eval.zig +++ b/test/cases/eval.zig @@ -22,7 +22,7 @@ test "testStaticAddOne" { test "inlinedLoop" { comptime var i = 0; comptime var sum = 0; - inline while (i <= 5; i += 1) + inline while (i <= 5) : (i += 1) sum += i; assert(sum == 15); } @@ -157,7 +157,7 @@ test "tryToTrickEvalWithRuntimeIf" { fn testTryToTrickEvalWithRuntimeIf(b: bool) -> usize { comptime var i: usize = 0; - inline while (i < 10; i += 1) { + inline while (i < 10) : (i += 1) { const result = if (b) false else true; } comptime { @@ -208,7 +208,7 @@ fn three(value: i32) -> i32 { value + 3 } fn performFn(comptime prefix_char: u8, start_value: i32) -> i32 { var result: i32 = start_value; comptime var i = 0; - inline while (i < cmd_fns.len; i += 1) { + inline while (i < cmd_fns.len) : (i += 1) { if (cmd_fns[i].name[0] == prefix_char) { result = cmd_fns[i].func(result); } diff --git a/test/cases/misc.zig b/test/cases/misc.zig index fcd3be9a87..56008a344b 100644 --- a/test/cases/misc.zig +++ b/test/cases/misc.zig @@ -380,7 +380,7 @@ test "cStringConcatenation" { const len = cstr.len(b); const len_with_null = len + 1; - {var i: u32 = 0; while (i < len_with_null; i += 1) { + {var i: u32 = 0; while (i < len_with_null) : (i += 1) { assert(a[i] == b[i]); }} assert(a[len] == 0); diff --git a/test/cases/var_args.zig b/test/cases/var_args.zig index 466f3c9ccf..e0d0235629 100644 --- a/test/cases/var_args.zig +++ b/test/cases/var_args.zig @@ -2,7 +2,7 @@ const assert = @import("std").debug.assert; fn add(args: ...) -> i32 { var sum = i32(0); - {comptime var i: usize = 0; inline while (i < args.len; i += 1) { + {comptime var i: usize = 0; inline while (i < args.len) : (i += 1) { sum += args[i]; }} return sum; diff --git a/test/cases/while.zig b/test/cases/while.zig index c3b7a84722..70a47db0ae 100644 --- a/test/cases/while.zig +++ b/test/cases/while.zig @@ -58,7 +58,7 @@ fn returnWithImplicitCastFromWhileLoopTest() -> %void { test "whileWithContinueExpr" { var sum: i32 = 0; - {var i: i32 = 0; while (i < 10; i += 1) { + {var i: i32 = 0; while (i < 10) : (i += 1) { if (i == 5) continue; sum += i; }} diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 51577d2e0a..6bdbeebdc8 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -156,18 +156,18 @@ pub fn addCases(cases: &tests.CompileErrorContext) { cases.add("implicit semicolon - while-continue statement", \\export fn entry() { - \\ while(true;{}) {} + \\ while(true):({}) {} \\ var good = {}; - \\ while(true;{}) ({}) + \\ while(true):({}) ({}) \\ var bad = {}; \\} , ".tmp_source.zig:5:5: error: invalid token: 'var'"); cases.add("implicit semicolon - while-continue expression", \\export fn entry() { - \\ _ = while(true;{}) {}; + \\ _ = while(true):({}) {}; \\ var good = {}; - \\ _ = while(true;{}) {} + \\ _ = while(true):({}) {} \\ var bad = {}; \\} , ".tmp_source.zig:5:5: error: invalid token: 'var'"); @@ -1231,7 +1231,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { cases.add("pass integer literal to var args", \\fn add(args: ...) -> i32 { \\ var sum = i32(0); - \\ {comptime var i: usize = 0; inline while (i < args.len; i += 1) { + \\ {comptime var i: usize = 0; inline while (i < args.len) : (i += 1) { \\ sum += args[i]; \\ }} \\ return sum; @@ -1315,7 +1315,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { cases.add("control flow uses comptime var at runtime", \\export fn foo() { \\ comptime var i = 0; - \\ while (i < 5; i += 1) { + \\ while (i < 5) : (i += 1) { \\ bar(); \\ } \\} @@ -1323,7 +1323,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { \\fn bar() { } , ".tmp_source.zig:3:5: error: control flow attempts to use compile-time variable at runtime", - ".tmp_source.zig:3:21: note: compile-time variable assigned here"); + ".tmp_source.zig:3:24: note: compile-time variable assigned here"); cases.add("ignored return value", \\export fn foo() { @@ -1373,7 +1373,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) { cases.add("integer literal on a non-comptime var", \\export fn foo() { \\ var i = 0; - \\ while (i < 10; i += 1) { } + \\ while (i < 10) : (i += 1) { } \\} , ".tmp_source.zig:2:5: error: unable to infer variable type"); diff --git a/test/tests.zig b/test/tests.zig index faa3b6b5cb..28a4a5b709 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -590,7 +590,7 @@ pub const CompileErrorContext = struct { }; tc.addSourceFile(".tmp_source.zig", source); comptime var arg_i = 0; - inline while (arg_i < expected_lines.len; arg_i += 1) { + inline while (arg_i < expected_lines.len) : (arg_i += 1) { // TODO mem.dupe is because of issue #336 tc.addExpectedError(%%mem.dupe(self.b.allocator, u8, expected_lines[arg_i])); } @@ -853,7 +853,7 @@ pub const ParseHContext = struct { }; tc.addSourceFile("source.h", source); comptime var arg_i = 0; - inline while (arg_i < expected_lines.len; arg_i += 1) { + inline while (arg_i < expected_lines.len) : (arg_i += 1) { // TODO mem.dupe is because of issue #336 tc.addExpectedError(%%mem.dupe(self.b.allocator, u8, expected_lines[arg_i])); }