Skip to content

Commit 0c3c09d

Browse files
authored
Merge pull request ocaml#13845 from ccasin/untypeast-pprintast-polytype
Fix bug in untypeast/pprintast for value bindings with polymorphic type annotations
2 parents dc6cbe7 + ca66d9e commit 0c3c09d

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

Changes

+4
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,10 @@ Working version
457457
the right-hand side of `_ as _`.
458458
(Samuel Vivien, review by Gabriel Scherer)
459459

460+
- #13845: Fix bug in untypeast/pprintast for value bindings with polymorphic
461+
type annotations.
462+
(Chris Casinghino, review by Florian Angeletti and Gabriel Scherer)
463+
460464
OCaml 5.3.0 (8 January 2025)
461465
----------------------------
462466

testsuite/tests/compiler-libs/test_untypeast.ml

+16
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,19 @@ run {| fun x y z -> (function w -> x y z w) |};;
3737
[%%expect{|
3838
- : string = "fun x y z -> (function | w -> x y z w)"
3939
|}];;
40+
41+
(***********************************)
42+
(* Untypeast/pprintast correctly handle value binding type annotations. *)
43+
44+
run {| let foo : 'a. 'a -> 'a = fun x -> x in foo |}
45+
46+
[%%expect{|
47+
- : string = "let foo : 'a . 'a -> 'a = fun x -> x in foo"
48+
|}];;
49+
50+
run {| let foo : type a . a -> a = fun x -> x in foo |}
51+
52+
[%%expect{|
53+
- : string =
54+
"let foo : 'a . 'a -> 'a = fun (type a) -> (fun x -> x : a -> a) in foo"
55+
|}]

typing/untypeast.ml

+11-3
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,17 @@ let case : type k . mapper -> k case -> _ = fun sub {c_lhs; c_guard; c_rhs} ->
387387
let value_binding sub vb =
388388
let loc = sub.location sub vb.vb_loc in
389389
let attrs = sub.attributes sub vb.vb_attributes in
390-
Vb.mk ~loc ~attrs
391-
(sub.pat sub vb.vb_pat)
392-
(sub.expr sub vb.vb_expr)
390+
let pat = sub.pat sub vb.vb_pat in
391+
let pat, value_constraint =
392+
match pat.ppat_desc with
393+
| Ppat_constraint (pat, ({ ptyp_desc = Ptyp_poly _; _ } as cty)) ->
394+
let constr =
395+
Pvc_constraint { locally_abstract_univars = []; typ = cty }
396+
in
397+
pat, Some constr
398+
| _ -> pat, None
399+
in
400+
Vb.mk ~loc ~attrs ?value_constraint pat (sub.expr sub vb.vb_expr)
393401

394402
let expression sub exp =
395403
let loc = sub.location sub exp.exp_loc in

0 commit comments

Comments
 (0)