Subject: bug#76573: 30.1; native compilation fails for record inside function

From: Eli Zaretskii <[email-masked]>
Date: Wed, 30 Apr 2025 11:28:49 +0300

> From: Andrea Corallo <[email-masked]>
> Cc: Eli Zaretskii <[email-masked]>,  [email-masked],  Lin Jian
>  <[email-masked]>
> Date: Wed, 30 Apr 2025 04:02:18 -0400
> 
> AKIYAMA Kouhei <[email-masked]> writes:
> 
> > Hello,
> >
> > I am the author of el-easydraw, mentioned here as misohena.
> >
> > I'm confused by this issue (errors during native compilation) because
> > I simply used the Record type as described in the Emacs Lisp manual. I
> > only used the `record' function, but neither the manual nor the
> > docstring mentions that the symbol specified as the first TYPE
> > argument needs to be pre-defined. The manual does mention avoiding
> > type name conflicts, so I added a prefix.
> >
> > https://www.gnu.org/software/emacs/manual/html_node/elisp/Records.html
> >
> > However, after seeing Eli's next message, I tried using `make-record'
> > instead of the `record' function, and the error being discussed no
> > longer occurred.
> 
> Hi!
> 
> 'make-record' works only because the optimizer knows only about 'record'
> :)
> 
> I'm more and more convinced now that if 'record' and 'make-record' are
> documented to the user the code is valid and the compiler should compile
> it.
> 
> I'm testing this which demotes the error to a warning:
> 
> =========
> modified   lisp/emacs-lisp/comp-cstr.el
> @@ -35,6 +35,7 @@
> 
> ;;; Code:
> +(require 'warnings)
>  (require 'cl-lib)
>  (require 'cl-extra) ;HACK: For `cl-find-class' when `cl-loaddefs' is missing.
> 
> @@ -338,7 +339,11 @@ comp--intersection
> (defun comp-supertypes (type)
>    "Return the ordered list of supertypes of TYPE."
>    (or (assq type (comp-cstr-ctxt-typeof-types comp-ctxt))
> -      (error "Type %S missing from typeof-types!" type)))
> +      (progn
> +        (display-warning
> +         'native-compiler
> +         (format "Type %S missing from typeof-types!" type))
> +        '(t))))
> 
>  (defun comp--union-typesets (&rest typesets)
>    "Union types present into TYPESETS."
> 
> =========
> 
> But if really this is, even if seldomly used, valid code maybe we should
> not even signal a warning at this point?  Dunno, Eli WDYT?

Possibly.  Stefan, WDYT?

Also, Andrea, you suggested in a previous message that we should
document make-record as low-level facility, and suggest that new types
are created using cl-defstruct and defclass.  If you still think so, I
think we should indeed make these changes in the documentation.
Stefan, any comments on this aspect?