emacs-bug-tracker
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#51089: closed (28.0.60; Using read-symbol-shorthands (("-" . "foo-")


From: GNU bug Tracking System
Subject: bug#51089: closed (28.0.60; Using read-symbol-shorthands (("-" . "foo-")) shouldn't shadow the '-' symbol)
Date: Mon, 11 Oct 2021 21:37:01 +0000

Your message dated Mon, 11 Oct 2021 22:36:14 +0100
with message-id <87pmsb5jn5.fsf@gmail.com>
and subject line Re: bug#51089: 28.0.60; Using read-symbol-shorthands (("-" . 
"foo-")) shouldn't shadow the '-' symbol
has caused the debbugs.gnu.org bug report #51089,
regarding 28.0.60; Using read-symbol-shorthands (("-" . "foo-")) shouldn't 
shadow the '-' symbol
to be marked as done.

(If you believe you have received this mail in error, please contact
help-debbugs@gnu.org.)


-- 
51089: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=51089
GNU Bug Tracking System
Contact help-debbugs@gnu.org with problems
--- Begin Message --- Subject: 28.0.60; Using read-symbol-shorthands (("-" . "foo-")) shouldn't shadow the '-' symbol Date: Thu, 07 Oct 2021 21:44:57 +0100
First reported in

    https://lists.gnu.org/archive/html/emacs-devel/2021-09/msg02297.html

Richard Stallman writes:

> It looks like "-" as a shorthand prefix should not rename `-'.  I can
> imagine various ways to fix this, some more general and some less
> general.

This is true.  We must fix this before importing Magnar Sveen's dash.el
library (which uses the very short '-' prefix) and any of its users in a
way that avoids the namespace pollution.

As Richard states, there are various ways to fix this.  I discuss
briefly 2 of them.  The first doesn't have any drawbacks in my opinion,
the second has a small one but it's overcome reasonably easy.

(1) The very simplest fix (and perhaps the most correct one) is just to
special-case the case of the '-' prefix and the '-' function.  That's
because the '-' symbol is the only one that exactly matches the
name-prefix separator that is used in Emacs.  In other words, I can't
think of any other "legitimate" use case for the shorthands feature that
would shadow a similar one-character symbol.  For example using

   (("/" . "some-longhand/"))

would shadow the '/' symbol but count as as "the user knows what she's
doing".


(2) Another natural, more generic, way would be to demand that the
shorthand in the 'car's of the elements of read-symbol-shorthands is
strictly shorter then the form about to be renamed.  In lread.c, I think
it would amount to this:

diff --git a/src/lread.c b/src/lread.c
index 07580d11d1..2950abf982 100644
--- a/src/lread.c
+++ b/src/lread.c
@@ -4666,7 +4666,7 @@ oblookup_considering_shorthand (Lisp_Object obarray, 
const char *in,
         version of the symbol name with xrealloc.  This isn't
         strictly needed, but it could later be used as a way for
         multiple transformations on a single symbol name.  */
-      if (sh_prefix_size <= size_byte
+      if (sh_prefix_size < size_byte
          && memcmp (SSDATA (sh_prefix), in, sh_prefix_size) == 0)
        {
          ptrdiff_t lh_prefix_size = SBYTES (lh_prefix);

However, this would also forbid another proposed use for shorthands,
which is to use them to rename whole collections symbols and allow them
to be used without a prefix.  An example would be the 'cl-' family of
symbols, which have "grown" a 'cl-' prefix some versions go, but which
some people would still like to use without that prefix.  Examples are

   cl-loop
   cl-first
   cl-plusp

Currently, it is possible to use shorthands to do

  (("cl-loop" . "loop")
   ("cl-first" . "first")
   ("cl-plusp" . "plusp"))

With the proposed fix above, it wouldn't be.

Therefore, I propos that for this use case, we introduce new syntax in
read-symbol-shorthands.  A trailing '$' character in the shorthand
portion would mean that it's OK to bypass that "strickly shorter"
limitation and rename the whole name.  This could be done with something
like this:

diff --git a/src/lread.c b/src/lread.c
index 07580d11d1..1bcaf5c64f 100644
--- a/src/lread.c
+++ b/src/lread.c
@@ -4658,6 +4658,13 @@ oblookup_considering_shorthand (Lisp_Object obarray, 
const char *in,
       if (!STRINGP (sh_prefix) || !STRINGP (lh_prefix))
        continue;
       ptrdiff_t sh_prefix_size = SBYTES (sh_prefix);
+      bool replace_whole = false;
+
+      if (SCHARS (sh_prefix) == (size + 1) &&
+         SSDATA (sh_prefix)[sh_prefix_size - 1] == '$') {
+       replace_whole = true;
+       sh_prefix_size--;
+      }
 
       /* Compare the prefix of the transformation pair to the symbol
         name.  If a match occurs, do the renaming and exit the loop.
@@ -4666,7 +4673,7 @@ oblookup_considering_shorthand (Lisp_Object obarray, 
const char *in,
         version of the symbol name with xrealloc.  This isn't
         strictly needed, but it could later be used as a way for
         multiple transformations on a single symbol name.  */
-      if (sh_prefix_size <= size_byte
+      if ((sh_prefix_size < size_byte || replace_whole)
          && memcmp (SSDATA (sh_prefix), in, sh_prefix_size) == 0)
        {
          ptrdiff_t lh_prefix_size = SBYTES (lh_prefix);

In addition to the C-side change, the Elisp code dealing with
read-symbol-shorthands prefixes needs to be changed to account for the
trailing "$".  Fortunately, there's not a lot of that code around it.

João



--- End Message ---
--- Begin Message --- Subject: Re: bug#51089: 28.0.60; Using read-symbol-shorthands (("-" . "foo-")) shouldn't shadow the '-' symbol Date: Mon, 11 Oct 2021 22:36:14 +0100 User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.60 (gnu/linux)
João Távora <joaotavora@gmail.com> writes:
>>  Yes.  But I think we could use strcspn for an easier, one-line, test
>>  of the same.
> Seems I didn't try very hard :-)  Using strspn() instead of strcpsn() does
> work.  Patch below, with a constant string of ASCII punctuation that
> you'll probably want to tweak.

Hello Eli,

I took the initiative and pushed the patch to emacs-28 after settling on
the string:

   "^*+-/<=>_|"

I chose this string after analysing the Emacs Lisp symbols that are only
punctuation.  I used this Elisp form (very inneficient, but did the job).

   (let ((all-punctuation))
     (mapatoms (lambda (symbol)
                 (when (with-temp-buffer
                         (insert (symbol-name symbol))
                         (goto-char (point-min))
                         (skip-syntax-forward "_")
                         (eobp))
                   (push symbol all-punctuation))))
     all-punctuation)

This returns the following symbols, which I think is the full set of
"protected" ones:

   ;; => (& * + - / < = > _ | ** ++ -- -/ -> /= <= <> >= || ¬ --- --> ->>)

One of them isn't ASCII, but as we discussed it's likely not used a
shorthand prefix.

Closing this bug.  Let me know if I should reopen.
João


--- End Message ---

reply via email to

[Prev in Thread] Current Thread [Next in Thread]