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.
This commit is contained in:
Andrew Kelley
2023-02-17 17:14:28 -07:00
parent 293d6bdc73
commit 5029e5364c
4 changed files with 44 additions and 1 deletions

View File

@@ -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 {

View File

@@ -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;
}

View File

@@ -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) {

View File

@@ -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