1 ;;;; tree-il.test --- test suite for compiling tree-il -*- scheme -*-
2 ;;;; Andy Wingo <wingo@pobox.com> --- May 2009
4 ;;;; Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
6 ;;;; This library is free software; you can redistribute it and/or
7 ;;;; modify it under the terms of the GNU Lesser General Public
8 ;;;; License as published by the Free Software Foundation; either
9 ;;;; version 3 of the License, or (at your option) any later version.
11 ;;;; This library is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;;; Lesser General Public License for more details.
16 ;;;; You should have received a copy of the GNU Lesser General Public
17 ;;;; License along with this library; if not, write to the Free Software
18 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 (define-module (test-suite tree-il)
21 #:use-module (test-suite lib)
22 #:use-module (system base compile)
23 #:use-module (system base pmatch)
24 #:use-module (system base message)
25 #:use-module (language tree-il)
26 #:use-module (language tree-il primitives)
27 #:use-module (language glil)
28 #:use-module (srfi srfi-13))
30 ;; Of course, the GLIL that is emitted depends on the source info of the
31 ;; input. Here we're not concerned about that, so we strip source
32 ;; information from the incoming tree-il.
34 (define (strip-source x)
35 (post-order! (lambda (x) (set! (tree-il-src x) #f))
38 (define-syntax assert-tree-il->glil
39 (syntax-rules (with-partial-evaluation without-partial-evaluation
41 ((_ with-partial-evaluation in pat test ...)
42 (assert-tree-il->glil with-options (#:partial-eval? #t)
44 ((_ without-partial-evaluation in pat test ...)
45 (assert-tree-il->glil with-options (#:partial-eval? #f)
47 ((_ with-options opts in pat test ...)
50 (let ((glil (unparse-glil
51 (compile (strip-source (parse-tree-il exp))
52 #:from 'tree-il #:to 'glil
55 (pat (guard test ...) #t)
58 (assert-tree-il->glil with-partial-evaluation
61 (define-syntax pass-if-tree-il->scheme
64 (assert-scheme->tree-il->scheme in pat #t))
67 (pmatch (tree-il->scheme
68 (compile 'in #:from 'scheme #:to 'tree-il))
69 (pat (guard guard-exp) #t)
73 ;; The partial evaluator.
74 (@@ (language tree-il optimize) peval))
76 (define-syntax pass-if-peval
77 (syntax-rules (resolve-primitives)
80 (compile 'in #:from 'scheme #:to 'tree-il)))
81 ((_ resolve-primitives in pat)
85 (compile 'in #:from 'scheme #:to 'tree-il)
89 (let ((evaled (unparse-tree-il (peval code))))
92 (_ (pk 'peval-mismatch)
93 ((@ (ice-9 pretty-print) pretty-print)
96 ((@ (ice-9 pretty-print) pretty-print)
99 ((@ (ice-9 pretty-print) pretty-print)
105 (with-test-prefix "tree-il->scheme"
106 (pass-if-tree-il->scheme
107 (case-lambda ((a) a) ((b c) (list b c)))
108 (case-lambda ((,a) ,a1) ((,b ,c) (list ,b1 ,c1)))
109 (and (eq? a a1) (eq? b b1) (eq? c c1))))
111 (with-test-prefix "void"
112 (assert-tree-il->glil
114 (program () (std-prelude 0 0 #f) (label _) (void) (call return 1)))
115 (assert-tree-il->glil
116 (begin (void) (const 1))
117 (program () (std-prelude 0 0 #f) (label _) (const 1) (call return 1)))
118 (assert-tree-il->glil
119 (apply (primitive +) (void) (const 1))
120 (program () (std-prelude 0 0 #f) (label _) (void) (call add1 1) (call return 1))))
122 (with-test-prefix "application"
123 (assert-tree-il->glil
124 (apply (toplevel foo) (const 1))
125 (program () (std-prelude 0 0 #f) (label _) (toplevel ref foo) (const 1) (call tail-call 1)))
126 (assert-tree-il->glil
127 (begin (apply (toplevel foo) (const 1)) (void))
128 (program () (std-prelude 0 0 #f) (label _) (call new-frame 0) (toplevel ref foo) (const 1) (mv-call 1 ,l1)
129 (call drop 1) (branch br ,l2)
130 (label ,l3) (mv-bind 0 #f)
132 (void) (call return 1))
133 (and (eq? l1 l3) (eq? l2 l4)))
134 (assert-tree-il->glil
135 (apply (toplevel foo) (apply (toplevel bar)))
136 (program () (std-prelude 0 0 #f) (label _) (toplevel ref foo) (call new-frame 0) (toplevel ref bar) (call call 0)
137 (call tail-call 1))))
139 (with-test-prefix "conditional"
140 (assert-tree-il->glil
141 (if (toplevel foo) (const 1) (const 2))
142 (program () (std-prelude 0 0 #f) (label _) (toplevel ref foo) (branch br-if-not ,l1)
143 (const 1) (call return 1)
144 (label ,l2) (const 2) (call return 1))
147 (assert-tree-il->glil without-partial-evaluation
148 (begin (if (toplevel foo) (const 1) (const 2)) (const #f))
149 (program () (std-prelude 0 0 #f) (label _) (toplevel ref foo) (branch br-if-not ,l1) (branch br ,l2)
150 (label ,l3) (label ,l4) (const #f) (call return 1))
151 (eq? l1 l3) (eq? l2 l4))
153 (assert-tree-il->glil
154 (apply (primitive null?) (if (toplevel foo) (const 1) (const 2)))
155 (program () (std-prelude 0 0 #f) (label _) (toplevel ref foo) (branch br-if-not ,l1)
156 (const 1) (branch br ,l2)
157 (label ,l3) (const 2) (label ,l4)
158 (call null? 1) (call return 1))
159 (eq? l1 l3) (eq? l2 l4)))
161 (with-test-prefix "primitive-ref"
162 (assert-tree-il->glil
164 (program () (std-prelude 0 0 #f) (label _) (toplevel ref +) (call return 1)))
166 (assert-tree-il->glil
167 (begin (primitive +) (const #f))
168 (program () (std-prelude 0 0 #f) (label _) (const #f) (call return 1)))
170 (assert-tree-il->glil
171 (apply (primitive null?) (primitive +))
172 (program () (std-prelude 0 0 #f) (label _) (toplevel ref +) (call null? 1)
175 (with-test-prefix "lexical refs"
176 (assert-tree-il->glil without-partial-evaluation
177 (let (x) (y) ((const 1)) (lexical x y))
178 (program () (std-prelude 0 1 #f) (label _)
179 (const 1) (bind (x #f 0)) (lexical #t #f set 0)
180 (lexical #t #f ref 0) (call return 1)
183 (assert-tree-il->glil without-partial-evaluation
184 (let (x) (y) ((const 1)) (begin (lexical x y) (const #f)))
185 (program () (std-prelude 0 1 #f) (label _)
186 (const 1) (bind (x #f 0)) (lexical #t #f set 0)
187 (const #f) (call return 1)
190 (assert-tree-il->glil without-partial-evaluation
191 (let (x) (y) ((const 1)) (apply (primitive null?) (lexical x y)))
192 (program () (std-prelude 0 1 #f) (label _)
193 (const 1) (bind (x #f 0)) (lexical #t #f set 0)
194 (lexical #t #f ref 0) (call null? 1) (call return 1)
197 (with-test-prefix "lexical sets"
198 (assert-tree-il->glil
199 ;; unreferenced sets may be optimized away -- make sure they are ref'd
200 (let (x) (y) ((const 1))
201 (set! (lexical x y) (apply (primitive 1+) (lexical x y))))
202 (program () (std-prelude 0 1 #f) (label _)
203 (const 1) (bind (x #t 0)) (lexical #t #t box 0)
204 (lexical #t #t ref 0) (call add1 1) (lexical #t #t set 0)
205 (void) (call return 1)
208 (assert-tree-il->glil
209 (let (x) (y) ((const 1))
210 (begin (set! (lexical x y) (apply (primitive 1+) (lexical x y)))
212 (program () (std-prelude 0 1 #f) (label _)
213 (const 1) (bind (x #t 0)) (lexical #t #t box 0)
214 (lexical #t #t ref 0) (call add1 1) (lexical #t #t set 0)
215 (lexical #t #t ref 0) (call return 1)
218 (assert-tree-il->glil
219 (let (x) (y) ((const 1))
220 (apply (primitive null?)
221 (set! (lexical x y) (apply (primitive 1+) (lexical x y)))))
222 (program () (std-prelude 0 1 #f) (label _)
223 (const 1) (bind (x #t 0)) (lexical #t #t box 0)
224 (lexical #t #t ref 0) (call add1 1) (lexical #t #t set 0) (void)
225 (call null? 1) (call return 1)
228 (with-test-prefix "module refs"
229 (assert-tree-il->glil
231 (program () (std-prelude 0 0 #f) (label _)
232 (module public ref (foo) bar)
235 (assert-tree-il->glil
236 (begin (@ (foo) bar) (const #f))
237 (program () (std-prelude 0 0 #f) (label _)
238 (module public ref (foo) bar) (call drop 1)
239 (const #f) (call return 1)))
241 (assert-tree-il->glil
242 (apply (primitive null?) (@ (foo) bar))
243 (program () (std-prelude 0 0 #f) (label _)
244 (module public ref (foo) bar)
245 (call null? 1) (call return 1)))
247 (assert-tree-il->glil
249 (program () (std-prelude 0 0 #f) (label _)
250 (module private ref (foo) bar)
253 (assert-tree-il->glil
254 (begin (@@ (foo) bar) (const #f))
255 (program () (std-prelude 0 0 #f) (label _)
256 (module private ref (foo) bar) (call drop 1)
257 (const #f) (call return 1)))
259 (assert-tree-il->glil
260 (apply (primitive null?) (@@ (foo) bar))
261 (program () (std-prelude 0 0 #f) (label _)
262 (module private ref (foo) bar)
263 (call null? 1) (call return 1))))
265 (with-test-prefix "module sets"
266 (assert-tree-il->glil
267 (set! (@ (foo) bar) (const 2))
268 (program () (std-prelude 0 0 #f) (label _)
269 (const 2) (module public set (foo) bar)
270 (void) (call return 1)))
272 (assert-tree-il->glil
273 (begin (set! (@ (foo) bar) (const 2)) (const #f))
274 (program () (std-prelude 0 0 #f) (label _)
275 (const 2) (module public set (foo) bar)
276 (const #f) (call return 1)))
278 (assert-tree-il->glil
279 (apply (primitive null?) (set! (@ (foo) bar) (const 2)))
280 (program () (std-prelude 0 0 #f) (label _)
281 (const 2) (module public set (foo) bar)
282 (void) (call null? 1) (call return 1)))
284 (assert-tree-il->glil
285 (set! (@@ (foo) bar) (const 2))
286 (program () (std-prelude 0 0 #f) (label _)
287 (const 2) (module private set (foo) bar)
288 (void) (call return 1)))
290 (assert-tree-il->glil
291 (begin (set! (@@ (foo) bar) (const 2)) (const #f))
292 (program () (std-prelude 0 0 #f) (label _)
293 (const 2) (module private set (foo) bar)
294 (const #f) (call return 1)))
296 (assert-tree-il->glil
297 (apply (primitive null?) (set! (@@ (foo) bar) (const 2)))
298 (program () (std-prelude 0 0 #f) (label _)
299 (const 2) (module private set (foo) bar)
300 (void) (call null? 1) (call return 1))))
302 (with-test-prefix "toplevel refs"
303 (assert-tree-il->glil
305 (program () (std-prelude 0 0 #f) (label _)
309 (assert-tree-il->glil without-partial-evaluation
310 (begin (toplevel bar) (const #f))
311 (program () (std-prelude 0 0 #f) (label _)
312 (toplevel ref bar) (call drop 1)
313 (const #f) (call return 1)))
315 (assert-tree-il->glil
316 (apply (primitive null?) (toplevel bar))
317 (program () (std-prelude 0 0 #f) (label _)
319 (call null? 1) (call return 1))))
321 (with-test-prefix "toplevel sets"
322 (assert-tree-il->glil
323 (set! (toplevel bar) (const 2))
324 (program () (std-prelude 0 0 #f) (label _)
325 (const 2) (toplevel set bar)
326 (void) (call return 1)))
328 (assert-tree-il->glil
329 (begin (set! (toplevel bar) (const 2)) (const #f))
330 (program () (std-prelude 0 0 #f) (label _)
331 (const 2) (toplevel set bar)
332 (const #f) (call return 1)))
334 (assert-tree-il->glil
335 (apply (primitive null?) (set! (toplevel bar) (const 2)))
336 (program () (std-prelude 0 0 #f) (label _)
337 (const 2) (toplevel set bar)
338 (void) (call null? 1) (call return 1))))
340 (with-test-prefix "toplevel defines"
341 (assert-tree-il->glil
342 (define bar (const 2))
343 (program () (std-prelude 0 0 #f) (label _)
344 (const 2) (toplevel define bar)
345 (void) (call return 1)))
347 (assert-tree-il->glil
348 (begin (define bar (const 2)) (const #f))
349 (program () (std-prelude 0 0 #f) (label _)
350 (const 2) (toplevel define bar)
351 (const #f) (call return 1)))
353 (assert-tree-il->glil
354 (apply (primitive null?) (define bar (const 2)))
355 (program () (std-prelude 0 0 #f) (label _)
356 (const 2) (toplevel define bar)
357 (void) (call null? 1) (call return 1))))
359 (with-test-prefix "constants"
360 (assert-tree-il->glil
362 (program () (std-prelude 0 0 #f) (label _)
363 (const 2) (call return 1)))
365 (assert-tree-il->glil
366 (begin (const 2) (const #f))
367 (program () (std-prelude 0 0 #f) (label _)
368 (const #f) (call return 1)))
370 (assert-tree-il->glil
371 ;; This gets simplified by `peval'.
372 (apply (primitive null?) (const 2))
373 (program () (std-prelude 0 0 #f) (label _)
374 (const #f) (call return 1))))
376 (with-test-prefix "letrec"
377 ;; simple bindings -> let
378 (assert-tree-il->glil without-partial-evaluation
379 (letrec (x y) (x1 y1) ((const 10) (const 20))
380 (apply (toplevel foo) (lexical x x1) (lexical y y1)))
381 (program () (std-prelude 0 2 #f) (label _)
382 (const 10) (const 20)
383 (bind (x #f 0) (y #f 1))
384 (lexical #t #f set 1) (lexical #t #f set 0)
386 (lexical #t #f ref 0) (lexical #t #f ref 1)
390 ;; complex bindings -> box and set! within let
391 (assert-tree-il->glil without-partial-evaluation
392 (letrec (x y) (x1 y1) ((apply (toplevel foo)) (apply (toplevel bar)))
393 (apply (primitive +) (lexical x x1) (lexical y y1)))
394 (program () (std-prelude 0 4 #f) (label _)
395 (void) (void) ;; what are these?
396 (bind (x #t 0) (y #t 1))
397 (lexical #t #t box 1) (lexical #t #t box 0)
398 (call new-frame 0) (toplevel ref foo) (call call 0)
399 (call new-frame 0) (toplevel ref bar) (call call 0)
400 (bind (x #f 2) (y #f 3)) (lexical #t #f set 3) (lexical #t #f set 2)
401 (lexical #t #f ref 2) (lexical #t #t set 0)
402 (lexical #t #f ref 3) (lexical #t #t set 1) (unbind)
403 (lexical #t #t ref 0) (lexical #t #t ref 1)
404 (call add 2) (call return 1) (unbind)))
406 ;; complex bindings in letrec* -> box and set! in order
407 (assert-tree-il->glil without-partial-evaluation
408 (letrec* (x y) (x1 y1) ((apply (toplevel foo)) (apply (toplevel bar)))
409 (apply (primitive +) (lexical x x1) (lexical y y1)))
410 (program () (std-prelude 0 2 #f) (label _)
411 (void) (void) ;; what are these?
412 (bind (x #t 0) (y #t 1))
413 (lexical #t #t box 1) (lexical #t #t box 0)
414 (call new-frame 0) (toplevel ref foo) (call call 0)
415 (lexical #t #t set 0)
416 (call new-frame 0) (toplevel ref bar) (call call 0)
417 (lexical #t #t set 1)
418 (lexical #t #t ref 0)
419 (lexical #t #t ref 1)
420 (call add 2) (call return 1) (unbind)))
422 ;; simple bindings in letrec* -> equivalent to letrec
423 (assert-tree-il->glil without-partial-evaluation
424 (letrec* (x y) (xx yy) ((const 1) (const 2))
426 (program () (std-prelude 0 1 #f) (label _)
428 (bind (y #f 0)) ;; X is removed, and Y is unboxed
429 (lexical #t #f set 0)
430 (lexical #t #f ref 0)
431 (call return 1) (unbind))))
433 (with-test-prefix "lambda"
434 (assert-tree-il->glil
436 (lambda-case (((x) #f #f #f () (y)) (const 2)) #f))
437 (program () (std-prelude 0 0 #f) (label _)
438 (program () (std-prelude 1 1 #f)
439 (bind (x #f 0)) (label _)
440 (const 2) (call return 1) (unbind))
443 (assert-tree-il->glil
445 (lambda-case (((x y) #f #f #f () (x1 y1))
448 (program () (std-prelude 0 0 #f) (label _)
449 (program () (std-prelude 2 2 #f)
450 (bind (x #f 0) (y #f 1)) (label _)
451 (const 2) (call return 1)
455 (assert-tree-il->glil
457 (lambda-case ((() #f x #f () (y)) (const 2))
459 (program () (std-prelude 0 0 #f) (label _)
460 (program () (opt-prelude 0 0 0 1 #f)
461 (bind (x #f 0)) (label _)
462 (const 2) (call return 1)
466 (assert-tree-il->glil
468 (lambda-case (((x) #f x1 #f () (y y1)) (const 2))
470 (program () (std-prelude 0 0 #f) (label _)
471 (program () (opt-prelude 1 0 1 2 #f)
472 (bind (x #f 0) (x1 #f 1)) (label _)
473 (const 2) (call return 1)
477 (assert-tree-il->glil
479 (lambda-case (((x) #f x1 #f () (y y1)) (lexical x y))
481 (program () (std-prelude 0 0 #f) (label _)
482 (program () (opt-prelude 1 0 1 2 #f)
483 (bind (x #f 0) (x1 #f 1)) (label _)
484 (lexical #t #f ref 0) (call return 1)
488 (assert-tree-il->glil
490 (lambda-case (((x) #f x1 #f () (y y1)) (lexical x1 y1))
492 (program () (std-prelude 0 0 #f) (label _)
493 (program () (opt-prelude 1 0 1 2 #f)
494 (bind (x #f 0) (x1 #f 1)) (label _)
495 (lexical #t #f ref 1) (call return 1)
499 (assert-tree-il->glil
501 (lambda-case (((x) #f #f #f () (x1))
503 (lambda-case (((y) #f #f #f () (y1))
507 (program () (std-prelude 0 0 #f) (label _)
508 (program () (std-prelude 1 1 #f)
509 (bind (x #f 0)) (label _)
510 (program () (std-prelude 1 1 #f)
511 (bind (y #f 0)) (label _)
512 (lexical #f #f ref 0) (call return 1)
514 (lexical #t #f ref 0)
515 (call make-closure 1)
520 (with-test-prefix "sequence"
521 (assert-tree-il->glil
522 (begin (begin (const 2) (const #f)) (const #t))
523 (program () (std-prelude 0 0 #f) (label _)
524 (const #t) (call return 1)))
526 (assert-tree-il->glil
527 ;; This gets simplified by `peval'.
528 (apply (primitive null?) (begin (const #f) (const 2)))
529 (program () (std-prelude 0 0 #f) (label _)
530 (const #f) (call return 1))))
532 (with-test-prefix "values"
533 (assert-tree-il->glil
534 (apply (primitive values)
535 (apply (primitive values) (const 1) (const 2)))
536 (program () (std-prelude 0 0 #f) (label _)
537 (const 1) (call return 1)))
539 (assert-tree-il->glil
540 (apply (primitive values)
541 (apply (primitive values) (const 1) (const 2))
543 (program () (std-prelude 0 0 #f) (label _)
544 (const 1) (const 3) (call return/values 2)))
546 (assert-tree-il->glil
548 (apply (primitive values) (const 1) (const 2)))
549 (program () (std-prelude 0 0 #f) (label _)
550 (const 1) (call return 1))))
552 ;; FIXME: binding info for or-hacked locals might bork the disassembler,
553 ;; and could be tightened in any case
554 (with-test-prefix "the or hack"
555 (assert-tree-il->glil without-partial-evaluation
556 (let (x) (y) ((const 1))
559 (let (a) (b) ((const 2))
561 (program () (std-prelude 0 1 #f) (label _)
562 (const 1) (bind (x #f 0)) (lexical #t #f set 0)
563 (lexical #t #f ref 0) (branch br-if-not ,l1)
564 (lexical #t #f ref 0) (call return 1)
566 (const 2) (bind (a #f 0)) (lexical #t #f set 0)
567 (lexical #t #f ref 0) (call return 1)
572 ;; second bound var is unreferenced
573 (assert-tree-il->glil without-partial-evaluation
574 (let (x) (y) ((const 1))
577 (let (a) (b) ((const 2))
579 (program () (std-prelude 0 1 #f) (label _)
580 (const 1) (bind (x #f 0)) (lexical #t #f set 0)
581 (lexical #t #f ref 0) (branch br-if-not ,l1)
582 (lexical #t #f ref 0) (call return 1)
584 (lexical #t #f ref 0) (call return 1)
588 (with-test-prefix "apply"
589 (assert-tree-il->glil
590 (apply (primitive @apply) (toplevel foo) (toplevel bar))
591 (program () (std-prelude 0 0 #f) (label _) (toplevel ref foo) (toplevel ref bar) (call tail-apply 2)))
592 (assert-tree-il->glil
593 (begin (apply (primitive @apply) (toplevel foo) (toplevel bar)) (void))
594 (program () (std-prelude 0 0 #f) (label _)
595 (call new-frame 0) (toplevel ref apply) (toplevel ref foo) (toplevel ref bar) (mv-call 2 ,l1)
596 (call drop 1) (branch br ,l2) (label ,l3) (mv-bind 0 #f)
598 (void) (call return 1))
599 (and (eq? l1 l3) (eq? l2 l4)))
600 (assert-tree-il->glil
601 (apply (toplevel foo) (apply (toplevel @apply) (toplevel bar) (toplevel baz)))
602 (program () (std-prelude 0 0 #f) (label _)
604 (call new-frame 0) (toplevel ref bar) (toplevel ref baz) (call apply 2)
605 (call tail-call 1))))
607 (with-test-prefix "call/cc"
608 (assert-tree-il->glil
609 (apply (primitive @call-with-current-continuation) (toplevel foo))
610 (program () (std-prelude 0 0 #f) (label _) (toplevel ref foo) (call tail-call/cc 1)))
611 (assert-tree-il->glil
612 (begin (apply (primitive @call-with-current-continuation) (toplevel foo)) (void))
613 (program () (std-prelude 0 0 #f) (label _)
614 (call new-frame 0) (toplevel ref call-with-current-continuation) (toplevel ref foo) (mv-call 1 ,l1)
615 (call drop 1) (branch br ,l2) (label ,l3) (mv-bind 0 #f)
617 (void) (call return 1))
618 (and (eq? l1 l3) (eq? l2 l4)))
619 (assert-tree-il->glil
620 (apply (toplevel foo)
621 (apply (toplevel @call-with-current-continuation) (toplevel bar)))
622 (program () (std-prelude 0 0 #f) (label _)
624 (toplevel ref bar) (call call/cc 1)
625 (call tail-call 1))))
628 (with-test-prefix "partial evaluation"
631 ;; First order, primitive.
632 (let ((x 1) (y 2)) (+ x y))
636 ;; First order, thunk.
638 (let ((f (lambda () (+ x y))))
642 (pass-if-peval resolve-primitives
643 ;; First order, let-values (requires primitive expansion for
644 ;; `call-with-values'.)
647 (lambda () (if (zero? x) (values 1 2) (values 3 4)))
653 ;; First order, coalesced, mutability preserved.
654 (cons 0 (cons 1 (cons 2 (list 3 4 5))))
655 (apply (primitive list)
656 (const 0) (const 1) (const 2) (const 3) (const 4) (const 5)))
659 ;; First order, coalesced, mutability preserved.
660 (cons 0 (cons 1 (cons 2 (list 3 4 5))))
661 ;; This must not be a constant.
662 (apply (primitive list)
663 (const 0) (const 1) (const 2) (const 3) (const 4) (const 5)))
666 ;; First order, coalesced, immutability preserved.
667 (cons 0 (cons 1 (cons 2 '(3 4 5))))
668 (apply (primitive cons) (const 0)
669 (apply (primitive cons) (const 1)
670 (apply (primitive cons) (const 2)
673 ;; These two tests doesn't work any more because we changed the way we
674 ;; deal with constants -- now the algorithm will see a construction as
675 ;; being bound to the lexical, so it won't propagate it. It can't
676 ;; even propagate it in the case that it is only referenced once,
679 ;; (let ((x (cons 1 2))) (lambda () x))
681 ;; is not the same as
683 ;; (lambda () (cons 1 2))
685 ;; Perhaps if we determined that not only was it only referenced once,
686 ;; it was not closed over by a lambda, then we could propagate it, and
687 ;; re-enable these two tests.
691 ;; First order, mutability preserved.
692 (let loop ((i 3) (r '()))
695 (loop (1- i) (cons (cons i i) r))))
696 (apply (primitive list)
697 (apply (primitive cons) (const 1) (const 1))
698 (apply (primitive cons) (const 2) (const 2))
699 (apply (primitive cons) (const 3) (const 3))))
704 ;; First order, evaluated.
709 (loop (1- i) (cons i r))))
712 ;; Instead here are tests for what happens for the above cases: they
713 ;; unroll but they don't fold.
715 (let loop ((i 3) (r '()))
718 (loop (1- i) (cons (cons i i) r))))
719 (letrec (loop) (_) (_)
721 ((apply (primitive list)
722 (apply (primitive cons) (const 3) (const 3))))
724 ((apply (primitive cons)
725 (apply (primitive cons) (const 2) (const 2))
727 (apply (primitive cons)
728 (apply (primitive cons) (const 1) (const 1))
737 (loop (1- i) (cons i r))))
738 (letrec (loop) (_) (_)
740 ((apply (primitive list) (const 4)))
742 ((apply (primitive cons)
746 ((apply (primitive cons)
750 ((apply (primitive cons)
753 (apply (primitive car)
758 (let loop ((l '(1 2 3 4)) (sum 0))
761 (loop (cdr l) (+ sum (car l)))))
765 ;; Mutability preserved.
766 ((lambda (x y z) (list x y z)) 1 2 3)
767 (apply (primitive list) (const 1) (const 2) (const 3)))
770 ;; Don't propagate effect-free expressions that operate on mutable
776 (let (x) (_) ((apply (primitive list) (const 1)))
777 (let (y) (_) ((apply (primitive car) (lexical x _)))
779 (apply (toplevel set-car!) (lexical x _) (const 0))
783 ;; Don't propagate effect-free expressions that operate on objects we
788 (let (y) (_) ((apply (primitive car) (toplevel x)))
790 (apply (toplevel set-car!) (toplevel x) (const 0))
794 ;; Infinite recursion
795 ((lambda (x) (x x)) (lambda (x) (x x)))
799 (apply (lexical x _) (lexical x _)))))
803 (apply (lexical x _) (lexical x _)))))))
806 ;; First order, aliased primitive.
807 (let* ((x *) (y (x 1 2))) y)
811 ;; First order, shadowed primitive.
813 (define (+ x y) (pk x y))
819 (((x y) #f #f #f () (_ _))
820 (apply (toplevel pk) (lexical x _) (lexical y _))))))
821 (apply (toplevel +) (const 1) (const 2))))
824 ;; First-order, effects preserved.
829 (apply (toplevel do-something!))
833 ;; First order, residual bindings removed.
836 (apply (primitive *) (const 5) (toplevel z)))
839 ;; First order, with lambda.
841 (define (bar z) (* z z))
846 (((x) #f #f #f () (_))
847 (apply (primitive +) (lexical x _) (const 9)))))))
850 ;; First order, with lambda inlined & specialized twice.
851 (let ((f (lambda (x y)
860 (apply (primitive +) ; (f 2 3)
865 (let (x) (_) ((toplevel something)) ; (f something 2)
866 ;; `something' is not const, so preserve order of
867 ;; effects with a lexical binding.
875 ;; First order, with lambda inlined & specialized 3 times.
876 (let ((f (lambda (x y) (if (> x 0) y x))))
883 (const -1) ; (f -1 0)
885 (begin (toplevel y) (const -1)) ; (f -1 y)
886 (toplevel y) ; (f 2 y)
887 (let (x y) (_ _) ((toplevel z) (toplevel y)) ; (f z y)
888 (if (apply (primitive >) (lexical x _) (const 0))
893 ;; First order, conditional.
901 (((x) #f #f #f () (_))
902 (apply (toplevel display) (lexical x _))))))
905 ;; First order, recursive procedure.
906 (letrec ((fibo (lambda (n)
915 ;; Don't propagate toplevel references, as intervening expressions
916 ;; could alter their bindings.
920 (let (x) (_) ((toplevel top))
922 (apply (toplevel foo))
928 (f (* (car x) (cadr x))))
935 ;; Higher order with optional argument (default value).
936 ((lambda* (f x #:optional (y 0))
937 (+ y (f (* (car x) (cadr x)))))
944 ;; Higher order with optional argument (caller-supplied value).
945 ((lambda* (f x #:optional (y 0))
946 (+ y (f (* (car x) (cadr x)))))
954 ;; Higher order with optional argument (side-effecting default
956 ((lambda* (f x #:optional (y (foo)))
957 (+ y (f (* (car x) (cadr x)))))
961 (let (y) (_) ((apply (toplevel foo)))
962 (apply (primitive +) (lexical y _) (const 7))))
965 ;; Higher order with optional argument (caller-supplied value).
966 ((lambda* (f x #:optional (y (foo)))
967 (+ y (f (* (car x) (cadr x)))))
976 ((lambda (f) (f x)) (lambda (x) x))
981 ;; <https://lists.gnu.org/archive/html/bug-guile/2011-09/msg00019.html>.
982 (let ((fold (lambda (f g) (f (g top)))))
983 (fold 1+ (lambda (x) x)))
984 (apply (primitive 1+) (toplevel top)))
987 ;; Procedure not inlined when residual code contains recursive calls.
988 ;; <http://debbugs.gnu.org/9542>
989 (letrec ((fold (lambda (f x3 b null? car cdr)
992 (f (car x3) (fold f (cdr x3) b null? car cdr))))))
993 (fold * x 1 zero? (lambda (x1) x1) (lambda (x2) (- x2 1))))
994 (letrec (fold) (_) (_)
995 (apply (lexical fold _)
1002 (((x1) #f #f #f () (_))
1006 (((x2) #f #f #f () (_))
1007 (apply (primitive -) (lexical x2 _) (const 1))))))))
1009 (pass-if "inlined lambdas are alpha-renamed"
1010 ;; In this example, `make-adder' is inlined more than once; thus,
1011 ;; they should use different gensyms for their arguments, because
1012 ;; the various optimization passes assume uniquely-named variables.
1015 ;; <https://lists.gnu.org/archive/html/bug-guile/2011-09/msg00019.html> and
1016 ;; <https://lists.gnu.org/archive/html/bug-guile/2011-09/msg00029.html>.
1017 (pmatch (unparse-tree-il
1020 (lambda (x) (lambda (y) (+ x y)))))
1021 (cons (make-adder 1) (make-adder 2)))
1023 ((apply (primitive cons)
1026 (((y) #f #f #f () (,gensym1))
1027 (apply (primitive +)
1029 (lexical y ,ref1)))))
1032 (((y) #f #f #f () (,gensym2))
1033 (apply (primitive +)
1035 (lexical y ,ref2))))))
1036 (and (eq? gensym1 ref1)
1038 (not (eq? gensym1 gensym2))))
1042 ;; Higher order, mutually recursive procedures.
1043 (letrec ((even? (lambda (x)
1047 (not (even? (- x 1))))))
1048 (and (even? 4) (odd? 7)))
1052 ;; Below are cases where constant propagation should bail out.
1056 ;; Non-constant lexical is not propagated.
1057 (let ((v (make-vector 6 #f)))
1059 (vector-set! v n n)))
1061 ((apply (toplevel make-vector) (const 6) (const #f)))
1064 (((n) #f #f #f () (_))
1065 (apply (toplevel vector-set!)
1066 (lexical v _) (lexical n _) (lexical n _)))))))
1069 ;; Mutable lexical is not propagated.
1070 (let ((v (vector 1 2 3)))
1074 ((apply (primitive vector) (const 1) (const 2) (const 3)))
1077 ((() #f #f #f () ())
1081 ;; Lexical that is not provably pure is not inlined nor propagated.
1082 (let* ((x (if (> p q) (frob!) (display 'chbouib)))
1085 (let (x) (_) ((if (apply (primitive >) (toplevel p) (toplevel q))
1086 (apply (toplevel frob!))
1087 (apply (toplevel display) (const chbouib))))
1088 (apply (primitive +) (lexical x _) (lexical x _)
1089 (apply (primitive *) (lexical x _) (const 2)))))
1092 ;; Non-constant arguments not propagated to lambdas.
1100 (let (x y z) (_ _ _)
1101 ((apply (primitive vector) (const 1) (const 2) (const 3))
1102 (apply (toplevel make-list) (const 10))
1103 (apply (primitive list) (const 1) (const 2) (const 3)))
1105 (apply (toplevel vector-set!)
1106 (lexical x _) (const 0) (const 0))
1107 (apply (toplevel set-car!)
1108 (lexical y _) (const 0))
1109 (apply (toplevel set-cdr!)
1110 (lexical z _) (const ())))))
1113 (let ((foo top-foo) (bar top-bar))
1114 (let* ((g (lambda (x y) (+ x y)))
1115 (f (lambda (g x) (g x x))))
1116 (+ (f g foo) (f g bar))))
1117 (let (foo bar) (_ _) ((toplevel top-foo) (toplevel top-bar))
1118 (apply (primitive +)
1119 (apply (primitive +) (lexical foo _) (lexical foo _))
1120 (apply (primitive +) (lexical bar _) (lexical bar _)))))
1123 ;; Fresh objects are not turned into constants, nor are constants
1124 ;; turned into fresh objects.
1129 (let (x) (_) ((apply (primitive cons) (const 1) (const (2 3))))
1130 (apply (primitive cons) (const 0) (lexical x _))))
1133 ;; Bindings mutated.
1137 (let (x) (_) ((const 2))
1139 (set! (lexical x _) (const 3))
1143 ;; Bindings mutated.
1148 (frob f) ; may mutate `x'
1150 (letrec (x) (_) ((const 0))
1152 (apply (toplevel frob) (lambda _ _))
1156 ;; Bindings mutated.
1157 (letrec ((f (lambda (x)
1158 (set! f (lambda (_) x))
1164 ;; Bindings possibly mutated.
1165 (let ((x (make-foo)))
1166 (frob! x) ; may mutate `x'
1168 (let (x) (_) ((apply (toplevel make-foo)))
1170 (apply (toplevel frob!) (lexical x _))
1174 ;; Inlining stops at recursive calls with dynamic arguments.
1176 (if (< x 0) x (loop (1- x))))
1177 (letrec (loop) (_) ((lambda (_)
1179 (((x) #f #f #f () (_))
1181 (apply (lexical loop _)
1182 (apply (primitive 1-)
1183 (lexical x _))))))))
1184 (apply (lexical loop _) (toplevel x))))
1187 ;; Recursion on the 2nd argument is fully evaluated.
1189 (let loop ((x x) (y 10))
1193 (let (x) (_) ((apply (toplevel top)))
1194 (letrec (loop) (_) (_)
1195 (apply (toplevel foo) (lexical x _) (const 0)))))
1198 ;; Inlining aborted when residual code contains recursive calls.
1200 ;; <http://debbugs.gnu.org/9542>
1201 (let loop ((x x) (y 0))
1203 (loop (1- x) (1- y))
1206 (loop (1+ x) (1+ y)))))
1207 (letrec (loop) (_) ((lambda (_)
1209 (((x y) #f #f #f () (_ _))
1210 (if (apply (primitive >)
1211 (lexical y _) (const 0))
1213 (apply (lexical loop _) (toplevel x) (const 0))))
1216 ;; Infinite recursion: `peval' gives up and leaves it as is.
1217 (letrec ((f (lambda (x) (g (1- x))))
1218 (g (lambda (x) (h (1+ x))))
1219 (h (lambda (x) (f x))))
1224 ;; Constant folding: cons
1225 (begin (cons 1 2) #f)
1229 ;; Constant folding: cons
1230 (begin (cons (foo) 2) #f)
1231 (begin (apply (toplevel foo)) (const #f)))
1234 ;; Constant folding: cons
1239 ;; Constant folding: car+cons
1244 ;; Constant folding: cdr+cons
1249 ;; Constant folding: car+cons, impure
1250 (car (cons 1 (bar)))
1251 (begin (apply (toplevel bar)) (const 1)))
1254 ;; Constant folding: cdr+cons, impure
1255 (cdr (cons (bar) 0))
1256 (begin (apply (toplevel bar)) (const 0)))
1259 ;; Constant folding: car+list
1264 ;; Constant folding: cdr+list
1266 (apply (primitive list) (const 0)))
1269 ;; Constant folding: car+list, impure
1270 (car (list 1 (bar)))
1271 (begin (apply (toplevel bar)) (const 1)))
1274 ;; Constant folding: cdr+list, impure
1275 (cdr (list (bar) 0))
1276 (begin (apply (toplevel bar)) (apply (primitive list) (const 0))))
1280 ;; Prompt is removed if tag is unreferenced
1281 (let ((tag (make-prompt-tag)))
1282 (call-with-prompt tag
1284 (lambda args args)))
1289 ;; Prompt is removed if tag is unreferenced, with explicit stem
1290 (let ((tag (make-prompt-tag "foo")))
1291 (call-with-prompt tag
1293 (lambda args args)))
1298 (with-test-prefix "tree-il-fold"
1300 (pass-if "empty tree"
1301 (let ((leaf? #f) (up? #f) (down? #f) (mark (list 'mark)))
1303 (tree-il-fold (lambda (x y) (set! leaf? #t) y)
1304 (lambda (x y) (set! down? #t) y)
1305 (lambda (x y) (set! up? #t) y)
1312 (pass-if "lambda and application"
1313 (let* ((leaves '()) (ups '()) (downs '())
1314 (result (tree-il-fold (lambda (x y)
1315 (set! leaves (cons x leaves))
1318 (set! downs (cons x downs))
1321 (set! ups (cons x ups))
1327 (((x y) #f #f #f () (x1 y1))
1332 (and (equal? (map strip-source leaves)
1333 (list (make-lexical-ref #f 'y 'y1)
1334 (make-lexical-ref #f 'x 'x1)
1335 (make-toplevel-ref #f '+)))
1336 (= (length downs) 3)
1337 (equal? (reverse (map strip-source ups))
1338 (map strip-source downs))))))
1345 ;; Make sure we get English messages.
1346 (setlocale LC_ALL "C")
1348 (define (call-with-warnings thunk)
1349 (let ((port (open-output-string)))
1350 (with-fluids ((*current-warning-port* port)
1351 (*current-warning-prefix* ""))
1353 (let ((warnings (get-output-string port)))
1354 (string-tokenize warnings
1355 (char-set-complement (char-set #\newline))))))
1357 (define %opts-w-unused
1358 '(#:warnings (unused-variable)))
1360 (define %opts-w-unused-toplevel
1361 '(#:warnings (unused-toplevel)))
1363 (define %opts-w-unbound
1364 '(#:warnings (unbound-variable)))
1366 (define %opts-w-arity
1367 '(#:warnings (arity-mismatch)))
1369 (define %opts-w-format
1370 '(#:warnings (format)))
1373 (with-test-prefix "warnings"
1375 (pass-if "unknown warning type"
1376 (let ((w (call-with-warnings
1378 (compile #t #:opts '(#:warnings (does-not-exist)))))))
1379 (and (= (length w) 1)
1380 (number? (string-contains (car w) "unknown warning")))))
1382 (with-test-prefix "unused-variable"
1385 (null? (call-with-warnings
1387 (compile '(lambda (x y) (+ x y))
1388 #:opts %opts-w-unused)))))
1390 (pass-if "let/unused"
1391 (let ((w (call-with-warnings
1393 (compile '(lambda (x)
1396 #:opts %opts-w-unused)))))
1397 (and (= (length w) 1)
1398 (number? (string-contains (car w) "unused variable `y'")))))
1400 (pass-if "shadowed variable"
1401 (let ((w (call-with-warnings
1403 (compile '(lambda (x)
1407 #:opts %opts-w-unused)))))
1408 (and (= (length w) 1)
1409 (number? (string-contains (car w) "unused variable `y'")))))
1412 (null? (call-with-warnings
1414 (compile '(lambda ()
1415 (letrec ((x (lambda () (y)))
1416 (y (lambda () (x))))
1418 #:opts %opts-w-unused)))))
1420 (pass-if "unused argument"
1421 ;; Unused arguments should not be reported.
1422 (null? (call-with-warnings
1424 (compile '(lambda (x y z) #t)
1425 #:opts %opts-w-unused)))))
1427 (pass-if "special variable names"
1428 (null? (call-with-warnings
1430 (compile '(lambda ()
1431 (let ((_ 'underscore)
1432 (#{gensym name}# 'ignore-me))
1435 #:opts %opts-w-unused))))))
1437 (with-test-prefix "unused-toplevel"
1439 (pass-if "used after definition"
1440 (null? (call-with-warnings
1442 (let ((in (open-input-string
1443 "(define foo 2) foo")))
1444 (read-and-compile in
1446 #:opts %opts-w-unused-toplevel))))))
1448 (pass-if "used before definition"
1449 (null? (call-with-warnings
1451 (let ((in (open-input-string
1452 "(define (bar) foo) (define foo 2) (bar)")))
1453 (read-and-compile in
1455 #:opts %opts-w-unused-toplevel))))))
1457 (pass-if "unused but public"
1458 (let ((in (open-input-string
1459 "(define-module (test-suite tree-il x) #:export (bar))
1460 (define (bar) #t)")))
1461 (null? (call-with-warnings
1463 (read-and-compile in
1465 #:opts %opts-w-unused-toplevel))))))
1467 (pass-if "unused but public (more)"
1468 (let ((in (open-input-string
1469 "(define-module (test-suite tree-il x) #:export (bar))
1470 (define (bar) (baz))
1471 (define (baz) (foo))
1472 (define (foo) #t)")))
1473 (null? (call-with-warnings
1475 (read-and-compile in
1477 #:opts %opts-w-unused-toplevel))))))
1479 (pass-if "unused but define-public"
1480 (null? (call-with-warnings
1482 (compile '(define-public foo 2)
1484 #:opts %opts-w-unused-toplevel)))))
1486 (pass-if "used by macro"
1487 ;; FIXME: See comment about macros at `unused-toplevel-analysis'.
1490 (null? (call-with-warnings
1492 (let ((in (open-input-string
1493 "(define (bar) 'foo)
1495 (syntax-rules () ((_) (bar))))")))
1496 (read-and-compile in
1498 #:opts %opts-w-unused-toplevel))))))
1501 (let ((w (call-with-warnings
1503 (compile '(define foo 2)
1505 #:opts %opts-w-unused-toplevel)))))
1506 (and (= (length w) 1)
1507 (number? (string-contains (car w)
1508 (format #f "top-level variable `~A'"
1511 (pass-if "unused recursive"
1512 (let ((w (call-with-warnings
1514 (compile '(define (foo) (foo))
1516 #:opts %opts-w-unused-toplevel)))))
1517 (and (= (length w) 1)
1518 (number? (string-contains (car w)
1519 (format #f "top-level variable `~A'"
1522 (pass-if "unused mutually recursive"
1523 (let* ((in (open-input-string
1524 "(define (foo) (bar)) (define (bar) (foo))"))
1525 (w (call-with-warnings
1527 (read-and-compile in
1529 #:opts %opts-w-unused-toplevel)))))
1530 (and (= (length w) 2)
1531 (number? (string-contains (car w)
1532 (format #f "top-level variable `~A'"
1534 (number? (string-contains (cadr w)
1535 (format #f "top-level variable `~A'"
1538 (pass-if "special variable names"
1539 (null? (call-with-warnings
1541 (compile '(define #{gensym name}# 'ignore-me)
1543 #:opts %opts-w-unused-toplevel))))))
1545 (with-test-prefix "unbound variable"
1548 (null? (call-with-warnings
1550 (compile '+ #:opts %opts-w-unbound)))))
1554 (w (call-with-warnings
1558 #:opts %opts-w-unbound)))))
1559 (and (= (length w) 1)
1560 (number? (string-contains (car w)
1561 (format #f "unbound variable `~A'"
1566 (w (call-with-warnings
1568 (compile `(set! ,v 7)
1570 #:opts %opts-w-unbound)))))
1571 (and (= (length w) 1)
1572 (number? (string-contains (car w)
1573 (format #f "unbound variable `~A'"
1576 (pass-if "module-local top-level is visible"
1577 (let ((m (make-module))
1579 (beautify-user-module! m)
1580 (compile `(define ,v 123)
1581 #:env m #:opts %opts-w-unbound)
1582 (null? (call-with-warnings
1587 #:opts %opts-w-unbound))))))
1589 (pass-if "module-local top-level is visible after"
1590 (let ((m (make-module))
1592 (beautify-user-module! m)
1593 (null? (call-with-warnings
1595 (let ((in (open-input-string
1598 (define chbouib 5)")))
1599 (read-and-compile in
1601 #:opts %opts-w-unbound)))))))
1603 (pass-if "optional arguments are visible"
1604 (null? (call-with-warnings
1606 (compile '(lambda* (x #:optional y z) (list x y z))
1607 #:opts %opts-w-unbound
1610 (pass-if "keyword arguments are visible"
1611 (null? (call-with-warnings
1613 (compile '(lambda* (x #:key y z) (list x y z))
1614 #:opts %opts-w-unbound
1617 (pass-if "GOOPS definitions are visible"
1618 (let ((m (make-module))
1620 (beautify-user-module! m)
1621 (module-use! m (resolve-interface '(oop goops)))
1622 (null? (call-with-warnings
1624 (let ((in (open-input-string
1625 "(define-class <foo> ()
1626 (bar #:getter foo-bar))
1627 (define z (foo-bar (make <foo>)))")))
1628 (read-and-compile in
1630 #:opts %opts-w-unbound))))))))
1632 (with-test-prefix "arity mismatch"
1635 (null? (call-with-warnings
1637 (compile '(cons 'a 'b) #:opts %opts-w-arity)))))
1639 (pass-if "direct application"
1640 (let ((w (call-with-warnings
1642 (compile '((lambda (x y) (or x y)) 1 2 3 4 5)
1643 #:opts %opts-w-arity
1645 (and (= (length w) 1)
1646 (number? (string-contains (car w)
1647 "wrong number of arguments to")))))
1649 (let ((w (call-with-warnings
1651 (compile '(let ((f (lambda (x y) (+ x y))))
1653 #:opts %opts-w-arity
1655 (and (= (length w) 1)
1656 (number? (string-contains (car w)
1657 "wrong number of arguments to")))))
1660 (let ((w (call-with-warnings
1662 (compile '(cons 1 2 3 4)
1663 #:opts %opts-w-arity
1665 (and (= (length w) 1)
1666 (number? (string-contains (car w)
1667 "wrong number of arguments to")))))
1669 (pass-if "alias to global"
1670 (let ((w (call-with-warnings
1672 (compile '(let ((f cons)) (f 1 2 3 4))
1673 #:opts %opts-w-arity
1675 (and (= (length w) 1)
1676 (number? (string-contains (car w)
1677 "wrong number of arguments to")))))
1679 (pass-if "alias to lexical to global"
1680 (let ((w (call-with-warnings
1682 (compile '(let ((f number?))
1685 #:opts %opts-w-arity
1687 (and (= (length w) 1)
1688 (number? (string-contains (car w)
1689 "wrong number of arguments to")))))
1691 (pass-if "alias to lexical"
1692 (let ((w (call-with-warnings
1694 (compile '(let ((f (lambda (x y z) (+ x y z))))
1697 #:opts %opts-w-arity
1699 (and (= (length w) 1)
1700 (number? (string-contains (car w)
1701 "wrong number of arguments to")))))
1704 (let ((w (call-with-warnings
1706 (compile '(letrec ((odd? (lambda (x) (even? (1- x))))
1711 #:opts %opts-w-arity
1713 (and (= (length w) 1)
1714 (number? (string-contains (car w)
1715 "wrong number of arguments to")))))
1717 (pass-if "case-lambda"
1718 (null? (call-with-warnings
1720 (compile '(let ((f (case-lambda
1727 #:opts %opts-w-arity
1730 (pass-if "case-lambda with wrong number of arguments"
1731 (let ((w (call-with-warnings
1733 (compile '(let ((f (case-lambda
1737 #:opts %opts-w-arity
1739 (and (= (length w) 1)
1740 (number? (string-contains (car w)
1741 "wrong number of arguments to")))))
1743 (pass-if "case-lambda*"
1744 (null? (call-with-warnings
1746 (compile '(let ((f (case-lambda*
1747 ((x #:optional y) 1)
1749 ((x y #:key z) 3))))
1754 #:opts %opts-w-arity
1757 (pass-if "case-lambda* with wrong arguments"
1758 (let ((w (call-with-warnings
1760 (compile '(let ((f (case-lambda*
1761 ((x #:optional y) 1)
1763 ((x y #:key z) 3))))
1766 #:opts %opts-w-arity
1768 (and (= (length w) 2)
1769 (null? (filter (lambda (w)
1773 w "wrong number of arguments to"))))
1776 (pass-if "local toplevel-defines"
1777 (let ((w (call-with-warnings
1779 (let ((in (open-input-string "
1780 (define (g x) (f x))
1782 (read-and-compile in
1783 #:opts %opts-w-arity
1784 #:to 'assembly))))))
1785 (and (= (length w) 1)
1786 (number? (string-contains (car w)
1787 "wrong number of arguments to")))))
1789 (pass-if "global toplevel alias"
1790 (let ((w (call-with-warnings
1792 (let ((in (open-input-string "
1794 (define (g) (f))")))
1795 (read-and-compile in
1796 #:opts %opts-w-arity
1797 #:to 'assembly))))))
1798 (and (= (length w) 1)
1799 (number? (string-contains (car w)
1800 "wrong number of arguments to")))))
1802 (pass-if "local toplevel overrides global"
1803 (null? (call-with-warnings
1805 (let ((in (open-input-string "
1807 (define (foo x) (cons))")))
1808 (read-and-compile in
1809 #:opts %opts-w-arity
1810 #:to 'assembly))))))
1812 (pass-if "keyword not passed and quiet"
1813 (null? (call-with-warnings
1815 (compile '(let ((f (lambda* (x #:key y) y)))
1817 #:opts %opts-w-arity
1820 (pass-if "keyword passed and quiet"
1821 (null? (call-with-warnings
1823 (compile '(let ((f (lambda* (x #:key y) y)))
1825 #:opts %opts-w-arity
1828 (pass-if "keyword passed to global and quiet"
1829 (null? (call-with-warnings
1831 (let ((in (open-input-string "
1832 (use-modules (system base compile))
1833 (compile '(+ 2 3) #:env (current-module))")))
1834 (read-and-compile in
1835 #:opts %opts-w-arity
1836 #:to 'assembly))))))
1838 (pass-if "extra keyword"
1839 (let ((w (call-with-warnings
1841 (compile '(let ((f (lambda* (x #:key y) y)))
1843 #:opts %opts-w-arity
1845 (and (= (length w) 1)
1846 (number? (string-contains (car w)
1847 "wrong number of arguments to")))))
1849 (pass-if "extra keywords allowed"
1850 (null? (call-with-warnings
1852 (compile '(let ((f (lambda* (x #:key y #:allow-other-keys)
1855 #:opts %opts-w-arity
1856 #:to 'assembly))))))
1858 (with-test-prefix "format"
1860 (pass-if "quiet (no args)"
1861 (null? (call-with-warnings
1863 (compile '(format #t "hey!")
1864 #:opts %opts-w-format
1867 (pass-if "quiet (1 arg)"
1868 (null? (call-with-warnings
1870 (compile '(format #t "hey ~A!" "you")
1871 #:opts %opts-w-format
1874 (pass-if "quiet (2 args)"
1875 (null? (call-with-warnings
1877 (compile '(format #t "~A ~A!" "hello" "world")
1878 #:opts %opts-w-format
1881 (pass-if "wrong port arg"
1882 (let ((w (call-with-warnings
1884 (compile '(format 10 "foo")
1885 #:opts %opts-w-format
1887 (and (= (length w) 1)
1888 (number? (string-contains (car w)
1889 "wrong port argument")))))
1891 (pass-if "non-literal format string"
1892 (let ((w (call-with-warnings
1894 (compile '(format #f fmt)
1895 #:opts %opts-w-format
1897 (and (= (length w) 1)
1898 (number? (string-contains (car w)
1899 "non-literal format string")))))
1901 (pass-if "non-literal format string using gettext"
1902 (null? (call-with-warnings
1904 (compile '(format #t (_ "~A ~A!") "hello" "world")
1905 #:opts %opts-w-format
1908 (pass-if "wrong format string"
1909 (let ((w (call-with-warnings
1911 (compile '(format #f 'not-a-string)
1912 #:opts %opts-w-format
1914 (and (= (length w) 1)
1915 (number? (string-contains (car w)
1916 "wrong format string")))))
1918 (pass-if "wrong number of args"
1919 (let ((w (call-with-warnings
1921 (compile '(format "shbweeb")
1922 #:opts %opts-w-format
1924 (and (= (length w) 1)
1925 (number? (string-contains (car w)
1926 "wrong number of arguments")))))
1928 (pass-if "~%, ~~, ~&, ~t, ~_, and ~\\n"
1929 (null? (call-with-warnings
1931 (compile '(format some-port "~&~3_~~ ~\n~12they~%")
1932 #:opts %opts-w-format
1935 (pass-if "one missing argument"
1936 (let ((w (call-with-warnings
1938 (compile '(format some-port "foo ~A~%")
1939 #:opts %opts-w-format
1941 (and (= (length w) 1)
1942 (number? (string-contains (car w)
1943 "expected 1, got 0")))))
1945 (pass-if "one missing argument, gettext"
1946 (let ((w (call-with-warnings
1948 (compile '(format some-port (_ "foo ~A~%"))
1949 #:opts %opts-w-format
1951 (and (= (length w) 1)
1952 (number? (string-contains (car w)
1953 "expected 1, got 0")))))
1955 (pass-if "two missing arguments"
1956 (let ((w (call-with-warnings
1958 (compile '(format #f "foo ~10,2f and bar ~S~%")
1959 #:opts %opts-w-format
1961 (and (= (length w) 1)
1962 (number? (string-contains (car w)
1963 "expected 2, got 0")))))
1965 (pass-if "one given, one missing argument"
1966 (let ((w (call-with-warnings
1968 (compile '(format #t "foo ~A and ~S~%" hey)
1969 #:opts %opts-w-format
1971 (and (= (length w) 1)
1972 (number? (string-contains (car w)
1973 "expected 2, got 1")))))
1975 (pass-if "too many arguments"
1976 (let ((w (call-with-warnings
1978 (compile '(format #t "foo ~A~%" 1 2)
1979 #:opts %opts-w-format
1981 (and (= (length w) 1)
1982 (number? (string-contains (car w)
1983 "expected 1, got 2")))))
1985 (with-test-prefix "conditionals"
1987 (null? (call-with-warnings
1989 (compile '(format #f "~A ~[foo~;bar~;baz~;~] ~10,2f"
1991 #:opts %opts-w-format
1994 (pass-if "literals with selector"
1995 (let ((w (call-with-warnings
1997 (compile '(format #f "~2[foo~;bar~;baz~;~] ~A"
1999 #:opts %opts-w-format
2001 (and (= (length w) 1)
2002 (number? (string-contains (car w)
2003 "expected 1, got 2")))))
2005 (pass-if "escapes (exact count)"
2006 (let ((w (call-with-warnings
2008 (compile '(format #f "~[~a~;~a~]")
2009 #:opts %opts-w-format
2011 (and (= (length w) 1)
2012 (number? (string-contains (car w)
2013 "expected 2, got 0")))))
2015 (pass-if "escapes with selector"
2016 (let ((w (call-with-warnings
2018 (compile '(format #f "~1[chbouib~;~a~]")
2019 #:opts %opts-w-format
2021 (and (= (length w) 1)
2022 (number? (string-contains (car w)
2023 "expected 1, got 0")))))
2025 (pass-if "escapes, range"
2026 (let ((w (call-with-warnings
2028 (compile '(format #f "~[chbouib~;~a~;~2*~a~]")
2029 #:opts %opts-w-format
2031 (and (= (length w) 1)
2032 (number? (string-contains (car w)
2033 "expected 1 to 4, got 0")))))
2036 (let ((w (call-with-warnings
2038 (compile '(format #f "~@[temperature=~d~]")
2039 #:opts %opts-w-format
2041 (and (= (length w) 1)
2042 (number? (string-contains (car w)
2043 "expected 1, got 0")))))
2046 (let ((w (call-with-warnings
2048 (compile '(format #f "~:[~[hey~;~a~;~va~]~;~3*~]")
2049 #:opts %opts-w-format
2051 (and (= (length w) 1)
2052 (number? (string-contains (car w)
2053 "expected 2 to 4, got 0")))))
2055 (pass-if "unterminated"
2056 (let ((w (call-with-warnings
2058 (compile '(format #f "~[unterminated")
2059 #:opts %opts-w-format
2061 (and (= (length w) 1)
2062 (number? (string-contains (car w)
2063 "unterminated conditional")))))
2065 (pass-if "unexpected ~;"
2066 (let ((w (call-with-warnings
2068 (compile '(format #f "foo~;bar")
2069 #:opts %opts-w-format
2071 (and (= (length w) 1)
2072 (number? (string-contains (car w)
2075 (pass-if "unexpected ~]"
2076 (let ((w (call-with-warnings
2078 (compile '(format #f "foo~]")
2079 #:opts %opts-w-format
2081 (and (= (length w) 1)
2082 (number? (string-contains (car w)
2086 (null? (call-with-warnings
2088 (compile '(format #f "~A ~{~S~} ~A"
2089 'hello '("ladies" "and")
2091 #:opts %opts-w-format
2094 (pass-if "~{...~}, too many args"
2095 (let ((w (call-with-warnings
2097 (compile '(format #f "~{~S~}" 1 2 3)
2098 #:opts %opts-w-format
2100 (and (= (length w) 1)
2101 (number? (string-contains (car w)
2102 "expected 1, got 3")))))
2105 (null? (call-with-warnings
2107 (compile '(format #f "~@{~S~}" 1 2 3)
2108 #:opts %opts-w-format
2111 (pass-if "~@{...~}, too few args"
2112 (let ((w (call-with-warnings
2114 (compile '(format #f "~A ~@{~S~}")
2115 #:opts %opts-w-format
2117 (and (= (length w) 1)
2118 (number? (string-contains (car w)
2119 "expected at least 1, got 0")))))
2121 (pass-if "unterminated ~{...~}"
2122 (let ((w (call-with-warnings
2124 (compile '(format #f "~{")
2125 #:opts %opts-w-format
2127 (and (= (length w) 1)
2128 (number? (string-contains (car w)
2132 (null? (call-with-warnings
2134 (compile '(format #f "~:@(~A ~A~)" 'foo 'bar)
2135 #:opts %opts-w-format
2139 (let ((w (call-with-warnings
2141 (compile '(format #f "~v_foo")
2142 #:opts %opts-w-format
2144 (and (= (length w) 1)
2145 (number? (string-contains (car w)
2146 "expected 1, got 0")))))
2148 (null? (call-with-warnings
2150 (compile '(format #f "~v:@y" 1 123)
2151 #:opts %opts-w-format
2156 (let ((w (call-with-warnings
2158 (compile '(format #f "~2*~a" 'a 'b)
2159 #:opts %opts-w-format
2161 (and (= (length w) 1)
2162 (number? (string-contains (car w)
2163 "expected 3, got 2")))))
2166 (null? (call-with-warnings
2168 (compile '(format #f "~?" "~d ~d" '(1 2))
2169 #:opts %opts-w-format
2172 (pass-if "complex 1"
2173 (let ((w (call-with-warnings
2175 (compile '(format #f
2176 "~4@S ~32S~@[;; ~1{~@?~}~]~@[~61t at ~a~]\n"
2178 #:opts %opts-w-format
2180 (and (= (length w) 1)
2181 (number? (string-contains (car w)
2182 "expected 4, got 6")))))
2184 (pass-if "complex 2"
2185 (let ((w (call-with-warnings
2187 (compile '(format #f
2188 "~:(~A~) Commands~:[~; [abbrev]~]:~2%"
2190 #:opts %opts-w-format
2192 (and (= (length w) 1)
2193 (number? (string-contains (car w)
2194 "expected 2, got 4")))))
2196 (pass-if "complex 3"
2197 (let ((w (call-with-warnings
2199 (compile '(format #f "~9@a~:[~*~3_~;~3d~] ~v:@y~%")
2200 #:opts %opts-w-format
2202 (and (= (length w) 1)
2203 (number? (string-contains (car w)
2204 "expected 5, got 0")))))
2206 (pass-if "ice-9 format"
2207 (let ((w (call-with-warnings
2209 (let ((in (open-input-string
2210 "(use-modules ((ice-9 format)
2211 #:renamer (symbol-prefix-proc 'i9-)))
2212 (i9-format #t \"yo! ~A\" 1 2)")))
2213 (read-and-compile in
2214 #:opts %opts-w-format
2215 #:to 'assembly))))))
2216 (and (= (length w) 1)
2217 (number? (string-contains (car w)
2218 "expected 1, got 2")))))
2220 (pass-if "not format"
2221 (null? (call-with-warnings
2223 (compile '(let ((format chbouib))
2224 (format #t "not ~A a format string"))
2225 #:opts %opts-w-format
2226 #:to 'assembly)))))))