tree-il.test 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  1. ;;;; tree-il.test --- test suite for compiling tree-il -*- scheme -*-
  2. ;;;; Andy Wingo <wingo@pobox.com> --- May 2009
  3. ;;;;
  4. ;;;; Copyright (C) 2009-2014,2018-2021 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. (define-module (test-suite tree-il)
  20. #:use-module (test-suite lib)
  21. #:use-module (system base compile)
  22. #:use-module (system base pmatch)
  23. #:use-module (system base message)
  24. #:use-module (language tree-il)
  25. #:use-module (language tree-il primitives)
  26. #:use-module (language tree-il optimize)
  27. #:use-module (ice-9 match)
  28. #:use-module (ice-9 regex)
  29. #:use-module (srfi srfi-13))
  30. (define-syntax-rule (pass-if-primitives-resolved in expected)
  31. (pass-if (format #f "primitives-resolved in ~s" 'in)
  32. (let* ((module (let ((m (make-module)))
  33. (beautify-user-module! m)
  34. m))
  35. (orig (parse-tree-il 'in))
  36. (resolved (expand-primitives (resolve-primitives orig module))))
  37. (or (equal? (unparse-tree-il resolved) 'expected)
  38. (begin
  39. (format (current-error-port)
  40. "primitive test failed: got ~s, expected ~s"
  41. resolved 'expected)
  42. #f)))))
  43. (define-syntax pass-if-tree-il->scheme
  44. (syntax-rules ()
  45. ((_ in pat)
  46. (assert-scheme->tree-il->scheme in pat #t))
  47. ((_ in pat guard-exp)
  48. (pass-if 'in
  49. (pmatch (tree-il->scheme
  50. (compile 'in #:from 'scheme #:to 'tree-il))
  51. (pat (guard guard-exp) #t)
  52. (_ #f))))))
  53. (with-test-prefix "primitives"
  54. (with-test-prefix "error"
  55. (pass-if-primitives-resolved
  56. (primcall error (const "message"))
  57. (primcall throw (const misc-error) (const #f)
  58. (const "message") (primcall list) (const #f)))
  59. (pass-if-primitives-resolved
  60. (primcall error (const "message") (const 42))
  61. (primcall throw (const misc-error) (const #f)
  62. (const "message ~S") (primcall list (const 42))
  63. (const #f)))
  64. (pass-if-equal "https://bugs.gnu.org/39509"
  65. '(throw 'misc-error #f "~A" (list "message") #f)
  66. (let ((module (make-fresh-user-module)))
  67. (decompile (expand-primitives
  68. (resolve-primitives
  69. (compile '(error ((lambda () "message")))
  70. #:to 'tree-il)
  71. module))
  72. #:from 'tree-il
  73. #:to 'scheme)))
  74. (pass-if-equal "https://bugs.gnu.org/39509 with argument"
  75. '(throw 'misc-error #f "~A ~S" (list "message" 42) #f)
  76. (let ((module (make-fresh-user-module)))
  77. (decompile (expand-primitives
  78. (resolve-primitives
  79. (compile '(error ((lambda () "message")) 42)
  80. #:to 'tree-il)
  81. module))
  82. #:from 'tree-il
  83. #:to 'scheme)))))
  84. (define* (compile+optimize exp #:key (env (current-module))
  85. (optimization-level 2) (opts '()))
  86. (let ((optimize (make-lowerer optimization-level opts)))
  87. (optimize (compile exp #:to 'tree-il #:env env) env)))
  88. (with-test-prefix "optimize"
  89. (pass-if-equal "https://debbugs.gnu.org/48098"
  90. '(begin
  91. (display "hey!\n")
  92. 42)
  93. (decompile
  94. (compile+optimize
  95. '(begin
  96. (call-with-prompt (make-prompt-tag)
  97. (lambda () (display "hey!\n"))
  98. (lambda (k) #f))
  99. 42)))))
  100. (with-test-prefix "tree-il->scheme"
  101. (pass-if-tree-il->scheme
  102. (case-lambda ((a) a) ((b c) (list b c)))
  103. (case-lambda ((,a) ,a1) ((,b ,c) (list ,b1 ,c1)))
  104. (and (eq? a a1) (eq? b b1) (eq? c c1))))
  105. (with-test-prefix "contification"
  106. (pass-if "http://debbugs.gnu.org/9769"
  107. ((compile '(lambda ()
  108. (let ((fail (lambda () #f)))
  109. (let ((test (lambda () (fail))))
  110. (test))
  111. #t))
  112. ;; Prevent inlining. We're testing contificatoin here,
  113. ;; and inlining it will reduce the entire thing to #t.
  114. #:opts '(#:partial-eval? #f)))))
  115. (define (sum . args)
  116. (apply + args))
  117. (with-test-prefix "many args"
  118. (pass-if "call with > 256 args"
  119. (equal? (compile `(1+ (sum ,@(iota 1000)))
  120. #:env (current-module))
  121. (1+ (apply sum (iota 1000)))))
  122. (pass-if "tail call with > 256 args"
  123. (equal? (compile `(sum ,@(iota 1000))
  124. #:env (current-module))
  125. (apply sum (iota 1000)))))
  126. (with-test-prefix "tree-il-fold"
  127. (pass-if "void"
  128. (let ((up 0) (down 0) (mark (list 'mark)))
  129. (and (eq? mark
  130. (tree-il-fold (lambda (x y) (set! down (1+ down)) y)
  131. (lambda (x y) (set! up (1+ up)) y)
  132. mark
  133. (make-void #f)))
  134. (= up 1)
  135. (= down 1))))
  136. (pass-if "lambda and application"
  137. (let* ((ups '()) (downs '())
  138. (result (tree-il-fold (lambda (x y)
  139. (set! downs (cons x downs))
  140. (1+ y))
  141. (lambda (x y)
  142. (set! ups (cons x ups))
  143. (1+ y))
  144. 0
  145. (parse-tree-il
  146. '(lambda ()
  147. (lambda-case
  148. (((x y) #f #f #f () (x1 y1))
  149. (call (toplevel +)
  150. (lexical x x1)
  151. (lexical y y1)))
  152. #f))))))
  153. (define (strip-source x)
  154. (post-order (lambda (x)
  155. (set! (tree-il-src x) #f)
  156. x)
  157. x))
  158. (and (= result 12)
  159. (equal? (map strip-source (list-head (reverse ups) 3))
  160. (list (make-toplevel-ref #f #f '+)
  161. (make-lexical-ref #f 'x 'x1)
  162. (make-lexical-ref #f 'y 'y1)))
  163. (equal? (map strip-source (reverse (list-head downs 3)))
  164. (list (make-toplevel-ref #f #f '+)
  165. (make-lexical-ref #f 'x 'x1)
  166. (make-lexical-ref #f 'y 'y1)))))))
  167. ;;;
  168. ;;; Warnings.
  169. ;;;
  170. ;; Make sure we get English messages.
  171. (when (defined? 'setlocale)
  172. (setlocale LC_ALL "C"))
  173. (define (call-with-warnings thunk)
  174. (let ((port (open-output-string)))
  175. ;; Disable any warnings added by default.
  176. (parameterize ((default-warning-level 0))
  177. (with-fluids ((*current-warning-port* port)
  178. (*current-warning-prefix* ""))
  179. (thunk)))
  180. (let ((warnings (get-output-string port)))
  181. (string-tokenize warnings
  182. (char-set-complement (char-set #\newline))))))
  183. (define %opts-w-unused
  184. '(#:warnings (unused-variable)))
  185. (define %opts-w-unused-toplevel
  186. '(#:warnings (unused-toplevel)))
  187. (define %opts-w-shadowed-toplevel
  188. '(#:warnings (shadowed-toplevel)))
  189. (define %opts-w-unbound
  190. '(#:warnings (unbound-variable)))
  191. (define %opts-w-use-before-definition
  192. '(#:warnings (use-before-definition)))
  193. (define %opts-w-non-idempotent-definition
  194. '(#:warnings (non-idempotent-definition)))
  195. (define %opts-w-arity
  196. '(#:warnings (arity-mismatch)))
  197. (define %opts-w-format
  198. '(#:warnings (format)))
  199. (define %opts-w-duplicate-case-datum
  200. '(#:warnings (duplicate-case-datum)))
  201. (define %opts-w-bad-case-datum
  202. '(#:warnings (bad-case-datum)))
  203. (with-test-prefix "warnings"
  204. (pass-if "unknown warning type"
  205. (let ((w (call-with-warnings
  206. (lambda ()
  207. (compile #t #:opts '(#:warnings (does-not-exist)))))))
  208. (and (= (length w) 1)
  209. (number? (string-contains (car w) "unknown warning")))))
  210. (with-test-prefix "unused-variable"
  211. (pass-if "quiet"
  212. (null? (call-with-warnings
  213. (lambda ()
  214. (compile '(lambda (x y) (+ x y))
  215. #:opts %opts-w-unused)))))
  216. (pass-if "let/unused"
  217. (let ((w (call-with-warnings
  218. (lambda ()
  219. (compile '(lambda (x)
  220. (let ((y (+ x 2)))
  221. x))
  222. #:opts %opts-w-unused)))))
  223. (and (= (length w) 1)
  224. (number? (string-contains (car w) "unused variable `y'")))))
  225. (pass-if "shadowed variable"
  226. (let ((w (call-with-warnings
  227. (lambda ()
  228. (compile '(lambda (x)
  229. (let ((y x))
  230. (let ((y (+ x 2)))
  231. (+ x y))))
  232. #:opts %opts-w-unused)))))
  233. (and (= (length w) 1)
  234. (number? (string-contains (car w) "unused variable `y'")))))
  235. (pass-if "letrec"
  236. (null? (call-with-warnings
  237. (lambda ()
  238. (compile '(lambda ()
  239. (letrec ((x (lambda () (y)))
  240. (y (lambda () (x))))
  241. y))
  242. #:opts %opts-w-unused)))))
  243. (pass-if "unused argument"
  244. ;; Unused arguments should not be reported.
  245. (null? (call-with-warnings
  246. (lambda ()
  247. (compile '(lambda (x y z) #t)
  248. #:opts %opts-w-unused)))))
  249. (pass-if "special variable names"
  250. (null? (call-with-warnings
  251. (lambda ()
  252. (compile '(lambda ()
  253. (let ((_ 'underscore)
  254. (#{gensym name}# 'ignore-me))
  255. #t))
  256. #:to 'cps
  257. #:opts %opts-w-unused))))))
  258. (with-test-prefix "unused-toplevel"
  259. (pass-if "used after definition"
  260. (null? (call-with-warnings
  261. (lambda ()
  262. (let ((in (open-input-string
  263. "(define foo 2) foo")))
  264. (read-and-compile in
  265. #:to 'cps
  266. #:opts %opts-w-unused-toplevel))))))
  267. (pass-if "used before definition"
  268. (null? (call-with-warnings
  269. (lambda ()
  270. (let ((in (open-input-string
  271. "(define (bar) foo) (define foo 2) (bar)")))
  272. (read-and-compile in
  273. #:to 'cps
  274. #:opts %opts-w-unused-toplevel))))))
  275. (pass-if "unused but public"
  276. (let ((in (open-input-string
  277. "(define-module (test-suite tree-il x) #:export (bar))
  278. (define (bar) #t)")))
  279. (null? (call-with-warnings
  280. (lambda ()
  281. (read-and-compile in
  282. #:to 'cps
  283. #:opts %opts-w-unused-toplevel))))))
  284. (pass-if "unused but public (more)"
  285. (let ((in (open-input-string
  286. "(define-module (test-suite tree-il x) #:export (bar))
  287. (define (bar) (baz))
  288. (define (baz) (foo))
  289. (define (foo) #t)")))
  290. (null? (call-with-warnings
  291. (lambda ()
  292. (read-and-compile in
  293. #:to 'cps
  294. #:opts %opts-w-unused-toplevel))))))
  295. (pass-if "unused but define-public"
  296. (null? (call-with-warnings
  297. (lambda ()
  298. (compile '(define-public foo 2)
  299. #:to 'cps
  300. #:opts %opts-w-unused-toplevel)))))
  301. (pass-if "used by macro"
  302. ;; FIXME: See comment about macros at `unused-toplevel-analysis'.
  303. (throw 'unresolved)
  304. (null? (call-with-warnings
  305. (lambda ()
  306. (let ((in (open-input-string
  307. "(define (bar) 'foo)
  308. (define-syntax baz
  309. (syntax-rules () ((_) (bar))))")))
  310. (read-and-compile in
  311. #:to 'cps
  312. #:opts %opts-w-unused-toplevel))))))
  313. (pass-if "unused"
  314. (let ((w (call-with-warnings
  315. (lambda ()
  316. (compile '(define foo 2)
  317. #:to 'cps
  318. #:opts %opts-w-unused-toplevel)))))
  319. (and (= (length w) 1)
  320. (number? (string-contains (car w)
  321. (format #f "top-level variable `~A'"
  322. 'foo))))))
  323. (pass-if "unused recursive"
  324. (let ((w (call-with-warnings
  325. (lambda ()
  326. (compile '(define (foo) (foo))
  327. #:to 'cps
  328. #:opts %opts-w-unused-toplevel)))))
  329. (and (= (length w) 1)
  330. (number? (string-contains (car w)
  331. (format #f "top-level variable `~A'"
  332. 'foo))))))
  333. (pass-if "unused mutually recursive"
  334. (let* ((in (open-input-string
  335. "(define (foo) (bar)) (define (bar) (foo))"))
  336. (w (call-with-warnings
  337. (lambda ()
  338. (read-and-compile in
  339. #:to 'cps
  340. #:opts %opts-w-unused-toplevel)))))
  341. (and (= (length w) 2)
  342. (number? (string-contains (car w)
  343. (format #f "top-level variable `~A'"
  344. 'foo)))
  345. (number? (string-contains (cadr w)
  346. (format #f "top-level variable `~A'"
  347. 'bar))))))
  348. (pass-if "special variable names"
  349. (null? (call-with-warnings
  350. (lambda ()
  351. (compile '(define #{gensym name}# 'ignore-me)
  352. #:to 'cps
  353. #:opts %opts-w-unused-toplevel))))))
  354. (with-test-prefix "shadowed-toplevel"
  355. (pass-if "quiet"
  356. (null? (call-with-warnings
  357. (lambda ()
  358. (let ((in (open-input-string
  359. "(define foo 2) (define bar 3)")))
  360. (read-and-compile in
  361. #:to 'cps
  362. #:opts
  363. %opts-w-shadowed-toplevel))))))
  364. (pass-if "internal define"
  365. (null? (call-with-warnings
  366. (lambda ()
  367. (let ((in (open-input-string
  368. "(define foo 2)
  369. (define (bar x) (define foo (+ x 2)) (* foo x))")))
  370. (read-and-compile in
  371. #:to 'cps
  372. #:opts
  373. %opts-w-shadowed-toplevel))))))
  374. (pass-if "one shadowing definition"
  375. (match (call-with-warnings
  376. (lambda ()
  377. (let ((in (open-input-string
  378. "(define foo 2)\n (define foo 3)")))
  379. (read-and-compile in
  380. #:to 'cps
  381. #:opts
  382. %opts-w-shadowed-toplevel))))
  383. ((message)
  384. (->bool (string-match ":2:2:.*previous.*foo.*:1:0" message)))))
  385. (pass-if "two shadowing definitions"
  386. (match (call-with-warnings
  387. (lambda ()
  388. (let ((in (open-input-string
  389. "(define-public foo 2)\n(define foo 3)
  390. (define (foo x) x)")))
  391. (read-and-compile in
  392. #:to 'cps
  393. #:opts
  394. %opts-w-shadowed-toplevel))))
  395. ((message1 message2)
  396. (->bool
  397. (and (string-match ":2:0:.*previous.*foo.*:1:0" message1)
  398. (string-match ":3:2:.*previous.*foo.*:1:0" message2))))))
  399. (pass-if "define-public"
  400. (match (call-with-warnings
  401. (lambda ()
  402. (let ((in (open-input-string
  403. "(define foo 2)\n(define-public foo 3)")))
  404. (read-and-compile in
  405. #:to 'cps
  406. #:opts
  407. %opts-w-shadowed-toplevel))))
  408. ((message)
  409. (->bool (string-match ":2:0:.*previous.*foo.*:1:0" message)))))
  410. (pass-if "macro"
  411. (match (call-with-warnings
  412. (lambda ()
  413. (let ((in (open-input-string
  414. "(define foo 42)
  415. (define-syntax-rule (defun proc (args ...) body ...)
  416. (define (proc args ...) body ...))
  417. (defun foo (a b c) (+ a b c))")))
  418. (read-and-compile in
  419. #:to 'cps
  420. #:opts
  421. %opts-w-shadowed-toplevel))))
  422. ((message)
  423. (->bool (string-match ":4:2:.*previous.*foo.*:1:0" message))))))
  424. (with-test-prefix "unbound variable"
  425. (pass-if "quiet"
  426. (null? (call-with-warnings
  427. (lambda ()
  428. (compile '+ #:opts %opts-w-unbound)))))
  429. (pass-if "ref"
  430. (let* ((v (gensym))
  431. (w (call-with-warnings
  432. (lambda ()
  433. (compile v
  434. #:to 'cps
  435. #:opts %opts-w-unbound)))))
  436. (and (= (length w) 1)
  437. (number? (string-contains (car w)
  438. (format #f "unbound variable `~A'"
  439. v))))))
  440. (pass-if "set!"
  441. (let* ((v (gensym))
  442. (w (call-with-warnings
  443. (lambda ()
  444. (compile `(set! ,v 7)
  445. #:to 'cps
  446. #:opts %opts-w-unbound)))))
  447. (and (= (length w) 1)
  448. (number? (string-contains (car w)
  449. (format #f "unbound variable `~A'"
  450. v))))))
  451. (pass-if "module-local top-level is visible"
  452. (let ((m (make-module))
  453. (v (gensym)))
  454. (beautify-user-module! m)
  455. (compile `(define ,v 123)
  456. #:env m #:opts %opts-w-unbound)
  457. (null? (call-with-warnings
  458. (lambda ()
  459. (compile v
  460. #:env m
  461. #:to 'cps
  462. #:opts %opts-w-unbound))))))
  463. (pass-if "module-local top-level is visible after"
  464. (let ((m (make-module))
  465. (v (gensym)))
  466. (beautify-user-module! m)
  467. (null? (call-with-warnings
  468. (lambda ()
  469. (let ((in (open-input-string
  470. "(define (f)
  471. (set! chbouib 3))
  472. (define chbouib 5)")))
  473. (read-and-compile in
  474. #:env m
  475. #:opts %opts-w-unbound)))))))
  476. (pass-if "optional arguments are visible"
  477. (null? (call-with-warnings
  478. (lambda ()
  479. (compile '(lambda* (x #:optional y z) (list x y z))
  480. #:opts %opts-w-unbound
  481. #:to 'cps)))))
  482. (pass-if "keyword arguments are visible"
  483. (null? (call-with-warnings
  484. (lambda ()
  485. (compile '(lambda* (x #:key y z) (list x y z))
  486. #:opts %opts-w-unbound
  487. #:to 'cps)))))
  488. (pass-if "GOOPS definitions are visible"
  489. (let ((m (make-module))
  490. (v (gensym)))
  491. (beautify-user-module! m)
  492. (module-use! m (resolve-interface '(oop goops)))
  493. (null? (call-with-warnings
  494. (lambda ()
  495. (let ((in (open-input-string
  496. "(define-class <foo> ()
  497. (bar #:getter foo-bar))
  498. (define z (foo-bar (make <foo>)))")))
  499. (read-and-compile in
  500. #:env m
  501. #:opts %opts-w-unbound))))))))
  502. (pass-if "re-exported binding" ;<https://bugs.gnu.org/47031>
  503. (null? (call-with-warnings
  504. (lambda ()
  505. (compile '(begin
  506. (use-modules (srfi srfi-35))
  507. ;; This 'condition' form expands to a
  508. ;; 'make-compound-condition' call, which is
  509. ;; re-exported from (ice-9 exceptions).
  510. (condition (&error)
  511. (&message (message "oh!"))))
  512. #:opts %opts-w-unbound)))))
  513. (with-test-prefix "use-before-definition"
  514. (define-syntax-rule (pass-if-warnings expr pat test)
  515. (pass-if 'expr
  516. (match (call-with-warnings
  517. (lambda ()
  518. (compile 'expr #:to 'cps
  519. #:opts %opts-w-use-before-definition)))
  520. (pat test)
  521. (_ #f))))
  522. (define-syntax-rule (pass-if-no-warnings expr)
  523. (pass-if-warnings expr () #t))
  524. (pass-if-no-warnings
  525. (begin (define x +) x))
  526. (pass-if-warnings
  527. (begin x (define x +))
  528. (w) (number? (string-contains w "`x' used before definition")))
  529. (pass-if-warnings
  530. (begin (set! x 1) (define x +))
  531. (w) (number? (string-contains w "`x' used before definition")))
  532. (pass-if-no-warnings
  533. (begin (lambda () x) (define x +)))
  534. (pass-if-no-warnings
  535. (begin (if (defined? 'x) x) (define x +))))
  536. (with-test-prefix "non-idempotent-definition"
  537. (define-syntax-rule (pass-if-warnings expr pat test)
  538. (pass-if 'expr
  539. (match (call-with-warnings
  540. (lambda ()
  541. (compile 'expr #:to 'cps
  542. #:opts %opts-w-non-idempotent-definition)))
  543. (pat test)
  544. (_ #f))))
  545. (define-syntax-rule (pass-if-no-warnings expr)
  546. (pass-if-warnings expr () #t))
  547. (pass-if-no-warnings
  548. (begin (define - +) (define y -)))
  549. (pass-if-warnings
  550. (begin - (define - +))
  551. (w) (number? (string-contains w "non-idempotent binding for `-'")))
  552. (pass-if-warnings
  553. (begin (define y -) (define - +))
  554. (w) (number? (string-contains w "non-idempotent binding for `-'")))
  555. (pass-if-no-warnings
  556. (begin (lambda () -) (define - +)))
  557. (pass-if-no-warnings
  558. (begin (if (defined? '-) -) (define - +))))
  559. (with-test-prefix "arity mismatch"
  560. (pass-if "quiet"
  561. (null? (call-with-warnings
  562. (lambda ()
  563. (compile '(cons 'a 'b) #:opts %opts-w-arity)))))
  564. (pass-if "direct application"
  565. (let ((w (call-with-warnings
  566. (lambda ()
  567. (compile '((lambda (x y) (or x y)) 1 2 3 4 5)
  568. #:opts %opts-w-arity
  569. #:to 'cps)))))
  570. (and (= (length w) 1)
  571. (number? (string-contains (car w)
  572. "wrong number of arguments to")))))
  573. (pass-if "local"
  574. (let ((w (call-with-warnings
  575. (lambda ()
  576. (compile '(let ((f (lambda (x y) (+ x y))))
  577. (f 2))
  578. #:opts %opts-w-arity
  579. #:to 'cps)))))
  580. (and (= (length w) 1)
  581. (number? (string-contains (car w)
  582. "wrong number of arguments to")))))
  583. (pass-if "global"
  584. (let ((w (call-with-warnings
  585. (lambda ()
  586. (compile '(cons 1 2 3 4)
  587. #:opts %opts-w-arity
  588. #:to 'cps)))))
  589. (and (= (length w) 1)
  590. (number? (string-contains (car w)
  591. "wrong number of arguments to")))))
  592. (pass-if "alias to global"
  593. (let ((w (call-with-warnings
  594. (lambda ()
  595. (compile '(let ((f cons)) (f 1 2 3 4))
  596. #:opts %opts-w-arity
  597. #:to 'cps)))))
  598. (and (= (length w) 1)
  599. (number? (string-contains (car w)
  600. "wrong number of arguments to")))))
  601. (pass-if "alias to lexical to global"
  602. (let ((w (call-with-warnings
  603. (lambda ()
  604. (compile '(let ((f number?))
  605. (let ((g f))
  606. (f 1 2 3 4)))
  607. #:opts %opts-w-arity
  608. #:to 'cps)))))
  609. (and (= (length w) 1)
  610. (number? (string-contains (car w)
  611. "wrong number of arguments to")))))
  612. (pass-if "alias to lexical"
  613. (let ((w (call-with-warnings
  614. (lambda ()
  615. (compile '(let ((f (lambda (x y z) (+ x y z))))
  616. (let ((g f))
  617. (g 1)))
  618. #:opts %opts-w-arity
  619. #:to 'cps)))))
  620. (and (= (length w) 1)
  621. (number? (string-contains (car w)
  622. "wrong number of arguments to")))))
  623. (pass-if "letrec"
  624. (let ((w (call-with-warnings
  625. (lambda ()
  626. (compile '(letrec ((odd? (lambda (x) (even? (1- x))))
  627. (even? (lambda (x)
  628. (or (= 0 x)
  629. (odd?)))))
  630. (odd? 1))
  631. #:opts %opts-w-arity
  632. #:to 'cps)))))
  633. (and (= (length w) 1)
  634. (number? (string-contains (car w)
  635. "wrong number of arguments to")))))
  636. (pass-if "case-lambda"
  637. (null? (call-with-warnings
  638. (lambda ()
  639. (compile '(let ((f (case-lambda
  640. ((x) 1)
  641. ((x y) 2)
  642. ((x y z) 3))))
  643. (list (f 1)
  644. (f 1 2)
  645. (f 1 2 3)))
  646. #:opts %opts-w-arity
  647. #:to 'cps)))))
  648. (pass-if "case-lambda with wrong number of arguments"
  649. (let ((w (call-with-warnings
  650. (lambda ()
  651. (compile '(let ((f (case-lambda
  652. ((x) 1)
  653. ((x y) 2))))
  654. (f 1 2 3))
  655. #:opts %opts-w-arity
  656. #:to 'cps)))))
  657. (and (= (length w) 1)
  658. (number? (string-contains (car w)
  659. "wrong number of arguments to")))))
  660. (pass-if "case-lambda*"
  661. (null? (call-with-warnings
  662. (lambda ()
  663. (compile '(let ((f (case-lambda*
  664. ((x #:optional y) 1)
  665. ((x #:key y) 2)
  666. ((x y #:key z) 3))))
  667. (list (f 1)
  668. (f 1 2)
  669. (f #:y 2)
  670. (f 1 2 #:z 3)))
  671. #:opts %opts-w-arity
  672. #:to 'cps)))))
  673. (pass-if "case-lambda* with wrong arguments"
  674. (let ((w (call-with-warnings
  675. (lambda ()
  676. (compile '(let ((f (case-lambda*
  677. ((x #:optional y) 1)
  678. ((x #:key y) 2)
  679. ((x y #:key z) 3))))
  680. (list (f)
  681. (f 1 #:z 3)))
  682. #:opts %opts-w-arity
  683. #:to 'cps)))))
  684. (and (= (length w) 2)
  685. (null? (filter (lambda (w)
  686. (not
  687. (number?
  688. (string-contains
  689. w "wrong number of arguments to"))))
  690. w)))))
  691. (pass-if "top-level applicable struct"
  692. (null? (call-with-warnings
  693. (lambda ()
  694. (compile '(let ((p current-warning-port))
  695. (p (+ (p) 1))
  696. (p))
  697. #:opts %opts-w-arity
  698. #:to 'cps)))))
  699. (pass-if "top-level applicable struct with wrong arguments"
  700. (let ((w (call-with-warnings
  701. (lambda ()
  702. (compile '(let ((p current-warning-port))
  703. (p 1 2 3))
  704. #:opts %opts-w-arity
  705. #:to 'cps)))))
  706. (and (= (length w) 1)
  707. (number? (string-contains (car w)
  708. "wrong number of arguments to")))))
  709. (pass-if "local toplevel-defines"
  710. (let ((w (call-with-warnings
  711. (lambda ()
  712. (let ((in (open-input-string "
  713. (define (g x) (f x))
  714. (define (f) 1)")))
  715. (read-and-compile in
  716. #:opts %opts-w-arity
  717. #:to 'cps))))))
  718. (and (= (length w) 1)
  719. (number? (string-contains (car w)
  720. "wrong number of arguments to")))))
  721. (pass-if "global toplevel alias"
  722. (let ((w (call-with-warnings
  723. (lambda ()
  724. (let ((in (open-input-string "
  725. (define f cons)
  726. (define (g) (f))")))
  727. (read-and-compile in
  728. #:opts %opts-w-arity
  729. #:to 'cps))))))
  730. (and (= (length w) 1)
  731. (number? (string-contains (car w)
  732. "wrong number of arguments to")))))
  733. (pass-if "local toplevel overrides global"
  734. (null? (call-with-warnings
  735. (lambda ()
  736. (let ((in (open-input-string "
  737. (define (cons) 0)
  738. (define (foo x) (cons))")))
  739. (read-and-compile in
  740. #:opts %opts-w-arity
  741. #:to 'cps))))))
  742. (pass-if "keyword not passed and quiet"
  743. (null? (call-with-warnings
  744. (lambda ()
  745. (compile '(let ((f (lambda* (x #:key y) y)))
  746. (f 2))
  747. #:opts %opts-w-arity
  748. #:to 'cps)))))
  749. (pass-if "keyword passed and quiet"
  750. (null? (call-with-warnings
  751. (lambda ()
  752. (compile '(let ((f (lambda* (x #:key y) y)))
  753. (f 2 #:y 3))
  754. #:opts %opts-w-arity
  755. #:to 'cps)))))
  756. (pass-if "keyword passed to global and quiet"
  757. (null? (call-with-warnings
  758. (lambda ()
  759. (let ((in (open-input-string "
  760. (use-modules (system base compile))
  761. (compile '(+ 2 3) #:env (current-module))")))
  762. (read-and-compile in
  763. #:opts %opts-w-arity
  764. #:to 'cps))))))
  765. (pass-if "extra keyword"
  766. (let ((w (call-with-warnings
  767. (lambda ()
  768. (compile '(let ((f (lambda* (x #:key y) y)))
  769. (f 2 #:Z 3))
  770. #:opts %opts-w-arity
  771. #:to 'cps)))))
  772. (and (= (length w) 1)
  773. (number? (string-contains (car w)
  774. "wrong number of arguments to")))))
  775. (pass-if "extra keywords allowed"
  776. (null? (call-with-warnings
  777. (lambda ()
  778. (compile '(let ((f (lambda* (x #:key y #:allow-other-keys)
  779. y)))
  780. (f 2 #:Z 3))
  781. #:opts %opts-w-arity
  782. #:to 'cps))))))
  783. (with-test-prefix "format"
  784. (pass-if "quiet (no args)"
  785. (null? (call-with-warnings
  786. (lambda ()
  787. (compile '(format #t "hey!")
  788. #:opts %opts-w-format
  789. #:to 'cps)))))
  790. (pass-if "quiet (1 arg)"
  791. (null? (call-with-warnings
  792. (lambda ()
  793. (compile '(format #t "hey ~A!" "you")
  794. #:opts %opts-w-format
  795. #:to 'cps)))))
  796. (pass-if "quiet (2 args)"
  797. (null? (call-with-warnings
  798. (lambda ()
  799. (compile '(format #t "~A ~A!" "hello" "world")
  800. #:opts %opts-w-format
  801. #:to 'cps)))))
  802. (pass-if "wrong port arg"
  803. (let ((w (call-with-warnings
  804. (lambda ()
  805. (compile '(format 10 "foo")
  806. #:opts %opts-w-format
  807. #:to 'cps)))))
  808. (and (= (length w) 1)
  809. (number? (string-contains (car w)
  810. "wrong port argument")))))
  811. (pass-if "non-literal format string"
  812. (let ((w (call-with-warnings
  813. (lambda ()
  814. (compile '(format #f fmt)
  815. #:opts %opts-w-format
  816. #:to 'cps)))))
  817. (and (= (length w) 1)
  818. (number? (string-contains (car w)
  819. "non-literal format string")))))
  820. (pass-if "non-literal format string using gettext"
  821. (null? (call-with-warnings
  822. (lambda ()
  823. (compile '(format #t (gettext "~A ~A!") "hello" "world")
  824. #:opts %opts-w-format
  825. #:to 'cps)))))
  826. (pass-if "non-literal format string using gettext as _"
  827. (null? (call-with-warnings
  828. (lambda ()
  829. (compile '(format #t (G_ "~A ~A!") "hello" "world")
  830. #:opts %opts-w-format
  831. #:to 'cps)))))
  832. (pass-if "non-literal format string using gettext as top-level _"
  833. (null? (call-with-warnings
  834. (lambda ()
  835. (compile '(begin
  836. (define (_ s) (gettext s "my-domain"))
  837. (format #t (G_ "~A ~A!") "hello" "world"))
  838. #:opts %opts-w-format
  839. #:to 'cps)))))
  840. (pass-if "non-literal format string using gettext as module-ref _"
  841. (null? (call-with-warnings
  842. (lambda ()
  843. (compile '(format #t ((@@ (foo) G_) "~A ~A!") "hello" "world")
  844. #:opts %opts-w-format
  845. #:to 'cps)))))
  846. (pass-if "non-literal format string using gettext as lexical _"
  847. (null? (call-with-warnings
  848. (lambda ()
  849. (compile '(let ((_ (lambda (s)
  850. (gettext s "my-domain"))))
  851. (format #t (G_ "~A ~A!") "hello" "world"))
  852. #:opts %opts-w-format
  853. #:to 'cps)))))
  854. (pass-if "non-literal format string using ngettext"
  855. (null? (call-with-warnings
  856. (lambda ()
  857. (compile '(format #t
  858. (ngettext "~a thing" "~a things" n "dom") n)
  859. #:opts %opts-w-format
  860. #:to 'cps)))))
  861. (pass-if "non-literal format string using ngettext as N_"
  862. (null? (call-with-warnings
  863. (lambda ()
  864. (compile '(format #t (N_ "~a thing" "~a things" n) n)
  865. #:opts %opts-w-format
  866. #:to 'cps)))))
  867. (pass-if "non-literal format string with (define _ gettext)"
  868. (null? (call-with-warnings
  869. (lambda ()
  870. (compile '(begin
  871. (define _ gettext)
  872. (define (foo)
  873. (format #t (G_ "~A ~A!") "hello" "world")))
  874. #:opts %opts-w-format
  875. #:to 'cps)))))
  876. (pass-if "wrong format string"
  877. (let ((w (call-with-warnings
  878. (lambda ()
  879. (compile '(format #f 'not-a-string)
  880. #:opts %opts-w-format
  881. #:to 'cps)))))
  882. (and (= (length w) 1)
  883. (number? (string-contains (car w)
  884. "wrong format string")))))
  885. (pass-if "wrong number of args"
  886. (let ((w (call-with-warnings
  887. (lambda ()
  888. (compile '(format "shbweeb")
  889. #:opts %opts-w-format
  890. #:to 'cps)))))
  891. (and (= (length w) 1)
  892. (number? (string-contains (car w)
  893. "wrong number of arguments")))))
  894. (pass-if "~%, ~~, ~&, ~t, ~_, ~!, ~|, ~/, ~q and ~\\n"
  895. (null? (call-with-warnings
  896. (lambda ()
  897. (compile '((@ (ice-9 format) format) some-port
  898. "~&~3_~~ ~\n~12they~% ~!~|~/~q")
  899. #:opts %opts-w-format
  900. #:to 'cps)))))
  901. (pass-if "one missing argument"
  902. (let ((w (call-with-warnings
  903. (lambda ()
  904. (compile '(format some-port "foo ~A~%")
  905. #:opts %opts-w-format
  906. #:to 'cps)))))
  907. (and (= (length w) 1)
  908. (number? (string-contains (car w)
  909. "expected 1, got 0")))))
  910. (pass-if "one missing argument, gettext"
  911. (let ((w (call-with-warnings
  912. (lambda ()
  913. (compile '(format some-port (gettext "foo ~A~%"))
  914. #:opts %opts-w-format
  915. #:to 'cps)))))
  916. (and (= (length w) 1)
  917. (number? (string-contains (car w)
  918. "expected 1, got 0")))))
  919. (pass-if "two missing arguments"
  920. (let ((w (call-with-warnings
  921. (lambda ()
  922. (compile '((@ (ice-9 format) format) #f
  923. "foo ~10,2f and bar ~S~%")
  924. #:opts %opts-w-format
  925. #:to 'cps)))))
  926. (and (= (length w) 1)
  927. (number? (string-contains (car w)
  928. "expected 2, got 0")))))
  929. (pass-if "one given, one missing argument"
  930. (let ((w (call-with-warnings
  931. (lambda ()
  932. (compile '(format #t "foo ~A and ~S~%" hey)
  933. #:opts %opts-w-format
  934. #:to 'cps)))))
  935. (and (= (length w) 1)
  936. (number? (string-contains (car w)
  937. "expected 2, got 1")))))
  938. (pass-if "too many arguments"
  939. (let ((w (call-with-warnings
  940. (lambda ()
  941. (compile '(format #t "foo ~A~%" 1 2)
  942. #:opts %opts-w-format
  943. #:to 'cps)))))
  944. (and (= (length w) 1)
  945. (number? (string-contains (car w)
  946. "expected 1, got 2")))))
  947. (pass-if "~h"
  948. (null? (call-with-warnings
  949. (lambda ()
  950. (compile '((@ (ice-9 format) format) #t
  951. "foo ~h ~a~%" 123.4 'bar)
  952. #:opts %opts-w-format
  953. #:to 'cps)))))
  954. (pass-if "~:h with locale object"
  955. (null? (call-with-warnings
  956. (lambda ()
  957. (compile '((@ (ice-9 format) format) #t
  958. "foo ~:h~%" 123.4 %global-locale)
  959. #:opts %opts-w-format
  960. #:to 'cps)))))
  961. (pass-if "~:h without locale object"
  962. (let ((w (call-with-warnings
  963. (lambda ()
  964. (compile '((@ (ice-9 format) format) #t "foo ~,2:h" 123.4)
  965. #:opts %opts-w-format
  966. #:to 'cps)))))
  967. (and (= (length w) 1)
  968. (number? (string-contains (car w)
  969. "expected 2, got 1")))))
  970. (with-test-prefix "conditionals"
  971. (pass-if "literals"
  972. (null? (call-with-warnings
  973. (lambda ()
  974. (compile '((@ (ice-9 format) format) #f "~A ~[foo~;bar~;baz~;~] ~10,2f"
  975. 'a 1 3.14)
  976. #:opts %opts-w-format
  977. #:to 'cps)))))
  978. (pass-if "literals with selector"
  979. (let ((w (call-with-warnings
  980. (lambda ()
  981. (compile '((@ (ice-9 format) format) #f "~2[foo~;bar~;baz~;~] ~A"
  982. 1 'dont-ignore-me)
  983. #:opts %opts-w-format
  984. #:to 'cps)))))
  985. (and (= (length w) 1)
  986. (number? (string-contains (car w)
  987. "expected 1, got 2")))))
  988. (pass-if "escapes (exact count)"
  989. (let ((w (call-with-warnings
  990. (lambda ()
  991. (compile '((@ (ice-9 format) format) #f "~[~a~;~a~]")
  992. #:opts %opts-w-format
  993. #:to 'cps)))))
  994. (and (= (length w) 1)
  995. (number? (string-contains (car w)
  996. "expected 2, got 0")))))
  997. (pass-if "escapes with selector"
  998. (let ((w (call-with-warnings
  999. (lambda ()
  1000. (compile '((@ (ice-9 format) format) #f "~1[chbouib~;~a~]")
  1001. #:opts %opts-w-format
  1002. #:to 'cps)))))
  1003. (and (= (length w) 1)
  1004. (number? (string-contains (car w)
  1005. "expected 1, got 0")))))
  1006. (pass-if "escapes, range"
  1007. (let ((w (call-with-warnings
  1008. (lambda ()
  1009. (compile '((@ (ice-9 format) format) #f "~[chbouib~;~a~;~2*~a~]")
  1010. #:opts %opts-w-format
  1011. #:to 'cps)))))
  1012. (and (= (length w) 1)
  1013. (number? (string-contains (car w)
  1014. "expected 1 to 4, got 0")))))
  1015. (pass-if "@"
  1016. (let ((w (call-with-warnings
  1017. (lambda ()
  1018. (compile '((@ (ice-9 format) format) #f "~@[temperature=~d~]")
  1019. #:opts %opts-w-format
  1020. #:to 'cps)))))
  1021. (and (= (length w) 1)
  1022. (number? (string-contains (car w)
  1023. "expected 1, got 0")))))
  1024. (pass-if "nested"
  1025. (let ((w (call-with-warnings
  1026. (lambda ()
  1027. (compile '((@ (ice-9 format) format) #f "~:[~[hey~;~a~;~va~]~;~3*~]")
  1028. #:opts %opts-w-format
  1029. #:to 'cps)))))
  1030. (and (= (length w) 1)
  1031. (number? (string-contains (car w)
  1032. "expected 2 to 4, got 0")))))
  1033. (pass-if "unterminated"
  1034. (let ((w (call-with-warnings
  1035. (lambda ()
  1036. (compile '((@ (ice-9 format) format) #f "~[unterminated")
  1037. #:opts %opts-w-format
  1038. #:to 'cps)))))
  1039. (and (= (length w) 1)
  1040. (number? (string-contains (car w)
  1041. "unterminated conditional")))))
  1042. (pass-if "unexpected ~;"
  1043. (let ((w (call-with-warnings
  1044. (lambda ()
  1045. (compile '((@ (ice-9 format) format) #f "foo~;bar")
  1046. #:opts %opts-w-format
  1047. #:to 'cps)))))
  1048. (and (= (length w) 1)
  1049. (number? (string-contains (car w)
  1050. "unexpected")))))
  1051. (pass-if "unexpected ~]"
  1052. (let ((w (call-with-warnings
  1053. (lambda ()
  1054. (compile '((@ (ice-9 format) format) #f "foo~]")
  1055. #:opts %opts-w-format
  1056. #:to 'cps)))))
  1057. (and (= (length w) 1)
  1058. (number? (string-contains (car w)
  1059. "unexpected"))))))
  1060. (pass-if "~{...~}"
  1061. (null? (call-with-warnings
  1062. (lambda ()
  1063. (compile '((@ (ice-9 format) format) #f "~A ~{~S~} ~A"
  1064. 'hello '("ladies" "and")
  1065. 'gentlemen)
  1066. #:opts %opts-w-format
  1067. #:to 'cps)))))
  1068. (pass-if "~{...~}, too many args"
  1069. (let ((w (call-with-warnings
  1070. (lambda ()
  1071. (compile '((@ (ice-9 format) format) #f "~{~S~}" 1 2 3)
  1072. #:opts %opts-w-format
  1073. #:to 'cps)))))
  1074. (and (= (length w) 1)
  1075. (number? (string-contains (car w)
  1076. "expected 1, got 3")))))
  1077. (pass-if "~@{...~}"
  1078. (null? (call-with-warnings
  1079. (lambda ()
  1080. (compile '((@ (ice-9 format) format) #f "~@{~S~}" 1 2 3)
  1081. #:opts %opts-w-format
  1082. #:to 'cps)))))
  1083. (pass-if "~@{...~}, too few args"
  1084. (let ((w (call-with-warnings
  1085. (lambda ()
  1086. (compile '((@ (ice-9 format) format) #f "~A ~@{~S~}")
  1087. #:opts %opts-w-format
  1088. #:to 'cps)))))
  1089. (and (= (length w) 1)
  1090. (number? (string-contains (car w)
  1091. "expected at least 1, got 0")))))
  1092. (pass-if "unterminated ~{...~}"
  1093. (let ((w (call-with-warnings
  1094. (lambda ()
  1095. (compile '((@ (ice-9 format) format) #f "~{")
  1096. #:opts %opts-w-format
  1097. #:to 'cps)))))
  1098. (and (= (length w) 1)
  1099. (number? (string-contains (car w)
  1100. "unterminated")))))
  1101. (pass-if "~(...~)"
  1102. (null? (call-with-warnings
  1103. (lambda ()
  1104. (compile '((@ (ice-9 format) format) #f "~:@(~A ~A~)" 'foo 'bar)
  1105. #:opts %opts-w-format
  1106. #:to 'cps)))))
  1107. (pass-if "~v"
  1108. (let ((w (call-with-warnings
  1109. (lambda ()
  1110. (compile '((@ (ice-9 format) format) #f "~v_foo")
  1111. #:opts %opts-w-format
  1112. #:to 'cps)))))
  1113. (and (= (length w) 1)
  1114. (number? (string-contains (car w)
  1115. "expected 1, got 0")))))
  1116. (pass-if "~v:@y"
  1117. (null? (call-with-warnings
  1118. (lambda ()
  1119. (compile '((@ (ice-9 format) format) #f "~v:@y" 1 123)
  1120. #:opts %opts-w-format
  1121. #:to 'cps)))))
  1122. (pass-if "~*"
  1123. (let ((w (call-with-warnings
  1124. (lambda ()
  1125. (compile '((@ (ice-9 format) format) #f "~2*~a" 'a 'b)
  1126. #:opts %opts-w-format
  1127. #:to 'cps)))))
  1128. (and (= (length w) 1)
  1129. (number? (string-contains (car w)
  1130. "expected 3, got 2")))))
  1131. (pass-if "~p"
  1132. (null? (call-with-warnings
  1133. (lambda ()
  1134. (compile '(((@ (ice-9 format) format) #f "thing~p" 2))
  1135. #:opts %opts-w-format
  1136. #:to 'cps)))))
  1137. (pass-if "~p, too few arguments"
  1138. (let ((w (call-with-warnings
  1139. (lambda ()
  1140. (compile '((@ (ice-9 format) format) #f "~p")
  1141. #:opts %opts-w-format
  1142. #:to 'cps)))))
  1143. (and (= (length w) 1)
  1144. (number? (string-contains (car w)
  1145. "expected 1, got 0")))))
  1146. (pass-if "~:p"
  1147. (null? (call-with-warnings
  1148. (lambda ()
  1149. (compile '(((@ (ice-9 format) format) #f "~d thing~:p" 2))
  1150. #:opts %opts-w-format
  1151. #:to 'cps)))))
  1152. (pass-if "~:@p, too many arguments"
  1153. (let ((w (call-with-warnings
  1154. (lambda ()
  1155. (compile '((@ (ice-9 format) format) #f "~d pupp~:@p" 5 5)
  1156. #:opts %opts-w-format
  1157. #:to 'cps)))))
  1158. (and (= (length w) 1)
  1159. (number? (string-contains (car w)
  1160. "expected 1, got 2")))))
  1161. (pass-if "~:@p, too few arguments"
  1162. (let ((w (call-with-warnings
  1163. (lambda ()
  1164. (compile '((@ (ice-9 format) format) #f "pupp~:@p")
  1165. #:opts %opts-w-format
  1166. #:to 'cps)))))
  1167. (and (= (length w) 1)
  1168. (number? (string-contains (car w)
  1169. "expected 1, got 0")))))
  1170. (pass-if "~?"
  1171. (null? (call-with-warnings
  1172. (lambda ()
  1173. (compile '((@ (ice-9 format) format) #f "~?" "~d ~d" '(1 2))
  1174. #:opts %opts-w-format
  1175. #:to 'cps)))))
  1176. (pass-if "~^"
  1177. (null? (call-with-warnings
  1178. (lambda ()
  1179. (compile '((@ (ice-9 format) format) #f "~a ~^ ~a" 0 1)
  1180. #:opts %opts-w-format
  1181. #:to 'cps)))))
  1182. (pass-if "~^, too few args"
  1183. (let ((w (call-with-warnings
  1184. (lambda ()
  1185. (compile '((@ (ice-9 format) format) #f "~a ~^ ~a")
  1186. #:opts %opts-w-format
  1187. #:to 'cps)))))
  1188. (and (= (length w) 1)
  1189. (number? (string-contains (car w)
  1190. "expected at least 1, got 0")))))
  1191. (pass-if "parameters: +,-,#, and '"
  1192. (null? (call-with-warnings
  1193. (lambda ()
  1194. (compile '((@ (ice-9 format) format) some-port
  1195. "~#~ ~,,-2f ~,,+2f ~'A~" 1234 1234)
  1196. #:opts %opts-w-format
  1197. #:to 'cps)))))
  1198. (pass-if "complex 1"
  1199. (let ((w (call-with-warnings
  1200. (lambda ()
  1201. (compile '((@ (ice-9 format) format) #f
  1202. "~4@S ~32S~@[;; ~1{~@?~}~]~@[~61t at ~a~]\n"
  1203. 1 2 3 4 5 6)
  1204. #:opts %opts-w-format
  1205. #:to 'cps)))))
  1206. (and (= (length w) 1)
  1207. (number? (string-contains (car w)
  1208. "expected 4, got 6")))))
  1209. (pass-if "complex 2"
  1210. (let ((w (call-with-warnings
  1211. (lambda ()
  1212. (compile '((@ (ice-9 format) format) #f
  1213. "~:(~A~) Commands~:[~; [abbrev]~]:~2%"
  1214. 1 2 3 4)
  1215. #:opts %opts-w-format
  1216. #:to 'cps)))))
  1217. (and (= (length w) 1)
  1218. (number? (string-contains (car w)
  1219. "expected 2, got 4")))))
  1220. (pass-if "complex 3"
  1221. (let ((w (call-with-warnings
  1222. (lambda ()
  1223. (compile '((@ (ice-9 format) format) #f "~9@a~:[~*~3_~;~3d~] ~v:@y~%")
  1224. #:opts %opts-w-format
  1225. #:to 'cps)))))
  1226. (and (= (length w) 1)
  1227. (number? (string-contains (car w)
  1228. "expected 5, got 0")))))
  1229. (pass-if "ice-9 format"
  1230. (let ((w (call-with-warnings
  1231. (lambda ()
  1232. (let ((in (open-input-string
  1233. "(use-modules ((ice-9 format) #:prefix i9-))
  1234. (i9-format #t \"yo! ~A\" 1 2)")))
  1235. (read-and-compile in
  1236. #:opts %opts-w-format
  1237. #:to 'cps))))))
  1238. (and (= (length w) 1)
  1239. (number? (string-contains (car w)
  1240. "expected 1, got 2")))))
  1241. (pass-if "not format"
  1242. (null? (call-with-warnings
  1243. (lambda ()
  1244. (compile '(let ((format chbouib))
  1245. (format #t "not ~A a format string"))
  1246. #:opts %opts-w-format
  1247. #:to 'cps)))))
  1248. (with-test-prefix "simple-format"
  1249. (pass-if "good"
  1250. (null? (call-with-warnings
  1251. (lambda ()
  1252. (compile '(simple-format #t "foo ~a bar ~s ~%~~" 1 2)
  1253. #:opts %opts-w-format
  1254. #:to 'cps)))))
  1255. (pass-if "wrong number of args"
  1256. (let ((w (call-with-warnings
  1257. (lambda ()
  1258. (compile '(simple-format #t "foo ~a ~s~%" 'one-missing)
  1259. #:opts %opts-w-format
  1260. #:to 'cps)))))
  1261. (and (= (length w) 1)
  1262. (number? (string-contains (car w) "wrong number")))))
  1263. (pass-if "unsupported"
  1264. (let ((w (call-with-warnings
  1265. (lambda ()
  1266. (compile '(simple-format #t "foo ~x~%" 16)
  1267. #:opts %opts-w-format
  1268. #:to 'cps)))))
  1269. (and (= (length w) 1)
  1270. (number? (string-contains (car w) "unsupported format option")))))
  1271. (pass-if "unsupported, gettext"
  1272. (let ((w (call-with-warnings
  1273. (lambda ()
  1274. (compile '(simple-format #t (gettext "foo ~2f~%") 3.14)
  1275. #:opts %opts-w-format
  1276. #:to 'cps)))))
  1277. (and (= (length w) 1)
  1278. (number? (string-contains (car w) "unsupported format option")))))
  1279. (pass-if "unsupported, ngettext"
  1280. (let ((w (call-with-warnings
  1281. (lambda ()
  1282. (compile '(simple-format #t (ngettext "s ~x" "p ~x" x) x)
  1283. #:opts %opts-w-format
  1284. #:to 'cps)))))
  1285. (and (= (length w) 1)
  1286. (number? (string-contains (car w) "unsupported format option")))))))
  1287. (with-test-prefix "duplicate-case-datum"
  1288. (pass-if "quiet"
  1289. (null? (call-with-warnings
  1290. (lambda ()
  1291. (compile '(case x ((1) 'one) ((2) 'two))
  1292. #:opts %opts-w-duplicate-case-datum
  1293. #:to 'cps)))))
  1294. (pass-if "one duplicate"
  1295. (let ((w (call-with-warnings
  1296. (lambda ()
  1297. (compile '(case x
  1298. ((1) 'one)
  1299. ((2) 'two)
  1300. ((1) 'one-again))
  1301. #:opts %opts-w-duplicate-case-datum
  1302. #:to 'cps)))))
  1303. (and (= (length w) 1)
  1304. (number? (string-contains (car w) "duplicate")))))
  1305. (pass-if "one duplicate"
  1306. (let ((w (call-with-warnings
  1307. (lambda ()
  1308. (compile '(case x
  1309. ((1 2 3) 'a)
  1310. ((1) 'one))
  1311. #:opts %opts-w-duplicate-case-datum
  1312. #:to 'cps)))))
  1313. (and (= (length w) 1)
  1314. (number? (string-contains (car w) "duplicate"))))))
  1315. (with-test-prefix "bad-case-datum"
  1316. (pass-if "quiet"
  1317. (null? (call-with-warnings
  1318. (lambda ()
  1319. (compile '(case x ((1) 'one) ((2) 'two))
  1320. #:opts %opts-w-bad-case-datum
  1321. #:to 'cps)))))
  1322. (pass-if "not eqv?"
  1323. (let ((w (call-with-warnings
  1324. (lambda ()
  1325. (compile '(case x
  1326. ((1) 'one)
  1327. (("bad") 'bad))
  1328. #:opts %opts-w-bad-case-datum
  1329. #:to 'cps)))))
  1330. (and (= (length w) 1)
  1331. (number? (string-contains (car w)
  1332. "cannot be meaningfully compared")))))
  1333. (pass-if "one clause element not eqv?"
  1334. (let ((w (call-with-warnings
  1335. (lambda ()
  1336. (compile '(case x
  1337. ((1 (2) 3) 'a))
  1338. #:opts %opts-w-duplicate-case-datum
  1339. #:to 'cps)))))
  1340. (and (= (length w) 1)
  1341. (number? (string-contains (car w)
  1342. "cannot be meaningfully compared")))))))
  1343. ;; Local Variables:
  1344. ;; eval: (put 'pass-if-primitives-resolved 'scheme-indent-function 1)
  1345. ;; eval: (put 'pass-if-tree-il->scheme 'scheme-indent-function 1)
  1346. ;; End: