summaryrefslogtreecommitdiff
path: root/testsuite/tests/letrec-check/basic.ml
blob: caa8c238ed6630cd49a2473e539e6c3222753fa8 (plain)
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
(* TEST
 expect;
*)

let rec x = (x; ());;
[%%expect{|
val x : unit = ()
|}];;

let rec x = "x";;
[%%expect{|
val x : string = "x"
|}];;

let rec x = let x = () in x;;
[%%expect{|
val x : unit = ()
|}];;

let rec x = let y = (x; ()) in y;;
[%%expect{|
val x : unit = ()
|}];;

let rec x = let y = () in x;;
[%%expect{|
Line 1, characters 12-27:
1 | let rec x = let y = () in x;;
                ^^^^^^^^^^^^^^^
Error: This kind of expression is not allowed as right-hand side of `let rec'
|}];;

let rec x = [y]
and y = let x = () in x;;
[%%expect{|
val x : unit list = [()]
val y : unit = ()
|}];;

let rec x = [y]
and y = let rec x = () in x;;
[%%expect{|
val x : unit list = [()]
val y : unit = ()
|}];;

let rec x =
  let a = x in
  fun () -> a ()
and y =
  [x];;
[%%expect{|
val x : unit -> 'a = <fun>
val y : (unit -> 'a) list = [<fun>]
|}];;

let rec x = [|y|] and y = 0;;
[%%expect{|
val x : int array = [|0|]
val y : int = 0
|}];;


let rec x = (y, y)
and y = fun () -> ignore x;;
[%%expect{|
val x : (unit -> unit) * (unit -> unit) = (<fun>, <fun>)
val y : unit -> unit = <fun>
|}];;

let rec x = Some y
and y = fun () -> ignore x
;;
[%%expect{|
val x : (unit -> unit) option = Some <fun>
val y : unit -> unit = <fun>
|}];;

let rec x = ignore x;;
[%%expect{|
Line 1, characters 12-20:
1 | let rec x = ignore x;;
                ^^^^^^^^
Error: This kind of expression is not allowed as right-hand side of `let rec'
|}];;

let rec x = y 0 and y _ = ();;
[%%expect{|
Line 1, characters 12-15:
1 | let rec x = y 0 and y _ = ();;
                ^^^
Error: This kind of expression is not allowed as right-hand side of `let rec'
|}];;

let rec b = if b then true else false;;
[%%expect{|
Line 1, characters 12-37:
1 | let rec b = if b then true else false;;
                ^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This kind of expression is not allowed as right-hand side of `let rec'
|}];;

let rec x = function
    Some _ -> ignore (y [])
  | None -> ignore (y [])
and y = function
    [] -> ignore (x None)
  | _ :: _ -> ignore (x None)
    ;;
[%%expect{|
val x : 'a option -> unit = <fun>
val y : 'a list -> unit = <fun>
|}];;

(* used to be accepted, see PR#7696 *)
let rec x = { x with contents = 3 }  [@ocaml.warning "-23"];;
[%%expect{|
Line 1, characters 12-35:
1 | let rec x = { x with contents = 3 }  [@ocaml.warning "-23"];;
                ^^^^^^^^^^^^^^^^^^^^^^^
Error: This kind of expression is not allowed as right-hand side of `let rec'
|}];;

(* this is rejected as `c` will be dereferenced during the copy,
   and is not yet fully defined *)
let rec c = { c with Complex.re = 1.0 };;
[%%expect{|
Line 1, characters 12-39:
1 | let rec c = { c with Complex.re = 1.0 };;
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This kind of expression is not allowed as right-hand side of `let rec'
|}];;

let rec x = `A y
and y = fun () -> ignore x
;;
[%%expect{|
val x : [> `A of unit -> unit ] = `A <fun>
val y : unit -> unit = <fun>
|}];;

let rec x = { contents = y }
and y = fun () -> ignore x;;
[%%expect{|
val x : (unit -> unit) ref = {contents = <fun>}
val y : unit -> unit = <fun>
|}];;

let r = ref (fun () -> ())
let rec x = fun () -> r := x;;
[%%expect{|
val r : (unit -> unit) ref = {contents = <fun>}
val x : unit -> unit = <fun>
|}];;

let rec x = fun () -> y.contents and y = { contents = 3 };;
[%%expect{|
val x : unit -> int = <fun>
val y : int ref = {contents = 3}
|}];;

let r = ref ()
let rec x = r := x;;
[%%expect{|
val r : unit ref = {contents = ()}
Line 2, characters 12-18:
2 | let rec x = r := x;;
                ^^^^^^
Error: This kind of expression is not allowed as right-hand side of `let rec'
|}];;

let rec x =
  for i = 0 to 1 do
    let z = y in ignore z
  done
and y = x; ();;
[%%expect{|
Lines 2-4, characters 2-6:
2 | ..for i = 0 to 1 do
3 |     let z = y in ignore z
4 |   done
Error: This kind of expression is not allowed as right-hand side of `let rec'
|}];;

let rec x =
  for i = 0 to y do
    ()
  done
and y = 10;;
[%%expect{|
Lines 2-4, characters 2-6:
2 | ..for i = 0 to y do
3 |     ()
4 |   done
Error: This kind of expression is not allowed as right-hand side of `let rec'
|}];;

let rec x =
  for i = y to 10 do
    ()
  done
and y = 0;;
[%%expect{|
Lines 2-4, characters 2-6:
2 | ..for i = y to 10 do
3 |     ()
4 |   done
Error: This kind of expression is not allowed as right-hand side of `let rec'
|}];;

let rec x =
  while false do
    let y = x in ignore y
  done
and y = x; ();;
[%%expect{|
Lines 2-4, characters 2-6:
2 | ..while false do
3 |     let y = x in ignore y
4 |   done
Error: This kind of expression is not allowed as right-hand side of `let rec'
|}];;

let rec x =
  while y do
    ()
  done
and y = false;;
[%%expect{|
Lines 2-4, characters 2-6:
2 | ..while y do
3 |     ()
4 |   done
Error: This kind of expression is not allowed as right-hand side of `let rec'
|}];;

let rec x =
  while y do
    let y = x in ignore y
  done
and y = false;;
[%%expect{|
Lines 2-4, characters 2-6:
2 | ..while y do
3 |     let y = x in ignore y
4 |   done
Error: This kind of expression is not allowed as right-hand side of `let rec'
|}];;



let rec x = y.contents and y = { contents = 3 };;
[%%expect{|
Line 1, characters 12-22:
1 | let rec x = y.contents and y = { contents = 3 };;
                ^^^^^^^^^^
Error: This kind of expression is not allowed as right-hand side of `let rec'
|}];;

let rec x = assert y and y = true;;
[%%expect{|
Line 1, characters 12-20:
1 | let rec x = assert y and y = true;;
                ^^^^^^^^
Error: This kind of expression is not allowed as right-hand side of `let rec'
|}];;

(* Recursively constructing arrays of known non-float type is permitted *)
let rec deep_cycle : [`Tuple of [`Shared of 'a] array] as 'a
  = `Tuple [| `Shared deep_cycle |];;
[%%expect{|
val deep_cycle : [ `Tuple of [ `Shared of 'a ] array ] as 'a =
  `Tuple [|`Shared <cycle>|]
|}];;

(* Constructing float arrays was disallowed altogether at one point
   by an overzealous check.  Constructing float arrays in recursive
   bindings is fine when they don't partake in the recursion. *)
let rec _x = let _ = [| 1.0 |] in 1. in ();;
[%%expect{|
- : unit = ()
|}];;

(* The builtin Stdlib.ref is currently treated as a constructor.
   Other functions of the same name should not be so treated. *)
let _ =
  let module Stdlib =
  struct
    let ref _ = assert false
  end in
  let rec x = Stdlib.ref y
  and y = fun () -> ignore x
  in (x, y)
;;
[%%expect{|
Line 6, characters 14-26:
6 |   let rec x = Stdlib.ref y
                  ^^^^^^^^^^^^
Error: This kind of expression is not allowed as right-hand side of `let rec'
|}];;

(* An example, from Leo White, of let rec bindings that allocate
   values of unknown size *)
let foo p x =
  let rec f =
    if p then (fun y -> x + g y) else (fun y -> g y)
  and g =
    if not p then (fun y -> x - f y) else (fun y -> f y)
  in
  (f, g)
;;
[%%expect{|
Line 3, characters 4-52:
3 |     if p then (fun y -> x + g y) else (fun y -> g y)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This kind of expression is not allowed as right-hand side of `let rec'
|}];;

let rec x =
  match let _ = y in raise Not_found with
    _ -> "x"
  | exception Not_found -> "z"
and y = match x with
  z -> ("y", z);;
[%%expect{|
Lines 2-4, characters 2-30:
2 | ..match let _ = y in raise Not_found with
3 |     _ -> "x"
4 |   | exception Not_found -> "z"
Error: This kind of expression is not allowed as right-hand side of `let rec'
|}];;


(* To compute the dependencies of mutually-recursive bindings,
   transitive dependencies must be taken into account.

   The example below was causing a segfault in 4.08+dev.
*)
let rec wrong =
  (* x depends on y,
     and y depends on wrong,
     so it is important to notice that x transitively depends on wrong;

     an earlier version of our letrec analysis would only report that
     y depends on wrong, which seems safe as y is not used in the
     body.
  *)
  let rec x = ref y
  and y = ref wrong
  in ref ("foo" ^ ! ! !x);;
[%%expect{|
Lines 10-12, characters 2-25:
10 | ..let rec x = ref y
11 |   and y = ref wrong
12 |   in ref ("foo" ^ ! ! !x)..
Error: This kind of expression is not allowed as right-hand side of `let rec'
|}]

(* in this case, x does not depend on y, so everything is fine *)
let rec okay =
  let rec x = ref "bar"
  and _y = ref okay in
  ref ("foo" ^ ! x);;
[%%expect{|
val okay : string ref = {contents = "foobar"}
|}]