Skip to content

Commit 0760c82

Browse files
authored
Disable module patterns in comprehensions (#131)
* Disable module patterns in comprehensions, and add a test * Copy tests to all three of array, iarray, list * Add a more correct fix, that avoids the [in] body re-enabling allow_modules mode * Relocate tests to more sensible place, and add a test to check the re-enabling of allow_modules mode * fix formatting
1 parent 6acac80 commit 0760c82

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed

testsuite/tests/comprehensions/array_comprehensions_pure.ml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,58 @@ Error: This expression has type float but an expression was expected of type
311311
because it is in a range-based for iterator start index in a comprehension
312312
|}];;
313313

314+
(* Using first-class module patterns isn't supported yet *)
315+
316+
module type S = sig
317+
type t
318+
val x : t
319+
end;;
320+
321+
let t = (module struct
322+
type t = int
323+
let x = 3
324+
end : S);;
325+
[%%expect {|
326+
module type S = sig type t val x : t end
327+
val t : (module S) = <module>
328+
|}];;
329+
330+
[| M.x for (module M : S) in [| t |] |];;
331+
[%%expect {|
332+
Line 1, characters 19-20:
333+
1 | [| M.x for (module M : S) in [| t |] |];;
334+
^
335+
Error: Modules are not allowed in this pattern.
336+
|}];;
337+
338+
[| M.x
339+
for (module M : S) in
340+
[| (module struct
341+
type t = int
342+
let x = 3
343+
end : S)
344+
|]
345+
|];;
346+
[%%expect {|
347+
Line 2, characters 15-16:
348+
2 | for (module M : S) in
349+
^
350+
Error: Modules are not allowed in this pattern.
351+
|}];;
352+
353+
[| M.x
354+
for (module M : S) in
355+
[| (let t = t in
356+
t)
357+
|]
358+
|];;
359+
[%%expect {|
360+
Line 2, characters 15-16:
361+
2 | for (module M : S) in
362+
^
363+
Error: Modules are not allowed in this pattern.
364+
|}];;
365+
314366
(* No duplicating variables in a for-and clause *)
315367

316368
[|i for i = 1 to 3 and i = 3 downto 1|];;

testsuite/tests/comprehensions/iarray_comprehensions_pure.ml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,58 @@ Error: This expression has type float but an expression was expected of type
409409
because it is in a range-based for iterator start index in a comprehension
410410
|}];;
411411

412+
(* Using first-class module patterns isn't supported yet *)
413+
414+
module type S = sig
415+
type t
416+
val x : t
417+
end;;
418+
419+
let t = (module struct
420+
type t = int
421+
let x = 3
422+
end : S);;
423+
[%%expect {|
424+
module type S = sig type t val x : t end
425+
val t : (module S) = <module>
426+
|}];;
427+
428+
[: M.x for (module M : S) in [: t :] :];;
429+
[%%expect {|
430+
Line 1, characters 19-20:
431+
1 | [: M.x for (module M : S) in [: t :] :];;
432+
^
433+
Error: Modules are not allowed in this pattern.
434+
|}];;
435+
436+
[: M.x
437+
for (module M : S) in
438+
[: (module struct
439+
type t = int
440+
let x = 3
441+
end : S)
442+
:]
443+
:];;
444+
[%%expect {|
445+
Line 2, characters 15-16:
446+
2 | for (module M : S) in
447+
^
448+
Error: Modules are not allowed in this pattern.
449+
|}];;
450+
451+
[: M.x
452+
for (module M : S) in
453+
[: (let t = t in
454+
t)
455+
:]
456+
:];;
457+
[%%expect {|
458+
Line 2, characters 15-16:
459+
2 | for (module M : S) in
460+
^
461+
Error: Modules are not allowed in this pattern.
462+
|}];;
463+
412464
(* No duplicating variables in a for-and clause *)
413465

414466
[:i for i = 1 to 3 and i = 3 downto 1:];;

testsuite/tests/comprehensions/list_comprehensions_pure.ml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,58 @@ Error: This expression has type float but an expression was expected of type
303303
because it is in a range-based for iterator start index in a comprehension
304304
|}];;
305305

306+
(* Using first-class module patterns isn't supported yet *)
307+
308+
module type S = sig
309+
type t
310+
val x : t
311+
end;;
312+
313+
let t = (module struct
314+
type t = int
315+
let x = 3
316+
end : S);;
317+
[%%expect {|
318+
module type S = sig type t val x : t end
319+
val t : (module S) = <module>
320+
|}];;
321+
322+
[ M.x for (module M : S) in [ t ] ];;
323+
[%%expect {|
324+
Line 1, characters 18-19:
325+
1 | [ M.x for (module M : S) in [ t ] ];;
326+
^
327+
Error: Modules are not allowed in this pattern.
328+
|}];;
329+
330+
[ M.x
331+
for (module M : S) in
332+
[ (module struct
333+
type t = int
334+
let x = 3
335+
end : S)
336+
]
337+
];;
338+
[%%expect {|
339+
Line 2, characters 14-15:
340+
2 | for (module M : S) in
341+
^
342+
Error: Modules are not allowed in this pattern.
343+
|}];;
344+
345+
[ M.x
346+
for (module M : S) in
347+
[ (let t = t in
348+
t)
349+
]
350+
];;
351+
[%%expect {|
352+
Line 2, characters 14-15:
353+
2 | for (module M : S) in
354+
^
355+
Error: Modules are not allowed in this pattern.
356+
|}];;
357+
306358
(* No duplicating variables in a for-and clause *)
307359

308360
[i for i = 1 to 3 and i = 3 downto 1];;

typing/typecore.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7137,6 +7137,9 @@ and type_comprehension_iterator
71377137
~explanation:(Comprehension_in_iterator comprehension_type)
71387138
seq_ty)
71397139
in
7140+
(* TODO: fix handling of first-class module patterns so we can remove
7141+
* this line. *)
7142+
allow_modules := false;
71407143
let pattern =
71417144
(* To understand why we can currently only provide [global] bindings for
71427145
the contents of sequences comprehensions iterate over, see "What

0 commit comments

Comments
 (0)