From 738c317473f7c54c688da56c9887f9dbb9cccc71 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Tue, 1 Dec 2026 19:56:26 +0000 Subject: [PATCH] Fix formatter moving an attribute off a type constraint 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 Claude-Session: https://claude.ai/code/session_01Kbf3gH9cXWp3xtBmfRV3Du Signed-off-by: Christoph Knittel --- changes/printer-constraint-attribute.fix.md | 1 + syntax/printer/res_parens.ml | 5 +++++ syntax/printer/res_printer.ml | 6 +++++- tests/syntax_tests/data/printer/expr/constraint.res | 5 +++++ .../syntax_tests/data/printer/expr/expected/coerce.res.txt | 2 +- .../data/printer/expr/expected/constraint.res.txt | 7 ++++++- 6 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 changes/printer-constraint-attribute.fix.md diff --git a/changes/printer-constraint-attribute.fix.md b/changes/printer-constraint-attribute.fix.md new file mode 100644 index 00000000000..5e3333941d5 --- /dev/null +++ b/changes/printer-constraint-attribute.fix.md @@ -0,0 +1 @@ +- Fix the formatter moving an attribute from a type constraint, such as `@foo (x: int)`, to the first operand of the expression. diff --git a/syntax/printer/res_parens.ml b/syntax/printer/res_parens.ml index c3134991f2b..9e9f8a42f8f 100644 --- a/syntax/printer/res_parens.ml +++ b/syntax/printer/res_parens.ml @@ -12,6 +12,11 @@ let expr_with_coercion_kind coercion_kind expr = Pexp_constraint ({pexp_desc = Pexp_pack _}, {ptyp_desc = Ptyp_package _}); } -> Nothing + (* With attributes, the printer puts the constraint or coercion in + parentheses itself, inside the attributes. *) + | {pexp_desc = Pexp_constraint _ | Pexp_coerce _; pexp_attributes = _ :: _} + -> + Nothing | {pexp_desc = Pexp_coerce _} -> coercion_kind | {pexp_desc = Pexp_constraint _} -> Parenthesized | _ -> Nothing) diff --git a/syntax/printer/res_printer.ml b/syntax/printer/res_printer.ml index dbb32987192..58c56243156 100644 --- a/syntax/printer/res_printer.ml +++ b/syntax/printer/res_printer.ml @@ -3771,7 +3771,11 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl = | Braced braces -> print_braces doc expr braces | Nothing -> doc in - Doc.concat [expr_doc; Doc.text ": "; print_typ_expr ~state typ cmt_tbl] + let doc = + Doc.concat [expr_doc; Doc.text ": "; print_typ_expr ~state typ cmt_tbl] + in + (* Keep attributes on the constraint rather than its operand. *) + if e.pexp_attributes <> [] then add_parens doc else doc | Pexp_letmodule ({txt = _modName}, _modExpr, _expr) -> print_expression_block ~state ~braces:true e cmt_tbl | Pexp_letexception (_extensionConstructor, _expr) -> diff --git a/tests/syntax_tests/data/printer/expr/constraint.res b/tests/syntax_tests/data/printer/expr/constraint.res index 0bf7e9a01c9..eb573989070 100644 --- a/tests/syntax_tests/data/printer/expr/constraint.res +++ b/tests/syntax_tests/data/printer/expr/constraint.res @@ -7,3 +7,8 @@ let x = (street["number"]: int) let x = (streets[0]: int) let x = (true ? 1 : 0: int) + +let constraintAttribute = @foo (v: b) +let constraintAttributeArgument = f(@foo (v: b)) +let constraintAttributeOperand = @foo (v: int) + 1 +let constraintAttributeBraced = @foo {(v: b)} diff --git a/tests/syntax_tests/data/printer/expr/expected/coerce.res.txt b/tests/syntax_tests/data/printer/expr/expected/coerce.res.txt index 8e428ce567c..69dd1a24f0c 100644 --- a/tests/syntax_tests/data/printer/expr/expected/coerce.res.txt +++ b/tests/syntax_tests/data/printer/expr/expected/coerce.res.txt @@ -60,7 +60,7 @@ let jsx = b}>{v :> b} foo(/* before */ v /* operand */ :> /* type */ b /* after */) foo(v :> b) // trailing line comment let attributed = (@foo v :> b) -let coercionAttribute = (@foo (v :> b)) +let coercionAttribute = @foo (v :> b) let long = functionWithAVeryLongName(valueWithAVeryLongName :> typeWithAVeryLongName) let arrayIndex = values[index :> int] diff --git a/tests/syntax_tests/data/printer/expr/expected/constraint.res.txt b/tests/syntax_tests/data/printer/expr/expected/constraint.res.txt index e341e13915c..1f582b67a7a 100644 --- a/tests/syntax_tests/data/printer/expr/expected/constraint.res.txt +++ b/tests/syntax_tests/data/printer/expr/expected/constraint.res.txt @@ -1,9 +1,14 @@ let x = (Numbers.ten: int) -let x = (@attr 10: int) +let x = @attr (10: int) let x = (@attr 10: int) let x = (street["number"]: int) let x = (streets[0]: int) let x = (true ? 1 : 0: int) + +let constraintAttribute = @foo (v: b) +let constraintAttributeArgument = f(@foo (v: b)) +let constraintAttributeOperand = (@foo (v: int)) + 1 +let constraintAttributeBraced = {(@foo (v: b))}