justinethier / cyclone

:cyclone: A brand-new compiler that allows practical application development using R7RS Scheme. We provide modern features and a stable system capable of generating fast native binaries.
http://justinethier.github.io/cyclone/
MIT License
824 stars 44 forks source link

C compiler warning when using match #455

Closed justinethier closed 3 years ago

justinethier commented 3 years ago

Consider the following example:

(import (scheme base)
        (scheme read)
        (cyclone match))

(match
  (read)
  (else
   'done))

This produces the following warning when compiled:

test.c: In function '__lambda_1':
test.c:438:3: warning: statement with no effect [-Wunused-value]
   &c_73993;__halt(__halt(obj_int2obj(0)));;

It seems the problem is that there is a continuation to handle the case when there is match, but when there is an else this continuation will never be called.

TBD if match can be enhanced to generate better code in this case.

Regardless, our compiler should not be generating C statements with no effect.

justinethier commented 3 years ago

Here is a simplified program leading to the same (or at least very similar) compilation warning:

((lambda (failure)
    ((lambda (else) 'done) 'v))
  (lambda () (error 'match "no matching pattern")))
justinethier commented 3 years ago

This program is equivalent to the original match example:

(((lambda (v)
    ((lambda (failure)
       ((lambda (else) 'done) v))
     (lambda () (error 'match "no matching pattern"))))
  (read)))

Seems ultimately this is not an issue with match but rather with the compiler itself.

justinethier commented 3 years ago

Updated the compiler to no longer emit code containing unused closures.