Skip to content

Fix parser crash on invalid @as escapes - #9359

Merged
cknitt merged 1 commit into
masterfrom
ast-payload-to-ml
Nov 21, 2026
Merged

cknitt merged 1 commit into
masterfrom
ast-payload-to-ml

Conversation

@cknitt

@cknitt cknitt commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Fixes #9351.

An invalid escape in an @as template string, such as type t = {@as(`\xZZ`) x: int}, made the parser throw Location.Error instead of reporting a syntax error. res_parser and the editor analysis (diagnosticSyntax, documentSymbol) crashed; bsc reported the error only because its top level catches the exception.

The parser builds fields and constructors with Ast_helper, which read @as with Ast_payload, and Ast_payload raises for an invalid escape. Ast_helper now has its own payload readers, Ast_helper.Payload, which never raise. An @as it cannot read stays in the attributes, and the parser's own diagnostic reports the escape:

  Syntax error!
  t.res:1:16-19

  1 │ type t = {@as(`\xZZ`) x: int}

  Invalid string escape sequence

bsc reports the same message, now as a syntax error, as it already did when the invalid @as came second.

This also takes Ast_payload out of the AST library: it moves to compiler/ml and reuses Ast_helper.Payload for everything except its raising readers. The parser uses PStr [] instead of Ast_payload.empty.

ASTs from PPXes still get an error, from the compiler: for a field, the leftover @as is read with the raising reader in Bs_ast_invariant and Record_runtime, as before. For a constructor, the leftover check now decodes the payload first, so it reports the invalid escape instead of the generic invalid-annotation error.

Tests: a syntax test with invalid escapes on fields, constructors, a duplicate @as and an inline record (tests/syntax_tests/data/parsing/errors/typeDef/asInvalidEscape.res), which crashes on master; unit tests that Ast_helper.Type.field and Type.constructor keep an @as they cannot read, and that the compiler then reports the escape for both (Ast_untagged_variants.process_constructor_tag, Record_runtime.extra_as_attribute).

🤖 Generated with Claude Code

https://claude.ai/code/session_01QchYQ7itFLVLUmZ368okjH

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Report an invalid escape in an @as template string as a syntax error, so the editor's syntax diagnostics and outline no longer fail on such a file.

Semantic Diff

1 commit · Full page

Fix parser crash on invalid @as escapes

ec3592f · cknitt + Claude · Nov 20, 2026, 21:17 UTC

Ast_helper read @as payloads with Ast_payload, which raises Location.Error for an invalid escape in a template string. The parser builds fields and constructors with Ast_helper, so the exception escaped from the parser, and res_parser and the editor analysis crashed. Ast_helper now has its own payload readers, which never raise, and leaves such an @as in place for the parser's own diagnostic. This also moves Ast_payload out of the AST library into compiler/ml. Fixes #9351.

Diagnostic modified (invalid @as escape)

type t = {@as(`\xZZ`) x: int}

diagnostic · res_parser -recover

-Fatal error: exception Location.Error(_)
+Syntax error!
+t.res:1:16-19
+Invalid string escape sequence

Review

  • Does an @as from a PPX with an invalid escape still get an error? Yes. For a field, Bs_ast_invariant and Record_runtime read the leftover @as with the raising reader. For a constructor, the leftover check now decodes the payload first, so it reports the escape instead of the generic invalid-annotation error.

@cknitt
cknitt added this pull request to stack #9361 November 20, 2026 21:22
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 636,567,697 → 636,561,278 (±0.00%).

Generated projects: 629,048,391 → 629,050,446 (±0.00%).

None of the 265 benchmarks changed by 0.1% or more.

Instructions

In total: 9,225,369,174 → 9,224,520,504 (-0.01%).

Benchmark Base Head Change
packages/@rescript/runtime/Stdlib_Math.resi 21,538,752 21,505,322 -0.16%
packages/@rescript/runtime/Stdlib_List.resi 24,763,195 24,726,432 -0.15%
packages/@rescript/runtime/Stdlib_Temporal_PlainDateTime.res 25,766,542 25,731,090 -0.14%
parse/RedBlackTreeNoComments.res 14,618,246 14,599,664 -0.13%
packages/@rescript/runtime/Stdlib_Temporal_PlainDate.res 24,317,311 24,290,713 -0.11%
packages/@rescript/runtime/Stdlib_Date.resi 34,523,064 34,485,310 -0.11%
parse/RedBlackTree.res 15,020,610 15,004,282 -0.11%
packages/@rescript/runtime/Stdlib_Temporal_ZonedDateTime.res 31,446,027 31,414,135 -0.10%
packages/@rescript/runtime/Stdlib_String.resi 29,692,377 29,662,304 -0.10%

Output size

In total: 25,969,863 → 25,969,643 (±0.00%).

None of the 13 benchmarks changed by 0.1% or more.

Pull request ec3592f compared with base 8907cdd in this run.

@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.20253% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.19%. Comparing base (a826170) to head (ec3592f).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
compiler/ast/ast_helper.ml 93.10% 2 Missing ⚠️
tests/ounit_tests/ounit_ast_helper_tests.ml 97.56% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9359      +/-   ##
==========================================
+ Coverage   82.17%   82.19%   +0.01%     
==========================================
  Files         458      459       +1     
  Lines       65678    65739      +61     
==========================================
+ Hits        53971    54031      +60     
- Misses      11707    11708       +1     
Files with missing lines Coverage Δ
compiler/ml/ast_payload.ml 71.42% <100.00%> (ø)
compiler/ml/ast_untagged_variants.ml 88.16% <100.00%> (+0.14%) ⬆️
compiler/syntax/src/res_core.ml 92.07% <ø> (ø)
tests/ounit_tests/ounit_tests_main.ml 100.00% <ø> (ø)
tests/ounit_tests/ounit_ast_helper_tests.ml 97.56% <97.56%> (ø)
compiler/ast/ast_helper.ml 96.05% <93.10%> (+0.41%) ⬆️

... and 8 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

pkg-pr-new Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9359

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9359

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9359

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9359

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9359

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9359

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9359

commit: ec3592f

@github-actions

Copy link
Copy Markdown

@cknitt

cknitt commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-21T07:55:14.990143Z ec3592f Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 6684d80d5b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Ast_helper read @as payloads with Ast_payload, which raises Location.Error
for an invalid escape in a template string. The parser builds fields and
constructors with Ast_helper, so the exception escaped from the parser, and
res_parser and the editor analysis crashed. Ast_helper now has its own
payload readers, which never raise, and leaves such an @as in place for the
parser's own diagnostic. This also moves Ast_payload out of the AST library
into compiler/ml. Fixes #9351.

Diagnostic modified (invalid @as escape):
Example:
    type t = {@as(`\xZZ`) x: int}
Before (diagnostic): res_parser -recover
    Fatal error: exception Location.Error(_)
After (diagnostic): res_parser -recover
    Syntax error!
    t.res:1:16-19
    Invalid string escape sequence

Review: Does an @as from a PPX with an invalid escape still get an error?
    Yes. For a field, Bs_ast_invariant and Record_runtime read the leftover
    @as with the raising reader. For a constructor, the leftover check now
    decodes the payload first, so it reports the escape instead of the
    generic invalid-annotation error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QchYQ7itFLVLUmZ368okjH
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt

cknitt commented Nov 21, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: ec3592fd87

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@cknitt
cknitt marked this pull request as ready for review November 21, 2026 07:56
@cknitt
cknitt merged commit e51f0af into master Nov 21, 2026
22 checks passed
@cknitt
cknitt deleted the ast-payload-to-ml branch November 21, 2026 10:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invalid escape in an @as template string crashes the parser

2 participants