Skip to content

Commit 6f8f703

Browse files
authored
Improve parsing of layout annotations in type parameters (#2688)
1 parent af60afe commit 6f8f703

7 files changed

+60
-65
lines changed

ocaml/parsing/parser.mly

+16-1
Original file line numberDiff line numberDiff line change
@@ -4536,9 +4536,24 @@ atomic_type:
45364536
{ [] }
45374537
| ty = atomic_type
45384538
{ [ty] }
4539-
| LPAREN tys = separated_nontrivial_llist(COMMA, core_type) RPAREN
4539+
| LPAREN
4540+
tys = separated_nontrivial_llist(COMMA, one_type_parameter_of_several)
4541+
RPAREN
45404542
{ tys }
45414543

4544+
(* Layout annotations on type expressions typically require parens, as in [('a :
4545+
float64)]. But this is unnecessary when the type expression is used as the
4546+
parameter of a tconstr with more than one argument, as in [(int, 'b :
4547+
float64) t]. *)
4548+
%inline one_type_parameter_of_several:
4549+
| core_type { $1 }
4550+
| QUOTE id=ident COLON jkind=jkind_annotation
4551+
{ Jane_syntax.Layouts.type_of ~loc:(make_loc $sloc) @@
4552+
Ltyp_var { name = Some id; jkind } }
4553+
| UNDERSCORE COLON jkind=jkind_annotation
4554+
{ Jane_syntax.Layouts.type_of ~loc:(make_loc $sloc) @@
4555+
Ltyp_var { name = None; jkind } }
4556+
45424557
%inline package_type: module_type
45434558
{ let (lid, cstrs, attrs) = package_type_of_module_type $1 in
45444559
let descr = Ptyp_package (lid, cstrs) in
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
(* TEST
2+
{
3+
compiler_reference = "${test_source_directory}/parsing_stable_beta.compilers.reference";
4+
toplevel;
5+
}{
6+
flags = "-extension layouts_beta";
7+
compiler_reference = "${test_source_directory}/parsing_stable_beta.compilers.reference";
8+
toplevel;
9+
}{
10+
flags = "-extension layouts_alpha";
11+
compiler_reference = "${test_source_directory}/parsing_alpha.compilers.reference";
12+
toplevel;
13+
}
14+
*)
15+
16+
type ('a : value) t0 = 'a list;;
17+
18+
type ('a : immediate) t0 = 'a list;;
19+
20+
type ('a : void) t0 = 'a list;;
21+
22+
type ('a : valu) t0 = 'a list;;
23+
24+
type t = float#;;
25+
26+
type t = int#;;
27+
28+
type t = Float.t#;;
29+
30+
type ('a : any, 'b : any, 'c : any) t;;
31+
32+
type 'a s1 = ('a : float64, int, bool) t;;
33+
34+
let f : ('a, _ : value, bool) t -> int = fun _ -> 42;;
35+
36+
type ('a, 'b, 'c) s2 = ('a, 'b, 'c : bits32) t;;

ocaml/testsuite/tests/typing-layouts/parsing_alpha.compilers.reference

+4
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ Line 2, characters 9-17:
2222
2 | type t = Float.t#;;
2323
^^^^^^^^
2424
Error: Unbound type constructor Float.t#
25+
type ('a : any, 'b : any, 'c : any) t
26+
type ('a : float64) s1 = ('a, int, bool) t
27+
val f : ('a : any) 'b. ('a, 'b, bool) t -> int = <fun>
28+
type ('a, 'b, 'c : bits32) s2 = ('a, 'b, 'c) t
2529

ocaml/testsuite/tests/typing-layouts/parsing_alpha.ml

-18
This file was deleted.

ocaml/testsuite/tests/typing-layouts/parsing_stable.compilers.reference

-22
This file was deleted.

ocaml/testsuite/tests/typing-layouts/parsing_beta.compilers.reference renamed to ocaml/testsuite/tests/typing-layouts/parsing_stable_beta.compilers.reference

+4
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@ Line 2, characters 9-17:
1919
2 | type t = Float.t#;;
2020
^^^^^^^^
2121
Error: Unbound type constructor Float.t#
22+
type ('a : any, 'b : any, 'c : any) t
23+
type ('a : float64) s1 = ('a, int, bool) t
24+
val f : ('a : any) 'b. ('a, 'b, bool) t -> int = <fun>
25+
type ('a, 'b, 'c : bits32) s2 = ('a, 'b, 'c) t
2226

ocaml/testsuite/tests/typing-layouts/parsing_stable_beta.ml

-24
This file was deleted.

0 commit comments

Comments
 (0)