GNU bug report logs - #53938
27.2; Defining function that modifies cl-struct before defining cl-struct

Previous Next

Package: emacs;

Reported by: Ignacio Casso <ignaciocasso <at> hotmail.com>

Date: Fri, 11 Feb 2022 10:03:02 UTC

Severity: normal

Tags: moreinfo, notabug

Found in version 27.2

Done: Lars Ingebrigtsen <larsi <at> gnus.org>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 53938 in the body.
You can then email your comments to 53938 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#53938; Package emacs. (Fri, 11 Feb 2022 10:03:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Ignacio Casso <ignaciocasso <at> hotmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Fri, 11 Feb 2022 10:03:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Ignacio Casso <ignaciocasso <at> hotmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: 27.2; Defining function that modifies cl-struct before defining
 cl-struct
Date: Fri, 11 Feb 2022 10:45:50 +0100
Hello,

I've noticed a possible bug in Emacs.

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)

(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)
  (let* ((v struct)) (\(setf\ my-struct-name\) new-name v))
  change-name(#s(my-struct :name nil) "asdf")
  eval((change-name my-struct "asdf") nil)
  elisp--eval-last-sexp(nil)
  eval-last-sexp(nil)
  funcall-interactively(eval-last-sexp nil)
  call-interactively(eval-last-sexp nil nil)
  command-execute(eval-last-sexp)

A more realistic example, which produces the same error:

(defun my-package-config ()
  (setf (package-struct-slot package-var) 'new-value))

(eval-after-load 'my-package '(my-package-config))

(cl-defstruct package-struct slot)

(defvar package-var (make-package-struct))

(provide 'my-package)

It does not work either if we use instead a form inside eval-after-load
instead of defining a function, as in this example:

(eval-after-load 'my-package '(setf (package-struct-slot package-var) 'new-value))

However, it does work if we do something like this:

(setq my-package-config '(setf (package-struct-slot package-var) 'new-value))

(eval-after-load 'my-package '(eval my-package-config))


I realize that this might be a design constraint given that this common
lisp feature is "emulated", but maybe it should be better documented or
produce more descriptive errors? It's a really uncommon case though, I
only encountered it trying to configure in a weird way mu4e which uses cl
structs.

Regards,

Ignacio


In GNU Emacs 27.2 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.20)
 of 2022-01-16 built on ignacio-IdeaPad-3-15ADA05
Windowing system distributor 'The X.Org Foundation', version 11.0.12013000
System Description: Ubuntu 20.04.3 LTS




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#53938; Package emacs. (Sat, 12 Feb 2022 06:56:01 GMT) Full text and rfc822 format available.

Message #8 received at 53938 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Ignacio Casso <ignaciocasso <at> hotmail.com>
Cc: 53938 <at> debbugs.gnu.org, Stefan Monnier <monnier <at> iro.umontreal.ca>
Subject: Re: bug#53938: 27.2; Defining function that modifies cl-struct
 before defining cl-struct
Date: Sat, 12 Feb 2022 07:55:46 +0100
Ignacio Casso <ignaciocasso <at> hotmail.com> 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)
>
> (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.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Added tag(s) moreinfo. Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Sat, 12 Feb 2022 06:57:02 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#53938; Package emacs. (Sat, 12 Feb 2022 14:46:02 GMT) Full text and rfc822 format available.

Message #13 received at 53938 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Monnier <monnier <at> iro.umontreal.ca>
To: Lars Ingebrigtsen <larsi <at> gnus.org>
Cc: Ignacio Casso <ignaciocasso <at> hotmail.com>, 53938 <at> debbugs.gnu.org
Subject: Re: bug#53938: 27.2; Defining function that modifies cl-struct
 before defining cl-struct
Date: Sat, 12 Feb 2022 09:45:44 -0500
Lars Ingebrigtsen [2022-02-12 07:55:46] wrote:
> Ignacio Casso <ignaciocasso <at> hotmail.com> 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





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#53938; Package emacs. (Sun, 13 Feb 2022 08:23:01 GMT) Full text and rfc822 format available.

Message #16 received at 53938 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Stefan Monnier <monnier <at> iro.umontreal.ca>
Cc: Ignacio Casso <ignaciocasso <at> hotmail.com>, 53938 <at> debbugs.gnu.org
Subject: Re: bug#53938: 27.2; Defining function that modifies cl-struct
 before defining cl-struct
Date: Sun, 13 Feb 2022 09:22:25 +0100
Stefan Monnier <monnier <at> iro.umontreal.ca> writes:

> 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`.

Thanks; then I guess there's nothing to be fixed here, and I'm closing
this bug report.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Added tag(s) notabug. Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Sun, 13 Feb 2022 08:23:02 GMT) Full text and rfc822 format available.

bug closed, send any further explanations to 53938 <at> debbugs.gnu.org and Ignacio Casso <ignaciocasso <at> hotmail.com> Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Sun, 13 Feb 2022 08:23:02 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sun, 13 Mar 2022 11:24:05 GMT) Full text and rfc822 format available.

This bug report was last modified 3 years and 100 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.