Skip to content

Commit 0a7c220

Browse files
committed
simplify poly vs. modes
1 parent 22bac57 commit 0a7c220

File tree

11 files changed

+77
-50
lines changed

11 files changed

+77
-50
lines changed

otherlibs/stdlib_alpha/capsule.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,8 @@ module Mutex = struct
584584
exception Poisoned
585585

586586
let[@inline] with_lock :
587-
(type k.
588-
k t
587+
type k.
588+
(k t
589589
-> (k Password.t @ local -> 'a) @ local
590590
-> 'a)
591591
@ portable
@@ -646,8 +646,8 @@ module Rwlock = struct
646646
exception Poisoned
647647

648648
let[@inline] with_write_lock :
649-
(type k.
650-
k t
649+
type k.
650+
(k t
651651
-> (k Password.t @ local -> 'a) @ local
652652
-> 'a)
653653
@ portable
@@ -678,8 +678,8 @@ module Rwlock = struct
678678
raise exn
679679

680680
let[@inline] with_read_lock :
681-
(type k.
682-
k t
681+
type k.
682+
(k t
683683
-> (k Password.Shared.t @ local unyielding -> 'a) @ local
684684
-> 'a)
685685
@ portable

parsing/parser.mly

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3175,8 +3175,8 @@ let_binding_body_no_punning:
31753175
(v, $5, Some (Pvc_constraint { locally_abstract_univars = []; typ }),
31763176
modes)
31773177
}
3178-
| let_ident_with_modes COLON TYPE ntys = newtypes DOT cty = core_type modes1 = empty_list EQUAL e = seq_expr
3179-
| let_ident_with_modes COLON LPAREN TYPE ntys = newtypes DOT cty = core_type RPAREN modes1=at_mode_expr EQUAL e = seq_expr
3178+
| let_ident_with_modes COLON TYPE ntys = newtypes DOT cty = core_type modes1 = empty_list EQUAL e = seq_expr
3179+
| let_ident_with_modes COLON TYPE ntys = newtypes DOT cty = tuple_type modes1=at_mode_expr EQUAL e = seq_expr
31803180
(* The code upstream looks like:
31813181
{[
31823182
let constraint' =
@@ -4301,9 +4301,13 @@ possibly_poly(X):
43014301
{ $1 }
43024302
;
43034303

4304+
%inline strictly_poly_tuple_type:
4305+
strictly_poly(tuple_type)
4306+
{ $1 }
4307+
43044308
%inline poly_tuple_type:
43054309
| tuple_type { $1 }
4306-
| LPAREN strictly_poly_type RPAREN { $2 }
4310+
| strictly_poly_tuple_type { $1 }
43074311
;
43084312

43094313
%inline poly_type_with_modes:
@@ -4317,7 +4321,7 @@ possibly_poly(X):
43174321

43184322
%inline strictly_poly_type_with_optional_modes:
43194323
| strictly_poly_type { $1, [] }
4320-
| LPAREN strictly_poly_type RPAREN at_mode_expr { $2, $4 }
4324+
| strictly_poly_tuple_type at_mode_expr { $1, $2 }
43214325
;
43224326

43234327
%inline poly_type_no_attr:

parsing/pprintast.ml

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,22 @@ and core_type1 ctxt f x =
611611
| (Ptyp_arrow _ | Ptyp_alias _ | Ptyp_poly _) ->
612612
paren true (core_type ctxt) f x
613613
614+
and core_type2 ctxt f x =
615+
if x.ptyp_attributes <> [] then core_type ctxt f x
616+
else
617+
match x.ptyp_desc with
618+
| Ptyp_poly (sl, ct) ->
619+
pp f "@[<2>%a%a@]"
620+
(fun f l -> match l with
621+
| [] -> ()
622+
| _ ->
623+
pp f "%a@;.@;"
624+
(list
625+
(tyvar_loc_jkind tyvar) ~sep:"@;")
626+
l)
627+
sl (core_type1 ctxt) ct
628+
| _ -> core_type1 ctxt f x
629+
614630
and tyvar_option f = function
615631
| None -> pp f "_"
616632
| Some name -> tyvar f name
@@ -637,7 +653,7 @@ and return_type ctxt f (x, m) =
637653
and core_type_with_optional_modes ctxt f (ty, modes) =
638654
match modes with
639655
| [] -> core_type ctxt f ty
640-
| _ :: _ -> pp f "%a%a" (core_type1 ctxt) ty optional_at_modes modes
656+
| _ :: _ -> pp f "%a%a" (core_type2 ctxt) ty optional_at_modes modes
641657
642658
(********************pattern********************)
643659
(* be cautious when use [pattern], [pattern1] is preferred *)
@@ -1699,15 +1715,16 @@ and pp_print_params_then_equals ctxt f x =
16991715
~delimiter:"="
17001716
| _ -> pp_print_pexp_newtype ctxt "=" f x
17011717
1702-
and poly_type ctxt f (vars, typ) =
1718+
and poly_type ctxt core_type f (vars, typ) =
17031719
pp f "type@;%a.@;%a"
17041720
(list ~sep:"@;" (tyvar_loc_jkind pp_print_string)) vars
17051721
(core_type ctxt) typ
17061722
17071723
and poly_type_with_optional_modes ctxt f (vars, typ, modes) =
17081724
match modes with
1709-
| [] -> poly_type ctxt f (vars, typ)
1710-
| _ :: _ -> pp f "(%a)%a" (poly_type ctxt) (vars, typ) optional_at_modes modes
1725+
| [] -> poly_type ctxt core_type f (vars, typ)
1726+
| _ :: _ -> pp f "%a%a" (poly_type ctxt core_type1) (vars, typ)
1727+
optional_at_modes modes
17111728
17121729
(* transform [f = fun g h -> ..] to [f g h = ... ] could be improved *)
17131730
and binding ctxt f {pvb_pat=p; pvb_expr=x; pvb_constraint = ct; pvb_modes = modes; _} =

testsuite/tests/capsule-api/data.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ let mk_ref : ('a -> 'a myref) @ portable = fun v -> {v}
1616
(* We need ['a] to be [portable] to return a [portable] value from the read.
1717
The return value is marked as [contended] because our callsites require that,
1818
but the typechecker does not implicitly downcast the result. *)
19-
let read_ref : (('a : value mod portable) .
20-
('a myref -> 'a @ portable contended)) @ portable = fun r -> r.v
19+
let read_ref : ('a : value mod portable) .
20+
('a myref -> 'a @ portable contended) @ portable = fun r -> r.v
2121

2222
(* We need ['a] to be [portable] and [contended] to capture it in
2323
a [portable] closure like this.*)
@@ -177,4 +177,3 @@ let ptr' : (int, lost_capsule) Capsule.Data.t =
177177
let () =
178178
assert (Capsule.Data.project ptr' = 111)
179179
;;
180-

testsuite/tests/capsule-api/rwlock_capsule.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ let with_read_guarded x (f : 'k . 'k Capsule.Password.Shared.t @ local -> ('a, '
8383
;;
8484

8585
(* reading from myref with the expected modes *)
86-
let read_ref : (('a : value mod portable) .
87-
('a myref @ shared -> 'a @ portable contended)) @ portable = fun r -> r.v
86+
let read_ref : ('a : value mod portable) .
87+
('a myref @ shared -> 'a @ portable contended) @ portable = fun r -> r.v
8888

8989
(* writing to myref with the expected modes *)
9090
let write_ref : ('a : value mod portable contended) .

testsuite/tests/capsule-api/shared.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ external reraise : exn -> 'a @ portable = "%reraise"
1818
type 'a myref = { mutable v : 'a }
1919

2020
let mk_ref : ('a -> 'a myref) @ portable = fun v -> {v}
21-
let read_ref : (('a : value mod portable) .
22-
('a myref -> 'a @ portable contended)) @ portable = fun r -> r.v
21+
let read_ref : ('a : value mod portable) .
22+
('a myref -> 'a @ portable contended) @ portable = fun r -> r.v
2323
let write_ref : ('a : value mod portable contended) .
2424
'a -> ('a myref -> unit) @ portable = fun v r -> r.v <- v
2525

testsuite/tests/compiler-libs/test_untypeast.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ run {| let foo : ('a -> 'a) @ portable = fun x -> x in foo |}
8686
"let (foo : 'a -> 'a) = ((fun x -> x : 'a -> 'a) : _ @ portable) in foo"
8787
|}];;
8888

89-
run {| let foo : ('a . 'a -> 'a) @ portable = fun x -> x in foo |}
89+
run {| let foo : 'a . ('a -> 'a) @ portable = fun x -> x in foo |}
9090

9191
[%%expect{|
9292
- : string = "let foo : ('a : value) . 'a -> 'a = fun x -> x in foo"

testsuite/tests/parsetree/source_jane_street.ml

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -335,44 +335,49 @@ let f ?(local_ x = 42)
335335
?(local_ x @ once portable = 42)
336336
?(local_ x : int @ once portable = 42)
337337
?(local_ x : ('a -> 'a) @ once portable = fun x -> x)
338-
?(local_ x : (('a : any) 'b . 'a) @ once portable = assert false)
339-
?(local_ x : (('a : any) 'b . 'a -> 'b) @ once portable = assert false)
338+
?(local_ x : ('a : any) 'b . 'a @ once portable = assert false)
339+
?(local_ x : ('a : any) 'b . 'a -> 'b @ once portable = assert false)
340+
?(local_ x : ('a : any) 'b . ('a -> 'b) @ once portable = assert false)
340341

341342
?x:(local_ (y, z) = 42)
342343
?x:(local_ (y, z) @ once portable = 42)
343344
?x:(local_ (y, z) : int @ once portable = 42)
344345
?x:(local_ (y, z) : ('a -> 'a) @ once portable = 42)
345-
?x:(local_ (y, z) : (('a : any) 'b . 'a) @ once portable = 42)
346-
?x:(local_ (y, z) : (('a : any) 'b . 'a -> 'b) @ once portable = 42)
346+
?x:(local_ (y, z) : ('a : any) 'b . 'a @ once portable = 42)
347+
?x:(local_ (y, z) : ('a : any) 'b . 'a -> 'b @ once portable = 42)
348+
?x:(local_ (y, z) : ('a : any) 'b . ('a -> 'b) @ once portable = 42)
347349

348350
~(local_ x)
349351
~(local_ x @ once portable)
350352
~(local_ x : int @ once portable)
351353
~(local_ x : ('a -> 'a) @ once portable)
352-
~(local_ x : (('a : any) 'b . 'a) @ once portable)
353-
~(local_ x : (('a : any) 'b . 'a -> 'b) @ once portable)
354+
~(local_ x : ('a : any) 'b . 'a @ once portable)
355+
~(local_ x : ('a : any) 'b . 'a -> 'b @ once portable)
356+
~(local_ x : ('a : any) 'b . ('a -> 'b) @ once portable)
354357

355358
~x:(local_ (y, z))
356359
~x:(local_ (y, z) @ once portable)
357360
~x:(local_ (y, z) : int @ once portable)
358361
~x:(local_ (y, z) : ('a -> 'a) @ once portable)
359-
~x:(local_ (y, z) : (('a : any) 'b . 'a) @ once portable)
360-
~x:(local_ (y, z) : (('a : any) 'b . 'a -> 'b) @ once portable)
362+
~x:(local_ (y, z) : ('a : any) 'b . 'a @ once portable)
363+
~x:(local_ (y, z) : ('a : any) 'b . ('a -> 'b) @ once portable)
364+
~x:(local_ (y, z) : ('a : any) 'b . 'a -> 'b @ once portable)
361365

362366
(local_ (y, z))
363367
(local_ (y, z) @ once portable)
364368
(local_ (y, z) : int @ once portable)
365369
(local_ (y, z) : ('a -> 'a) @ once portable)
366-
(local_ (y, z) : (('a : any) 'b . 'a) @ once portable)
367-
(local_ (y, z) : (('a : any) 'b . 'a -> 'b) @ once portable)
370+
(local_ (y, z) : ('a : any) 'b . 'a @ once portable)
371+
(local_ (y, z) : ('a : any) 'b . ('a -> 'b) @ once portable)
372+
(local_ (y, z) : ('a : any) 'b . 'a -> 'b @ once portable)
368373

369374
= ();;
370375

371376
(* This file is only about syntax - type error is fine *)
372377
[%%expect{|
373-
Line 5, characters 8-55:
374-
5 | ?(local_ x : (('a : any) 'b . 'a) @ once portable = assert false)
375-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
378+
Line 5, characters 8-53:
379+
5 | ?(local_ x : ('a : any) 'b . 'a @ once portable = assert false)
380+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
376381
Error: Optional parameters cannot be polymorphic
377382
|}]
378383

@@ -392,11 +397,13 @@ let g () =
392397
let local_ a : int @ once portable = 42 in
393398
let local_ a : (int -> int) @ once portable = 42 in
394399

395-
let local_ a : (('a : any) 'b. 'a) @ once portable = 42 in
396-
let local_ a : (('a : any) 'b. 'a -> 'a) @ once portable = 42 in
400+
let local_ a : ('a : any) 'b. 'a @ once portable = 42 in
401+
let local_ a : ('a : any) 'b. 'a -> 'a @ once portable = 42 in
402+
let local_ a : ('a : any) 'b. ('a -> 'a) @ once portable = 42 in
397403

398-
let a : (type (a : any) b. int) @ once portable = 42 in
399-
let a : (type (a : any) b. a -> b) @ once portable = 42 in
404+
let a : type (a : any) b. int @ once portable = 42 in
405+
let a : type (a : any) b. a -> b @ once portable = 42 in
406+
let a : type (a : any) b. (a -> b) @ once portable = 42 in
400407

401408
let (a, b) @ once portable = 42 in
402409
let (a, b) : int @ once portable = 42 in
@@ -503,8 +510,8 @@ type typvar_fn = a:('a. 'a) @ local unique portable contended -> unit
503510
let f ~(x1 @ many)
504511
~(x2 : string @ local)
505512
~(x3 : (string -> string) @ local)
506-
~(x4 : ('a. 'a -> 'a) @ local)
507-
~(x9 : ('a. 'a) @ local)
513+
~(x4 : 'a. ('a -> 'a) @ local)
514+
~(x9 : 'a. 'a @ local)
508515
~(local_ x5)
509516
~x6:(local_ true | false @ many)
510517
~x7:(local_ true | false : bool @ many)

testsuite/tests/typing-modes/modes.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ Line 1, characters 4-33:
1919
Error: This value escapes its region.
2020
|}]
2121

22-
let local_ foo : ('a. 'a -> 'a) @ unique = fun x -> x
22+
let local_ foo : 'a. ('a -> 'a) @ unique = fun x -> x
2323
[%%expect{|
2424
Line 1, characters 4-53:
25-
1 | let local_ foo : ('a. 'a -> 'a) @ unique = fun x -> x
25+
1 | let local_ foo : 'a. ('a -> 'a) @ unique = fun x -> x
2626
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2727
Error: This value escapes its region.
2828
|}]
2929

30-
let foo : (type a. a -> a) @ unique = fun x -> x
30+
let foo : type a. (a -> a) @ unique = fun x -> x
3131
[%%expect{|
3232
val foo : 'a -> 'a = <fun>
3333
|}]
@@ -401,10 +401,10 @@ let foo ?(local_ x : _ @ unique once = 42) () = ()
401401
val foo : ?x:int @ local unique once -> unit -> unit = <fun>
402402
|}]
403403

404-
let foo ?(local_ x : ('a. 'a -> 'a) @ unique once) = ()
404+
let foo ?(local_ x : 'a. ('a -> 'a) @ unique once) = ()
405405
[%%expect{|
406406
Line 1, characters 10-49:
407-
1 | let foo ?(local_ x : ('a. 'a -> 'a) @ unique once) = ()
407+
1 | let foo ?(local_ x : 'a. ('a -> 'a) @ unique once) = ()
408408
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
409409
Error: Optional parameters cannot be polymorphic
410410
|}]
@@ -419,10 +419,10 @@ let foo ?x:(local_ (x,y) : _ @ unique once = (42, 42)) () = ()
419419
val foo : ?x:int * int @ local unique once -> unit -> unit = <fun>
420420
|}]
421421

422-
let foo ?x:(local_ (x,y) : ('a.'a->'a) @ unique once) () = ()
422+
let foo ?x:(local_ (x,y) : 'a.('a->'a) @ unique once) () = ()
423423
[%%expect{|
424424
Line 1, characters 12-52:
425-
1 | let foo ?x:(local_ (x,y) : ('a.'a->'a) @ unique once) () = ()
425+
1 | let foo ?x:(local_ (x,y) : 'a.('a->'a) @ unique once) () = ()
426426
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
427427
Error: Optional parameters cannot be polymorphic
428428
|}]

testsuite/tests/typing-modes/syntax-error.compilers.reference

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Line 2, characters 35-36:
1111
^
1212
Error: Syntax error: "mode expression" expected.
1313
Line 2, characters 30-31:
14-
2 | let foo : (type a. a -> a) @ = fun x -> x;;
14+
2 | let foo : type a. (a -> a) @ = fun x -> x;;
1515
^
1616
Error: Syntax error: "mode expression" expected.
1717
Line 2, characters 13-14:

0 commit comments

Comments
 (0)