1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
(* TEST
ocamlrunparam = "l=1000000"
* expect *)
(* #9623 *)
module RhsScopeCheck = struct
module type Sig1 = sig
type t
type u = t
end
(* A scoping error here is intentional:
with-constraints "with <lhs> = <rhs>"
have their <rhs> evaluated in the current
typing environment, not within the signature
that they are constraining. [t] is unbound
in the current environment, so [with u = t]
must be rejected. *)
module type Check1 = Sig1
with type u = t
end
[%%expect{|
Line 15, characters 18-19:
15 | with type u = t
^
Error: Unbound type constructor t
|}]
module VarianceEnv = struct
module type Sig = sig
type +'a abstract
type +'a user = Foo of 'a abstract
end
module type Problem = sig
include Sig
module M : Sig
with type 'a abstract = 'a abstract
and type 'a user = 'a user
(* the variance annotation of [+'a foo] should be accepted, which
would not be the case if the with-constraint [and type 'a
user = 'a user] had its variance type-checked in the wrong typing
environment: see #9624 *)
type +'a foo = 'a M.user
end
end
[%%expect{|
module VarianceEnv :
sig
module type Sig =
sig type +'a abstract type 'a user = Foo of 'a abstract end
module type Problem =
sig
type +'a abstract
type 'a user = Foo of 'a abstract
module M :
sig
type 'a abstract = 'a abstract/2
type 'a user = 'a user/2 = Foo of 'a abstract
end
type 'a foo = 'a M.user
end
end
|}]
module UnboxedEnv = struct
module type Sig = sig
type 'a ind = 'a * int
type t = T : 'e ind -> t [@@unboxed]
end
module type Problem = sig
include Sig
module type ReboundSig = Sig
with type 'a ind = 'a ind
and type t = t
(* the with-definition [and type t = t] above should be accepted,
which would not be the case if its definition had separability
checked in the wrong typing environment: see #9624 *)
end
end
[%%expect{|
module UnboxedEnv :
sig
module type Sig =
sig type 'a ind = 'a * int type t = T : 'e ind -> t [@@unboxed] end
module type Problem =
sig
type 'a ind = 'a * int
type t = T : 'e ind -> t [@@unboxed]
module type ReboundSig =
sig
type 'a ind = 'a ind/2
type t = t/2 = T : 'a ind -> t [@@unboxed]
end
end
end
|}]
(* We can also have environment issues when unifying type parameters;
regression test contributed by Jacques Garrigue in #9623. *)
module ParamsUnificationEnv = struct
module type Sig =
sig type 'a u = 'a list type +'a t constraint 'a = 'b u end
type +'a t = 'b constraint 'a = 'b list
module type Sig2 = Sig with type +'a t = 'a t
end
[%%expect{|
module ParamsUnificationEnv :
sig
module type Sig =
sig type 'a u = 'a list type +'a t constraint 'a = 'b u end
type +'a t = 'b constraint 'a = 'b list
module type Sig2 =
sig type 'a u = 'a list type +'a t = 'a t/2 constraint 'a = 'b u end
end
|}]
(* The construction of the "signature environment" was also broken
in earlier versions of the code. Regression test by Leo White in #9623. *)
module CorrectEnvConstructionTest = struct
module type Sig = sig
type +'a user = Foo of 'a abstract
and +'a abstract
end
module type Problem = sig
include Sig
module M : Sig
with type 'a abstract = 'a abstract
and type 'a user = 'a user
type +'a foo = 'a M.user
end
end
[%%expect{|
module CorrectEnvConstructionTest :
sig
module type Sig =
sig type 'a user = Foo of 'a abstract and +'a abstract end
module type Problem =
sig
type 'a user = Foo of 'a abstract
and +'a abstract
module M :
sig
type 'a user = 'a user/2 = Foo of 'a abstract
and 'a abstract = 'a abstract/2
end
type 'a foo = 'a M.user
end
end
|}]
(* #9640 *)
module type Packet_type = sig
type t
end
module type Unpacked_header = sig
module Packet_type : Packet_type
type t
val f : t -> Packet_type.t -> unit
end
module type Header = sig
module Packet_type : Packet_type
module Unpacked : Unpacked_header with module Packet_type := Packet_type
end
module type S = sig
module Packet_type : Packet_type
module Header : Header with module Packet_type = Packet_type
end
[%%expect{|
module type Packet_type = sig type t end
module type Unpacked_header =
sig
module Packet_type : Packet_type
type t
val f : t -> Packet_type.t -> unit
end
module type Header =
sig
module Packet_type : Packet_type
module Unpacked : sig type t val f : t -> Packet_type.t -> unit end
end
module type S =
sig
module Packet_type : Packet_type
module Header :
sig
module Packet_type : sig type t = Packet_type.t end
module Unpacked : sig type t val f : t -> Packet_type.t -> unit end
end
end
|}]
module type Iobuf_packet = sig
module Make (Header : Header) () :
S
with module Packet_type = Header.Packet_type
with module Header.Unpacked = Header.Unpacked
end
[%%expect{|
module type Iobuf_packet =
sig
module Make :
functor (Header : Header) () ->
sig
module Packet_type : sig type t = Header.Packet_type.t end
module Header :
sig
module Packet_type : sig type t = Packet_type.t end
module Unpacked :
sig
type t = Header.Unpacked.t
val f : t -> Header.Packet_type.t -> unit
end
end
end
end
|}]
(* Simpler example by @gasche *)
module type S = sig
type t
type u = t
end
module type Pack = sig
module M : S
end
[%%expect{|
module type S = sig type t type u = t end
module type Pack = sig module M : S end
|}]
module type Weird = sig
module M : S
module P : Pack
with type M.t = M.t
with type M.u = M.u
end
[%%expect{|
module type Weird =
sig
module M : S
module P : sig module M : sig type t = M.t type u = M.u end end
end
|}]
(* Recursion issues *)
(* Should fail rather than stack overflow *)
module type S = sig
type 'a t = 'a
constraint 'a = < m : r >
and r = (< m : r >) t
end
module type T = S with type 'a t = 'b constraint 'a = < m : 'b >;;
[%%expect{|
module type S =
sig type 'a t = 'a constraint 'a = < m : r > and r = < m : r > t end
Uncaught exception: Stack overflow
|}]
(* Correct *)
module type S = sig
type t = Foo of r
and r = t
end
type s = Foo of s
module type T = S with type t = s
[%%expect{|
module type S = sig type t = Foo of r and r = t end
type s = Foo of s
module type T = sig type t = s = Foo of r and r = t end
|}]
(* Correct *)
module type S = sig
type r = t
and t = Foo of r
end
type s = Foo of s
module type T = S with type t = s
[%%expect{|
module type S = sig type r = t and t = Foo of r end
type s = Foo of s
module type T = sig type r = t and t = s = Foo of r end
|}]
(* Should succeed *)
module type S = sig
module rec M : sig
type t = Foo of M.r
type r = t
end
end
type s = Foo of s
module type T = S with type M.t = s
[%%expect{|
module type S = sig module rec M : sig type t = Foo of M.r type r = t end end
type s = Foo of s
Line 10, characters 23-35:
10 | module type T = S with type M.t = s
^^^^^^^^^^^^
Error: This variant or record definition does not match that of type s
Constructors do not match:
Foo of s
is not the same as:
Foo of M.r
The type s is not equal to the type M.r = M.t
|}]
(* Should succeed *)
module type S = sig
module rec M : sig
type t = private [`Foo of M.r]
type r = t
end
end
type s = [`Foo of s]
module type T = S with type M.t = s
[%%expect{|
module type S =
sig module rec M : sig type t = private [ `Foo of M.r ] type r = t end end
type s = [ `Foo of s ]
Line 10, characters 16-35:
10 | module type T = S with type M.t = s
^^^^^^^^^^^^^^^^^^^
Error: In this `with' constraint, the new definition of M.t
does not match its original definition in the constrained signature:
Type declarations do not match:
type t = s
is not included in
type t = private [ `Foo of M.r ]
The type s = [ `Foo of s ] is not equal to the type [ `Foo of M.r ]
Type s = [ `Foo of s ] is not equal to type M.r = M.t
Types for tag `Foo are incompatible
|}]
(* Should succeed *)
module type S = sig
module rec M : sig
module N : sig type t = private [`Foo of M.r] end
type r = M.N.t
end
end
module X = struct type t = [`Foo of t] end
module type T = S with module M.N = X
[%%expect{|
module type S =
sig
module rec M :
sig
module N : sig type t = private [ `Foo of M.r ] end
type r = M.N.t
end
end
module X : sig type t = [ `Foo of t ] end
Line 10, characters 16-37:
10 | module type T = S with module M.N = X
^^^^^^^^^^^^^^^^^^^^^
Error: In this `with' constraint, the new definition of M.N
does not match its original definition in the constrained signature:
Modules do not match:
sig type t = [ `Foo of t ] end
is not included in
sig type t = private [ `Foo of M.r ] end
Type declarations do not match:
type t = [ `Foo of t ]
is not included in
type t = private [ `Foo of M.r ]
The type [ `Foo of t ] is not equal to the type [ `Foo of M.r ]
Type t = [ `Foo of t ] is not equal to type M.r = M.N.t
Types for tag `Foo are incompatible
|}]
(* Should succeed *)
module type S = sig
module rec M : sig
module N : sig type t = M.r type s end
type r = N.s
end
end
module X = struct type t type s = t end
module type T = S with module M.N = X
[%%expect{|
module type S =
sig
module rec M :
sig module N : sig type t = M.r type s end type r = N.s end
end
module X : sig type t type s = t end
Line 10, characters 16-37:
10 | module type T = S with module M.N = X
^^^^^^^^^^^^^^^^^^^^^
Error: In this `with' constraint, the new definition of M.N
does not match its original definition in the constrained signature:
Modules do not match:
sig type t = X.t type s = t end
is not included in
sig type t = M.r type s end
Type declarations do not match:
type t = X.t
is not included in
type t = M.r
The type X.t is not equal to the type M.r = M.N.s
|}]
|