From e488959c1f0504dc3b2aae19d79211e725916c76 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Tue, 16 Jun 2026 10:21:47 +0200 Subject: [PATCH] grammar: remove negative lookahead causing inconsistency It is not clear why this lookahead was added in the first place, but it does not reflect the behavior of the Parse.zig implementation as demonstrated with the new test case. --- doc/langref/grammar.peg | 2 +- lib/std/zig/parser_fuzz.zig | 5 +++++ lib/std/zig/parser_generated_oracle.zig | 7 +------ 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/doc/langref/grammar.peg b/doc/langref/grammar.peg index e30b8f6029c4195e2b0faab06f4179b6302674f6..0f7b8d223c7c55995295ef4981c7770effe87b24 100644 --- a/doc/langref/grammar.peg +++ b/doc/langref/grammar.peg @@ -20,7 +20,7 @@ VarDeclProto <- (KEYWORD_const / KEYWORD_var) IDENTIFIER (COLON TypeExpr)? ByteA GlobalVarDecl <- VarDeclProto (EQUAL Expr)? SEMICOLON -ContainerField <- doc_comment? KEYWORD_comptime? !KEYWORD_fn (IDENTIFIER COLON)? TypeExpr ByteAlign? (EQUAL Expr)? +ContainerField <- doc_comment? KEYWORD_comptime? (IDENTIFIER COLON)? TypeExpr ByteAlign? (EQUAL Expr)? # *** Block Level *** BlockStatement diff --git a/lib/std/zig/parser_fuzz.zig b/lib/std/zig/parser_fuzz.zig index 550b03510683da942848c51358491465c0ecd0ab..024384306449b0083321e42bae7dce3f9e5a3b76 100644 --- a/lib/std/zig/parser_fuzz.zig +++ b/lib/std/zig/parser_fuzz.zig @@ -63,6 +63,11 @@ test "expression nesting" { try checkAgainstOracle("test{*comptime 0 == 0;}"); } +// Found using AFL++ +test "comptime fn" { + try checkAgainstOracle("comptime fn()0"); +} + fn checkAgainstOracle(source: [:0]const u8) !void { var fba_buf: [1 << 18]u8 = undefined; var fba: std.heap.FixedBufferAllocator = .init(&fba_buf); diff --git a/lib/std/zig/parser_generated_oracle.zig b/lib/std/zig/parser_generated_oracle.zig index 943c86cf288cfe7eb4d1c3eb17db1a5b1e29a2ba..ae0a81de132c97859bfe9f6edf1867545b456b6b 100644 --- a/lib/std/zig/parser_generated_oracle.zig +++ b/lib/std/zig/parser_generated_oracle.zig @@ -164,12 +164,7 @@ const Parser = struct { pub fn parseContainerField(p: *Parser) bool { return blk_0: { const pos_0 = p.i; - if ((p.parsedoc_comment() or true) and (p.parseKEYWORD_comptime() or true) and blk_1: { - const pos_1 = p.i; - const match_1 = p.parseKEYWORD_fn(); - p.i = pos_1; - break :blk_1 !match_1; - } and (blk_3: { + if ((p.parsedoc_comment() or true) and (p.parseKEYWORD_comptime() or true) and (blk_3: { const pos_3 = p.i; if (p.parseIDENTIFIER() and p.parseCOLON()) break :blk_3 true; p.i = pos_3; -- 2.54.0