1 USING: accessors arrays generic stack-checker
2 stack-checker.backend stack-checker.errors kernel classes
3 kernel.private math math.parser math.private namespaces
4 namespaces.private parser sequences strings vectors words
5 quotations effects tools.test continuations generic.standard
6 sorting assocs definitions prettyprint io inspector
7 classes.tuple classes.union classes.predicate debugger
8 threads.private io.streams.string io.timeouts io.thread
9 sequences.private destructors combinators eval locals.backend
11 IN: stack-checker.tests
15 { 0 2 } [ 2 "Hello" ] must-infer-as
16 { 1 2 } [ dup ] must-infer-as
18 { 1 2 } [ [ dup ] call ] must-infer-as
19 [ [ call ] infer ] must-fail
21 { 2 4 } [ 2dup ] must-infer-as
23 { 1 0 } [ [ ] [ ] if ] must-infer-as
24 [ [ if ] infer ] must-fail
25 [ [ [ ] if ] infer ] must-fail
26 [ [ [ 2 ] [ ] if ] infer ] must-fail
27 { 4 3 } [ [ rot ] [ -rot ] if ] must-infer-as
31 [ swap 3 ] [ nip 5 5 ] if
37 { 1 1 } [ dup [ ] when ] must-infer-as
38 { 1 1 } [ dup [ dup fixnum* ] when ] must-infer-as
39 { 2 1 } [ [ dup fixnum* ] when ] must-infer-as
41 { 1 0 } [ [ drop ] when* ] must-infer-as
42 { 1 1 } [ [ { { [ ] } } ] unless* ] must-infer-as
45 [ [ 2 2 fixnum+ ] dup [ ] when call ] must-infer-as
48 [ [ [ 2 2 fixnum+ ] ] [ [ 2 2 fixnum* ] ] if call ] infer
51 ! Test inference of termination of control flow
52 : termination-test-1 ( -- * ) "foo" throw ;
54 : termination-test-2 ( ? -- x ) [ termination-test-1 ] [ 3 ] if ;
56 { 1 1 } [ termination-test-2 ] must-infer-as
58 : simple-recursion-1 ( obj -- obj )
59 dup [ simple-recursion-1 ] [ ] if ;
61 { 1 1 } [ simple-recursion-1 ] must-infer-as
63 : simple-recursion-2 ( obj -- obj )
64 dup [ ] [ simple-recursion-2 ] if ;
66 { 1 1 } [ simple-recursion-2 ] must-infer-as
68 : bad-recursion-2 ( obj -- obj )
69 dup [ dup first swap second bad-recursion-2 ] [ ] if ;
71 [ [ bad-recursion-2 ] infer ] must-fail
73 : funny-recursion ( obj -- obj )
74 dup [ funny-recursion 1 ] [ 2 ] if drop ;
76 { 1 1 } [ funny-recursion ] must-infer-as
79 { 1 2 } [ [ first ] keep second ] must-infer-as
84 : fie ( element obj -- ? )
85 dup array? [ foe ] [ eq? ] if ;
87 : foe ( element tree -- ? )
102 { 2 1 } [ fie ] must-infer-as
103 { 2 1 } [ foe ] must-infer-as
112 { 0 0 } [ nested-when ] must-infer-as
114 : nested-when* ( obj -- )
121 { 1 0 } [ nested-when* ] must-infer-as
125 { 0 1 } [ sym-test ] must-infer-as
127 : terminator-branch ( a -- b )
134 { 1 1 } [ terminator-branch ] must-infer-as
136 : recursive-terminator ( obj -- )
143 { 1 0 } [ recursive-terminator ] must-infer-as
145 GENERIC: potential-hang ( obj -- obj )
146 M: fixnum potential-hang dup [ potential-hang ] when ;
148 [ ] [ [ 5 potential-hang ] infer drop ] unit-test
150 TUPLE: funny-cons car cdr ;
151 GENERIC: iterate ( obj -- )
152 M: funny-cons iterate cdr>> iterate ;
154 M: real iterate drop ;
156 { 1 0 } [ iterate ] must-infer-as
159 : cat ( obj -- * ) dup [ throw ] [ throw ] if ;
160 : dog ( a b c -- ) dup [ cat ] [ 3drop ] if ;
161 { 3 0 } [ dog ] must-infer-as
165 : friend ( a b c -- ) dup [ friend ] [ monkey ] if ;
166 : monkey ( a b c -- ) dup [ 3drop ] [ friend ] if ;
167 { 3 0 } [ friend ] must-infer-as
169 ! Regression -- same as above but we infer the second word first
171 : blah ( a b c -- ) dup [ blah ] [ blah2 ] if ;
172 : blah2 ( a b c -- ) dup [ blah ] [ 3drop ] if ;
173 { 3 0 } [ blah2 ] must-infer-as
178 dup [ blah3 ] [ dup [ blah4 ] [ blah3 ] if ] if ;
180 dup [ blah4 ] [ dup [ 3drop ] [ blah3 ] if ] if ;
181 { 3 0 } [ blah4 ] must-infer-as
184 : bad-combinator ( obj quot: ( -- ) -- )
188 [ swap slip ] keep swap bad-combinator
189 ] if ; inline recursive
191 [ [ [ 1 ] [ ] bad-combinator ] infer ] must-fail
195 dup string? [ 2array throw ] unless
196 over string? [ 2array throw ] unless
201 ! This order of branches works
203 : more-crap ( obj -- ) dup [ drop ] [ dup do-crap call ] if ;
204 : do-crap ( obj -- ) dup [ more-crap ] [ do-crap ] if ;
205 [ [ do-crap ] infer ] must-fail
209 : more-crap* ( obj -- ) dup [ drop ] [ dup do-crap* call ] if ;
210 : do-crap* ( obj -- ) dup [ do-crap* ] [ more-crap* ] if ;
211 [ [ do-crap* ] infer ] must-fail
214 : too-deep ( a b -- c )
215 dup [ drop ] [ 2dup too-deep too-deep * ] if ; inline recursive
216 { 2 1 } [ too-deep ] must-infer-as
218 ! Error reporting is wrong
219 MATH: xyz ( a b -- c )
220 M: fixnum xyz 2array ;
222 [ 3 ] bi@ swapd [ 2array swap ] dip 2array swap ;
224 [ [ xyz ] infer ] [ inference-error? ] must-fail-with
226 ! Doug Coleman discovered this one while working on the
256 { 1 0 } [ A ] must-infer-as
257 { 1 0 } [ B ] must-infer-as
258 { 1 0 } [ C ] must-infer-as
260 ! I found this bug by thinking hard about the previous one
262 : X ( a b -- c d ) dup [ swap Y ] [ ] if ;
263 : Y ( a b -- c d ) X ;
265 { 2 2 } [ X ] must-infer-as
266 { 2 2 } [ Y ] must-infer-as
268 ! This one comes from UI code
270 : #2 ( a b: ( -- ) -- ) dup [ call ] [ 2drop ] if ; inline
271 : #3 ( a -- ) [ #1 ] #2 ;
272 : #4 ( a -- ) dup [ drop ] [ dup #4 dup #3 call ] if ;
273 : #1 ( a -- ) dup [ dup #4 dup #3 ] [ ] if drop ;
275 [ \ #4 def>> infer ] must-fail
276 [ [ #1 ] infer ] must-fail
280 : foo ( a b -- c d ) dup [ 2drop f f bar ] [ ] if ;
281 : bar ( a b -- ) [ 2 2 + ] t foo drop call drop ;
283 [ [ foo ] infer ] must-fail
285 [ 1234 infer ] must-fail
288 [ [ [ dup call ] dup call ] infer ]
289 [ inference-error? ] must-fail-with
291 : m dup call ; inline
293 [ [ [ m ] m ] infer ] [ inference-error? ] must-fail-with
295 : m' dup curry call ; inline
297 [ [ [ m' ] m' ] infer ] [ inference-error? ] must-fail-with
299 : m'' [ dup curry ] ; inline
301 : m''' m'' call call ; inline
303 [ [ [ m''' ] m''' ] infer ] [ inference-error? ] must-fail-with
305 : m-if t over if ; inline
307 [ [ [ m-if ] m-if ] infer ] [ inference-error? ] must-fail-with
309 ! This doesn't hang but it's also an example of the
311 [ [ [ [ drop 3 ] swap call ] dup call ] infer ]
312 [ inference-error? ] must-fail-with
314 ! This form should not have a stack effect
316 : bad-recursion-1 ( a -- b )
317 dup [ drop bad-recursion-1 5 ] [ ] if ;
319 [ [ bad-recursion-1 ] infer ] must-fail
321 : bad-bin ( a b -- ) 5 [ 5 bad-bin bad-bin 5 ] [ 2drop ] if ;
322 [ [ bad-bin ] infer ] must-fail
324 [ [ 1 drop-locals ] infer ] [ inference-error? ] must-fail-with
327 [ [ cleave ] infer ] [ inference-error? ] must-fail-with
329 ! Test some curry stuff
330 { 1 1 } [ 3 [ ] curry 4 [ ] curry if ] must-infer-as
332 { 2 1 } [ [ ] curry 4 [ ] curry if ] must-infer-as
334 [ [ 3 [ ] curry 1 2 [ ] 2curry if ] infer ] must-fail
336 ! Test number protocol
354 ! Test object protocol
357 \ hashcode* must-infer
359 ! Test sequence protocol
362 \ set-length must-infer
365 \ new-resizable must-infer
367 \ lengthen must-infer
369 ! Test assoc protocol
372 \ new-assoc must-infer
373 \ delete-at must-infer
374 \ clear-assoc must-infer
375 \ assoc-size must-infer
376 \ assoc-like must-infer
377 \ assoc-clone-like must-infer
379 { 1 3 } [ [ 2drop f ] assoc-find ] must-infer-as
381 ! Test some random library words
382 \ 1quotation must-infer
383 \ string>number must-infer
393 \ natural-sort must-infer
396 \ define-class must-infer
397 \ define-tuple-class must-infer
398 \ define-union-class must-infer
399 \ define-predicate-class must-infer
400 \ instance? must-infer
401 \ next-method-quot must-infer
403 ! Test words with continuations
404 { 0 0 } [ [ drop ] callcc0 ] must-infer-as
405 { 0 1 } [ [ 4 swap continue-with ] callcc1 ] must-infer-as
406 { 2 1 } [ [ + ] [ ] [ ] cleanup ] must-infer-as
407 { 2 1 } [ [ + ] [ 3drop 0 ] recover ] must-infer-as
411 ! Test stream protocol
412 \ set-timeout must-infer
413 \ stream-read must-infer
414 \ stream-read1 must-infer
415 \ stream-readln must-infer
416 \ stream-read-until must-infer
417 \ stream-write must-infer
418 \ stream-write1 must-infer
419 \ stream-nl must-infer
420 \ stream-flush must-infer
422 ! Test stream utilities
424 \ contents must-infer
426 ! Test prettyprinting
431 \ describe must-infer
435 \ io-thread must-infer
437 ! Incorrect stack declarations on inline recursive words should
439 : fooxxx ( a b -- c ) over [ foo ] when ; inline
440 : barxxx ( a b -- c ) fooxxx ;
442 [ [ barxxx ] infer ] must-fail
445 { 1 0 } [ { [ ] } dispatch ] must-infer-as
447 DEFER: inline-recursive-2
448 : inline-recursive-1 ( -- ) inline-recursive-2 ;
449 : inline-recursive-2 ( -- ) inline-recursive-1 ;
451 { 0 0 } [ inline-recursive-1 ] must-infer-as
455 HOOK: my-hook my-var ( -- x )
457 M: integer my-hook "an integer" ;
458 M: string my-hook "a string" ;
460 { 0 1 } [ my-hook ] must-infer-as
464 { 1 1 } [ [ deferred-word ] [ 3 ] if ] must-infer-as
467 DEFER: an-inline-word
469 : normal-word-3 ( -- )
470 3 [ [ 2 + ] curry ] an-inline-word call drop ;
472 : normal-word-2 ( -- )
475 : normal-word ( x -- x )
476 dup [ normal-word-2 ] when ;
478 : an-inline-word ( obj quot -- )
479 [ normal-word ] dip call ; inline
481 { 1 1 } [ [ 3 * ] an-inline-word ] must-infer-as
483 { 0 1 } [ [ 2 ] [ 2 ] [ + ] compose compose call ] must-infer-as
485 ERROR: custom-error ;
487 [ T{ effect f 0 0 t } ] [
488 [ custom-error ] infer
491 : funny-throw throw ; inline
493 [ T{ effect f 0 0 t } ] [
494 [ 3 funny-throw ] infer
497 [ T{ effect f 0 0 t } ] [
498 [ custom-error inference-error ] infer
501 [ T{ effect f 1 2 t } ] [
502 [ dup [ 3 throw ] dip ] infer
505 ! This was a false trigger of the undecidable quotation
507 { 2 1 } [ find-last-sep ] must-infer-as
510 : missing->r-check 1 load-locals ;
512 [ [ missing->r-check ] infer ] must-fail
515 [ [ [ f dup ] [ dup ] [ ] produce ] infer ] must-fail
517 [ [ [ f dup ] [ ] while ] infer ] must-fail
519 : erg's-inference-bug ( -- ) f dup [ erg's-inference-bug ] when ; inline
521 [ [ erg's-inference-bug ] infer ] must-fail
523 : inference-invalidation-a ( -- ) ;
524 : inference-invalidation-b ( quot -- ) [ inference-invalidation-a ] dip call ; inline
525 : inference-invalidation-c ( a b -- c ) [ + ] inference-invalidation-b ; inline
527 [ 7 ] [ 4 3 inference-invalidation-c ] unit-test
529 { 2 1 } [ [ + ] inference-invalidation-b ] must-infer-as
531 [ ] [ "IN: stack-checker.tests : inference-invalidation-a ( -- a b ) 1 2 ;" eval ] unit-test
533 [ 3 ] [ inference-invalidation-c ] unit-test
535 { 0 1 } [ inference-invalidation-c ] must-infer-as
537 GENERIC: inference-invalidation-d ( obj -- )
539 M: object inference-invalidation-d inference-invalidation-c 2drop ;
541 \ inference-invalidation-d must-infer
543 [ ] [ "IN: stack-checker.tests : inference-invalidation-a ( -- ) ;" eval ] unit-test
545 [ [ inference-invalidation-d ] infer ] must-fail
547 : bad-recursion-3 ( -- ) dup [ [ bad-recursion-3 ] dip ] when ; inline
548 [ [ bad-recursion-3 ] infer ] must-fail
550 : bad-recursion-4 ( -- ) 4 [ dup call roll ] times ; inline
551 [ [ [ ] [ 1 2 3 ] over dup bad-recursion-4 ] infer ] must-fail
553 : bad-recursion-5 ( obj quot: ( -- ) -- ) dup call swap bad-recursion-5 ; inline recursive
554 [ [ f [ ] bad-recursion-5 ] infer ] must-fail
556 : bad-recursion-6 ( quot: ( -- ) -- )
557 dup bad-recursion-6 call ; inline recursive
558 [ [ [ drop f ] bad-recursion-6 ] infer ] must-fail
560 { 3 0 } [ [ 2drop "A" throw ] [ ] if 2drop ] must-infer-as
561 { 2 0 } [ drop f f [ 2drop "A" throw ] [ ] if 2drop ] must-infer-as
563 : unbalanced-retain-usage ( a b -- )
564 dup 10 < [ 2drop 5 1 + unbalanced-retain-usage ] [ 2drop ] if ;
567 [ [ unbalanced-retain-usage ] infer ] [ inference-error? ] must-fail-with
570 : ddd' ( ? -- ) [ f eee' ] when ; inline recursive
571 : eee' ( ? -- ) [ swap [ ] ] dip ddd' call ; inline recursive
573 [ [ eee' ] infer ] [ inference-error? ] must-fail-with
575 : bogus-error ( x -- )
576 dup "A" throw [ bogus-error ] [ drop ] if ; inline recursive
578 [ bogus-error ] must-infer
580 [ [ clear ] infer. ] [ inference-error? ] must-fail-with
582 : debugging-curry-folding ( quot -- )
583 [ debugging-curry-folding ] curry call ; inline recursive
585 [ [ ] debugging-curry-folding ] must-infer
587 [ [ exit ] [ 1 2 3 ] if ] must-infer