Subject: 31.0.50; comp.el comp--type-check-optim pass causes issues

From: Iurie Marian <[email-masked]>
Date: Sun, 15 Sep 2024 10:27:48 +0200

Dear Emacs maintainers,

I encountered an issue with latest Emacs' master branch. I am using a
package `pcre2el'
and I noticed a wrong behavior after updating Emacs. I tracked the change
which causes the
issue, and it seems it's the below commit:
#+begin_comment
a1775552cef5a8bc0ba13e802ecf343423a53364
Author:     Andrea Corallo <[email-masked]>
AuthorDate: Tue May 23 11:18:07 2023 +0200
Commit:     Andrea Corallo <[email-masked]>
CommitDate: Thu Jul 11 16:26:49 2024 +0200
#+end_comment
probably the newly introduced `comp--type-check-optim' is causing the issue.

Issue description:
* `pcre2el''s rxt-adt->strings execution expectation
#+begin_comment
ELISP> (rxt-adt->strings (rxt-parse-pcre (rxt--add-flags "t([es]{2})" nil)))
("tss" "tse" "tes" "tee")
#+end_comment

* The error
#+begin_comment
ELISP> (rxt-adt->strings (rxt-parse-pcre (rxt--add-flags "t([es]{4})" nil)))
*** Eval error ***  Wrong type argument: listp, #s(rxt-repeat 4 4
#s(rxt-char-set-union (115 101) nil nil nil) t)
#+end_comment

That's how the above function [[
https://github.com/joddie/pcre2el/blob/b4d846d80dddb313042131cf2b8fbf647567e000/pcre2el.el#L2984C1-L3004C46][rxt-adt->strings]]
looks like:

#+begin_src emacs-lisp
(defun rxt-adt->strings (re)
  (cl-typecase re
    (rxt-primitive
     (list ""))
    (rxt-string
     (list (rxt-string-chars re)))
    (rxt-seq
     (rxt-seq-elts->strings (rxt-seq-elts re)))
    (rxt-choice
     (rxt-choice-elts->strings (rxt-choice-elts re)))
    (rxt-submatch
     (rxt-adt->strings (rxt-submatch-body re)))
    (rxt-submatch-numbered
     (rxt-adt->strings (rxt-submatch-numbered-body re)))
    (rxt-repeat
     (rxt-repeat->strings re))
    (rxt-char-set-union
     (rxt-char-set->strings re))
    (t
     (error "Can't generate productions of %s"
            (rxt-syntax-tree-readable re)))))
#+end_src

The issue comes from
#+begin_comment
  (cl-typecase re
#+end_comment

instead of matching below:
#+begin_comment
    (rxt-submatch
     (rxt-adt->strings (rxt-submatch-body re)))
#+end_comment

it wrongly matches:
#+begin_comment
    (rxt-seq
     (rxt-seq-elts->strings (rxt-seq-elts re)))
#+end_comment

and throws the error
#+begin_comment
ELISP> (rxt-adt->strings (rxt-parse-pcre (rxt--add-flags "t([es]{4})" nil)))
*** Eval error ***  Wrong type argument: listp, #s(rxt-repeat 4 4
#s(rxt-char-set-union (115 101) nil nil nil) t)
#+end_comment

To quickly check this I've added some traces:
#+begin_src emacs-lisp
(defun rxt-adt->strings (re)
  (message "type-of re: %S, re = %S" (type-of re) re)
  (cl-typecase re
    (rxt-primitive
     (progn (message "match: rxt-primitive")
            (list "")))
    (rxt-string
     (progn (message "match: rxt-string")
            (list (rxt-string-chars re))))
    (rxt-seq
     (progn (message "match: rxt-seq")
            (rxt-seq-elts->strings (rxt-seq-elts re))))
    (rxt-choice
     (progn (message "match: rxt-choice")
            (rxt-choice-elts->strings (rxt-choice-elts re))))
    (rxt-submatch
     (progn (message "match: rxt-submatch")
            (rxt-adt->strings (rxt-submatch-body re))))
    (rxt-submatch-numbered
     (progn (message "match: rxt-submatch-numbered")
            (rxt-adt->strings (rxt-submatch-numbered-body re))))
    (rxt-repeat
     (progn (message "match: rxt-repeat")
            (rxt-repeat->strings re)))
    (rxt-char-set-union
     (progn (message "match: rxt-char-set-union")
            (rxt-char-set->strings re)))
    (t
     (error "Can't generate productions of %s"
            (rxt-syntax-tree-readable re)))))
#+end_src

which produces below messages:

#+begin_comment
type-of re: rxt-seq, re = #s(rxt-seq (#s(rxt-string "t" nil)
#s(rxt-submatch #s(rxt-repeat 4 4 #s(rxt-char-set-union (115 101) nil nil
nil) t))))
match: rxt-seq

type-of re: rxt-string, re = #s(rxt-string "t" nil)
match: rxt-string

type-of re: rxt-submatch, re = #s(rxt-submatch #s(rxt-repeat 4 4
#s(rxt-char-set-union (115 101) nil nil nil) t))
match: rxt-seq
#+end_comment

By commenting line (`comp--type-check-optim') in `comp-passes' (see below),
it fixes locally the issue, which makes me think that this is the culprit.

#+begin_src emacs-lisp

(defconst comp-passes '(comp--spill-lap
                        comp--limplify
                        comp--fwprop
                        comp--call-optim
                        comp--ipa-pure
                        comp--add-cstrs
                        comp--fwprop
                        ;; comp--type-check-optim
                        comp--tco
                        comp--fwprop
                        comp--remove-type-hints
                        comp--sanitizer
                        comp--compute-function-types
                        comp--final)
  "Passes to be executed in order.")

#+end_src

Kind Regards,
Iurie

In GNU Emacs 31.0.50 (build 6, x86_64-pc-linux-gnu, GTK+ Version
 3.24.41, cairo version 1.18.0) of 2024-09-13 built on rrouwprlc0222
Repository revision: 7376623a244a91d1de5245645b4b3e8c9469d422
Repository branch: master
System Description: Ubuntu 24.04.1 LTS

Configured using:
 'configure 'CFLAGS= -O3 -fallow-store-data-races
 -fno-semantic-interposition -flto -fuse-ld=gold' LD=/usr/bin/ld.gold
 --prefix=/tools/emacs/build --sysconfdir=/etc
 --libexecdir=/tools/emacs/build/usr/lib
 --localstatedir=/tools/emacs/build/var --with-modules
 --without-gconf --without-gsettings --enable-link-time-optimization
 --with-x-toolkit=yes --without-xaw3d --without-m17n-flt --with-cairo
 --with-xwidgets --without-compress-install
 --with-native-compilation=aot --with-mailutils --with-xft --with-rsvg
 --with-pgtk'

Configured features:
CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS HARFBUZZ JPEG LIBOTF LIBSELINUX
LIBXML2 MODULES NATIVE_COMP NOTIFY INOTIFY PDUMPER PGTK PNG RSVG SECCOMP
SOUND SQLITE3 THREADS TIFF TOOLKIT_SCROLL_BARS TREE_SITTER WEBP XIM
XWIDGETS GTK3 ZLIB

Important settings:
  value of $LC_ALL: C
  value of $LANG: en_US.UTF-8
  value of $XMODIFIERS: @im=ibus
  locale-coding-system: utf-8