Summary

Optimizer sometimes incorrectly drops procedure arguments

Metadata

Description

Found by megane while looking into a patch of mine to improve variable replacements:

 (define (foo bindings)                                                                            
   (define (append-map proc lst1)                                                                                                        
     (if lst1                                                                                                                            
         (proc 1)                                                                                                                        
         (proc 1 2)))                                                                                                                    
   (append-map (lambda (b a) (begin)) bindings))

Compile this with -O3 and it will error out with this message:

 
 After fixing this, we can remove the {{{(not captured)}}} test in the {{{'replaceable}}} code.

Changes and comments

[2019-07-05 09:08:25 UTC] sjamaan changed description

[2019-07-05 09:10:16 UTC] sjamaan changed summary

[2019-07-05 10:33:31 UTC] megane wrote:

Simpler still:

 (define (foo x)
   (let ((bar (lambda (a b) (print a b))))
     (if x
         (bar 1)
         (bar 1 2))))

[2019-07-05 10:39:11 UTC] felix wrote:

I don't see the problem here. Tha example code is clearly incorrect.

[2019-07-05 11:18:53 UTC] felix changed status from new to closed

[2019-07-05 11:18:53 UTC] felix set resolution to worksforme

[2019-07-05 12:51:28 UTC] megane removed resolution worksforme

[2019-07-05 12:51:28 UTC] megane wrote:

This fails too, but shouldn't. The true branch is never called.

 (define (foo x y)
   (define (append-map proc lst1 . lsts)
     (if (null? lsts)
         (proc 1)
         (proc 1 2)))
   (append-map (lambda (a b) (print a b)) x y))

[2019-07-05 12:51:28 UTC] megane changed status from closed to reopened

[2019-07-08 13:34:29 UTC] sjamaan wrote:

This code seems valid to me but also triggers the error when compiled with -O2:

 (define (foo x y)
   (define (append-map proc lst1 . lsts)
     (if (null? lsts)
         (proc 1)
         (proc 1 2)))
   (append-map (lambda (a b) (print a b)) x y)
   (append-map (lambda (a) (print a)) x))

[2019-07-09 19:02:16 UTC] felix changed difficulty from medium to hard

[2019-07-09 19:02:16 UTC] felix wrote:

Here is another test, submitted by megane. This triggers a compiler error, with the standard CHICKEN:

 (define (foo x y)
   (define (append-map proc . lsts)
     (if (null? lsts)
         (proc 1)
         (apply proc lsts)))
   (append-map (lambda (a) (print a)))
   (append-map (lambda (a b) (print a b)) x y))
 
 (foo 3 4)

[2019-07-10 19:17:52 UTC] sjamaan changed status from reopened to closed

[2019-07-10 19:17:52 UTC] sjamaan set resolution to fixed

[2019-07-10 19:17:52 UTC] sjamaan wrote:

Fixed by cf5d2aed000cfd292708d41b3774321bfec5eb67