authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-04 20:54:09-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-04 20:54:09-07:00
log434fce2146a9f1dc096fdae6827e0855799351ca
tree1bf5d9fc2efa93a4991097934162f2035ffea0b2
parent041212a41cfaf029dc3eb9740467b721c76f406c

zig fmt: recovery: missing while rbrace

Previously, this test case resulted in zig fmt entering an endless loop.

2 files changed, 16 insertions(+), 3 deletions(-)

lib/std/zig/parse.zig+6-3
......@@ -937,14 +937,17 @@ const Parser = struct {
937937 /// If a parse error occurs, reports an error, but then finds the next statement
938938 /// and returns that one instead. If a parse error occurs but there is no following
939939 /// statement, returns 0.
940 fn expectStatementRecoverable(p: *Parser) error{OutOfMemory}!Node.Index {
940 fn expectStatementRecoverable(p: *Parser) Error!Node.Index {
941941 while (true) {
942942 return p.expectStatement() catch |err| switch (err) {
943943 error.OutOfMemory => return error.OutOfMemory,
944944 error.ParseError => {
945945 p.findNextStmt(); // Try to skip to the next statement.
946 if (p.token_tags[p.tok_i] == .r_brace) return null_node;
947 continue;
946 switch (p.token_tags[p.tok_i]) {
947 .r_brace => return null_node,
948 .eof => return error.ParseError,
949 else => continue,
950 }
948951 },
949952 };
950953 }
lib/std/zig/parser_test.zig+10
......@@ -4580,6 +4580,16 @@ test "recovery: missing comma in params" {
45804580 });
45814581}
45824582
4583test "recovery: missing while rbrace" {
4584 try testError(
4585 \\fn a() b {
4586 \\ while (d) {
4587 \\}
4588 , &[_]Error{
4589 .expected_statement,
4590 });
4591}
4592
45834593const std = @import("std");
45844594const mem = std.mem;
45854595const warn = std.debug.warn;