Skip to content

Commit db88eda

Browse files
committed
Preserve coercion grouping in restricted expression contexts
Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent df3784c commit db88eda

3 files changed

Lines changed: 35 additions & 6 deletions

File tree

compiler/syntax/src/res_printer.ml

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2394,7 +2394,7 @@ and print_value_binding ~state ~rec_flag (vb : Parsetree.value_binding) cmt_tbl
23942394
print_typ_expr ~state pvc_type cmt_tbl;
23952395
Doc.text " =";
23962396
Doc.line;
2397-
print_expression_with_comments ~state expr cmt_tbl;
2397+
print_expression_with_comments_and_parens ~state expr cmt_tbl;
23982398
]);
23992399
])
24002400
| {
@@ -2438,7 +2438,8 @@ and print_value_binding ~state ~rec_flag (vb : Parsetree.value_binding) cmt_tbl
24382438
Doc.concat
24392439
[
24402440
Doc.line;
2441-
print_expression_with_comments ~state expr cmt_tbl;
2441+
print_expression_with_comments_and_parens ~state expr
2442+
cmt_tbl;
24422443
];
24432444
]);
24442445
])
@@ -2465,7 +2466,8 @@ and print_value_binding ~state ~rec_flag (vb : Parsetree.value_binding) cmt_tbl
24652466
Doc.concat
24662467
[
24672468
Doc.line;
2468-
print_expression_with_comments ~state expr cmt_tbl;
2469+
print_expression_with_comments_and_parens ~state expr
2470+
cmt_tbl;
24692471
];
24702472
]);
24712473
]))
@@ -3060,6 +3062,13 @@ and print_expression_with_comments ~state expr cmt_tbl : Doc.t =
30603062
let doc = print_expression ~state expr cmt_tbl in
30613063
print_comments doc cmt_tbl expr.Parsetree.pexp_loc
30623064

3065+
and print_expression_with_comments_and_parens ~state expr cmt_tbl =
3066+
let doc = print_expression_with_comments ~state expr cmt_tbl in
3067+
match Parens.expr expr with
3068+
| Parens.Parenthesized -> add_parens doc
3069+
| Braced braces -> print_braces doc expr braces
3070+
| Nothing -> doc
3071+
30633072
and print_if_chain ~state pexp_attributes ifs else_expr cmt_tbl =
30643073
let if_docs =
30653074
Doc.join ~sep:Doc.space
@@ -5014,7 +5023,7 @@ and print_jsx_prop ~state prop cmt_tbl =
50145023
[
50155024
Doc.lbrace;
50165025
Doc.dotdotdot;
5017-
print_expression_with_comments ~state value cmt_tbl;
5026+
print_expression_with_comments_and_parens ~state value cmt_tbl;
50185027
Doc.rbrace;
50195028
])
50205029
in
@@ -5418,7 +5427,7 @@ and print_case ~state (case : Parsetree.case) cmt_tbl =
54185427
[
54195428
Doc.line;
54205429
Doc.text "if ";
5421-
print_expression_with_comments ~state expr cmt_tbl;
5430+
print_expression_with_comments_and_parens ~state expr cmt_tbl;
54225431
])
54235432
in
54245433
let should_inline_rhs =
@@ -5977,7 +5986,7 @@ and print_payload ~state (payload : Parsetree.payload) cmt_tbl =
59775986
[
59785987
Doc.line;
59795988
Doc.text "if ";
5980-
print_expression_with_comments ~state expr cmt_tbl;
5989+
print_expression_with_comments_and_parens ~state expr cmt_tbl;
59815990
]
59825991
| None -> Doc.nil
59835992
in

tests/syntax_tests/data/printer/expr/coerce.res

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,13 @@ let beforeJsx = () => {
8888
let value = (x :> string)
8989
<option value />
9090
}
91+
92+
// These grammar positions don't parse a bare trailing coercion.
93+
let locallyAbstract: type a. a = (value :> a)
94+
let guarded = switch value {
95+
| _ if (flag :> bool) => value
96+
}
97+
let jsxSpread = <Component {...(props :> propsType)} />
98+
99+
@attr(?value if (flag :> bool))
100+
let attributeGuard = value

tests/syntax_tests/data/printer/expr/expected/coerce.res.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,13 @@ let beforeJsx = () => {
9898
let value = (x :> string)
9999
<option value />
100100
}
101+
102+
// These grammar positions don't parse a bare trailing coercion.
103+
let locallyAbstract: type a. a = (value :> a)
104+
let guarded = switch value {
105+
| _ if (flag :> bool) => value
106+
}
107+
let jsxSpread = <Component {...(props :> propsType)} />
108+
109+
@attr(? value if (flag :> bool))
110+
let attributeGuard = value

0 commit comments

Comments
 (0)