Skip to content

Commit 10902c1

Browse files
authored
Handle punned labelled arguments with type constraint in function applications (ocaml#10434) (#1756)
Update the parser with ocaml/ocaml#10434 Accept labelled argument punning with type constraint in pexp_apply For example, function application of the form ~(x:int) instead of the explicit ~x:(x:int).
1 parent bb738be commit 10902c1

File tree

5 files changed

+23
-0
lines changed

5 files changed

+23
-0
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222

2323
+ Handle merlin typed holes (#1698, @gpetiot)
2424

25+
+ Handle punned labelled arguments with type constraint in function applications.
26+
For example, function application of the form `foo ~(x:int)` instead of the explicit `foo ~x:(x:int)`. (ocaml#10434) (#1756, @gpetiot)
27+
2528
### 0.19.0 (2021-07-16)
2629

2730
#### Bug fixes

lib/Fmt_ast.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,16 @@ and fmt_label_arg ?(box = true) ?epi ?parens ?eol c
13501350
| (Labelled l | Optional l), Pexp_ident {txt= Lident i; loc}
13511351
when String.equal l i && List.is_empty arg.pexp_attributes ->
13521352
Cmts.fmt c loc @@ Cmts.fmt c ?eol arg.pexp_loc @@ fmt_label lbl ""
1353+
| ( (Labelled l | Optional l)
1354+
, Pexp_constraint ({pexp_desc= Pexp_ident {txt= Lident i; _}; _}, _) )
1355+
when String.equal l i && List.is_empty arg.pexp_attributes ->
1356+
let lbl =
1357+
match lbl with
1358+
| Labelled _ -> str "~"
1359+
| Optional _ -> str "?"
1360+
| Nolabel -> noop
1361+
in
1362+
lbl $ fmt_expression c ~box ?epi ?parens xarg
13531363
| _ -> fmt_label lbl ":@," $ fmt_expression c ~box ?epi ?parens xarg
13541364

13551365
and expression_width c xe =

test/passing/tests/apply.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,7 @@ let _ =
8383
(loooooooooooong looooooooooooooong loooooooooooooong
8484
[loooooooooong; loooooooooooong; loooooooooooooooooooooong]
8585
)
86+
87+
let _ =
88+
let f ~y = y + 1 in
89+
f ~(y : int)

vendor/ocaml-4.13/parser.mly

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2474,6 +2474,9 @@ labeled_simple_expr:
24742474
| TILDE label = LIDENT
24752475
{ let loc = $loc(label) in
24762476
(Labelled label, mkexpvar ~loc label) }
2477+
| TILDE LPAREN label = LIDENT ty = type_constraint RPAREN
2478+
{ (Labelled label, mkexp_constraint ~loc:($startpos($2), $endpos)
2479+
(mkexpvar ~loc:$loc(label) label) ty) }
24772480
| QUESTION label = LIDENT
24782481
{ let loc = $loc(label) in
24792482
(Optional label, mkexpvar ~loc label) }

vendor/parse-wyc/lib/parser.mly

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2464,6 +2464,9 @@ labeled_simple_expr:
24642464
| TILDE label = LIDENT
24652465
{ let loc = $loc(label) in
24662466
(Labelled label, mkexpvar ~loc label) }
2467+
| TILDE LPAREN label = LIDENT ty = type_constraint RPAREN
2468+
{ (Labelled label, mkexp_constraint ~loc:($startpos($2), $endpos)
2469+
(mkexpvar ~loc:$loc(label) label) ty) }
24672470
| QUESTION label = LIDENT
24682471
{ let loc = $loc(label) in
24692472
(Optional label, mkexpvar ~loc label) }

0 commit comments

Comments
 (0)