diff --git a/changes/printer-constraint-attribute.fix.md b/changes/printer-constraint-attribute.fix.md new file mode 100644 index 0000000000..5e3333941d --- /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 c3134991f2..9e9f8a42f8 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 dbb3298719..58c5624315 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 0bf7e9a01c..eb57398907 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 8e428ce567..69dd1a24f0 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 e341e13915..1f582b67a7 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))}