From 5029e5364caccac07f2296c1f30f2f238f13864d Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 17 Feb 2023 17:14:28 -0700 Subject: [PATCH] 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. --- lib/std/zig/Ast.zig | 12 ++++++++++++ lib/std/zig/Parse.zig | 5 ++++- lib/std/zig/render.zig | 11 +++++++++++ src/AstGen.zig | 17 +++++++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/lib/std/zig/Ast.zig b/lib/std/zig/Ast.zig index 42eb280966..3784f06160 100644 --- a/lib/std/zig/Ast.zig +++ b/lib/std/zig/Ast.zig @@ -2525,6 +2525,18 @@ pub const full = struct { then_expr: Node.Index, else_expr: Node.Index, }; + + /// TODO: remove this after zig 0.11.0 is tagged. + pub fn isOldSyntax(f: For, token_tags: []const Token.Tag) bool { + if (f.ast.inputs.len != 1) return false; + if (token_tags[f.payload_token + 1] == .comma) return true; + if (token_tags[f.payload_token] == .asterisk and + token_tags[f.payload_token + 2] == .comma) + { + return true; + } + return false; + } }; pub const ContainerField = struct { diff --git a/lib/std/zig/Parse.zig b/lib/std/zig/Parse.zig index 7ef884d22c..258e3b0368 100644 --- a/lib/std/zig/Parse.zig +++ b/lib/std/zig/Parse.zig @@ -2143,7 +2143,10 @@ fn forPrefix(p: *Parse) Error!usize { _ = p.eatToken(.asterisk); const identifier = try p.expectToken(.identifier); captures += 1; - if (captures > inputs and !warned_excess) { + if (!warned_excess and inputs == 1 and captures == 2) { + // TODO remove the above condition after 0.11.0 release. this silences + // the error so that zig fmt can fix it. + } else if (captures > inputs and !warned_excess) { try p.warnMsg(.{ .tag = .extra_for_capture, .token = identifier }); warned_excess = true; } diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig index 0e8d3125ac..ea3748a9bd 100644 --- a/lib/std/zig/render.zig +++ b/lib/std/zig/render.zig @@ -1211,6 +1211,17 @@ fn renderFor(gpa: Allocator, ais: *Ais, tree: Ast, for_node: Ast.full.For, space const lparen = for_node.ast.for_token + 1; try renderParamList(gpa, ais, tree, lparen, for_node.ast.inputs, .space); + // TODO remove this after zig 0.11.0 + if (for_node.isOldSyntax(token_tags)) { + // old: for (a) |b, c| {} + // new: for (a, 0..) |b, c| {} + const array_list = ais.underlying_writer.context; // abstractions? who needs 'em! + if (mem.endsWith(u8, array_list.items, ") ")) { + array_list.items.len -= 2; + try array_list.appendSlice(", 0..) "); + } + } + var cur = for_node.payload_token; const pipe = std.mem.indexOfScalarPos(std.zig.Token.Tag, token_tags, cur, .pipe).?; if (token_tags[pipe - 1] == .comma) { diff --git a/src/AstGen.zig b/src/AstGen.zig index b90201713e..c39dcfc9ef 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -6302,6 +6302,23 @@ fn forExpr( const node_data = tree.nodes.items(.data); const gpa = astgen.gpa; + // TODO this can be deleted after zig 0.11.0 is released because it + // will be caught in the parser. + if (for_full.isOldSyntax(token_tags)) { + return astgen.failTokNotes( + for_full.payload_token + 2, + "extra capture in for loop", + .{}, + &[_]u32{ + try astgen.errNoteTok( + for_full.payload_token + 2, + "run 'zig fmt' to upgrade your code automatically", + .{}, + ), + }, + ); + } + // For counters, this is the start value; for indexables, this is the base // pointer that can be used with elem_ptr and similar instructions. // Special value `none` means that this is a counter and its start value is