Subject: Re: bug#40562: [patch] Treat records as arrays in ert object comparisons and add support for cl-structs

From: Štěpán Němec <[email-masked]>
Date: Sun, 12 Apr 2020 01:26:41 +0200

On Sat, 11 Apr 2020 16:34:57 -0400
Clément Pit-Claudel wrote:

[...]

> (defvar xyz123 (make-xyz :a 1 :b 2 :c 3))
> (defvar xyz143 (make-xyz :a 1 :b 4 :c 3))

[...]

> The first attached patch fixes this for all records.

Seems like a nice addition.

> Test failed: ((should (equal xyz123 xyz143)) 
>   :form (equal #s(xyz 1 2 3) #s(xyz 1 4 3))
>   :value nil
>   :explanation (struct-field c (different-atoms (2 "#x2" "?") (4 "#x4" "?"))))
                               ^gotcha

>  lisp/emacs-lisp/ert.el | 7 +++++++
>  1 file changed, 7 insertions(+)
>
> diff --git a/lisp/emacs-lisp/ert.el b/lisp/emacs-lisp/ert.el
> index 74202183cc..e3666a84c0 100644
> --- a/lisp/emacs-lisp/ert.el
> +++ b/lisp/emacs-lisp/ert.el
> @@ -515,6 +515,13 @@ ert--explain-equal-rec
>                         `(cdr ,cdr-x)
>                       (cl-assert (equal a b) t)
>                       nil))))))))
> +      ((pred cl-struct-p)
> +       (cl-loop for slot in (cdr (cl-struct-slot-info (type-of a)))
> +                for ai across a
                   ^^^^^^^^^^^^^^^

This is incorrect, as witnessed by your very example (`c' instead of
`b'). Records are accessible with `aref', but the first slot is the type
descriptor, so you're making an off-by-one error here.

> +                for bi across b
> +                for xf = (ert--explain-equal-rec ai bi)
> +                do (when xf (cl-return `(struct-field ,(car slot) ,xf)))
> +                finally (cl-assert (equal a b) t)))
>        ((or (pred arrayp) (pred recordp))
>         ;; For mixed unibyte/multibyte string comparisons, make both multibyte.
>         (when (and (stringp a)

Thanks,

  Štěpán