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 "labels allocation"
629 (pass-if "http://debbugs.gnu.org/9769"
630 ((compile '(lambda ()
631 (let ((fail (lambda () #f)))
632 (let ((test (lambda () (fail))))
635 ;; Prevent inlining. We're testing analyze.scm's
636 ;; labels allocator here, and inlining it will
637 ;; reduce the entire thing to #t.
638 #:opts '(#:partial-eval? #f)))))
641 (with-test-prefix "partial evaluation"
644 ;; First order, primitive.
645 (let ((x 1) (y 2)) (+ x y))
649 ;; First order, thunk.
651 (let ((f (lambda () (+ x y))))
655 (pass-if-peval resolve-primitives
656 ;; First order, let-values (requires primitive expansion for
657 ;; `call-with-values'.)
660 (lambda () (if (zero? x) (values 1 2) (values 3 4)))
666 ;; First order, coalesced, mutability preserved.
667 (cons 0 (cons 1 (cons 2 (list 3 4 5))))
668 (apply (primitive list)
669 (const 0) (const 1) (const 2) (const 3) (const 4) (const 5)))
672 ;; First order, coalesced, mutability preserved.
673 (cons 0 (cons 1 (cons 2 (list 3 4 5))))
674 ;; This must not be a constant.
675 (apply (primitive list)
676 (const 0) (const 1) (const 2) (const 3) (const 4) (const 5)))
679 ;; First order, coalesced, immutability preserved.
680 (cons 0 (cons 1 (cons 2 '(3 4 5))))
681 (apply (primitive cons) (const 0)
682 (apply (primitive cons) (const 1)
683 (apply (primitive cons) (const 2)
686 ;; These two tests doesn't work any more because we changed the way we
687 ;; deal with constants -- now the algorithm will see a construction as
688 ;; being bound to the lexical, so it won't propagate it. It can't
689 ;; even propagate it in the case that it is only referenced once,
692 ;; (let ((x (cons 1 2))) (lambda () x))
694 ;; is not the same as
696 ;; (lambda () (cons 1 2))
698 ;; Perhaps if we determined that not only was it only referenced once,
699 ;; it was not closed over by a lambda, then we could propagate it, and
700 ;; re-enable these two tests.
704 ;; First order, mutability preserved.
705 (let loop ((i 3) (r '()))
708 (loop (1- i) (cons (cons i i) r))))
709 (apply (primitive list)
710 (apply (primitive cons) (const 1) (const 1))
711 (apply (primitive cons) (const 2) (const 2))
712 (apply (primitive cons) (const 3) (const 3))))
717 ;; First order, evaluated.
722 (loop (1- i) (cons i r))))
725 ;; Instead here are tests for what happens for the above cases: they
726 ;; unroll but they don't fold.
728 (let loop ((i 3) (r '()))
731 (loop (1- i) (cons (cons i i) r))))
733 ((apply (primitive list)
734 (apply (primitive cons) (const 3) (const 3))))
736 ((apply (primitive cons)
737 (apply (primitive cons) (const 2) (const 2))
739 (apply (primitive cons)
740 (apply (primitive cons) (const 1) (const 1))
749 (loop (1- i) (cons i r))))
751 ((apply (primitive list) (const 4)))
753 ((apply (primitive cons)
757 ((apply (primitive cons)
761 ((apply (primitive cons)
764 (apply (primitive car)
769 (let loop ((l '(1 2 3 4)) (sum 0))
772 (loop (cdr l) (+ sum (car l)))))
775 (pass-if-peval resolve-primitives
787 (string->chars "yo"))
788 (apply (primitive list) (const #\y) (const #\o)))
791 ;; Primitives in module-refs are resolved (the expansion of `pmatch'
792 ;; below leads to calls to (@@ (system base pmatch) car) and
793 ;; similar, which is what we want to be inlined.)
795 (use-modules (system base pmatch))
804 ;; Mutability preserved.
805 ((lambda (x y z) (list x y z)) 1 2 3)
806 (apply (primitive list) (const 1) (const 2) (const 3)))
809 ;; Don't propagate effect-free expressions that operate on mutable
815 (let (x) (_) ((apply (primitive list) (const 1)))
816 (let (y) (_) ((apply (primitive car) (lexical x _)))
818 (apply (toplevel set-car!) (lexical x _) (const 0))
822 ;; Don't propagate effect-free expressions that operate on objects we
827 (let (y) (_) ((apply (primitive car) (toplevel x)))
829 (apply (toplevel set-car!) (toplevel x) (const 0))
833 ;; Infinite recursion
834 ((lambda (x) (x x)) (lambda (x) (x x)))
839 (apply (lexical x _) (lexical x _))))))
840 (apply (lexical x _) (lexical x _))))
843 ;; First order, aliased primitive.
844 (let* ((x *) (y (x 1 2))) y)
848 ;; First order, shadowed primitive.
850 (define (+ x y) (pk x y))
856 (((x y) #f #f #f () (_ _))
857 (apply (toplevel pk) (lexical x _) (lexical y _))))))
858 (apply (toplevel +) (const 1) (const 2))))
861 ;; First-order, effects preserved.
866 (apply (toplevel do-something!))
870 ;; First order, residual bindings removed.
873 (apply (primitive *) (const 5) (toplevel z)))
876 ;; First order, with lambda.
878 (define (bar z) (* z z))
883 (((x) #f #f #f () (_))
884 (apply (primitive +) (lexical x _) (const 9)))))))
887 ;; First order, with lambda inlined & specialized twice.
888 (let ((f (lambda (x y)
897 (apply (primitive +) ; (f 2 3)
902 (let (x) (_) ((toplevel something)) ; (f something 2)
903 ;; `something' is not const, so preserve order of
904 ;; effects with a lexical binding.
912 ;; First order, with lambda inlined & specialized 3 times.
913 (let ((f (lambda (x y) (if (> x 0) y x))))
920 (const -1) ; (f -1 0)
922 (begin (toplevel y) (const -1)) ; (f -1 y)
923 (toplevel y) ; (f 2 y)
924 (let (x y) (_ _) ((toplevel z) (toplevel y)) ; (f z y)
925 (if (apply (primitive >) (lexical x _) (const 0))
930 ;; First order, conditional.
938 (((x) #f #f #f () (_))
939 (apply (toplevel display) (lexical x _))))))
942 ;; First order, recursive procedure.
943 (letrec ((fibo (lambda (n)
952 ;; Don't propagate toplevel references, as intervening expressions
953 ;; could alter their bindings.
957 (let (x) (_) ((toplevel top))
959 (apply (toplevel foo))
965 (f (* (car x) (cadr x))))
972 ;; Higher order with optional argument (default value).
973 ((lambda* (f x #:optional (y 0))
974 (+ y (f (* (car x) (cadr x)))))
981 ;; Higher order with optional argument (caller-supplied value).
982 ((lambda* (f x #:optional (y 0))
983 (+ y (f (* (car x) (cadr x)))))
991 ;; Higher order with optional argument (side-effecting default
993 ((lambda* (f x #:optional (y (foo)))
994 (+ y (f (* (car x) (cadr x)))))
998 (let (y) (_) ((apply (toplevel foo)))
999 (apply (primitive +) (lexical y _) (const 7))))
1002 ;; Higher order with optional argument (caller-supplied value).
1003 ((lambda* (f x #:optional (y (foo)))
1004 (+ y (f (* (car x) (cadr x)))))
1013 ((lambda (f) (f x)) (lambda (x) x))
1018 ;; <https://lists.gnu.org/archive/html/bug-guile/2011-09/msg00019.html>.
1019 (let ((fold (lambda (f g) (f (g top)))))
1020 (fold 1+ (lambda (x) x)))
1021 (apply (primitive 1+) (toplevel top)))
1024 ;; Procedure not inlined when residual code contains recursive calls.
1025 ;; <http://debbugs.gnu.org/9542>
1026 (letrec ((fold (lambda (f x3 b null? car cdr)
1029 (f (car x3) (fold f (cdr x3) b null? car cdr))))))
1030 (fold * x 1 zero? (lambda (x1) x1) (lambda (x2) (- x2 1))))
1031 (letrec (fold) (_) (_)
1032 (apply (lexical fold _)
1039 (((x1) #f #f #f () (_))
1043 (((x2) #f #f #f () (_))
1044 (apply (primitive -) (lexical x2 _) (const 1))))))))
1046 (pass-if "inlined lambdas are alpha-renamed"
1047 ;; In this example, `make-adder' is inlined more than once; thus,
1048 ;; they should use different gensyms for their arguments, because
1049 ;; the various optimization passes assume uniquely-named variables.
1052 ;; <https://lists.gnu.org/archive/html/bug-guile/2011-09/msg00019.html> and
1053 ;; <https://lists.gnu.org/archive/html/bug-guile/2011-09/msg00029.html>.
1054 (pmatch (unparse-tree-il
1057 (lambda (x) (lambda (y) (+ x y)))))
1058 (cons (make-adder 1) (make-adder 2)))
1060 ((apply (primitive cons)
1063 (((y) #f #f #f () (,gensym1))
1064 (apply (primitive +)
1066 (lexical y ,ref1)))))
1069 (((y) #f #f #f () (,gensym2))
1070 (apply (primitive +)
1072 (lexical y ,ref2))))))
1073 (and (eq? gensym1 ref1)
1075 (not (eq? gensym1 gensym2))))
1079 ;; Unused letrec bindings are pruned.
1080 (letrec ((a (lambda () (b)))
1087 ;; Unused letrec bindings are pruned.
1092 (begin (apply (toplevel foo!))
1096 ;; Higher order, mutually recursive procedures.
1097 (letrec ((even? (lambda (x)
1102 (and (even? 4) (odd? 7)))
1106 ;; Memv with constants.
1111 ;; Memv with non-constant list. It could fold but doesn't
1113 (memv 1 (list 3 2 1))
1114 (apply (primitive memv)
1116 (apply (primitive list) (const 3) (const 2) (const 1))))
1119 ;; Memv with non-constant key, constant list, test context
1123 (if (let (t) (_) ((toplevel foo))
1124 (if (apply (primitive eqv?) (lexical t _) (const 3))
1126 (if (apply (primitive eqv?) (lexical t _) (const 2))
1128 (apply (primitive eqv?) (lexical t _) (const 1)))))
1133 ;; Memv with non-constant key, empty list, test context. Currently
1134 ;; doesn't fold entirely.
1138 (if (begin (toplevel foo) (const #f))
1143 ;; Below are cases where constant propagation should bail out.
1147 ;; Non-constant lexical is not propagated.
1148 (let ((v (make-vector 6 #f)))
1150 (vector-set! v n n)))
1152 ((apply (toplevel make-vector) (const 6) (const #f)))
1155 (((n) #f #f #f () (_))
1156 (apply (toplevel vector-set!)
1157 (lexical v _) (lexical n _) (lexical n _)))))))
1160 ;; Mutable lexical is not propagated.
1161 (let ((v (vector 1 2 3)))
1165 ((apply (primitive vector) (const 1) (const 2) (const 3)))
1168 ((() #f #f #f () ())
1172 ;; Lexical that is not provably pure is not inlined nor propagated.
1173 (let* ((x (if (> p q) (frob!) (display 'chbouib)))
1176 (let (x) (_) ((if (apply (primitive >) (toplevel p) (toplevel q))
1177 (apply (toplevel frob!))
1178 (apply (toplevel display) (const chbouib))))
1179 (let (y) (_) ((apply (primitive *) (lexical x _) (const 2)))
1180 (apply (primitive +)
1181 (lexical x _) (lexical x _) (lexical y _)))))
1184 ;; Non-constant arguments not propagated to lambdas.
1192 (let (x y z) (_ _ _)
1193 ((apply (primitive vector) (const 1) (const 2) (const 3))
1194 (apply (toplevel make-list) (const 10))
1195 (apply (primitive list) (const 1) (const 2) (const 3)))
1197 (apply (toplevel vector-set!)
1198 (lexical x _) (const 0) (const 0))
1199 (apply (toplevel set-car!)
1200 (lexical y _) (const 0))
1201 (apply (toplevel set-cdr!)
1202 (lexical z _) (const ())))))
1205 (let ((foo top-foo) (bar top-bar))
1206 (let* ((g (lambda (x y) (+ x y)))
1207 (f (lambda (g x) (g x x))))
1208 (+ (f g foo) (f g bar))))
1209 (let (foo bar) (_ _) ((toplevel top-foo) (toplevel top-bar))
1210 (apply (primitive +)
1211 (apply (primitive +) (lexical foo _) (lexical foo _))
1212 (apply (primitive +) (lexical bar _) (lexical bar _)))))
1215 ;; Fresh objects are not turned into constants, nor are constants
1216 ;; turned into fresh objects.
1221 (let (x) (_) ((apply (primitive cons) (const 1) (const (2 3))))
1222 (apply (primitive cons) (const 0) (lexical x _))))
1225 ;; Bindings mutated.
1229 (let (x) (_) ((const 2))
1231 (set! (lexical x _) (const 3))
1235 ;; Bindings mutated.
1240 (frob f) ; may mutate `x'
1242 (letrec (x) (_) ((const 0))
1244 (apply (toplevel frob) (lambda _ _))
1248 ;; Bindings mutated.
1249 (letrec ((f (lambda (x)
1250 (set! f (lambda (_) x))
1256 ;; Bindings possibly mutated.
1257 (let ((x (make-foo)))
1258 (frob! x) ; may mutate `x'
1260 (let (x) (_) ((apply (toplevel make-foo)))
1262 (apply (toplevel frob!) (lexical x _))
1266 ;; Inlining stops at recursive calls with dynamic arguments.
1268 (if (< x 0) x (loop (1- x))))
1269 (letrec (loop) (_) ((lambda (_)
1271 (((x) #f #f #f () (_))
1273 (apply (lexical loop _)
1274 (apply (primitive 1-)
1275 (lexical x _))))))))
1276 (apply (lexical loop _) (toplevel x))))
1279 ;; Recursion on the 2nd argument is fully evaluated.
1281 (let loop ((x x) (y 10))
1285 (let (x) (_) ((apply (toplevel top)))
1286 (apply (toplevel foo) (lexical x _) (const 0))))
1289 ;; Inlining aborted when residual code contains recursive calls.
1291 ;; <http://debbugs.gnu.org/9542>
1292 (let loop ((x x) (y 0))
1294 (loop (1- x) (1- y))
1297 (loop (1+ x) (1+ y)))))
1298 (letrec (loop) (_) ((lambda (_)
1300 (((x y) #f #f #f () (_ _))
1301 (if (apply (primitive >)
1302 (lexical y _) (const 0))
1304 (apply (lexical loop _) (toplevel x) (const 0))))
1307 ;; Infinite recursion: `peval' gives up and leaves it as is.
1308 (letrec ((f (lambda (x) (g (1- x))))
1309 (g (lambda (x) (h (1+ x))))
1310 (h (lambda (x) (f x))))
1315 ;; Infinite recursion: all the arguments to `loop' are static, but
1316 ;; unrolling it would lead `peval' to enter an infinite loop.
1320 (letrec (loop) (_) ((lambda . _))
1321 (apply (lexical loop _) (const 0))))
1324 ;; This test checks that the `start' binding is indeed residualized.
1325 ;; See the `referenced?' procedure in peval's `prune-bindings'.
1327 (set! pos 1) ;; Cause references to `pos' to residualize.
1328 (let ((here (let ((start pos)) (lambda () start))))
1330 (let (pos) (_) ((const 0))
1332 (set! (lexical pos _) (const 1))
1334 (apply (lexical here _))))))
1337 ;; FIXME: should this one residualize the binding?
1343 ;; This is a fun one for peval to handle.
1346 (letrec (a) (_) ((lexical a _))
1350 ;; Another interesting recursive case.
1351 (letrec ((a b) (b a))
1353 (letrec (a) (_) ((lexical a _))
1357 ;; Another pruning case, that `a' is residualized.
1358 (letrec ((a (lambda () (a)))
1364 ;; "b c a" is the current order that we get with unordered letrec,
1365 ;; but it's not important to this test, so if it changes, just adapt
1367 (letrec (b c a) (_ _ _)
1370 ((() #f #f #f () ())
1371 (apply (lexical a _)))))
1374 (((x) #f #f #f () (_))
1378 ((() #f #f #f () ())
1379 (apply (lexical a _))))))
1382 ((apply (toplevel foo) (lexical b _)))
1383 (apply (lexical c _)
1387 ;; In this case, we can prune the bindings. `a' ends up being copied
1388 ;; because it is only referenced once in the source program. Oh
1390 (letrec* ((a (lambda (x) (top x)))
1393 (apply (toplevel foo)
1396 (((x) #f #f #f () (_))
1397 (apply (toplevel top) (lexical x _)))))
1400 (((x) #f #f #f () (_))
1401 (apply (toplevel top) (lexical x _)))))))
1404 ;; Constant folding: cons
1405 (begin (cons 1 2) #f)
1409 ;; Constant folding: cons
1410 (begin (cons (foo) 2) #f)
1411 (begin (apply (toplevel foo)) (const #f)))
1414 ;; Constant folding: cons
1419 ;; Constant folding: car+cons
1424 ;; Constant folding: cdr+cons
1429 ;; Constant folding: car+cons, impure
1430 (car (cons 1 (bar)))
1431 (begin (apply (toplevel bar)) (const 1)))
1434 ;; Constant folding: cdr+cons, impure
1435 (cdr (cons (bar) 0))
1436 (begin (apply (toplevel bar)) (const 0)))
1439 ;; Constant folding: car+list
1444 ;; Constant folding: cdr+list
1446 (apply (primitive list) (const 0)))
1449 ;; Constant folding: car+list, impure
1450 (car (list 1 (bar)))
1451 (begin (apply (toplevel bar)) (const 1)))
1454 ;; Constant folding: cdr+list, impure
1455 (cdr (list (bar) 0))
1456 (begin (apply (toplevel bar)) (apply (primitive list) (const 0))))
1460 ;; Prompt is removed if tag is unreferenced
1461 (let ((tag (make-prompt-tag)))
1462 (call-with-prompt tag
1464 (lambda args args)))
1469 ;; Prompt is removed if tag is unreferenced, with explicit stem
1470 (let ((tag (make-prompt-tag "foo")))
1471 (call-with-prompt tag
1473 (lambda args args)))
1478 ;; `while' without `break' or `continue' has no prompts and gets its
1479 ;; condition folded. Unfortunately the outer `lp' does not yet get
1485 ((() #f #f #f () ())
1489 ((() #f #f #f () ())
1490 (apply (lexical loop _))))))
1491 (apply (lexical loop _)))))))
1492 (apply (lexical lp _)))))
1496 (with-test-prefix "tree-il-fold"
1498 (pass-if "empty tree"
1499 (let ((leaf? #f) (up? #f) (down? #f) (mark (list 'mark)))
1501 (tree-il-fold (lambda (x y) (set! leaf? #t) y)
1502 (lambda (x y) (set! down? #t) y)
1503 (lambda (x y) (set! up? #t) y)
1510 (pass-if "lambda and application"
1511 (let* ((leaves '()) (ups '()) (downs '())
1512 (result (tree-il-fold (lambda (x y)
1513 (set! leaves (cons x leaves))
1516 (set! downs (cons x downs))
1519 (set! ups (cons x ups))
1525 (((x y) #f #f #f () (x1 y1))
1530 (and (equal? (map strip-source leaves)
1531 (list (make-lexical-ref #f 'y 'y1)
1532 (make-lexical-ref #f 'x 'x1)
1533 (make-toplevel-ref #f '+)))
1534 (= (length downs) 3)
1535 (equal? (reverse (map strip-source ups))
1536 (map strip-source downs))))))
1543 ;; Make sure we get English messages.
1544 (setlocale LC_ALL "C")
1546 (define (call-with-warnings thunk)
1547 (let ((port (open-output-string)))
1548 (with-fluids ((*current-warning-port* port)
1549 (*current-warning-prefix* ""))
1551 (let ((warnings (get-output-string port)))
1552 (string-tokenize warnings
1553 (char-set-complement (char-set #\newline))))))
1555 (define %opts-w-unused
1556 '(#:warnings (unused-variable)))
1558 (define %opts-w-unused-toplevel
1559 '(#:warnings (unused-toplevel)))
1561 (define %opts-w-unbound
1562 '(#:warnings (unbound-variable)))
1564 (define %opts-w-arity
1565 '(#:warnings (arity-mismatch)))
1567 (define %opts-w-format
1568 '(#:warnings (format)))
1571 (with-test-prefix "warnings"
1573 (pass-if "unknown warning type"
1574 (let ((w (call-with-warnings
1576 (compile #t #:opts '(#:warnings (does-not-exist)))))))
1577 (and (= (length w) 1)
1578 (number? (string-contains (car w) "unknown warning")))))
1580 (with-test-prefix "unused-variable"
1583 (null? (call-with-warnings
1585 (compile '(lambda (x y) (+ x y))
1586 #:opts %opts-w-unused)))))
1588 (pass-if "let/unused"
1589 (let ((w (call-with-warnings
1591 (compile '(lambda (x)
1594 #:opts %opts-w-unused)))))
1595 (and (= (length w) 1)
1596 (number? (string-contains (car w) "unused variable `y'")))))
1598 (pass-if "shadowed variable"
1599 (let ((w (call-with-warnings
1601 (compile '(lambda (x)
1605 #:opts %opts-w-unused)))))
1606 (and (= (length w) 1)
1607 (number? (string-contains (car w) "unused variable `y'")))))
1610 (null? (call-with-warnings
1612 (compile '(lambda ()
1613 (letrec ((x (lambda () (y)))
1614 (y (lambda () (x))))
1616 #:opts %opts-w-unused)))))
1618 (pass-if "unused argument"
1619 ;; Unused arguments should not be reported.
1620 (null? (call-with-warnings
1622 (compile '(lambda (x y z) #t)
1623 #:opts %opts-w-unused)))))
1625 (pass-if "special variable names"
1626 (null? (call-with-warnings
1628 (compile '(lambda ()
1629 (let ((_ 'underscore)
1630 (#{gensym name}# 'ignore-me))
1633 #:opts %opts-w-unused))))))
1635 (with-test-prefix "unused-toplevel"
1637 (pass-if "used after definition"
1638 (null? (call-with-warnings
1640 (let ((in (open-input-string
1641 "(define foo 2) foo")))
1642 (read-and-compile in
1644 #:opts %opts-w-unused-toplevel))))))
1646 (pass-if "used before definition"
1647 (null? (call-with-warnings
1649 (let ((in (open-input-string
1650 "(define (bar) foo) (define foo 2) (bar)")))
1651 (read-and-compile in
1653 #:opts %opts-w-unused-toplevel))))))
1655 (pass-if "unused but public"
1656 (let ((in (open-input-string
1657 "(define-module (test-suite tree-il x) #:export (bar))
1658 (define (bar) #t)")))
1659 (null? (call-with-warnings
1661 (read-and-compile in
1663 #:opts %opts-w-unused-toplevel))))))
1665 (pass-if "unused but public (more)"
1666 (let ((in (open-input-string
1667 "(define-module (test-suite tree-il x) #:export (bar))
1668 (define (bar) (baz))
1669 (define (baz) (foo))
1670 (define (foo) #t)")))
1671 (null? (call-with-warnings
1673 (read-and-compile in
1675 #:opts %opts-w-unused-toplevel))))))
1677 (pass-if "unused but define-public"
1678 (null? (call-with-warnings
1680 (compile '(define-public foo 2)
1682 #:opts %opts-w-unused-toplevel)))))
1684 (pass-if "used by macro"
1685 ;; FIXME: See comment about macros at `unused-toplevel-analysis'.
1688 (null? (call-with-warnings
1690 (let ((in (open-input-string
1691 "(define (bar) 'foo)
1693 (syntax-rules () ((_) (bar))))")))
1694 (read-and-compile in
1696 #:opts %opts-w-unused-toplevel))))))
1699 (let ((w (call-with-warnings
1701 (compile '(define foo 2)
1703 #:opts %opts-w-unused-toplevel)))))
1704 (and (= (length w) 1)
1705 (number? (string-contains (car w)
1706 (format #f "top-level variable `~A'"
1709 (pass-if "unused recursive"
1710 (let ((w (call-with-warnings
1712 (compile '(define (foo) (foo))
1714 #:opts %opts-w-unused-toplevel)))))
1715 (and (= (length w) 1)
1716 (number? (string-contains (car w)
1717 (format #f "top-level variable `~A'"
1720 (pass-if "unused mutually recursive"
1721 (let* ((in (open-input-string
1722 "(define (foo) (bar)) (define (bar) (foo))"))
1723 (w (call-with-warnings
1725 (read-and-compile in
1727 #:opts %opts-w-unused-toplevel)))))
1728 (and (= (length w) 2)
1729 (number? (string-contains (car w)
1730 (format #f "top-level variable `~A'"
1732 (number? (string-contains (cadr w)
1733 (format #f "top-level variable `~A'"
1736 (pass-if "special variable names"
1737 (null? (call-with-warnings
1739 (compile '(define #{gensym name}# 'ignore-me)
1741 #:opts %opts-w-unused-toplevel))))))
1743 (with-test-prefix "unbound variable"
1746 (null? (call-with-warnings
1748 (compile '+ #:opts %opts-w-unbound)))))
1752 (w (call-with-warnings
1756 #:opts %opts-w-unbound)))))
1757 (and (= (length w) 1)
1758 (number? (string-contains (car w)
1759 (format #f "unbound variable `~A'"
1764 (w (call-with-warnings
1766 (compile `(set! ,v 7)
1768 #:opts %opts-w-unbound)))))
1769 (and (= (length w) 1)
1770 (number? (string-contains (car w)
1771 (format #f "unbound variable `~A'"
1774 (pass-if "module-local top-level is visible"
1775 (let ((m (make-module))
1777 (beautify-user-module! m)
1778 (compile `(define ,v 123)
1779 #:env m #:opts %opts-w-unbound)
1780 (null? (call-with-warnings
1785 #:opts %opts-w-unbound))))))
1787 (pass-if "module-local top-level is visible after"
1788 (let ((m (make-module))
1790 (beautify-user-module! m)
1791 (null? (call-with-warnings
1793 (let ((in (open-input-string
1796 (define chbouib 5)")))
1797 (read-and-compile in
1799 #:opts %opts-w-unbound)))))))
1801 (pass-if "optional arguments are visible"
1802 (null? (call-with-warnings
1804 (compile '(lambda* (x #:optional y z) (list x y z))
1805 #:opts %opts-w-unbound
1808 (pass-if "keyword arguments are visible"
1809 (null? (call-with-warnings
1811 (compile '(lambda* (x #:key y z) (list x y z))
1812 #:opts %opts-w-unbound
1815 (pass-if "GOOPS definitions are visible"
1816 (let ((m (make-module))
1818 (beautify-user-module! m)
1819 (module-use! m (resolve-interface '(oop goops)))
1820 (null? (call-with-warnings
1822 (let ((in (open-input-string
1823 "(define-class <foo> ()
1824 (bar #:getter foo-bar))
1825 (define z (foo-bar (make <foo>)))")))
1826 (read-and-compile in
1828 #:opts %opts-w-unbound))))))))
1830 (with-test-prefix "arity mismatch"
1833 (null? (call-with-warnings
1835 (compile '(cons 'a 'b) #:opts %opts-w-arity)))))
1837 (pass-if "direct application"
1838 (let ((w (call-with-warnings
1840 (compile '((lambda (x y) (or x y)) 1 2 3 4 5)
1841 #:opts %opts-w-arity
1843 (and (= (length w) 1)
1844 (number? (string-contains (car w)
1845 "wrong number of arguments to")))))
1847 (let ((w (call-with-warnings
1849 (compile '(let ((f (lambda (x y) (+ x y))))
1851 #:opts %opts-w-arity
1853 (and (= (length w) 1)
1854 (number? (string-contains (car w)
1855 "wrong number of arguments to")))))
1858 (let ((w (call-with-warnings
1860 (compile '(cons 1 2 3 4)
1861 #:opts %opts-w-arity
1863 (and (= (length w) 1)
1864 (number? (string-contains (car w)
1865 "wrong number of arguments to")))))
1867 (pass-if "alias to global"
1868 (let ((w (call-with-warnings
1870 (compile '(let ((f cons)) (f 1 2 3 4))
1871 #:opts %opts-w-arity
1873 (and (= (length w) 1)
1874 (number? (string-contains (car w)
1875 "wrong number of arguments to")))))
1877 (pass-if "alias to lexical to global"
1878 (let ((w (call-with-warnings
1880 (compile '(let ((f number?))
1883 #:opts %opts-w-arity
1885 (and (= (length w) 1)
1886 (number? (string-contains (car w)
1887 "wrong number of arguments to")))))
1889 (pass-if "alias to lexical"
1890 (let ((w (call-with-warnings
1892 (compile '(let ((f (lambda (x y z) (+ x y z))))
1895 #:opts %opts-w-arity
1897 (and (= (length w) 1)
1898 (number? (string-contains (car w)
1899 "wrong number of arguments to")))))
1902 (let ((w (call-with-warnings
1904 (compile '(letrec ((odd? (lambda (x) (even? (1- x))))
1909 #:opts %opts-w-arity
1911 (and (= (length w) 1)
1912 (number? (string-contains (car w)
1913 "wrong number of arguments to")))))
1915 (pass-if "case-lambda"
1916 (null? (call-with-warnings
1918 (compile '(let ((f (case-lambda
1925 #:opts %opts-w-arity
1928 (pass-if "case-lambda with wrong number of arguments"
1929 (let ((w (call-with-warnings
1931 (compile '(let ((f (case-lambda
1935 #:opts %opts-w-arity
1937 (and (= (length w) 1)
1938 (number? (string-contains (car w)
1939 "wrong number of arguments to")))))
1941 (pass-if "case-lambda*"
1942 (null? (call-with-warnings
1944 (compile '(let ((f (case-lambda*
1945 ((x #:optional y) 1)
1947 ((x y #:key z) 3))))
1952 #:opts %opts-w-arity
1955 (pass-if "case-lambda* with wrong arguments"
1956 (let ((w (call-with-warnings
1958 (compile '(let ((f (case-lambda*
1959 ((x #:optional y) 1)
1961 ((x y #:key z) 3))))
1964 #:opts %opts-w-arity
1966 (and (= (length w) 2)
1967 (null? (filter (lambda (w)
1971 w "wrong number of arguments to"))))
1974 (pass-if "local toplevel-defines"
1975 (let ((w (call-with-warnings
1977 (let ((in (open-input-string "
1978 (define (g x) (f x))
1980 (read-and-compile in
1981 #:opts %opts-w-arity
1982 #:to 'assembly))))))
1983 (and (= (length w) 1)
1984 (number? (string-contains (car w)
1985 "wrong number of arguments to")))))
1987 (pass-if "global toplevel alias"
1988 (let ((w (call-with-warnings
1990 (let ((in (open-input-string "
1992 (define (g) (f))")))
1993 (read-and-compile in
1994 #:opts %opts-w-arity
1995 #:to 'assembly))))))
1996 (and (= (length w) 1)
1997 (number? (string-contains (car w)
1998 "wrong number of arguments to")))))
2000 (pass-if "local toplevel overrides global"
2001 (null? (call-with-warnings
2003 (let ((in (open-input-string "
2005 (define (foo x) (cons))")))
2006 (read-and-compile in
2007 #:opts %opts-w-arity
2008 #:to 'assembly))))))
2010 (pass-if "keyword not passed and quiet"
2011 (null? (call-with-warnings
2013 (compile '(let ((f (lambda* (x #:key y) y)))
2015 #:opts %opts-w-arity
2018 (pass-if "keyword passed and quiet"
2019 (null? (call-with-warnings
2021 (compile '(let ((f (lambda* (x #:key y) y)))
2023 #:opts %opts-w-arity
2026 (pass-if "keyword passed to global and quiet"
2027 (null? (call-with-warnings
2029 (let ((in (open-input-string "
2030 (use-modules (system base compile))
2031 (compile '(+ 2 3) #:env (current-module))")))
2032 (read-and-compile in
2033 #:opts %opts-w-arity
2034 #:to 'assembly))))))
2036 (pass-if "extra keyword"
2037 (let ((w (call-with-warnings
2039 (compile '(let ((f (lambda* (x #:key y) y)))
2041 #:opts %opts-w-arity
2043 (and (= (length w) 1)
2044 (number? (string-contains (car w)
2045 "wrong number of arguments to")))))
2047 (pass-if "extra keywords allowed"
2048 (null? (call-with-warnings
2050 (compile '(let ((f (lambda* (x #:key y #:allow-other-keys)
2053 #:opts %opts-w-arity
2054 #:to 'assembly))))))
2056 (with-test-prefix "format"
2058 (pass-if "quiet (no args)"
2059 (null? (call-with-warnings
2061 (compile '(format #t "hey!")
2062 #:opts %opts-w-format
2065 (pass-if "quiet (1 arg)"
2066 (null? (call-with-warnings
2068 (compile '(format #t "hey ~A!" "you")
2069 #:opts %opts-w-format
2072 (pass-if "quiet (2 args)"
2073 (null? (call-with-warnings
2075 (compile '(format #t "~A ~A!" "hello" "world")
2076 #:opts %opts-w-format
2079 (pass-if "wrong port arg"
2080 (let ((w (call-with-warnings
2082 (compile '(format 10 "foo")
2083 #:opts %opts-w-format
2085 (and (= (length w) 1)
2086 (number? (string-contains (car w)
2087 "wrong port argument")))))
2089 (pass-if "non-literal format string"
2090 (let ((w (call-with-warnings
2092 (compile '(format #f fmt)
2093 #:opts %opts-w-format
2095 (and (= (length w) 1)
2096 (number? (string-contains (car w)
2097 "non-literal format string")))))
2099 (pass-if "non-literal format string using gettext"
2100 (null? (call-with-warnings
2102 (compile '(format #t (_ "~A ~A!") "hello" "world")
2103 #:opts %opts-w-format
2106 (pass-if "wrong format string"
2107 (let ((w (call-with-warnings
2109 (compile '(format #f 'not-a-string)
2110 #:opts %opts-w-format
2112 (and (= (length w) 1)
2113 (number? (string-contains (car w)
2114 "wrong format string")))))
2116 (pass-if "wrong number of args"
2117 (let ((w (call-with-warnings
2119 (compile '(format "shbweeb")
2120 #:opts %opts-w-format
2122 (and (= (length w) 1)
2123 (number? (string-contains (car w)
2124 "wrong number of arguments")))))
2126 (pass-if "~%, ~~, ~&, ~t, ~_, and ~\\n"
2127 (null? (call-with-warnings
2129 (compile '(format some-port "~&~3_~~ ~\n~12they~%")
2130 #:opts %opts-w-format
2133 (pass-if "one missing argument"
2134 (let ((w (call-with-warnings
2136 (compile '(format some-port "foo ~A~%")
2137 #:opts %opts-w-format
2139 (and (= (length w) 1)
2140 (number? (string-contains (car w)
2141 "expected 1, got 0")))))
2143 (pass-if "one missing argument, gettext"
2144 (let ((w (call-with-warnings
2146 (compile '(format some-port (_ "foo ~A~%"))
2147 #:opts %opts-w-format
2149 (and (= (length w) 1)
2150 (number? (string-contains (car w)
2151 "expected 1, got 0")))))
2153 (pass-if "two missing arguments"
2154 (let ((w (call-with-warnings
2156 (compile '(format #f "foo ~10,2f and bar ~S~%")
2157 #:opts %opts-w-format
2159 (and (= (length w) 1)
2160 (number? (string-contains (car w)
2161 "expected 2, got 0")))))
2163 (pass-if "one given, one missing argument"
2164 (let ((w (call-with-warnings
2166 (compile '(format #t "foo ~A and ~S~%" hey)
2167 #:opts %opts-w-format
2169 (and (= (length w) 1)
2170 (number? (string-contains (car w)
2171 "expected 2, got 1")))))
2173 (pass-if "too many arguments"
2174 (let ((w (call-with-warnings
2176 (compile '(format #t "foo ~A~%" 1 2)
2177 #:opts %opts-w-format
2179 (and (= (length w) 1)
2180 (number? (string-contains (car w)
2181 "expected 1, got 2")))))
2183 (with-test-prefix "conditionals"
2185 (null? (call-with-warnings
2187 (compile '(format #f "~A ~[foo~;bar~;baz~;~] ~10,2f"
2189 #:opts %opts-w-format
2192 (pass-if "literals with selector"
2193 (let ((w (call-with-warnings
2195 (compile '(format #f "~2[foo~;bar~;baz~;~] ~A"
2197 #:opts %opts-w-format
2199 (and (= (length w) 1)
2200 (number? (string-contains (car w)
2201 "expected 1, got 2")))))
2203 (pass-if "escapes (exact count)"
2204 (let ((w (call-with-warnings
2206 (compile '(format #f "~[~a~;~a~]")
2207 #:opts %opts-w-format
2209 (and (= (length w) 1)
2210 (number? (string-contains (car w)
2211 "expected 2, got 0")))))
2213 (pass-if "escapes with selector"
2214 (let ((w (call-with-warnings
2216 (compile '(format #f "~1[chbouib~;~a~]")
2217 #:opts %opts-w-format
2219 (and (= (length w) 1)
2220 (number? (string-contains (car w)
2221 "expected 1, got 0")))))
2223 (pass-if "escapes, range"
2224 (let ((w (call-with-warnings
2226 (compile '(format #f "~[chbouib~;~a~;~2*~a~]")
2227 #:opts %opts-w-format
2229 (and (= (length w) 1)
2230 (number? (string-contains (car w)
2231 "expected 1 to 4, got 0")))))
2234 (let ((w (call-with-warnings
2236 (compile '(format #f "~@[temperature=~d~]")
2237 #:opts %opts-w-format
2239 (and (= (length w) 1)
2240 (number? (string-contains (car w)
2241 "expected 1, got 0")))))
2244 (let ((w (call-with-warnings
2246 (compile '(format #f "~:[~[hey~;~a~;~va~]~;~3*~]")
2247 #:opts %opts-w-format
2249 (and (= (length w) 1)
2250 (number? (string-contains (car w)
2251 "expected 2 to 4, got 0")))))
2253 (pass-if "unterminated"
2254 (let ((w (call-with-warnings
2256 (compile '(format #f "~[unterminated")
2257 #:opts %opts-w-format
2259 (and (= (length w) 1)
2260 (number? (string-contains (car w)
2261 "unterminated conditional")))))
2263 (pass-if "unexpected ~;"
2264 (let ((w (call-with-warnings
2266 (compile '(format #f "foo~;bar")
2267 #:opts %opts-w-format
2269 (and (= (length w) 1)
2270 (number? (string-contains (car w)
2273 (pass-if "unexpected ~]"
2274 (let ((w (call-with-warnings
2276 (compile '(format #f "foo~]")
2277 #:opts %opts-w-format
2279 (and (= (length w) 1)
2280 (number? (string-contains (car w)
2284 (null? (call-with-warnings
2286 (compile '(format #f "~A ~{~S~} ~A"
2287 'hello '("ladies" "and")
2289 #:opts %opts-w-format
2292 (pass-if "~{...~}, too many args"
2293 (let ((w (call-with-warnings
2295 (compile '(format #f "~{~S~}" 1 2 3)
2296 #:opts %opts-w-format
2298 (and (= (length w) 1)
2299 (number? (string-contains (car w)
2300 "expected 1, got 3")))))
2303 (null? (call-with-warnings
2305 (compile '(format #f "~@{~S~}" 1 2 3)
2306 #:opts %opts-w-format
2309 (pass-if "~@{...~}, too few args"
2310 (let ((w (call-with-warnings
2312 (compile '(format #f "~A ~@{~S~}")
2313 #:opts %opts-w-format
2315 (and (= (length w) 1)
2316 (number? (string-contains (car w)
2317 "expected at least 1, got 0")))))
2319 (pass-if "unterminated ~{...~}"
2320 (let ((w (call-with-warnings
2322 (compile '(format #f "~{")
2323 #:opts %opts-w-format
2325 (and (= (length w) 1)
2326 (number? (string-contains (car w)
2330 (null? (call-with-warnings
2332 (compile '(format #f "~:@(~A ~A~)" 'foo 'bar)
2333 #:opts %opts-w-format
2337 (let ((w (call-with-warnings
2339 (compile '(format #f "~v_foo")
2340 #:opts %opts-w-format
2342 (and (= (length w) 1)
2343 (number? (string-contains (car w)
2344 "expected 1, got 0")))))
2346 (null? (call-with-warnings
2348 (compile '(format #f "~v:@y" 1 123)
2349 #:opts %opts-w-format
2354 (let ((w (call-with-warnings
2356 (compile '(format #f "~2*~a" 'a 'b)
2357 #:opts %opts-w-format
2359 (and (= (length w) 1)
2360 (number? (string-contains (car w)
2361 "expected 3, got 2")))))
2364 (null? (call-with-warnings
2366 (compile '(format #f "~?" "~d ~d" '(1 2))
2367 #:opts %opts-w-format
2370 (pass-if "complex 1"
2371 (let ((w (call-with-warnings
2373 (compile '(format #f
2374 "~4@S ~32S~@[;; ~1{~@?~}~]~@[~61t at ~a~]\n"
2376 #:opts %opts-w-format
2378 (and (= (length w) 1)
2379 (number? (string-contains (car w)
2380 "expected 4, got 6")))))
2382 (pass-if "complex 2"
2383 (let ((w (call-with-warnings
2385 (compile '(format #f
2386 "~:(~A~) Commands~:[~; [abbrev]~]:~2%"
2388 #:opts %opts-w-format
2390 (and (= (length w) 1)
2391 (number? (string-contains (car w)
2392 "expected 2, got 4")))))
2394 (pass-if "complex 3"
2395 (let ((w (call-with-warnings
2397 (compile '(format #f "~9@a~:[~*~3_~;~3d~] ~v:@y~%")
2398 #:opts %opts-w-format
2400 (and (= (length w) 1)
2401 (number? (string-contains (car w)
2402 "expected 5, got 0")))))
2404 (pass-if "ice-9 format"
2405 (let ((w (call-with-warnings
2407 (let ((in (open-input-string
2408 "(use-modules ((ice-9 format)
2409 #:renamer (symbol-prefix-proc 'i9-)))
2410 (i9-format #t \"yo! ~A\" 1 2)")))
2411 (read-and-compile in
2412 #:opts %opts-w-format
2413 #:to 'assembly))))))
2414 (and (= (length w) 1)
2415 (number? (string-contains (car w)
2416 "expected 1, got 2")))))
2418 (pass-if "not format"
2419 (null? (call-with-warnings
2421 (compile '(let ((format chbouib))
2422 (format #t "not ~A a format string"))
2423 #:opts %opts-w-format
2424 #:to 'assembly)))))))