Skip to content

Commit c808a09

Browse files
committed
Clarify coercion parenthesis contexts
Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent dc10686 commit c808a09

7 files changed

Lines changed: 33 additions & 32 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272

7373
#### :nail_care: Polish
7474

75-
- Omit unnecessary parentheses around coercions in call arguments, collection elements, and array indices while preserving expression grouping. https://github.com/rescript-lang/rescript/pull/8614
75+
- Omit unnecessary parentheses around coercions where the surrounding syntax already delimits the expression, while preserving required grouping. https://github.com/rescript-lang/rescript/pull/8614
7676
- Print external declarations in signatures and type errors with their processed attributes instead of the `"#rescript-external"` placeholder, and print inline constants using `@inline` syntax. https://github.com/rescript-lang/rescript/pull/8581
7777
- Improve diagnostics for dynamic imports of local values and attempts to use `import` as a first-class value. https://github.com/rescript-lang/rescript/pull/8582
7878
- Allow inferred labeled functions to be called with labels in any order by removing legacy curried-arrow commutation locks. https://github.com/rescript-lang/rescript/pull/8547

compiler/syntax/src/res_parens.ml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Parsetree_viewer = Res_parsetree_viewer
22
type kind = Parenthesized | Braced of Location.t | Nothing
33

4-
let expr ?(allow_coercion = false) expr =
4+
let expr_with_coercion_kind coercion_kind expr =
55
let opt_braces, _ = Parsetree_viewer.process_braces_attr expr in
66
match opt_braces with
77
| Some ({Location.loc = braces_loc}, _) -> Braced braces_loc
@@ -12,10 +12,13 @@ let expr ?(allow_coercion = false) expr =
1212
Pexp_constraint ({pexp_desc = Pexp_pack _}, {ptyp_desc = Ptyp_package _});
1313
} ->
1414
Nothing
15-
| {pexp_desc = Pexp_coerce _} when allow_coercion -> Nothing
16-
| {pexp_desc = Pexp_constraint _ | Pexp_coerce _} -> Parenthesized
15+
| {pexp_desc = Pexp_coerce _} -> coercion_kind
16+
| {pexp_desc = Pexp_constraint _} -> Parenthesized
1717
| _ -> Nothing)
1818

19+
let expr expr = expr_with_coercion_kind Parenthesized expr
20+
let expr_allowing_coercion expr = expr_with_coercion_kind Nothing expr
21+
1922
let expr_record_row_rhs ~optional e =
2023
let kind = expr e in
2124
match kind with

compiler/syntax/src/res_parens.mli

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
type kind = Parenthesized | Braced of Location.t | Nothing
22

3-
(* Set [allow_coercion] to [true] only in grammar positions accepting a trailing
4-
coercion without parentheses, such as call arguments and collection elements.
5-
Bindings and arrow bodies require parentheses. *)
6-
val expr : ?allow_coercion:bool -> Parsetree.expression -> kind
3+
val expr : Parsetree.expression -> kind
4+
5+
(* Unlike [expr], this does not request parentheses for a top-level coercion.
6+
Use only where the surrounding grammar delimits the expression, such as call
7+
arguments and collection elements. *)
8+
val expr_allowing_coercion : Parsetree.expression -> kind
79
val structure_expr : Parsetree.expression -> kind
810

911
val unary_expr_operand : Parsetree.expression -> kind

compiler/syntax/src/res_printer.ml

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,7 +1716,7 @@ and print_spread_dict_expr ~state parts (expr : Parsetree.expression) cmt_tbl =
17161716
in
17171717
let spread_doc =
17181718
let doc = print_expression ~state spread_expr cmt_tbl in
1719-
match Parens.expr ~allow_coercion:true spread_expr with
1719+
match Parens.expr_allowing_coercion spread_expr with
17201720
| Parens.Parenthesized -> add_parens doc
17211721
| Braced braces -> print_braces doc spread_expr braces
17221722
| Nothing -> doc
@@ -3044,7 +3044,7 @@ and print_expression_with_comments_and_parens ~state expr cmt_tbl =
30443044
and print_expression_args ~state (args : Parsetree.expression list) cmt_tbl =
30453045
let print_arg expr =
30463046
let doc = print_expression_with_comments ~state expr cmt_tbl in
3047-
match Parens.expr ~allow_coercion:true expr with
3047+
match Parens.expr_allowing_coercion expr with
30483048
| Parens.Parenthesized -> add_parens doc
30493049
| Braced braces -> print_braces doc expr braces
30503050
| Nothing -> doc
@@ -3270,7 +3270,7 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
32703270
Doc.line;
32713271
Doc.dotdotdot;
32723272
(let doc = print_expression_with_comments ~state expr cmt_tbl in
3273-
match Parens.expr ~allow_coercion:true expr with
3273+
match Parens.expr_allowing_coercion expr with
32743274
| Parens.Parenthesized -> add_parens doc
32753275
| Braced braces -> print_braces doc expr braces
32763276
| Nothing -> doc);
@@ -3292,7 +3292,7 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
32923292
let doc =
32933293
print_expression_with_comments ~state expr cmt_tbl
32943294
in
3295-
match Parens.expr ~allow_coercion:true expr with
3295+
match Parens.expr_allowing_coercion expr with
32963296
| Parens.Parenthesized -> add_parens doc
32973297
| Braced braces -> print_braces doc expr braces
32983298
| Nothing -> doc)
@@ -3324,7 +3324,7 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
33243324
let doc =
33253325
print_expression_with_comments ~state expr cmt_tbl
33263326
in
3327-
match Parens.expr ~allow_coercion:true expr with
3327+
match Parens.expr_allowing_coercion expr with
33283328
| Parens.Parenthesized -> add_parens doc
33293329
| Braced braces -> print_braces doc expr braces
33303330
| Nothing -> doc)
@@ -3353,7 +3353,7 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
33533353
let doc =
33543354
print_expression_with_comments ~state expr cmt_tbl
33553355
in
3356-
match Parens.expr ~allow_coercion:true expr with
3356+
match Parens.expr_allowing_coercion expr with
33573357
| Parens.Parenthesized -> add_parens doc
33583358
| Braced braces -> print_braces doc expr braces
33593359
| Nothing -> doc)
@@ -3387,7 +3387,7 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
33873387
Doc.concat
33883388
[
33893389
Doc.dotdotdot;
3390-
(match Parens.expr ~allow_coercion:true expr with
3390+
(match Parens.expr_allowing_coercion expr with
33913391
| Parens.Parenthesized -> add_parens doc
33923392
| Braced braces -> print_braces doc expr braces
33933393
| Nothing -> doc);
@@ -3737,14 +3737,10 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
37373737
print_cases ~state cases cmt_tbl;
37383738
]
37393739
| Pexp_coerce (expr, (), typ) ->
3740-
let doc_expr = print_expression_with_comments ~state expr cmt_tbl in
3741-
let doc_typ = print_typ_expr ~state typ cmt_tbl in
37423740
let doc_expr =
3743-
match Parens.expr expr with
3744-
| Parens.Parenthesized -> add_parens doc_expr
3745-
| Braced braces -> print_braces doc_expr expr braces
3746-
| Nothing -> doc_expr
3741+
print_expression_with_comments_and_parens ~state expr cmt_tbl
37473742
in
3743+
let doc_typ = print_typ_expr ~state typ cmt_tbl in
37483744
let doc = Doc.concat [doc_expr; Doc.text " :> "; doc_typ] in
37493745
(* Keep attributes on the coercion rather than its operand. *)
37503746
if Parsetree_viewer.has_printable_attributes e.pexp_attributes then
@@ -4250,7 +4246,7 @@ and print_array_spread_apply ~state sub_lists cmt_tbl =
42504246
(* Print expression without leading comments (they're already extracted) *)
42514247
let expr_doc =
42524248
let doc = print_expression ~state expr cmt_tbl in
4253-
match Parens.expr ~allow_coercion:true expr with
4249+
match Parens.expr_allowing_coercion expr with
42544250
| Parens.Parenthesized -> add_parens doc
42554251
| Braced braces -> print_braces doc expr braces
42564252
| Nothing -> doc
@@ -4282,7 +4278,7 @@ and print_array_spread_apply ~state sub_lists cmt_tbl =
42824278
(List.map
42834279
(fun expr ->
42844280
let doc = print_expression_with_comments ~state expr cmt_tbl in
4285-
match Parens.expr ~allow_coercion:true expr with
4281+
match Parens.expr_allowing_coercion expr with
42864282
| Parens.Parenthesized -> add_parens doc
42874283
| Braced braces -> print_braces doc expr braces
42884284
| Nothing -> doc)
@@ -4317,7 +4313,7 @@ and print_list_spread_apply ~state sub_lists cmt_tbl =
43174313
comma_before_spread;
43184314
Doc.dotdotdot;
43194315
(let doc = print_expression_with_comments ~state expr cmt_tbl in
4320-
match Parens.expr ~allow_coercion:true expr with
4316+
match Parens.expr_allowing_coercion expr with
43214317
| Parens.Parenthesized -> add_parens doc
43224318
| Braced braces -> print_braces doc expr braces
43234319
| Nothing -> doc);
@@ -4338,7 +4334,7 @@ and print_list_spread_apply ~state sub_lists cmt_tbl =
43384334
(List.map
43394335
(fun expr ->
43404336
let doc = print_expression_with_comments ~state expr cmt_tbl in
4341-
match Parens.expr ~allow_coercion:true expr with
4337+
match Parens.expr_allowing_coercion expr with
43424338
| Parens.Parenthesized -> add_parens doc
43434339
| Braced braces -> print_braces doc expr braces
43444340
| Nothing -> doc)
@@ -4438,7 +4434,7 @@ and print_pexp_apply ~state expr cmt_tbl =
44384434
let member =
44394435
let member_doc =
44404436
let doc = print_expression_with_comments ~state member_expr cmt_tbl in
4441-
match Parens.expr ~allow_coercion:true member_expr with
4437+
match Parens.expr_allowing_coercion member_expr with
44424438
| Parens.Parenthesized -> add_parens doc
44434439
| Braced braces -> print_braces doc member_expr braces
44444440
| Nothing -> doc
@@ -4485,7 +4481,7 @@ and print_pexp_apply ~state expr cmt_tbl =
44854481
let member =
44864482
let member_doc =
44874483
let doc = print_expression_with_comments ~state member_expr cmt_tbl in
4488-
match Parens.expr ~allow_coercion:true member_expr with
4484+
match Parens.expr_allowing_coercion member_expr with
44894485
| Parens.Parenthesized -> add_parens doc
44904486
| Braced braces -> print_braces doc member_expr braces
44914487
| Nothing -> doc
@@ -5138,7 +5134,7 @@ and print_arguments ~state ~partial
51385134
| [(Nolabel, arg)] when Parsetree_viewer.is_huggable_expression arg ->
51395135
let arg_doc =
51405136
let doc = print_expression_with_comments ~state arg cmt_tbl in
5141-
match Parens.expr ~allow_coercion:true arg with
5137+
match Parens.expr_allowing_coercion arg with
51425138
| Parens.Parenthesized -> add_parens doc
51435139
| Braced braces -> print_braces doc arg braces
51445140
| Nothing -> doc
@@ -5252,7 +5248,7 @@ and print_argument ~state (arg_lbl, arg) cmt_tbl =
52525248
in
52535249
let printed_expr =
52545250
let doc = print_expression_with_comments ~state expr cmt_tbl in
5255-
match Parens.expr ~allow_coercion:true expr with
5251+
match Parens.expr_allowing_coercion expr with
52565252
| Parenthesized -> add_parens doc
52575253
| Braced braces -> print_braces doc expr braces
52585254
| Nothing -> doc

packages/dev-playground/src/UrlState.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ let applyUrlState = (~encoded, ~config: PlaygroundConfig.t) => {
4242
params->UrlSearchParams.delete("sourceMapSourcesContent")
4343
params->UrlSearchParams.delete("sourceMapRoot")
4444
| sourceMapMode =>
45-
params->UrlSearchParams.set("sourceMap", (sourceMapMode :> string))
45+
params->UrlSearchParams.set("sourceMap", sourceMapMode :> string)
4646
params->UrlSearchParams.set(
4747
"sourceMapSourcesContent",
4848
config.sourceMapSourcesContent ? "true" : "false",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ let foo = (~a=(3:int:>int), b) => 34
2525

2626
let x = (/* c0 */ x /* c1 */ :> /* c2 */ int /* c3 */)
2727

28-
// Delimited arguments need no extra parentheses.
28+
// Delimited expression positions need no extra parentheses.
2929
foo(v :> b)
3030
foo((v :> b))
3131
foo(~arg=(v :> b), ~optional=?(v :> b))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ let foo = (~a=(3: int) :> int, b) => 34
2525

2626
let x = (/* c0 */ x /* c1 */ :> /* c2 */ int /* c3 */)
2727

28-
// Delimited arguments need no extra parentheses.
28+
// Delimited expression positions need no extra parentheses.
2929
foo(v :> b)
3030
foo(v :> b)
3131
foo(~arg=v :> b, ~optional=?v :> b)

0 commit comments

Comments
 (0)