Subject: bug#75504: Crash of type-fold when compiling procedure

From: "Ricardo G. Herdt" <[email-masked]>
Date: Sat, 11 Jan 2025 23:43:11 +0000

Hi,

There is a weird bug when trying to evaluate/compile following 
procedure:

------------------------
$ cat example.scm
(define (test n)
     (make-vector n 1)
     (floor (sqrt n)))

$ guild compile example.scm
Backtrace:
In /home/rgherdt/.guix-profile/bin/guild:
     72:17 19 (main _)
In srfi/srfi-1.scm:
     634:9 18 (for-each #<procedure 7fc101fbc230 at scripts/compile.…> …)
In scripts/compile.scm:
    279:27 17 (_ _)
In system/base/target.scm:
      72:6 16 (with-target _ _)
In system/base/compile.scm:
     187:6 15 (compile-file "example.scm" #:output-file _ #:from _ # _ …)
      53:4 14 (call-with-output-file/atomic _ _ _)
In ice-9/boot-9.scm:
   1752:10 13 (with-exception-handler _ _ #:unwind? _ # _)
In system/base/compile.scm:
     69:11 12 (_)
    190:11 11 (_ #<closed: file 7fc101f95e00>)
    314:15 10 (read-and-compile _ #:from _ #:to _ #:env _ # _ # _ # _)
    352:28  9 (compile _ #:from _ #:to _ #:env _ #:optimization-level …)
    265:44  8 (_ _ _)
    261:33  7 (_ #<intmap 0-41> #<module (#{ g106}#) 7fc1020f2460>)
In language/cps/optimize.scm:
    130:12  6 (_ _ _)
     102:3  5 (optimize-higher-order-cps _ _)
In language/cps/type-fold.scm:
     778:7  4 (type-fold _)
    771:27  3 (fold-functions-in-renumbered-program #<procedure loca…> …)
    611:10  2 (local-type-fold _ 30 _)
      48:4  1 (materialize-constant _ _ _ _ _)
In ice-9/boot-9.scm:
   1685:16  0 (raise-exception _ #:continuable? _)

ice-9/boot-9.scm:1685:16: In procedure raise-exception:
In procedure =: Wrong type argument in position 1: #<unspecified>
------------------------

Somehow the type passed to materialize-constant is #<unspecified>, 
leading to the crash when checking "(zero? type)". Swapping the 
procedures like this doesn't lead to the same error:

(define (test n)
    (make-vector n 1)
    (sqrt (floor n)))

Thanks to gsdlb for discovering the bug and mwette for helping getting a 
better backtrace.
Tested with Guile 3.0.9.

Regards,

Ricardo G. Herdt