@@ -2554,54 +2554,63 @@ and over_parse_constrained_or_coerced_or_arrow_expression p expr =
2554
2554
| EqualGreater ->
2555
2555
Parser. next p;
2556
2556
let body = parse_expr p in
2557
- let pat =
2557
+ let pat, expr_is_unit =
2558
2558
match expr.pexp_desc with
2559
2559
| Pexp_ident longident ->
2560
- Ast_helper.Pat. var ~loc: expr.pexp_loc
2561
- (Location. mkloc
2562
- (Longident. flatten longident.txt |> String. concat " ." )
2563
- longident.loc)
2560
+ ( Ast_helper.Pat. var ~loc: expr.pexp_loc
2561
+ (Location. mkloc
2562
+ (Longident. flatten longident.txt |> String. concat " ." )
2563
+ longident.loc)
2564
+ , false )
2565
+ | Pexp_construct ({txt = Longident. Lident "()" } as lid , None) ->
2566
+ (Ast_helper.Pat. construct ~loc: expr.pexp_loc lid None , true )
2564
2567
(* TODO: can we convert more expressions to patterns?*)
2565
2568
| _ ->
2566
- Ast_helper.Pat. var ~loc: expr.pexp_loc
2567
- (Location. mkloc " pattern" expr.pexp_loc)
2569
+ ( Ast_helper.Pat. var ~loc: expr.pexp_loc
2570
+ (Location. mkloc " pattern" expr.pexp_loc)
2571
+ , false )
2568
2572
in
2569
2573
let arrow1 =
2570
2574
Ast_helper.Exp. fun_
2571
2575
~loc: (mk_loc expr.pexp_loc.loc_start body.pexp_loc.loc_end)
2572
2576
~arity: None Asttypes. Nolabel None pat
2573
2577
(Ast_helper.Exp. constraint_ body typ)
2574
2578
in
2575
- let arrow2 =
2576
- Ast_helper.Exp. fun_
2577
- ~loc: (mk_loc expr.pexp_loc.loc_start body.pexp_loc.loc_end)
2578
- ~arity: None Asttypes. Nolabel None
2579
- (Ast_helper.Pat. constraint_ pat typ)
2580
- body
2581
- in
2582
- let msg =
2583
- Doc. breakable_group ~force_break: true
2584
- (Doc. concat
2585
- [
2586
- Doc. text
2587
- " Did you mean to annotate the parameter type or the return \
2588
- type?" ;
2589
- Doc. indent
2590
- (Doc. concat
2591
- [
2592
- Doc. line;
2593
- Doc. text " 1) " ;
2594
- ResPrinter. print_expression arrow1 CommentTable. empty;
2595
- Doc. line;
2596
- Doc. text " 2) " ;
2597
- ResPrinter. print_expression arrow2 CommentTable. empty;
2598
- ]);
2599
- ])
2600
- |> Doc. to_string ~width: 80
2601
- in
2602
- Parser. err ~start_pos: expr.pexp_loc.loc_start
2603
- ~end_pos: body.pexp_loc.loc_end p (Diagnostics. message msg);
2604
- arrow1
2579
+ (* When the "expr" was `()`, the colon must apply to the return type, so
2580
+ skip the ambiguity diagnostic and keep the parameter as unit. *)
2581
+ if expr_is_unit then
2582
+ arrow1
2583
+ else (
2584
+ let arrow2 =
2585
+ Ast_helper.Exp. fun_
2586
+ ~loc: (mk_loc expr.pexp_loc.loc_start body.pexp_loc.loc_end)
2587
+ ~arity: None Asttypes. Nolabel None
2588
+ (Ast_helper.Pat. constraint_ pat typ)
2589
+ body
2590
+ in
2591
+ let msg =
2592
+ Doc. breakable_group ~force_break: true
2593
+ (Doc. concat
2594
+ [
2595
+ Doc. text
2596
+ " Did you mean to annotate the parameter type or the return \
2597
+ type?" ;
2598
+ Doc. indent
2599
+ (Doc. concat
2600
+ [
2601
+ Doc. line;
2602
+ Doc. text " 1) " ;
2603
+ ResPrinter. print_expression arrow1 CommentTable. empty;
2604
+ Doc. line;
2605
+ Doc. text " 2) " ;
2606
+ ResPrinter. print_expression arrow2 CommentTable. empty;
2607
+ ]);
2608
+ ])
2609
+ |> Doc. to_string ~width: 80
2610
+ in
2611
+ Parser. err ~start_pos: expr.pexp_loc.loc_start
2612
+ ~end_pos: body.pexp_loc.loc_end p (Diagnostics. message msg);
2613
+ arrow1)
2605
2614
| _ ->
2606
2615
let loc = mk_loc expr.pexp_loc.loc_start typ.ptyp_loc.loc_end in
2607
2616
let expr = Ast_helper.Exp. constraint_ ~loc expr typ in
0 commit comments