Subject: bug#65051: internal_equal manipulates symbols with position without checking symbols-with-pos-enabled.

From: Alan Mackenzie <[email-masked]>
Date: Sat, 5 Aug 2023 10:45:37 +0000

Hello, Eli.

On Fri, Aug 04, 2023 at 21:01:30 +0300, Eli Zaretskii wrote:
> > Date: Fri, 4 Aug 2023 17:06:10 +0000
> > Cc: [email-masked]
> > From: Alan Mackenzie <[email-masked]>

> > On Fri, Aug 04, 2023 at 18:27:55 +0300, Eli Zaretskii wrote:
> > > > Date: Fri, 4 Aug 2023 14:59:58 +0000
> > > > Cc: [email-masked], [email-masked]
> > > > From: Alan Mackenzie <[email-masked]>

> > > > > What will happen to the comparison in internal_equal when
> > > > > symbols_with_pos_enabled is zero and the two objects have different
> > > > > positions, or one has a position, the other doesn't?

> > > > In these cases, equal will return nil.  This is correct.

> > > It is?  I thought when symbols with position are disabled, symbols
> > > that are 'eq', but have different positions, should compare equal?
> > > Why not?

> > With symbols-with-pos-enabled nil, #<symbol foo at 42> is not EQ to
> > #<symbol foo at 666>.  Neither are these two objects `equal'.  This is
> > because the special, time consuming processing which makes them EQ or
> > `equal' is enabled by that variable being bound to non-nil.

> But I thought that with symbols-with-pos-enabled OFF, we just ignore
> the positions?  Truth is, neither the ELisp manual nor the doc string
> tell us what happens when this variable is nil, they only tell what
> happens when it's non-nil.  So how about documenting that somewhere?

OK, please see the patch below.

> > That's the theory.  In practice, the handling in internal_equal forgot to
> > check for symbols-with-pos-enabled.  That's what I want to fix, now.

> I understand, but I question the correctness of your proposed fix.
> And for now, I'm utterly confused regarding the expected semantics of
> these comparisons when symbols-with-pos-enabled is nil.

> > > > In the other case, when two symbols with position have the same base
> > > > symbol and the same position, yet aren't identical, this will also return
> > > > nil, which is incorrect.

> > > How can they be not identical if the symbols and the positions are the
> > > same?  Or maybe I don't understand what you mean by "base symbol"?

> > By "base symbol" I mean 'foo in #<symbol foo at 42>.  By "identical" I
> > meant that the two Lisp_Objects would have the same hex value (i.e. be
> > EQ without symbols-with-pos-enabled), as contrasted to two distinct
> > Lisp_Objects with the same base symbol, and the same position, i.e.
> > should be `equal'.

> So we can have two different copies of #<symbol foo at 42>, such that
> their hex values are different?  Isn't that a bug? why don't we
> conflate such identical symbols?

No, it's not a bug, anymore than having two `equal' copies of '(a b c)
would be a bug.  It's vanishingly unlikely to happen in practice.  I
think it could only happen if a user creates a SWP with something like

    (setq bar (position-symbol 'foo 42))

, when there is already such a SWP created by read-positioning-symbols.
Or, perhaps, if reading one function produced a certain SWP which hangs
around in a variable such as cl--labels-convert-cache, and reading the
next function produces another SWP which happens to have the same bare
symbol and position.

Anyway, I've added code (in that patch below) to check two SWPs properly
in the event of symbols-with-pos-enabled being nil.

As already discussed, this is intended for master, not the release
branch.

diff --git a/doc/lispref/symbols.texi b/doc/lispref/symbols.texi
index 34db0caf3a8..a6ecfe896ad 100644
--- a/doc/lispref/symbols.texi
+++ b/doc/lispref/symbols.texi
@@ -784,9 +784,11 @@ Symbols with Position
 @cindex bare symbol
 A @dfn{symbol with position} is a symbol, the @dfn{bare symbol},
 together with an unsigned integer called the @dfn{position}.  These
-objects are intended for use by the byte compiler, which records in
+objects are for the use of the byte compiler, which records in
 them the position of each symbol occurrence and uses those positions
-in warning and error messages.
+in warning and error messages.  They shouldn't normally be used
+otherwise.  Doing so can cause unexpected results with basic Emacs
+functions such as @code{eq} and @code{equal}.
 
 The printed representation of a symbol with position uses the hash
 notation outlined in @ref{Printed Representation}.  It looks like
@@ -798,11 +800,20 @@ Symbols with Position
 
 For most purposes, when the flag variable
 @code{symbols-with-pos-enabled} is non-@code{nil}, symbols with
-positions behave just as bare symbols do.  For example, @samp{(eq
-#<symbol foo at 12345> foo)} has a value @code{t} when that variable
-is set (but @code{nil} when it isn't set).  Most of the time in Emacs this
-variable is @code{nil}, but the byte compiler binds it to @code{t}
-when it runs.
+positions behave just as their bare symbols do.  For example,
+@samp{(eq #<symbol foo at 12345> foo)} has a value @code{t} when the
+variable is set; likewise, @code{equal} will treat a symbol with
+position argument as its bare symbol.
+
+When @code{symbols-with-pos-enabled} is @code{nil}, any symbols with
+position continue to exist, but do not behave as symbols, or have the
+other useful properties outlined in the previous paragraph.  @code{eq}
+returns @code{t} when given identical arguments, and @code{equal}
+returns @code{t} when given arguments with @code{equal} components.
+
+Most of the time in Emacs @code{symbols-with-pos-enabled} is
+@code{nil}, but the byte compiler and the native compiler bind it to
+@code{t} when they run.
 
 Typically, symbols with position are created by the byte compiler
 calling the reader function @code{read-positioning-symbols}
@@ -820,7 +831,7 @@ Symbols with Position
 a symbol with position, ignoring the position.
 @end defvar
 
-@defun symbol-with-pos-p symbol.
+@defun symbol-with-pos-p symbol
 This function returns @code{t} if @var{symbol} is a symbol with
 position, @code{nil} otherwise.
 @end defun
diff --git a/src/fns.c b/src/fns.c
index bfd19e8c8f2..d47098c8791 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -2773,10 +2773,13 @@ internal_equal (Lisp_Object o1, Lisp_Object o2, enum equal_kind equal_kind,
 
   /* A symbol with position compares the contained symbol, and is
      `equal' to the corresponding ordinary symbol.  */
-  if (SYMBOL_WITH_POS_P (o1))
-    o1 = SYMBOL_WITH_POS_SYM (o1);
-  if (SYMBOL_WITH_POS_P (o2))
-    o2 = SYMBOL_WITH_POS_SYM (o2);
+  if (symbols_with_pos_enabled)
+    {
+      if (SYMBOL_WITH_POS_P (o1))
+	o1 = SYMBOL_WITH_POS_SYM (o1);
+      if (SYMBOL_WITH_POS_P (o2))
+	o2 = SYMBOL_WITH_POS_SYM (o2);
+    }
 
   if (BASE_EQ (o1, o2))
     return true;
@@ -2824,8 +2827,8 @@ internal_equal (Lisp_Object o1, Lisp_Object o2, enum equal_kind equal_kind,
 	if (ASIZE (o2) != size)
 	  return false;
 
-	/* Compare bignums, overlays, markers, and boolvectors
-	   specially, by comparing their values.  */
+	/* Compare bignums, overlays, markers, boolvectors, and
+	   symbols with position specially, by comparing their values.  */
 	if (BIGNUMP (o1))
 	  return mpz_cmp (*xbignum_val (o1), *xbignum_val (o2)) == 0;
 	if (OVERLAYP (o1))
@@ -2857,6 +2860,13 @@ internal_equal (Lisp_Object o1, Lisp_Object o2, enum equal_kind equal_kind,
 	if (TS_NODEP (o1))
 	  return treesit_node_eq (o1, o2);
 #endif
+	if (SYMBOL_WITH_POS_P(o1)) /* symbols_with_pos_enabled is false.  */
+	  return (internal_equal (XSYMBOL_WITH_POS (o1)->sym,
+				  XSYMBOL_WITH_POS (o2)->sym,
+				  equal_kind, depth + 1, ht)
+		  && internal_equal (XSYMBOL_WITH_POS (o1)->pos,
+				     XSYMBOL_WITH_POS (o2)->pos,
+				     equal_kind, depth + 1, ht));
 
 	/* Aside from them, only true vectors, char-tables, compiled
 	   functions, and fonts (font-spec, font-entity, font-object)

-- 
Alan Mackenzie (Nuremberg, Germany).