aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorVexu <git@vexu.eu>2020-05-16 12:29:01 +0300
committerVexu <git@vexu.eu>2020-05-16 12:29:01 +0300
commited62081d38736b2fabb3949fa821af2cf03dbe6e (patch)
tree99e63615c23af7d09a2403109c93c887c2d197d3 /lib
parent6ca0def499a610c638c38cd79d8df91035493083 (diff)
downloadzig-ed62081d38736b2fabb3949fa821af2cf03dbe6e.tar.gz
zig-ed62081d38736b2fabb3949fa821af2cf03dbe6e.zip
recover from missing semicolon after if stmt
Diffstat (limited to 'lib')
-rw-r--r--lib/std/zig/parse.zig23
-rw-r--r--lib/std/zig/parser_test.zig16
2 files changed, 26 insertions, 13 deletions
diff --git a/lib/std/zig/parse.zig b/lib/std/zig/parse.zig
index 68706883bd..4a86a75f95 100644
--- a/lib/std/zig/parse.zig
+++ b/lib/std/zig/parse.zig
@@ -724,16 +724,12 @@ fn parseIfStatement(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node
const if_prefix = if_node.cast(Node.If).?;
const block_expr = (try parseBlockExpr(arena, it, tree));
- const assign_expr = if (block_expr == null) blk: {
- break :blk (try parseAssignExpr(arena, it, tree)) orelse null;
- } else null;
-
- if (block_expr == null and assign_expr == null) {
- try tree.errors.push(.{
+ const assign_expr = if (block_expr == null)
+ try expectNode(arena, it, tree, parseAdditionExpr, .{
.ExpectedBlockOrAssignment = .{ .token = it.index },
- });
- return error.ParseError;
- }
+ })
+ else
+ null;
const semicolon = if (assign_expr != null) eatToken(it, .Semicolon) else null;
@@ -770,10 +766,9 @@ fn parseIfStatement(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node
try tree.errors.push(.{
.ExpectedSemiOrElse = .{ .token = it.index },
});
- return error.ParseError;
}
- unreachable;
+ return if_node;
}
/// LabeledStatement <- BlockLabel? (Block / LoopStatement)
@@ -879,7 +874,8 @@ fn parseForStatement(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node
try tree.errors.push(.{
.ExpectedSemiOrElse = .{ .token = it.index },
});
- return null;
+
+ return node;
}
return null;
@@ -941,7 +937,8 @@ fn parseWhileStatement(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*No
try tree.errors.push(.{
.ExpectedSemiOrElse = .{ .token = it.index },
});
- return null;
+
+ return node;
}
return null;
diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig
index afb5db9dbd..992abc57c7 100644
--- a/lib/std/zig/parser_test.zig
+++ b/lib/std/zig/parser_test.zig
@@ -184,6 +184,22 @@ test "recovery: invalid global error set access" {
});
}
+test "recovery: missing semicolon after if, for, while stmt" {
+ try testError(
+ \\test "" {
+ \\ if (foo) bar
+ \\ for (foo) |a| bar
+ \\ while (foo) bar
+ \\ a && b;
+ \\}
+ , &[_]Error{
+ .ExpectedSemiOrElse,
+ .ExpectedSemiOrElse,
+ .ExpectedSemiOrElse,
+ .InvalidAnd,
+ });
+}
+
test "zig fmt: top-level fields" {
try testCanonical(
\\a: did_you_know,