Debian Codegraph @

test-suite/tests/tree-il.test

raw
1 ;;;; tree-il.test --- test suite for compiling tree-il -*- scheme -*-
2 ;;;; Andy Wingo <[email protected]> --- May 2009
3 ;;;;
4 ;;;; Copyright (C) 2009-2014,2018-2021,2023 Free Software Foundation, Inc.
5 ;;;;
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.
10 ;;;;
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.
15 ;;;;
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
19
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 tree-il optimize)
28 #:use-module (ice-9 match)
29 #:use-module (ice-9 regex)
30 #:use-module (srfi srfi-13))
31
32 (define-syntax-rule (pass-if-primitives-resolved in expected)
33 (pass-if (format #f "primitives-resolved in ~s" 'in)
34 (let* ((module (let ((m (make-module)))
35 (beautify-user-module! m)
36 m))
37 (orig (parse-tree-il 'in))
38 (resolved (expand-primitives (resolve-primitives orig module))))
39 (or (equal? (unparse-tree-il resolved) 'expected)
40 (begin
41 (format (current-error-port)
42 "primitive test failed: got ~s, expected ~s"
43 resolved 'expected)
44 #f)))))
45
46 (define-syntax pass-if-tree-il->scheme
47 (syntax-rules ()
48 ((_ in pat)
49 (assert-scheme->tree-il->scheme in pat #t))
50 ((_ in pat guard-exp)
51 (pass-if 'in
52 (pmatch (tree-il->scheme
53 (compile 'in #:from 'scheme #:to 'tree-il))
54 (pat (guard guard-exp) #t)
55 (_ #f))))))
56
57
58 (with-test-prefix "primitives"
59
60 (with-test-prefix "error"
61 (pass-if-primitives-resolved
62 (primcall error (const "message"))
63 (primcall throw (const misc-error) (const #f)
64 (const "message") (primcall list) (const #f)))
65
66 (pass-if-primitives-resolved
67 (primcall error (const "message") (const 42))
68 (primcall throw (const misc-error) (const #f)
69 (const "message ~S") (primcall list (const 42))
70 (const #f)))
71
72 (pass-if-equal "https://bugs.gnu.org/39509"
73 '(throw 'misc-error #f "~A" (list "message") #f)
74 (let ((module (make-fresh-user-module)))
75 (decompile (expand-primitives
76 (resolve-primitives
77 (compile '(error ((lambda () "message")))
78 #:to 'tree-il)
79 module))
80 #:from 'tree-il
81 #:to 'scheme)))
82
83 (pass-if-equal "https://bugs.gnu.org/39509 with argument"
84 '(throw 'misc-error #f "~A ~S" (list "message" 42) #f)
85 (let ((module (make-fresh-user-module)))
86 (decompile (expand-primitives
87 (resolve-primitives
88 (compile '(error ((lambda () "message")) 42)
89 #:to 'tree-il)
90 module))
91 #:from 'tree-il
92 #:to 'scheme)))))
93
94
95 (define* (compile+optimize exp #:key (env (current-module))
96 (optimization-level 2) (opts '()))
97 (let ((optimize (make-lowerer optimization-level opts)))
98 (optimize (compile exp #:to 'tree-il #:env env) env)))
99
100 (with-test-prefix "optimize"
101
102 (pass-if-equal "https://debbugs.gnu.org/48098"
103 '(begin
104 (display "hey!\n")
105 42)
106 (decompile
107 (compile+optimize
108 '(begin
109 (call-with-prompt (make-prompt-tag)
110 (lambda () (display "hey!\n"))
111 (lambda (k) #f))
112 42)))))
113
114
115 (with-test-prefix "tree-il->scheme"
116 (pass-if-tree-il->scheme
117 (case-lambda ((a) a) ((b c) (list b c)))
118 (case-lambda ((,a) ,a1) ((,b ,c) (list ,b1 ,c1)))
119 (and (eq? a a1) (eq? b b1) (eq? c c1))))
120
121
122 (with-test-prefix "contification"
123 (pass-if "http://debbugs.gnu.org/9769"
124 ((compile '(lambda ()
125 (let ((fail (lambda () #f)))
126 (let ((test (lambda () (fail))))
127 (test))
128 #t))
129 ;; Prevent inlining. We're testing contificatoin here,
130 ;; and inlining it will reduce the entire thing to #t.
131 #:opts '(#:partial-eval? #f)))))
132
133
134 (define (sum . args)
135 (apply + args))
136
137 (with-test-prefix "many args"
138 (pass-if "call with > 256 args"
139 (equal? (compile `(1+ (sum ,@(iota 1000)))
140 #:env (current-module))
141 (1+ (apply sum (iota 1000)))))
142
143 (pass-if "tail call with > 256 args"
144 (equal? (compile `(sum ,@(iota 1000))
145 #:env (current-module))
146 (apply sum (iota 1000)))))
147
148
149
150 (with-test-prefix "tree-il-fold"
151
152 (pass-if "void"
153 (let ((up 0) (down 0) (mark (list 'mark)))
154 (and (eq? mark
155 (tree-il-fold (lambda (x y) (set! down (1+ down)) y)
156 (lambda (x y) (set! up (1+ up)) y)
157 mark
158 (make-void #f)))
159 (= up 1)
160 (= down 1))))
161
162 (pass-if "lambda and application"
163 (let* ((ups '()) (downs '())
164 (result (tree-il-fold (lambda (x y)
165 (set! downs (cons x downs))
166 (1+ y))
167 (lambda (x y)
168 (set! ups (cons x ups))
169 (1+ y))
170 0
171 (parse-tree-il
172 '(lambda ()
173 (lambda-case
174 (((x y) #f #f #f () (x1 y1))
175 (call (toplevel +)
176 (lexical x x1)
177 (lexical y y1)))
178 #f))))))
179 (define (strip-source x)
180 (post-order (lambda (x)
181 (set! (tree-il-src x) #f)
182 x)
183 x))
184 (and (= result 12)
185 (equal? (map strip-source (list-head (reverse ups) 3))
186 (list (make-toplevel-ref #f #f '+)
187 (make-lexical-ref #f 'x 'x1)
188 (make-lexical-ref #f 'y 'y1)))
189 (equal? (map strip-source (reverse (list-head downs 3)))
190 (list (make-toplevel-ref #f #f '+)
191 (make-lexical-ref #f 'x 'x1)
192 (make-lexical-ref #f 'y 'y1)))))))
193
194
195 ;;;
196 ;;; Warnings.
197 ;;;
198
199 ;; Make sure we get English messages.
200 (when (defined? 'setlocale)
201 (setlocale LC_ALL "C"))
202
203 (define (call-with-warnings thunk)
204 (let ((port (open-output-string)))
205 ;; Disable any warnings added by default.
206 (parameterize ((default-warning-level 0))
207 (with-fluids ((*current-warning-port* port)
208 (*current-warning-prefix* ""))
209 (thunk)))
210 (let ((warnings (get-output-string port)))
211 (string-tokenize warnings
212 (char-set-complement (char-set #\newline))))))
213
214 (define %opts-w-unused
215 '(#:warnings (unused-variable)))
216
217 (define %opts-w-unused-toplevel
218 '(#:warnings (unused-toplevel)))
219
220 (define %opts-w-unused-module
221 '(#:warnings (unused-module)))
222
223 (define %opts-w-shadowed-toplevel
224 '(#:warnings (shadowed-toplevel)))
225
226 (define %opts-w-unbound
227 '(#:warnings (unbound-variable)))
228
229 (define %opts-w-use-before-definition
230 '(#:warnings (use-before-definition)))
231
232 (define %opts-w-non-idempotent-definition
233 '(#:warnings (non-idempotent-definition)))
234
235 (define %opts-w-arity
236 '(#:warnings (arity-mismatch)))
237
238 (define %opts-w-format
239 '(#:warnings (format)))
240
241 (define %opts-w-duplicate-case-datum
242 '(#:warnings (duplicate-case-datum)))
243
244 (define %opts-w-bad-case-datum
245 '(#:warnings (bad-case-datum)))
246
247
248 (with-test-prefix "warnings"
249
250 (pass-if "unknown warning type"
251 (let ((w (call-with-warnings
252 (lambda ()
253 (compile #t #:opts '(#:warnings (does-not-exist)))))))
254 (and (= (length w) 1)
255 (number? (string-contains (car w) "unknown warning")))))
256
257 (with-test-prefix "unused-variable"
258
259 (pass-if "quiet"
260 (null? (call-with-warnings
261 (lambda ()
262 (compile '(lambda (x y) (+ x y))
263 #:opts %opts-w-unused)))))
264
265 (pass-if "let/unused"
266 (let ((w (call-with-warnings
267 (lambda ()
268 (compile '(lambda (x)
269 (let ((y (+ x 2)))
270 x))
271 #:opts %opts-w-unused)))))
272 (and (= (length w) 1)
273 (number? (string-contains (car w) "unused variable `y'")))))
274
275 (pass-if "shadowed variable"
276 (let ((w (call-with-warnings
277 (lambda ()
278 (compile '(lambda (x)
279 (let ((y x))
280 (let ((y (+ x 2)))
281 (+ x y))))
282 #:opts %opts-w-unused)))))
283 (and (= (length w) 1)
284 (number? (string-contains (car w) "unused variable `y'")))))
285
286 (pass-if "letrec"
287 (null? (call-with-warnings
288 (lambda ()
289 (compile '(lambda ()
290 (letrec ((x (lambda () (y)))
291 (y (lambda () (x))))
292 y))
293 #:opts %opts-w-unused)))))
294
295 (pass-if "unused argument"
296 ;; Unused arguments should not be reported.
297 (null? (call-with-warnings
298 (lambda ()
299 (compile '(lambda (x y z) #t)
300 #:opts %opts-w-unused)))))
301
302 (pass-if "special variable names"
303 (null? (call-with-warnings
304 (lambda ()
305 (compile '(lambda ()
306 (let ((_ 'underscore)
307 (#{gensym name}# 'ignore-me))
308 #t))
309 #:to 'cps
310 #:opts %opts-w-unused))))))
311
312 (with-test-prefix "unused-toplevel"
313
314 (pass-if "used after definition"
315 (null? (call-with-warnings
316 (lambda ()
317 (let ((in (open-input-string
318 "(define foo 2) foo")))
319 (read-and-compile in
320 #:to 'cps
321 #:opts %opts-w-unused-toplevel))))))
322
323 (pass-if "used before definition"
324 (null? (call-with-warnings
325 (lambda ()
326 (let ((in (open-input-string
327 "(define (bar) foo) (define foo 2) (bar)")))
328 (read-and-compile in
329 #:to 'cps
330 #:opts %opts-w-unused-toplevel))))))
331
332 (pass-if "unused but public"
333 (let ((in (open-input-string
334 "(define-module (test-suite tree-il x) #:export (bar))
335 (define (bar) #t)")))
336 (null? (call-with-warnings
337 (lambda ()
338 (read-and-compile in
339 #:to 'cps
340 #:opts %opts-w-unused-toplevel))))))
341
342 (pass-if "unused but public (more)"
343 (let ((in (open-input-string
344 "(define-module (test-suite tree-il x) #:export (bar))
345 (define (bar) (baz))
346 (define (baz) (foo))
347 (define (foo) #t)")))
348 (null? (call-with-warnings
349 (lambda ()
350 (read-and-compile in
351 #:to 'cps
352 #:opts %opts-w-unused-toplevel))))))
353
354 (pass-if "unused but define-public"
355 (null? (call-with-warnings
356 (lambda ()
357 (compile '(define-public foo 2)
358 #:to 'cps
359 #:opts %opts-w-unused-toplevel)))))
360
361 (pass-if "used by macro"
362 ;; FIXME: See comment about macros at `unused-toplevel-analysis'.
363 (throw 'unresolved)
364
365 (null? (call-with-warnings
366 (lambda ()
367 (let ((in (open-input-string
368 "(define (bar) 'foo)
369 (define-syntax baz
370 (syntax-rules () ((_) (bar))))")))
371 (read-and-compile in
372 #:to 'cps
373 #:opts %opts-w-unused-toplevel))))))
374
375 (pass-if "unused"
376 (let ((w (call-with-warnings
377 (lambda ()
378 (compile '(define foo 2)
379 #:to 'cps
380 #:opts %opts-w-unused-toplevel)))))
381 (and (= (length w) 1)
382 (number? (string-contains (car w)
383 (format #f "top-level variable `~A'"
384 'foo))))))
385
386 (pass-if "unused recursive"
387 (let ((w (call-with-warnings
388 (lambda ()
389 (compile '(define (foo) (foo))
390 #:to 'cps
391 #:opts %opts-w-unused-toplevel)))))
392 (and (= (length w) 1)
393 (number? (string-contains (car w)
394 (format #f "top-level variable `~A'"
395 'foo))))))
396
397 (pass-if "unused mutually recursive"
398 (let* ((in (open-input-string
399 "(define (foo) (bar)) (define (bar) (foo))"))
400 (w (call-with-warnings
401 (lambda ()
402 (read-and-compile in
403 #:to 'cps
404 #:opts %opts-w-unused-toplevel)))))
405 (and (= (length w) 2)
406 (number? (string-contains (car w)
407 (format #f "top-level variable `~A'"
408 'foo)))
409 (number? (string-contains (cadr w)
410 (format #f "top-level variable `~A'"
411 'bar))))))
412
413 (pass-if "special variable names"
414 (null? (call-with-warnings
415 (lambda ()
416 (compile '(define #{gensym name}# 'ignore-me)
417 #:to 'cps
418 #:opts %opts-w-unused-toplevel))))))
419
420 (with-test-prefix "unused-module"
421
422 (pass-if-equal "quiet"
423 '()
424 (call-with-warnings
425 (lambda ()
426 (compile '(begin
427 (use-modules (ice-9 popen))
428 (define (proc cmd)
429 (open-input-pipe cmd)))
430 #:env (make-fresh-user-module)
431 #:opts %opts-w-unused-module))))
432
433 (pass-if-equal "quiet, renamer"
434 '()
435 (call-with-warnings
436 (lambda ()
437 (compile '(begin
438 (use-modules ((ice-9 popen) #:prefix p-))
439 (define (proc cmd)
440 (p-open-input-pipe cmd)))
441 #:env (make-fresh-user-module)
442 #:opts %opts-w-unused-module))))
443
444 (pass-if "definitely unused"
445 (let* ((defmod '(define-module (foo)
446 #:use-module (ice-9 vlist)
447 #:use-module (ice-9 popen)
448 #:export (proc)))
449 (w (call-with-warnings
450 (lambda ()
451 (set-source-properties! defmod
452 '((filename . "foo.scm")
453 (line . 0)
454 (column . 0)))
455 (compile `(begin
456 ,defmod
457 (define (frob x)
458 (vlist-cons x vlist-null)))
459 #:env (make-fresh-user-module)
460 #:opts %opts-w-unused-module)))))
461 (and (= (length w) 1)
462 (string-prefix? "foo.scm:1:0" (car w))
463 (number? (string-contains (car w)
464 "unused module (ice-9 popen)")))))
465
466 (pass-if "definitely unused, use-modules"
467 (let* ((usemod '(use-modules (rnrs bytevectors)
468 (ice-9 q)))
469 (w (call-with-warnings
470 (lambda ()
471 (set-source-properties! usemod
472 '((filename . "bar.scm")
473 (line . 5)
474 (column . 0)))
475 (compile `(begin
476 ,usemod
477 (define (square x)
478 (* x x)))
479 #:env (make-fresh-user-module)
480 #:opts %opts-w-unused-module)))))
481 (and (= (length w) 2)
482 (string-prefix? "bar.scm:6:0" (car w))
483 (number? (string-contains (car w)
484 "unused module (rnrs bytevectors)"))
485 (number? (string-contains (cadr w)
486 "unused module (ice-9 q)")))))
487
488 (pass-if "definitely unused, local binding shadows imported one"
489 (let ((w (call-with-warnings
490 (lambda ()
491 (compile `(begin
492 (define-module (whatever x y z)
493 #:use-module (ice-9 popen)
494 #:export (frob))
495
496 (define (open-input-pipe x)
497 ;; Shadows the one from (ice-9 popen).
498 x)
499 (define (frob y)
500 (close-port (open-input-pipe y))))
501 #:env (make-fresh-user-module)
502 #:opts %opts-w-unused-module)))))
503 (and (= (length w) 1)
504 (number? (string-contains (car w)
505 "unused module (ice-9 popen)")))))
506
507 (pass-if-equal "(ice-9 match) is actually used"
508 '()
509 ;; (ice-9 match) is used and the macro expansion of the 'match'
510 ;; form refers to (@@ (ice-9 match) car) and the likes.
511 (call-with-warnings
512 (lambda ()
513 (compile '(begin
514 (use-modules (ice-9 match))
515 (define (proc lst)
516 (match lst
517 ((a b c) (+ a (* b c))))))
518 #:env (make-fresh-user-module)
519 #:opts %opts-w-unused-module))))
520
521 (pass-if-equal "re-exporting is using"
522 '()
523 ;; This module re-exports a binding from (ice-9 q), so (ice-9 q)
524 ;; should be considered as used.
525 (call-with-warnings
526 (lambda ()
527 (compile '(begin
528 (define-module (this is an ice-9 q user)
529 #:use-module (ice-9 q)
530 #:re-export (make-q)
531 #:export (proc))
532 (define (proc a b)
533 (* a b)))
534 #:env (make-fresh-user-module)
535 #:opts %opts-w-unused-module))))
536
537 (pass-if "(srfi srfi-26) might be unused"
538 ;; At the tree-il level, it is impossible to know whether (srfi
539 ;; srfi-26) is actually use, because all we see is the output of
540 ;; macro expansion, and in this case it doesn't capture any
541 ;; binding from (srfi srfi-26).
542 (let* ((w (call-with-warnings
543 (lambda ()
544 (compile `(begin
545 (define-module (whatever)
546 #:use-module (srfi srfi-26)
547 #:export (square))
548 (define double
549 (cut * 2 <>)))
550 #:env (make-fresh-user-module)
551 #:opts %opts-w-unused-module)))))
552 (and (= (length w) 1)
553 (number? (string-contains (car w)
554 "possibly unused module (srfi srfi-26)")))))
555
556 (pass-if-equal "(ice-9 format) is actually used"
557 '()
558 ;; The 'format' binding of (ice-9 format) takes precedence over
559 ;; (@@ (guile) format), so (ice-9 format) must not be reported as
560 ;; unused.
561 (call-with-warnings
562 (lambda ()
563 (compile '(begin
564 (define-module (whatever-else)
565 #:use-module (ice-9 format)
566 #:export (proc))
567 (define (proc lst)
568 (format #f "~{~a ~}~%" lst)))
569 #:env (make-fresh-user-module)
570 #:opts %opts-w-unused-module)))))
571
572 (with-test-prefix "shadowed-toplevel"
573
574 (pass-if "quiet"
575 (null? (call-with-warnings
576 (lambda ()
577 (let ((in (open-input-string
578 "(define foo 2) (define bar 3)")))
579 (read-and-compile in
580 #:to 'cps
581 #:opts
582 %opts-w-shadowed-toplevel))))))
583
584 (pass-if "internal define"
585 (null? (call-with-warnings
586 (lambda ()
587 (let ((in (open-input-string
588 "(define foo 2)
589 (define (bar x) (define foo (+ x 2)) (* foo x))")))
590 (read-and-compile in
591 #:to 'cps
592 #:opts
593 %opts-w-shadowed-toplevel))))))
594
595 (pass-if "one shadowing definition"
596 (match (call-with-warnings
597 (lambda ()
598 (let ((in (open-input-string
599 "(define foo 2)\n (define foo 3)")))
600 (read-and-compile in
601 #:to 'cps
602 #:opts
603 %opts-w-shadowed-toplevel))))
604 ((message)
605 (->bool (string-match ":2:2:.*previous.*foo.*:1:0" message)))))
606
607 (pass-if "two shadowing definitions"
608 (match (call-with-warnings
609 (lambda ()
610 (let ((in (open-input-string
611 "(define-public foo 2)\n(define foo 3)
612 (define (foo x) x)")))
613 (read-and-compile in
614 #:to 'cps
615 #:opts
616 %opts-w-shadowed-toplevel))))
617 ((message1 message2)
618 (->bool
619 (and (string-match ":2:0:.*previous.*foo.*:1:0" message1)
620 (string-match ":3:2:.*previous.*foo.*:1:0" message2))))))
621
622 (pass-if "define-public"
623 (match (call-with-warnings
624 (lambda ()
625 (let ((in (open-input-string
626 "(define foo 2)\n(define-public foo 3)")))
627 (read-and-compile in
628 #:to 'cps
629 #:opts
630 %opts-w-shadowed-toplevel))))
631 ((message)
632 (->bool (string-match ":2:0:.*previous.*foo.*:1:0" message)))))
633
634 (pass-if "macro"
635 (match (call-with-warnings
636 (lambda ()
637 (let ((in (open-input-string
638 "(define foo 42)
639 (define-syntax-rule (defun proc (args ...) body ...)
640 (define (proc args ...) body ...))
641 (defun foo (a b c) (+ a b c))")))
642 (read-and-compile in
643 #:to 'cps
644 #:opts
645 %opts-w-shadowed-toplevel))))
646 ((message)
647 (->bool (string-match ":4:2:.*previous.*foo.*:1:0" message))))))
648
649 (with-test-prefix "unbound variable"
650
651 (pass-if "quiet"
652 (null? (call-with-warnings
653 (lambda ()
654 (compile '+ #:opts %opts-w-unbound)))))
655
656 (pass-if "ref"
657 (let* ((v (gensym))
658 (w (call-with-warnings
659 (lambda ()
660 (compile v
661 #:to 'cps
662 #:opts %opts-w-unbound)))))
663 (and (= (length w) 1)
664 (number? (string-contains (car w)
665 (format #f "unbound variable `~A'"
666 v))))))
667
668 (pass-if "set!"
669 (let* ((v (gensym))
670 (w (call-with-warnings
671 (lambda ()
672 (compile `(set! ,v 7)
673 #:to 'cps
674 #:opts %opts-w-unbound)))))
675 (and (= (length w) 1)
676 (number? (string-contains (car w)
677 (format #f "unbound variable `~A'"
678 v))))))
679
680 (pass-if "module-local top-level is visible"
681 (let ((m (make-module))
682 (v (gensym)))
683 (beautify-user-module! m)
684 (compile `(define ,v 123)
685 #:env m #:opts %opts-w-unbound)
686 (null? (call-with-warnings
687 (lambda ()
688 (compile v
689 #:env m
690 #:to 'cps
691 #:opts %opts-w-unbound))))))
692
693 (pass-if "module-local top-level is visible after"
694 (let ((m (make-module))
695 (v (gensym)))
696 (beautify-user-module! m)
697 (null? (call-with-warnings
698 (lambda ()
699 (let ((in (open-input-string
700 "(define (f)
701 (set! chbouib 3))
702 (define chbouib 5)")))
703 (read-and-compile in
704 #:env m
705 #:opts %opts-w-unbound)))))))
706
707 (pass-if "optional arguments are visible"
708 (null? (call-with-warnings
709 (lambda ()
710 (compile '(lambda* (x #:optional y z) (list x y z))
711 #:opts %opts-w-unbound
712 #:to 'cps)))))
713
714 (pass-if "keyword arguments are visible"
715 (null? (call-with-warnings
716 (lambda ()
717 (compile '(lambda* (x #:key y z) (list x y z))
718 #:opts %opts-w-unbound
719 #:to 'cps)))))
720
721 (pass-if "GOOPS definitions are visible"
722 (let ((m (make-module))
723 (v (gensym)))
724 (beautify-user-module! m)
725 (module-use! m (resolve-interface '(oop goops)))
726 (null? (call-with-warnings
727 (lambda ()
728 (let ((in (open-input-string
729 "(define-class <foo> ()
730 (bar #:getter foo-bar))
731 (define z (foo-bar (make <foo>)))")))
732 (read-and-compile in
733 #:env m
734 #:opts %opts-w-unbound))))))))
735
736 (pass-if "re-exported binding" ;<https://bugs.gnu.org/47031>
737 (null? (call-with-warnings
738 (lambda ()
739 (compile '(begin
740 (use-modules (srfi srfi-35))
741
742 ;; This 'condition' form expands to a
743 ;; 'make-compound-condition' call, which is
744 ;; re-exported from (ice-9 exceptions).
745 (condition (&error)
746 (&message (message "oh!"))))
747 #:opts %opts-w-unbound)))))
748
749 (with-test-prefix "use-before-definition"
750 (define-syntax-rule (pass-if-warnings expr pat test)
751 (pass-if 'expr
752 (match (call-with-warnings
753 (lambda ()
754 (compile 'expr #:to 'cps
755 #:opts %opts-w-use-before-definition)))
756 (pat test)
757 (_ #f))))
758
759 (define-syntax-rule (pass-if-no-warnings expr)
760 (pass-if-warnings expr () #t))
761
762 (pass-if-no-warnings
763 (begin (define x +) x))
764 (pass-if-warnings
765 (begin x (define x +))
766 (w) (number? (string-contains w "`x' used before definition")))
767 (pass-if-warnings
768 (begin (set! x 1) (define x +))
769 (w) (number? (string-contains w "`x' used before definition")))
770 (pass-if-no-warnings
771 (begin (lambda () x) (define x +)))
772 (pass-if-no-warnings
773 (begin (if (defined? 'x) x) (define x +))))
774
775 (with-test-prefix "non-idempotent-definition"
776 (define-syntax-rule (pass-if-warnings expr pat test)
777 (pass-if 'expr
778 (match (call-with-warnings
779 (lambda ()
780 (compile 'expr #:to 'cps
781 #:opts %opts-w-non-idempotent-definition)))
782 (pat test)
783 (_ #f))))
784
785 (define-syntax-rule (pass-if-no-warnings expr)
786 (pass-if-warnings expr () #t))
787
788 (pass-if-no-warnings
789 (begin (define - +) (define y -)))
790 (pass-if-warnings
791 (begin - (define - +))
792 (w) (number? (string-contains w "non-idempotent binding for `-'")))
793 (pass-if-warnings
794 (begin (define y -) (define - +))
795 (w) (number? (string-contains w "non-idempotent binding for `-'")))
796 (pass-if-no-warnings
797 (begin (lambda () -) (define - +)))
798 (pass-if-no-warnings
799 (begin (if (defined? '-) -) (define - +))))
800
801 (with-test-prefix "arity mismatch"
802
803 (pass-if "quiet"
804 (null? (call-with-warnings
805 (lambda ()
806 (compile '(cons 'a 'b) #:opts %opts-w-arity)))))
807
808 (pass-if "direct application"
809 (let ((w (call-with-warnings
810 (lambda ()
811 (compile '((lambda (x y) (or x y)) 1 2 3 4 5)
812 #:opts %opts-w-arity
813 #:to 'cps)))))
814 (and (= (length w) 1)
815 (number? (string-contains (car w)
816 "wrong number of arguments to")))))
817 (pass-if "local"
818 (let ((w (call-with-warnings
819 (lambda ()
820 (compile '(let ((f (lambda (x y) (+ x y))))
821 (f 2))
822 #:opts %opts-w-arity
823 #:to 'cps)))))
824 (and (= (length w) 1)
825 (number? (string-contains (car w)
826 "wrong number of arguments to")))))
827
828 (pass-if "global"
829 (let ((w (call-with-warnings
830 (lambda ()
831 (compile '(cons 1 2 3 4)
832 #:opts %opts-w-arity
833 #:to 'cps)))))
834 (and (= (length w) 1)
835 (number? (string-contains (car w)
836 "wrong number of arguments to")))))
837
838 (pass-if "alias to global"
839 (let ((w (call-with-warnings
840 (lambda ()
841 (compile '(let ((f cons)) (f 1 2 3 4))
842 #:opts %opts-w-arity
843 #:to 'cps)))))
844 (and (= (length w) 1)
845 (number? (string-contains (car w)
846 "wrong number of arguments to")))))
847
848 (pass-if "alias to lexical to global"
849 (let ((w (call-with-warnings
850 (lambda ()
851 (compile '(let ((f number?))
852 (let ((g f))
853 (f 1 2 3 4)))
854 #:opts %opts-w-arity
855 #:to 'cps)))))
856 (and (= (length w) 1)
857 (number? (string-contains (car w)
858 "wrong number of arguments to")))))
859
860 (pass-if "alias to lexical"
861 (let ((w (call-with-warnings
862 (lambda ()
863 (compile '(let ((f (lambda (x y z) (+ x y z))))
864 (let ((g f))
865 (g 1)))
866 #:opts %opts-w-arity
867 #:to 'cps)))))
868 (and (= (length w) 1)
869 (number? (string-contains (car w)
870 "wrong number of arguments to")))))
871
872 (pass-if "letrec"
873 (let ((w (call-with-warnings
874 (lambda ()
875 (compile '(letrec ((odd? (lambda (x) (even? (1- x))))
876 (even? (lambda (x)
877 (or (= 0 x)
878 (odd?)))))
879 (odd? 1))
880 #:opts %opts-w-arity
881 #:to 'cps)))))
882 (and (= (length w) 1)
883 (number? (string-contains (car w)
884 "wrong number of arguments to")))))
885
886 (pass-if "case-lambda"
887 (null? (call-with-warnings
888 (lambda ()
889 (compile '(let ((f (case-lambda
890 ((x) 1)
891 ((x y) 2)
892 ((x y z) 3))))
893 (list (f 1)
894 (f 1 2)
895 (f 1 2 3)))
896 #:opts %opts-w-arity
897 #:to 'cps)))))
898
899 (pass-if "case-lambda with wrong number of arguments"
900 (let ((w (call-with-warnings
901 (lambda ()
902 (compile '(let ((f (case-lambda
903 ((x) 1)
904 ((x y) 2))))
905 (f 1 2 3))
906 #:opts %opts-w-arity
907 #:to 'cps)))))
908 (and (= (length w) 1)
909 (number? (string-contains (car w)
910 "wrong number of arguments to")))))
911
912 (pass-if "case-lambda*"
913 (null? (call-with-warnings
914 (lambda ()
915 (compile '(let ((f (case-lambda*
916 ((x #:optional y) 1)
917 ((x #:key y) 2)
918 ((x y #:key z) 3))))
919 (list (f 1)
920 (f 1 2)
921 (f #:y 2)
922 (f 1 2 #:z 3)))
923 #:opts %opts-w-arity
924 #:to 'cps)))))
925
926 (pass-if "case-lambda* with wrong arguments"
927 (let ((w (call-with-warnings
928 (lambda ()
929 (compile '(let ((f (case-lambda*
930 ((x #:optional y) 1)
931 ((x #:key y) 2)
932 ((x y #:key z) 3))))
933 (list (f)
934 (f 1 #:z 3)))
935 #:opts %opts-w-arity
936 #:to 'cps)))))
937 (and (= (length w) 2)
938 (null? (filter (lambda (w)
939 (not
940 (number?
941 (string-contains
942 w "wrong number of arguments to"))))
943 w)))))
944
945 (pass-if "top-level applicable struct"
946 (null? (call-with-warnings
947 (lambda ()
948 (compile '(let ((p current-warning-port))
949 (p (+ (p) 1))
950 (p))
951 #:opts %opts-w-arity
952 #:to 'cps)))))
953
954 (pass-if "top-level applicable struct with wrong arguments"
955 (let ((w (call-with-warnings
956 (lambda ()
957 (compile '(let ((p current-warning-port))
958 (p 1 2 3))
959 #:opts %opts-w-arity
960 #:to 'cps)))))
961 (and (= (length w) 1)
962 (number? (string-contains (car w)
963 "wrong number of arguments to")))))
964
965 (pass-if "local toplevel-defines"
966 (let ((w (call-with-warnings
967 (lambda ()
968 (let ((in (open-input-string "
969 (define (g x) (f x))
970 (define (f) 1)")))
971 (read-and-compile in
972 #:opts %opts-w-arity
973 #:to 'cps))))))
974 (and (= (length w) 1)
975 (number? (string-contains (car w)
976 "wrong number of arguments to")))))
977
978 (pass-if "global toplevel alias"
979 (let ((w (call-with-warnings
980 (lambda ()
981 (let ((in (open-input-string "
982 (define f cons)
983 (define (g) (f))")))
984 (read-and-compile in
985 #:opts %opts-w-arity
986 #:to 'cps))))))
987 (and (= (length w) 1)
988 (number? (string-contains (car w)
989 "wrong number of arguments to")))))
990
991 (pass-if "local toplevel overrides global"
992 (null? (call-with-warnings
993 (lambda ()
994 (let ((in (open-input-string "
995 (define (cons) 0)
996 (define (foo x) (cons))")))
997 (read-and-compile in
998 #:opts %opts-w-arity
999 #:to 'cps))))))
1000
1001 (pass-if "keyword not passed and quiet"
1002 (null? (call-with-warnings
1003 (lambda ()
1004 (compile '(let ((f (lambda* (x #:key y) y)))
1005 (f 2))
1006 #:opts %opts-w-arity
1007 #:to 'cps)))))
1008
1009 (pass-if "keyword passed and quiet"
1010 (null? (call-with-warnings
1011 (lambda ()
1012 (compile '(let ((f (lambda* (x #:key y) y)))
1013 (f 2 #:y 3))
1014 #:opts %opts-w-arity
1015 #:to 'cps)))))
1016
1017 (pass-if "keyword passed to global and quiet"
1018 (null? (call-with-warnings
1019 (lambda ()
1020 (let ((in (open-input-string "
1021 (use-modules (system base compile))
1022 (compile '(+ 2 3) #:env (current-module))")))
1023 (read-and-compile in
1024 #:opts %opts-w-arity
1025 #:to 'cps))))))
1026
1027 (pass-if "extra keyword"
1028 (let ((w (call-with-warnings
1029 (lambda ()
1030 (compile '(let ((f (lambda* (x #:key y) y)))
1031 (f 2 #:Z 3))
1032 #:opts %opts-w-arity
1033 #:to 'cps)))))
1034 (and (= (length w) 1)
1035 (number? (string-contains (car w)
1036 "wrong number of arguments to")))))
1037
1038 (pass-if "extra keywords allowed"
1039 (null? (call-with-warnings
1040 (lambda ()
1041 (compile '(let ((f (lambda* (x #:key y #:allow-other-keys)
1042 y)))
1043 (f 2 #:Z 3))
1044 #:opts %opts-w-arity
1045 #:to 'cps))))))
1046
1047 (with-test-prefix "format"
1048
1049 (pass-if "quiet (no args)"
1050 (null? (call-with-warnings
1051 (lambda ()
1052 (compile '(format #t "hey!")
1053 #:opts %opts-w-format
1054 #:to 'cps)))))
1055
1056 (pass-if "quiet (1 arg)"
1057 (null? (call-with-warnings
1058 (lambda ()
1059 (compile '(format #t "hey ~A!" "you")
1060 #:opts %opts-w-format
1061 #:to 'cps)))))
1062
1063 (pass-if "quiet (2 args)"
1064 (null? (call-with-warnings
1065 (lambda ()
1066 (compile '(format #t "~A ~A!" "hello" "world")
1067 #:opts %opts-w-format
1068 #:to 'cps)))))
1069
1070 (pass-if "wrong port arg"
1071 (let ((w (call-with-warnings
1072 (lambda ()
1073 (compile '(format 10 "foo")
1074 #:opts %opts-w-format
1075 #:to 'cps)))))
1076 (and (= (length w) 1)
1077 (number? (string-contains (car w)
1078 "wrong port argument")))))
1079
1080 (pass-if "non-literal format string"
1081 (let ((w (call-with-warnings
1082 (lambda ()
1083 (compile '(format #f fmt)
1084 #:opts %opts-w-format
1085 #:to 'cps)))))
1086 (and (= (length w) 1)
1087 (number? (string-contains (car w)
1088 "non-literal format string")))))
1089
1090 (pass-if "non-literal format string using gettext"
1091 (null? (call-with-warnings
1092 (lambda ()
1093 (compile '(format #t (gettext "~A ~A!") "hello" "world")
1094 #:opts %opts-w-format
1095 #:to 'cps)))))
1096
1097 (pass-if "non-literal format string using gettext as _"
1098 (null? (call-with-warnings
1099 (lambda ()
1100 (compile '(format #t (G_ "~A ~A!") "hello" "world")
1101 #:opts %opts-w-format
1102 #:to 'cps)))))
1103
1104 (pass-if "non-literal format string using gettext as top-level _"
1105 (null? (call-with-warnings
1106 (lambda ()
1107 (compile '(begin
1108 (define (_ s) (gettext s "my-domain"))
1109 (format #t (G_ "~A ~A!") "hello" "world"))
1110 #:opts %opts-w-format
1111 #:to 'cps)))))
1112
1113 (pass-if "non-literal format string using gettext as module-ref _"
1114 (null? (call-with-warnings
1115 (lambda ()
1116 (compile '(format #t ((@@ (foo) G_) "~A ~A!") "hello" "world")
1117 #:opts %opts-w-format
1118 #:to 'cps)))))
1119
1120 (pass-if "non-literal format string using gettext as lexical _"
1121 (null? (call-with-warnings
1122 (lambda ()
1123 (compile '(let ((_ (lambda (s)
1124 (gettext s "my-domain"))))
1125 (format #t (G_ "~A ~A!") "hello" "world"))
1126 #:opts %opts-w-format
1127 #:to 'cps)))))
1128
1129 (pass-if "non-literal format string using ngettext"
1130 (null? (call-with-warnings
1131 (lambda ()
1132 (compile '(format #t
1133 (ngettext "~a thing" "~a things" n "dom") n)
1134 #:opts %opts-w-format
1135 #:to 'cps)))))
1136
1137 (pass-if "non-literal format string using ngettext as N_"
1138 (null? (call-with-warnings
1139 (lambda ()
1140 (compile '(format #t (N_ "~a thing" "~a things" n) n)
1141 #:opts %opts-w-format
1142 #:to 'cps)))))
1143
1144 (pass-if "non-literal format string with (define _ gettext)"
1145 (null? (call-with-warnings
1146 (lambda ()
1147 (compile '(begin
1148 (define _ gettext)
1149 (define (foo)
1150 (format #t (G_ "~A ~A!") "hello" "world")))
1151 #:opts %opts-w-format
1152 #:to 'cps)))))
1153
1154 (pass-if "wrong format string"
1155 (let ((w (call-with-warnings
1156 (lambda ()
1157 (compile '(format #f 'not-a-string)
1158 #:opts %opts-w-format
1159 #:to 'cps)))))
1160 (and (= (length w) 1)
1161 (number? (string-contains (car w)
1162 "wrong format string")))))
1163
1164 (pass-if "wrong number of args"
1165 (let ((w (call-with-warnings
1166 (lambda ()
1167 (compile '(format "shbweeb")
1168 #:opts %opts-w-format
1169 #:to 'cps)))))
1170 (and (= (length w) 1)
1171 (number? (string-contains (car w)
1172 "wrong number of arguments")))))
1173
1174 (pass-if "~%, ~~, ~&, ~t, ~_, ~!, ~|, ~/, ~q and ~\\n"
1175 (null? (call-with-warnings
1176 (lambda ()
1177 (compile '((@ (ice-9 format) format) some-port
1178 "~&~3_~~ ~\n~12they~% ~!~|~/~q")
1179 #:opts %opts-w-format
1180 #:to 'cps)))))
1181
1182 (pass-if "one missing argument"
1183 (let ((w (call-with-warnings
1184 (lambda ()
1185 (compile '(format some-port "foo ~A~%")
1186 #:opts %opts-w-format
1187 #:to 'cps)))))
1188 (and (= (length w) 1)
1189 (number? (string-contains (car w)
1190 "expected 1, got 0")))))
1191
1192 (pass-if "one missing argument, gettext"
1193 (let ((w (call-with-warnings
1194 (lambda ()
1195 (compile '(format some-port (gettext "foo ~A~%"))
1196 #:opts %opts-w-format
1197 #:to 'cps)))))
1198 (and (= (length w) 1)
1199 (number? (string-contains (car w)
1200 "expected 1, got 0")))))
1201
1202 (pass-if "two missing arguments"
1203 (let ((w (call-with-warnings
1204 (lambda ()
1205 (compile '((@ (ice-9 format) format) #f
1206 "foo ~10,2f and bar ~S~%")
1207 #:opts %opts-w-format
1208 #:to 'cps)))))
1209 (and (= (length w) 1)
1210 (number? (string-contains (car w)
1211 "expected 2, got 0")))))
1212
1213 (pass-if "one given, one missing argument"
1214 (let ((w (call-with-warnings
1215 (lambda ()
1216 (compile '(format #t "foo ~A and ~S~%" hey)
1217 #:opts %opts-w-format
1218 #:to 'cps)))))
1219 (and (= (length w) 1)
1220 (number? (string-contains (car w)
1221 "expected 2, got 1")))))
1222
1223 (pass-if "too many arguments"
1224 (let ((w (call-with-warnings
1225 (lambda ()
1226 (compile '(format #t "foo ~A~%" 1 2)
1227 #:opts %opts-w-format
1228 #:to 'cps)))))
1229 (and (= (length w) 1)
1230 (number? (string-contains (car w)
1231 "expected 1, got 2")))))
1232
1233 (pass-if "~h"
1234 (null? (call-with-warnings
1235 (lambda ()
1236 (compile '((@ (ice-9 format) format) #t
1237 "foo ~h ~a~%" 123.4 'bar)
1238 #:opts %opts-w-format
1239 #:to 'cps)))))
1240
1241 (pass-if "~:h with locale object"
1242 (null? (call-with-warnings
1243 (lambda ()
1244 (compile '((@ (ice-9 format) format) #t
1245 "foo ~:h~%" 123.4 %global-locale)
1246 #:opts %opts-w-format
1247 #:to 'cps)))))
1248
1249 (pass-if "~:h without locale object"
1250 (let ((w (call-with-warnings
1251 (lambda ()
1252 (compile '((@ (ice-9 format) format) #t "foo ~,2:h" 123.4)
1253 #:opts %opts-w-format
1254 #:to 'cps)))))
1255 (and (= (length w) 1)
1256 (number? (string-contains (car w)
1257 "expected 2, got 1")))))
1258
1259 (with-test-prefix "conditionals"
1260 (pass-if "literals"
1261 (null? (call-with-warnings
1262 (lambda ()
1263 (compile '((@ (ice-9 format) format) #f "~A ~[foo~;bar~;baz~;~] ~10,2f"
1264 'a 1 3.14)
1265 #:opts %opts-w-format
1266 #:to 'cps)))))
1267
1268 (pass-if "literals with selector"
1269 (let ((w (call-with-warnings
1270 (lambda ()
1271 (compile '((@ (ice-9 format) format) #f "~2[foo~;bar~;baz~;~] ~A"
1272 1 'dont-ignore-me)
1273 #:opts %opts-w-format
1274 #:to 'cps)))))
1275 (and (= (length w) 1)
1276 (number? (string-contains (car w)
1277 "expected 1, got 2")))))
1278
1279 (pass-if "escapes (exact count)"
1280 (let ((w (call-with-warnings
1281 (lambda ()
1282 (compile '((@ (ice-9 format) format) #f "~[~a~;~a~]")
1283 #:opts %opts-w-format
1284 #:to 'cps)))))
1285 (and (= (length w) 1)
1286 (number? (string-contains (car w)
1287 "expected 2, got 0")))))
1288
1289 (pass-if "escapes with selector"
1290 (let ((w (call-with-warnings
1291 (lambda ()
1292 (compile '((@ (ice-9 format) format) #f "~1[chbouib~;~a~]")
1293 #:opts %opts-w-format
1294 #:to 'cps)))))
1295 (and (= (length w) 1)
1296 (number? (string-contains (car w)
1297 "expected 1, got 0")))))
1298
1299 (pass-if "escapes, range"
1300 (let ((w (call-with-warnings
1301 (lambda ()
1302 (compile '((@ (ice-9 format) format) #f "~[chbouib~;~a~;~2*~a~]")
1303 #:opts %opts-w-format
1304 #:to 'cps)))))
1305 (and (= (length w) 1)
1306 (number? (string-contains (car w)
1307 "expected 1 to 4, got 0")))))
1308
1309 (pass-if "@"
1310 (let ((w (call-with-warnings
1311 (lambda ()
1312 (compile '((@ (ice-9 format) format) #f "~@[temperature=~d~]")
1313 #:opts %opts-w-format
1314 #:to 'cps)))))
1315 (and (= (length w) 1)
1316 (number? (string-contains (car w)
1317 "expected 1, got 0")))))
1318
1319 (pass-if "nested"
1320 (let ((w (call-with-warnings
1321 (lambda ()
1322 (compile '((@ (ice-9 format) format) #f "~:[~[hey~;~a~;~va~]~;~3*~]")
1323 #:opts %opts-w-format
1324 #:to 'cps)))))
1325 (and (= (length w) 1)
1326 (number? (string-contains (car w)
1327 "expected 2 to 4, got 0")))))
1328
1329 (pass-if "unterminated"
1330 (let ((w (call-with-warnings
1331 (lambda ()
1332 (compile '((@ (ice-9 format) format) #f "~[unterminated")
1333 #:opts %opts-w-format
1334 #:to 'cps)))))
1335 (and (= (length w) 1)
1336 (number? (string-contains (car w)
1337 "unterminated conditional")))))
1338
1339 (pass-if "unexpected ~;"
1340 (let ((w (call-with-warnings
1341 (lambda ()
1342 (compile '((@ (ice-9 format) format) #f "foo~;bar")
1343 #:opts %opts-w-format
1344 #:to 'cps)))))
1345 (and (= (length w) 1)
1346 (number? (string-contains (car w)
1347 "unexpected")))))
1348
1349 (pass-if "unexpected ~]"
1350 (let ((w (call-with-warnings
1351 (lambda ()
1352 (compile '((@ (ice-9 format) format) #f "foo~]")
1353 #:opts %opts-w-format
1354 #:to 'cps)))))
1355 (and (= (length w) 1)
1356 (number? (string-contains (car w)
1357 "unexpected"))))))
1358
1359 (pass-if "~{...~}"
1360 (null? (call-with-warnings
1361 (lambda ()
1362 (compile '((@ (ice-9 format) format) #f "~A ~{~S~} ~A"
1363 'hello '("ladies" "and")
1364 'gentlemen)
1365 #:opts %opts-w-format
1366 #:to 'cps)))))
1367
1368 (pass-if "~{...~}, too many args"
1369 (let ((w (call-with-warnings
1370 (lambda ()
1371 (compile '((@ (ice-9 format) format) #f "~{~S~}" 1 2 3)
1372 #:opts %opts-w-format
1373 #:to 'cps)))))
1374 (and (= (length w) 1)
1375 (number? (string-contains (car w)
1376 "expected 1, got 3")))))
1377
1378 (pass-if "~@{...~}"
1379 (null? (call-with-warnings
1380 (lambda ()
1381 (compile '((@ (ice-9 format) format) #f "~@{~S~}" 1 2 3)
1382 #:opts %opts-w-format
1383 #:to 'cps)))))
1384
1385 (pass-if "~@{...~}, too few args"
1386 (let ((w (call-with-warnings
1387 (lambda ()
1388 (compile '((@ (ice-9 format) format) #f "~A ~@{~S~}")
1389 #:opts %opts-w-format
1390 #:to 'cps)))))
1391 (and (= (length w) 1)
1392 (number? (string-contains (car w)
1393 "expected at least 1, got 0")))))
1394
1395 (pass-if "unterminated ~{...~}"
1396 (let ((w (call-with-warnings
1397 (lambda ()
1398 (compile '((@ (ice-9 format) format) #f "~{")
1399 #:opts %opts-w-format
1400 #:to 'cps)))))
1401 (and (= (length w) 1)
1402 (number? (string-contains (car w)
1403 "unterminated")))))
1404
1405 (pass-if "~(...~)"
1406 (null? (call-with-warnings
1407 (lambda ()
1408 (compile '((@ (ice-9 format) format) #f "~:@(~A ~A~)" 'foo 'bar)
1409 #:opts %opts-w-format
1410 #:to 'cps)))))
1411
1412 (pass-if "~v"
1413 (let ((w (call-with-warnings
1414 (lambda ()
1415 (compile '((@ (ice-9 format) format) #f "~v_foo")
1416 #:opts %opts-w-format
1417 #:to 'cps)))))
1418 (and (= (length w) 1)
1419 (number? (string-contains (car w)
1420 "expected 1, got 0")))))
1421 (pass-if "~v:@y"
1422 (null? (call-with-warnings
1423 (lambda ()
1424 (compile '((@ (ice-9 format) format) #f "~v:@y" 1 123)
1425 #:opts %opts-w-format
1426 #:to 'cps)))))
1427
1428
1429 (pass-if "~*"
1430 (let ((w (call-with-warnings
1431 (lambda ()
1432 (compile '((@ (ice-9 format) format) #f "~2*~a" 'a 'b)
1433 #:opts %opts-w-format
1434 #:to 'cps)))))
1435 (and (= (length w) 1)
1436 (number? (string-contains (car w)
1437 "expected 3, got 2")))))
1438
1439 (pass-if "~p"
1440 (null? (call-with-warnings
1441 (lambda ()
1442 (compile '(((@ (ice-9 format) format) #f "thing~p" 2))
1443 #:opts %opts-w-format
1444 #:to 'cps)))))
1445
1446 (pass-if "~p, too few arguments"
1447 (let ((w (call-with-warnings
1448 (lambda ()
1449 (compile '((@ (ice-9 format) format) #f "~p")
1450 #:opts %opts-w-format
1451 #:to 'cps)))))
1452 (and (= (length w) 1)
1453 (number? (string-contains (car w)
1454 "expected 1, got 0")))))
1455
1456 (pass-if "~:p"
1457 (null? (call-with-warnings
1458 (lambda ()
1459 (compile '(((@ (ice-9 format) format) #f "~d thing~:p" 2))
1460 #:opts %opts-w-format
1461 #:to 'cps)))))
1462
1463 (pass-if "~:@p, too many arguments"
1464 (let ((w (call-with-warnings
1465 (lambda ()
1466 (compile '((@ (ice-9 format) format) #f "~d pupp~:@p" 5 5)
1467 #:opts %opts-w-format
1468 #:to 'cps)))))
1469 (and (= (length w) 1)
1470 (number? (string-contains (car w)
1471 "expected 1, got 2")))))
1472
1473 (pass-if "~:@p, too few arguments"
1474 (let ((w (call-with-warnings
1475 (lambda ()
1476 (compile '((@ (ice-9 format) format) #f "pupp~:@p")
1477 #:opts %opts-w-format
1478 #:to 'cps)))))
1479 (and (= (length w) 1)
1480 (number? (string-contains (car w)
1481 "expected 1, got 0")))))
1482
1483 (pass-if "~?"
1484 (null? (call-with-warnings
1485 (lambda ()
1486 (compile '((@ (ice-9 format) format) #f "~?" "~d ~d" '(1 2))
1487 #:opts %opts-w-format
1488 #:to 'cps)))))
1489
1490 (pass-if "~^"
1491 (null? (call-with-warnings
1492 (lambda ()
1493 (compile '((@ (ice-9 format) format) #f "~a ~^ ~a" 0 1)
1494 #:opts %opts-w-format
1495 #:to 'cps)))))
1496
1497 (pass-if "~^, too few args"
1498 (let ((w (call-with-warnings
1499 (lambda ()
1500 (compile '((@ (ice-9 format) format) #f "~a ~^ ~a")
1501 #:opts %opts-w-format
1502 #:to 'cps)))))
1503 (and (= (length w) 1)
1504 (number? (string-contains (car w)
1505 "expected at least 1, got 0")))))
1506
1507 (pass-if "parameters: +,-,#, and '"
1508 (null? (call-with-warnings
1509 (lambda ()
1510 (compile '((@ (ice-9 format) format) some-port
1511 "~#~ ~,,-2f ~,,+2f ~'A~" 1234 1234)
1512 #:opts %opts-w-format
1513 #:to 'cps)))))
1514
1515 (pass-if "complex 1"
1516 (let ((w (call-with-warnings
1517 (lambda ()
1518 (compile '((@ (ice-9 format) format) #f
1519 "~4@S ~32S~@[;; ~1{~@?~}~]~@[~61t at ~a~]\n"
1520 1 2 3 4 5 6)
1521 #:opts %opts-w-format
1522 #:to 'cps)))))
1523 (and (= (length w) 1)
1524 (number? (string-contains (car w)
1525 "expected 4, got 6")))))
1526
1527 (pass-if "complex 2"
1528 (let ((w (call-with-warnings
1529 (lambda ()
1530 (compile '((@ (ice-9 format) format) #f
1531 "~:(~A~) Commands~:[~; [abbrev]~]:~2%"
1532 1 2 3 4)
1533 #:opts %opts-w-format
1534 #:to 'cps)))))
1535 (and (= (length w) 1)
1536 (number? (string-contains (car w)
1537 "expected 2, got 4")))))
1538
1539 (pass-if "complex 3"
1540 (let ((w (call-with-warnings
1541 (lambda ()
1542 (compile '((@ (ice-9 format) format) #f "~9@a~:[~*~3_~;~3d~] ~v:@y~%")
1543 #:opts %opts-w-format
1544 #:to 'cps)))))
1545 (and (= (length w) 1)
1546 (number? (string-contains (car w)
1547 "expected 5, got 0")))))
1548
1549 (pass-if "ice-9 format"
1550 (let ((w (call-with-warnings
1551 (lambda ()
1552 (let ((in (open-input-string
1553 "(use-modules ((ice-9 format) #:prefix i9-))
1554 (i9-format #t \"yo! ~A\" 1 2)")))
1555 (read-and-compile in
1556 #:opts %opts-w-format
1557 #:to 'cps))))))
1558 (and (= (length w) 1)
1559 (number? (string-contains (car w)
1560 "expected 1, got 2")))))
1561
1562 (pass-if "not format"
1563 (null? (call-with-warnings
1564 (lambda ()
1565 (compile '(let ((format chbouib))
1566 (format #t "not ~A a format string"))
1567 #:opts %opts-w-format
1568 #:to 'cps)))))
1569
1570 (with-test-prefix "simple-format"
1571
1572 (pass-if "good"
1573 (null? (call-with-warnings
1574 (lambda ()
1575 (compile '(simple-format #t "foo ~a bar ~s ~%~~" 1 2)
1576 #:opts %opts-w-format
1577 #:to 'cps)))))
1578
1579 (pass-if "wrong number of args"
1580 (let ((w (call-with-warnings
1581 (lambda ()
1582 (compile '(simple-format #t "foo ~a ~s~%" 'one-missing)
1583 #:opts %opts-w-format
1584 #:to 'cps)))))
1585 (and (= (length w) 1)
1586 (number? (string-contains (car w) "wrong number")))))
1587
1588 (pass-if "unsupported"
1589 (let ((w (call-with-warnings
1590 (lambda ()
1591 (compile '(simple-format #t "foo ~x~%" 16)
1592 #:opts %opts-w-format
1593 #:to 'cps)))))
1594 (and (= (length w) 1)
1595 (number? (string-contains (car w) "unsupported format option")))))
1596
1597 (pass-if "unsupported, gettext"
1598 (let ((w (call-with-warnings
1599 (lambda ()
1600 (compile '(simple-format #t (gettext "foo ~2f~%") 3.14)
1601 #:opts %opts-w-format
1602 #:to 'cps)))))
1603 (and (= (length w) 1)
1604 (number? (string-contains (car w) "unsupported format option")))))
1605
1606 (pass-if "unsupported, ngettext"
1607 (let ((w (call-with-warnings
1608 (lambda ()
1609 (compile '(simple-format #t (ngettext "s ~x" "p ~x" x) x)
1610 #:opts %opts-w-format
1611 #:to 'cps)))))
1612 (and (= (length w) 1)
1613 (number? (string-contains (car w) "unsupported format option")))))))
1614
1615 (with-test-prefix "duplicate-case-datum"
1616
1617 (pass-if "quiet"
1618 (null? (call-with-warnings
1619 (lambda ()
1620 (compile '(case x ((1) 'one) ((2) 'two))
1621 #:opts %opts-w-duplicate-case-datum
1622 #:to 'cps)))))
1623
1624 (pass-if "one duplicate"
1625 (let ((w (call-with-warnings
1626 (lambda ()
1627 (compile '(case x
1628 ((1) 'one)
1629 ((2) 'two)
1630 ((1) 'one-again))
1631 #:opts %opts-w-duplicate-case-datum
1632 #:to 'cps)))))
1633 (and (= (length w) 1)
1634 (number? (string-contains (car w) "duplicate")))))
1635
1636 (pass-if "one duplicate"
1637 (let ((w (call-with-warnings
1638 (lambda ()
1639 (compile '(case x
1640 ((1 2 3) 'a)
1641 ((1) 'one))
1642 #:opts %opts-w-duplicate-case-datum
1643 #:to 'cps)))))
1644 (and (= (length w) 1)
1645 (number? (string-contains (car w) "duplicate"))))))
1646
1647 (with-test-prefix "bad-case-datum"
1648
1649 (pass-if "quiet"
1650 (null? (call-with-warnings
1651 (lambda ()
1652 (compile '(case x ((1) 'one) ((2) 'two))
1653 #:opts %opts-w-bad-case-datum
1654 #:to 'cps)))))
1655
1656 (pass-if "not eqv?"
1657 (let ((w (call-with-warnings
1658 (lambda ()
1659 (compile '(case x
1660 ((1) 'one)
1661 (("bad") 'bad))
1662 #:opts %opts-w-bad-case-datum
1663 #:to 'cps)))))
1664 (and (= (length w) 1)
1665 (number? (string-contains (car w)
1666 "cannot be meaningfully compared")))))
1667
1668 (pass-if "one clause element not eqv?"
1669 (let ((w (call-with-warnings
1670 (lambda ()
1671 (compile '(case x
1672 ((1 (2) 3) 'a))
1673 #:opts %opts-w-duplicate-case-datum
1674 #:to 'cps)))))
1675 (and (= (length w) 1)
1676 (number? (string-contains (car w)
1677 "cannot be meaningfully compared"))))))
1678
1679 (with-test-prefix "location"
1680 (define (call-with-fake-input-file filename contents thunk)
1681 (call-with-input-string contents
1682 (lambda (port)
1683 (set-port-filename! port filename)
1684 (thunk port))))
1685
1686 (pass-if "unused variable"
1687 (let ((w (call-with-warnings
1688 (lambda ()
1689 (call-with-fake-input-file
1690 "unused-variable.scm"
1691 "\
1692 (lambda (x)
1693 (let ((y (+ x 2)))
1694 x))"
1695 (lambda (port)
1696 (read-and-compile port #:opts %opts-w-unused #:to 'cps)))))))
1697 (and (= (length w) 1)
1698 (number? (string-contains (car w) "unused variable `y'"))
1699 (number? (string-contains (car w) "unused-variable.scm:2:2")))))
1700
1701 (pass-if "unbound variable (spaces)"
1702 (let ((w (call-with-warnings
1703 (lambda ()
1704 (call-with-fake-input-file
1705 "unbound-spaces.scm"
1706 " (foo)"
1707 (lambda (port)
1708 (read-and-compile port #:opts %opts-w-unbound #:to 'cps)))))))
1709 (and (= (length w) 1)
1710 (number? (string-contains (car w) "unbound variable `foo'"))
1711 (number? (string-contains (car w) "unbound-spaces.scm:1:3")))))
1712
1713 (pass-if "unbound variable (tabs)"
1714 (let ((w (call-with-warnings
1715 (lambda ()
1716 (call-with-fake-input-file
1717 "unbound-tabs.scm"
1718 "\t\t(foo)"
1719 (lambda (port)
1720 (read-and-compile port #:opts %opts-w-unbound #:to 'cps)))))))
1721 (and (= (length w) 1)
1722 (number? (string-contains (car w) "unbound variable `foo'"))
1723 (number? (string-contains (car w) "unbound-tabs.scm:1:17")))))))
1724
1725 ;; Local Variables:
1726 ;; eval: (put 'pass-if-primitives-resolved 'scheme-indent-function 1)
1727 ;; eval: (put 'pass-if-tree-il->scheme 'scheme-indent-function 1)
1728 ;; End: