Subject: bug#53938: 27.2; Defining function that modifies cl-struct before defining cl-struct

From: Stefan Monnier <[email-masked]>
Date: Sat, 12 Feb 2022 09:45:44 -0500

Lars Ingebrigtsen [2022-02-12 07:55:46] wrote:
> Ignacio Casso <[email-masked]> writes:
>> If you define a function that uses a cl struct before defining the cl
>> struct (e.g., because the struct is defined in a package to be autoloaded and the
>> function in your init file), there is an error when calling the function
>> later.
>>
>> Here is a simple elisp file to reproduce it, starting Emacs with "emacs
>> -Q":
>>
>> (require 'cl-macs)

Please require `cl-lib` rather than `cl-macs` here.

>> (defun change-name (struct new-name)
>>   (setf (my-struct-name struct) new-name))
>>
>> (cl-defstruct my-struct
>>   name)
>>
>> (setq my-struct (make-my-struct))
>>
>> (change-name my-struct "My struct")
>>
>> This produces the following error:
>>
>> Debugger entered--Lisp error: (void-function \(setf\ my-struct-name\))
>>   (\(setf\ my-struct-name\) new-name v)
>
> If you'd byte-compiled this, then the compiler would say
>
> In end of data:
> cl.el: Warning: the function ‘(setf my-struct-name)’ is not known to be
>     defined.
>
> So I think that Emacs is probably working as designed here.  But perhaps
> Stefan has some comments; added to the CCs.

Yes, it's working as designed (because the accessors are supposed to be
inlined).  OTOH, it should work like you want it if you pass
the :noinline option to `cl-defstruct`.

        Stefan