Skip to content

Fix formatter moving an attribute off a type constraint - #9498

Merged
cknitt merged 1 commit into
masterfrom
fix-printer-constraint-attributes
Dec 2, 2026
Merged

cknitt merged 1 commit into
masterfrom
fix-printer-constraint-attributes

Conversation

@cknitt

@cknitt cknitt commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Fixes #9497.

The formatter changed what an attribute on a type constraint applies to:

let a = @warning("-102") ([1] == [2]: bool)

was printed as

let a = (@warning("-102") [1] == [2]: bool)

The printer printed the constraint's attributes in front of e: t, and the caller then put both in parentheses. Parsed again, the attribute sits on [1], the first operand of ==, instead of on the constraint.

The fix. A constraint with attributes now prints its own parentheses inside the attributes, as a coercion already does. Where a constraint or coercion only needs parentheses for itself, such as a let value or an argument, Res_parens no longer adds a second pair around the attributes. So the example stays let a = @warning("-102") ([1] == [2]: bool), and a coercion prints as @foo (v :> b) instead of (@foo (v :> b)). As an operand, the attribute itself still needs parentheses: (@foo (v: int)) + 1.

Tests. The existing printer test constraint.res had two inputs, @attr (10: int) and (@attr 10: int), that the old printer printed identically; they now print differently. New cases cover a constraint with an attribute as a let value, an argument, an operand and inside braces. The format check of the whole repository passes unchanged.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gH9cXWp3xtBmfRV3Du

@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix the formatter moving an attribute from a type constraint, such as @foo (x: int), to the first operand of the expression.

Semantic Diff

1 commit · Full page

✏️ Fix formatter moving an attribute off a type constraint

738c317 · cknitt + Claude · Dec 01, 2026, 19:56 UTC · edited

Why

The printer printed the attributes of a type constraint in front of the constraint e: t, and the caller then put both in parentheses. Parsed again, the attribute applies to the first operand of e, so formatting changed the program: @warning("-102") ([1] == [2]: bool) became (@warning("-102") [1] == [2]: bool), where the attribute is on [1].

A constraint with attributes now prints its own parentheses inside the attributes, as a coercion already does. Where a constraint or coercion only needs parentheses for itself, such as a let value or an argument, no second pair is added around the attributes.

Format modified (attribute on a type constraint or coercion)

let x = @attr (10: int)
let y = @attr (v :> b)

rescript

-let x = (@attr 10: int)
-let y = (@attr (v :> b))
+let x = @attr (10: int)
+let y = @attr (v :> b)

Fixes #9497

@cknitt

cknitt commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 1, 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-12-01T20:06:33.584611Z 738c317 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. Can't wait for the next one!

Reviewed commit: 719bc8d5d6

ℹ️ 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".

@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.50%. Comparing base (69e11bf) to head (738c317).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9498   +/-   ##
=======================================
  Coverage   82.49%   82.50%           
=======================================
  Files         467      467           
  Lines       66125    66127    +2     
=======================================
+ Hits        54553    54555    +2     
  Misses      11572    11572           
Files with missing lines Coverage Δ
syntax/printer/res_parens.ml 86.61% <100.00%> (+0.04%) ⬆️
syntax/printer/res_printer.ml 95.59% <100.00%> (+<0.01%) ⬆️
🚀 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.

Why:
    The printer printed the attributes of a type constraint in front of
    the constraint `e: t`, and the caller then put both in parentheses.
    Parsed again, the attribute applies to the first operand of `e`, so
    formatting changed the program: `@warning("-102") ([1] == [2]: bool)`
    became `(@warning("-102") [1] == [2]: bool)`, where the attribute
    is on `[1]`.

A constraint with attributes now prints its own parentheses inside the
attributes, as a coercion already does. Where a constraint or coercion
only needs parentheses for itself, such as a let value or an argument,
no second pair is added around the attributes.

Format modified (attribute on a type constraint or coercion):
Example:
    let x = @attr (10: int)
    let y = @attr (v :> b)
Before (rescript):
    let x = (@attr 10: int)
    let y = (@attr (v :> b))
After (rescript):
    let x = @attr (10: int)
    let y = @attr (v :> b)

Fixes #9497

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

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 655,166,095 → 655,165,489 (±0.00%).

Generated projects: 627,288,844 → 627,288,764 (±0.00%).

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

Instructions

In total: 9,128,411,299 → 9,128,442,720 (±0.00%).

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

Output size

In total: 27,368,788 → 27,368,926 (±0.00%).

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

Pull request 738c317 compared with base 69e11bf in this run.

@pkg-pr-new

pkg-pr-new Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 738c317

@cknitt
cknitt force-pushed the fix-printer-constraint-attributes branch from 719bc8d to 738c317 Compare December 1, 2026 20:04
@cknitt

cknitt commented Dec 1, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 738c317473

ℹ️ 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".

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown

@cknitt
cknitt requested a review from cristianoc December 1, 2026 20:48
@cknitt
cknitt merged commit ceb3fc0 into master Dec 2, 2026
23 checks passed
@cknitt
cknitt deleted the fix-printer-constraint-attributes branch December 2, 2026 06:10
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.

Formatter moves an attribute from a type constraint to the first operand

2 participants