tree-il.test 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  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. (with-test-prefix "location"
  1344. (define (call-with-fake-input-file filename contents thunk)
  1345. (call-with-input-string contents
  1346. (lambda (port)
  1347. (set-port-filename! port filename)
  1348. (thunk port))))
  1349. (pass-if "unused variable"
  1350. (let ((w (call-with-warnings
  1351. (lambda ()
  1352. (call-with-fake-input-file
  1353. "unused-variable.scm"
  1354. "\
  1355. (lambda (x)
  1356. (let ((y (+ x 2)))
  1357. x))"
  1358. (lambda (port)
  1359. (read-and-compile port #:opts %opts-w-unused #:to 'cps)))))))
  1360. (and (= (length w) 1)
  1361. (number? (string-contains (car w) "unused variable `y'"))
  1362. (number? (string-contains (car w) "unused-variable.scm:2:2")))))
  1363. (pass-if "unbound variable (spaces)"
  1364. (let ((w (call-with-warnings
  1365. (lambda ()
  1366. (call-with-fake-input-file
  1367. "unbound-spaces.scm"
  1368. " (foo)"
  1369. (lambda (port)
  1370. (read-and-compile port #:opts %opts-w-unbound #:to 'cps)))))))
  1371. (and (= (length w) 1)
  1372. (number? (string-contains (car w) "unbound variable `foo'"))
  1373. (number? (string-contains (car w) "unbound-spaces.scm:1:3")))))
  1374. (pass-if "unbound variable (tabs)"
  1375. (let ((w (call-with-warnings
  1376. (lambda ()
  1377. (call-with-fake-input-file
  1378. "unbound-tabs.scm"
  1379. "\t\t(foo)"
  1380. (lambda (port)
  1381. (read-and-compile port #:opts %opts-w-unbound #:to 'cps)))))))
  1382. (and (= (length w) 1)
  1383. (number? (string-contains (car w) "unbound variable `foo'"))
  1384. (number? (string-contains (car w) "unbound-tabs.scm:1:17")))))))
  1385. ;; Local Variables:
  1386. ;; eval: (put 'pass-if-primitives-resolved 'scheme-indent-function 1)
  1387. ;; eval: (put 'pass-if-tree-il->scheme 'scheme-indent-function 1)
  1388. ;; End: