authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-02-17 17:14:28-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-02-18 19:17:21-07:00
log5029e5364caccac07f2296c1f30f2f238f13864d
tree053498ffe1bdc9c1b2a378cbe2b7607038e52060
parent293d6bdc73c5fe01b07ebe3d09c9a78613fed093

make zig fmt perform upgrade to new for loop syntax

The intent here is to revert this commit after Zig 0.10.0 is released.

4 files changed, 44 insertions(+), 1 deletions(-)

lib/std/zig/Ast.zig+12
......@@ -2525,6 +2525,18 @@ pub const full = struct {
25252525 then_expr: Node.Index,
25262526 else_expr: Node.Index,
25272527 };
2528
2529 /// TODO: remove this after zig 0.11.0 is tagged.
2530 pub fn isOldSyntax(f: For, token_tags: []const Token.Tag) bool {
2531 if (f.ast.inputs.len != 1) return false;
2532 if (token_tags[f.payload_token + 1] == .comma) return true;
2533 if (token_tags[f.payload_token] == .asterisk and
2534 token_tags[f.payload_token + 2] == .comma)
2535 {
2536 return true;
2537 }
2538 return false;
2539 }
25282540 };
25292541
25302542 pub const ContainerField = struct {
lib/std/zig/Parse.zig+4-1
......@@ -2143,7 +2143,10 @@ fn forPrefix(p: *Parse) Error!usize {
21432143 _ = p.eatToken(.asterisk);
21442144 const identifier = try p.expectToken(.identifier);
21452145 captures += 1;
2146 if (captures > inputs and !warned_excess) {
2146 if (!warned_excess and inputs == 1 and captures == 2) {
2147 // TODO remove the above condition after 0.11.0 release. this silences
2148 // the error so that zig fmt can fix it.
2149 } else if (captures > inputs and !warned_excess) {
21472150 try p.warnMsg(.{ .tag = .extra_for_capture, .token = identifier });
21482151 warned_excess = true;
21492152 }
lib/std/zig/render.zig+11
......@@ -1211,6 +1211,17 @@ fn renderFor(gpa: Allocator, ais: *Ais, tree: Ast, for_node: Ast.full.For, space
12111211 const lparen = for_node.ast.for_token + 1;
12121212 try renderParamList(gpa, ais, tree, lparen, for_node.ast.inputs, .space);
12131213
1214 // TODO remove this after zig 0.11.0
1215 if (for_node.isOldSyntax(token_tags)) {
1216 // old: for (a) |b, c| {}
1217 // new: for (a, 0..) |b, c| {}
1218 const array_list = ais.underlying_writer.context; // abstractions? who needs 'em!
1219 if (mem.endsWith(u8, array_list.items, ") ")) {
1220 array_list.items.len -= 2;
1221 try array_list.appendSlice(", 0..) ");
1222 }
1223 }
1224
12141225 var cur = for_node.payload_token;
12151226 const pipe = std.mem.indexOfScalarPos(std.zig.Token.Tag, token_tags, cur, .pipe).?;
12161227 if (token_tags[pipe - 1] == .comma) {
src/AstGen.zig+17
......@@ -6302,6 +6302,23 @@ fn forExpr(
63026302 const node_data = tree.nodes.items(.data);
63036303 const gpa = astgen.gpa;
63046304
6305 // TODO this can be deleted after zig 0.11.0 is released because it
6306 // will be caught in the parser.
6307 if (for_full.isOldSyntax(token_tags)) {
6308 return astgen.failTokNotes(
6309 for_full.payload_token + 2,
6310 "extra capture in for loop",
6311 .{},
6312 &[_]u32{
6313 try astgen.errNoteTok(
6314 for_full.payload_token + 2,
6315 "run 'zig fmt' to upgrade your code automatically",
6316 .{},
6317 ),
6318 },
6319 );
6320 }
6321
63056322 // For counters, this is the start value; for indexables, this is the base
63066323 // pointer that can be used with elem_ptr and similar instructions.
63076324 // Special value `none` means that this is a counter and its start value is