all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Mattias Engdegård" <mattiase@acm.org>
To: 40671@debbugs.gnu.org
Cc: Kevin Vigouroux <ke.vigouroux@laposte.net>
Subject: bug#40671: [DOC] modify literal objects
Date: Fri, 17 Apr 2020 18:37:23 +0200	[thread overview]
Message-ID: <A25E0620-7A8F-4856-86A5-045E370A7357@acm.org> (raw)
In-Reply-To: <0B653323-49E1-4AC7-A85B-18346D220585@acm.org>

[-- Attachment #1: Type: text/plain, Size: 422 bytes --]

tags 40671 patch
stop

[Sorry about the truncated message.]

> Can we modify literal objects? 

No, and the manual should do a much better job at explaining this. At the very least it should not promulgate bad ideas by including mutation of literals in example code. Patch attached, suggested for emacs-27.

We should not even try to show what happens when the user breaks the rule, because it is undefined.



[-- Attachment #2: 0001-Don-t-mutate-literals-in-manual-examples-bug-40671.patch --]
[-- Type: application/octet-stream, Size: 8588 bytes --]

From 9801ee1b12574f8b1b50ba5b76b7ee41a7fb8bc4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mattias=20Engdeg=C3=A5rd?= <mattiase@acm.org>
Date: Fri, 17 Apr 2020 18:00:34 +0200
Subject: [PATCH] Don't mutate literals in manual examples (bug#40671)

* doc/lispref/edebug.texi (Printing in Edebug):
* doc/lispref/keymaps.texi (Changing Key Bindings):
* doc/lispref/lists.texi (Setcar, Setcdr, Rearrangement, Sets And Lists)
(Association Lists, Plist Access):
* doc/lispref/sequences.texi (Sequence Functions, Array Functions):
* doc/lispref/strings.texi (Text Comparison):
Rewrite example code to not mutate constant (literal) lists, vectors
or strings.  Noticed by Kevin Vigouroux.
---
 doc/lispref/edebug.texi    |  2 +-
 doc/lispref/keymaps.texi   |  8 ++---
 doc/lispref/lists.texi     | 62 +++++++++-----------------------------
 doc/lispref/sequences.texi | 26 ++++++++--------
 doc/lispref/strings.texi   |  4 +--
 5 files changed, 34 insertions(+), 68 deletions(-)

diff --git a/doc/lispref/edebug.texi b/doc/lispref/edebug.texi
index cfef5c12d1..5970e7cf80 100644
--- a/doc/lispref/edebug.texi
+++ b/doc/lispref/edebug.texi
@@ -858,7 +858,7 @@ Printing in Edebug
   Here is an example of code that creates a circular structure:
 
 @example
-(setq a '(x y))
+(setq a (list 'x 'y))
 (setcar a a)
 @end example
 
diff --git a/doc/lispref/keymaps.texi b/doc/lispref/keymaps.texi
index 2c90d208c0..fd207a184e 100644
--- a/doc/lispref/keymaps.texi
+++ b/doc/lispref/keymaps.texi
@@ -1441,10 +1441,10 @@ Changing Key Bindings
 
 @smallexample
 @group
-(setq map '(keymap
-            (?1 . olddef-1)
-            (?2 . olddef-2)
-            (?3 . olddef-1)))
+(setq map (list 'keymap
+                (cons ?1 'olddef-1)
+                (cons ?2 'olddef-2)
+                (cons ?3 'olddef-1)))
 @result{} (keymap (49 . olddef-1) (50 . olddef-2) (51 . olddef-1))
 @end group
 
diff --git a/doc/lispref/lists.texi b/doc/lispref/lists.texi
index 27fa5385e3..d1a12e9819 100644
--- a/doc/lispref/lists.texi
+++ b/doc/lispref/lists.texi
@@ -906,7 +906,7 @@ Setcar
 
 @example
 @group
-(setq x '(1 2))
+(setq x (list 1 2))
      @result{} (1 2)
 @end group
 @group
@@ -927,7 +927,7 @@ Setcar
 @example
 @group
 ;; @r{Create two lists that are partly shared.}
-(setq x1 '(a b c))
+(setq x1 (list 'a 'b 'c))
      @result{} (a b c)
 (setq x2 (cons 'z (cdr x1)))
      @result{} (z b c)
@@ -1017,7 +1017,7 @@ Setcdr
 
 @example
 @group
-(setq x '(1 2 3))
+(setq x (list 1 2 3))
      @result{} (1 2 3)
 @end group
 @group
@@ -1037,7 +1037,7 @@ Setcdr
 
 @example
 @group
-(setq x1 '(a b c))
+(setq x1 (list 'a 'b 'c))
      @result{} (a b c)
 (setcdr x1 (cdr (cdr x1)))
      @result{} (c)
@@ -1069,7 +1069,7 @@ Setcdr
 
 @example
 @group
-(setq x1 '(a b c))
+(setq x1 (list 'a 'b 'c))
      @result{} (a b c)
 (setcdr x1 (cons 'd (cdr x1)))
      @result{} (d b c)
@@ -1130,7 +1130,7 @@ Rearrangement
 
 @example
 @group
-(setq x '(1 2 3))
+(setq x (list 1 2 3))
      @result{} (1 2 3)
 @end group
 @group
@@ -1150,7 +1150,7 @@ Rearrangement
 
 @example
 @group
-(setq x '(1 2 3))
+(setq x (list 1 2 3))
      @result{} (1 2 3)
 @end group
 @group
@@ -1163,41 +1163,7 @@ Rearrangement
 @end group
 @end example
 
-However, the other arguments (all but the last) must be lists.
-
-A common pitfall is to use a quoted constant list as a non-last
-argument to @code{nconc}.  If you do this, your program will change
-each time you run it!  Here is what happens:
-
-@smallexample
-@group
-(defun add-foo (x)            ; @r{We want this function to add}
-  (nconc '(foo) x))           ;   @r{@code{foo} to the front of its arg.}
-@end group
-
-@group
-(symbol-function 'add-foo)
-     @result{} (lambda (x) (nconc '(foo) x))
-@end group
-
-@group
-(setq xx (add-foo '(1 2)))    ; @r{It seems to work.}
-     @result{} (foo 1 2)
-@end group
-@group
-(setq xy (add-foo '(3 4)))    ; @r{What happened?}
-     @result{} (foo 1 2 3 4)
-@end group
-@group
-(eq xx xy)
-     @result{} t
-@end group
-
-@group
-(symbol-function 'add-foo)
-     @result{} (lambda (x) (nconc '(foo 1 2 3 4) x))
-@end group
-@end smallexample
+However, the other arguments (all but the last) must be non-constant lists.
 @end defun
 
 @node Sets And Lists
@@ -1260,7 +1226,7 @@ Sets And Lists
 
 @example
 @group
-(delq 'a '(a b c)) @equiv{} (cdr '(a b c))
+(delq 'a (list 'a 'b 'c)) @equiv{} (cdr (list 'a 'b 'c))
 @end group
 @end example
 
@@ -1270,7 +1236,7 @@ Sets And Lists
 
 @example
 @group
-(setq sample-list '(a b c (4)))
+(setq sample-list (list 'a 'b 'c '(4)))
      @result{} (a b c (4))
 @end group
 @group
@@ -1407,7 +1373,7 @@ Sets And Lists
 
 @example
 @group
-(setq l '((2) (1) (2)))
+(setq l (list '(2) '(1) '(2)))
 (delete '(2) l)
      @result{} ((1))
 l
@@ -1416,7 +1382,7 @@ Sets And Lists
 ;; @r{write @code{(setq l (delete '(2) l))}.}
 @end group
 @group
-(setq l '((2) (1) (2)))
+(setq l (list '(2) '(1) '(2)))
 (delete '(1) l)
      @result{} ((2) (2))
 l
@@ -1759,7 +1725,7 @@ Association Lists
 than looking at the saved value of @var{alist}.
 
 @example
-(setq alist '((foo 1) (bar 2) (foo 3) (lose 4)))
+(setq alist (list '(foo 1) '(bar 2) '(foo 3) '(lose 4)))
      @result{} ((foo 1) (bar 2) (foo 3) (lose 4))
 (assq-delete-all 'foo alist)
      @result{} ((bar 2) (lose 4))
@@ -1926,7 +1892,7 @@ Plist Access
 in the place where you got @var{plist}.  For example,
 
 @example
-(setq my-plist '(bar t foo 4))
+(setq my-plist (list 'bar t 'foo 4))
      @result{} (bar t foo 4)
 (setq my-plist (plist-put my-plist 'foo 69))
      @result{} (bar t foo 69)
diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi
index 1a3a04f680..d35b4d05e3 100644
--- a/doc/lispref/sequences.texi
+++ b/doc/lispref/sequences.texi
@@ -183,7 +183,7 @@ Sequence Functions
 
 @example
 @group
-(setq bar '(1 2))
+(setq bar (list 1 2))
      @result{} (1 2)
 @end group
 @group
@@ -278,7 +278,7 @@ Sequence Functions
 
 @example
 @group
-(setq x '(a b c))
+(setq x (list 'a 'b 'c))
      @result{} (a b c)
 @end group
 @group
@@ -374,11 +374,11 @@ Sequence Functions
 
 @example
 @group
-(setq nums '(1 3 2 6 5 4 0))
+(setq nums (list 1 3 2 6 5 4 0))
      @result{} (1 3 2 6 5 4 0)
 @end group
 @group
-(sort nums '<)
+(sort nums #'<)
      @result{} (0 1 2 3 4 5 6)
 @end group
 @group
@@ -396,7 +396,7 @@ Sequence Functions
 the variable that held the original list:
 
 @example
-(setq nums (sort nums '<))
+(setq nums (sort nums #'<))
 @end example
 
 For the better understanding of what stable sort is, consider the following
@@ -1228,7 +1228,7 @@ Array Functions
 
 @example
 @group
-(setq w [foo bar baz])
+(setq w (vector 'foo 'bar 'baz))
      @result{} [foo bar baz]
 (aset w 0 'fu)
      @result{} fu
@@ -1237,12 +1237,12 @@ Array Functions
 @end group
 
 @group
-(setq x "asdfasfd")
-     @result{} "asdfasfd"
+(setq x (string ?a ?b ?c ?d ?e))
+     @result{} "abcde"
 (aset x 3 ?Z)
      @result{} 90
 x
-     @result{} "asdZasfd"
+     @result{} "abcZe"
 @end group
 @end example
 
@@ -1257,7 +1257,7 @@ Array Functions
 
 @example
 @group
-(setq a [a b c d e f g])
+(setq a (vector 'a 'b 'c 'd 'e 'f 'g))
      @result{} [a b c d e f g]
 (fillarray a 0)
      @result{} [0 0 0 0 0 0 0]
@@ -1265,10 +1265,10 @@ Array Functions
      @result{} [0 0 0 0 0 0 0]
 @end group
 @group
-(setq s "When in the course")
-     @result{} "When in the course"
+(setq s (string ?S ?e ?c ?r ?e ?t))
+     @result{} "Secret"
 (fillarray s ?-)
-     @result{} "------------------"
+     @result{} "------"
 @end group
 @end example
 
diff --git a/doc/lispref/strings.texi b/doc/lispref/strings.texi
index 14cabc5d79..3d375409a9 100644
--- a/doc/lispref/strings.texi
+++ b/doc/lispref/strings.texi
@@ -591,7 +591,7 @@ Text Comparison
 
 @example
 @group
-(sort '("11" "12" "1 1" "1 2" "1.1" "1.2") 'string-collate-lessp)
+(sort (list "11" "12" "1 1" "1 2" "1.1" "1.2") 'string-collate-lessp)
      @result{} ("11" "1 1" "1.1" "12" "1 2" "1.2")
 @end group
 @end example
@@ -608,7 +608,7 @@ Text Comparison
 
 @example
 @group
-(sort '("11" "12" "1 1" "1 2" "1.1" "1.2")
+(sort (list "11" "12" "1 1" "1 2" "1.1" "1.2")
       (lambda (s1 s2) (string-collate-lessp s1 s2 "POSIX")))
      @result{} ("1 1" "1 2" "1.1" "1.2" "11" "12")
 @end group
-- 
2.21.1 (Apple Git-122.3)


  reply	other threads:[~2020-04-17 16:37 UTC|newest]

Thread overview: 170+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-16 19:28 bug#40671: [DOC] modify literal objects Kevin Vigouroux via Bug reports for GNU Emacs, the Swiss army knife of text editors
2020-04-17 16:09 ` Mattias Engdegård
2020-04-17 16:37   ` Mattias Engdegård [this message]
2020-04-17 17:27     ` Eli Zaretskii
2020-04-18 20:10 ` Paul Eggert
2020-04-18 21:54   ` Drew Adams
2020-04-19  2:39     ` Noam Postavsky
2020-04-19 20:39     ` Paul Eggert
2020-04-19 21:01       ` Drew Adams
2020-04-19 21:16         ` Paul Eggert
2020-04-19 22:24           ` Drew Adams
2020-04-19 22:51             ` Paul Eggert
2020-04-20  5:32               ` Drew Adams
2020-04-22 17:36                 ` Paul Eggert
2020-05-01  3:03     ` Dmitry Gutov
2020-05-01  5:16       ` Drew Adams
2020-05-01 21:46       ` Paul Eggert
2020-05-01 23:37         ` Dmitry Gutov
2020-04-19  2:26   ` Richard Stallman
2020-04-19 13:56   ` Eli Zaretskii
2020-04-19 16:59     ` Mattias Engdegård
2020-04-19 19:21       ` Eli Zaretskii
2020-04-19 21:02       ` Paul Eggert
2020-04-19 21:11         ` Drew Adams
2020-04-19 21:57         ` Michael Heerdegen
2020-04-19 22:41           ` Paul Eggert
2020-04-19 23:45             ` Michael Heerdegen
2020-04-20  0:24               ` Paul Eggert
2020-04-20  0:53                 ` Michael Heerdegen
2020-04-20  3:23                   ` Paul Eggert
2020-04-20  3:36                     ` Michael Heerdegen
2020-04-22  6:30                       ` Paul Eggert
2020-04-20  5:54                     ` Drew Adams
2020-04-22 17:21                       ` Paul Eggert
2020-04-23  0:49                         ` Michael Heerdegen
2020-04-24  2:36                           ` Richard Stallman
2020-04-24 15:08                             ` Drew Adams
2020-04-25  1:58                               ` Paul Eggert
2020-04-24 16:39                             ` Mattias Engdegård
2020-04-24 16:46                               ` Dmitry Gutov
2020-04-25  2:21                                 ` Paul Eggert
2020-04-25  2:40                                   ` Dmitry Gutov
2020-04-25  3:20                                     ` Paul Eggert
2020-04-25 19:30                                       ` Dmitry Gutov
2020-04-26  3:49                                         ` Paul Eggert
2020-04-26 14:03                                           ` Dmitry Gutov
2020-04-26 14:19                                             ` Eli Zaretskii
2020-04-26 14:34                                               ` Dmitry Gutov
2020-04-26 15:46                                                 ` Eli Zaretskii
2020-04-26 16:02                                                   ` Dmitry Gutov
2020-04-26 16:58                                                     ` Eli Zaretskii
2020-04-26 17:39                                                       ` Dmitry Gutov
2020-04-26 18:14                                                         ` Eli Zaretskii
2020-04-26 18:32                                                           ` Dmitry Gutov
2020-04-26 18:41                                                             ` Eli Zaretskii
2020-04-26 18:53                                                               ` Dmitry Gutov
2020-04-26 18:57                                             ` Paul Eggert
2020-04-26 19:22                                               ` Philipp Stephani
2020-04-26 20:14                                                 ` Paul Eggert
2020-04-26 21:23                                               ` Dmitry Gutov
2020-04-26 23:13                                                 ` Paul Eggert
2020-04-27  0:53                                                   ` Dmitry Gutov
2020-04-27  1:49                                                     ` Paul Eggert
2020-04-28  3:05                                                       ` Dmitry Gutov
2020-04-28  8:17                                                         ` Paul Eggert
2020-04-28 13:54                                                           ` Dmitry Gutov
2020-04-28 17:59                                                             ` Paul Eggert
2020-04-28 18:46                                                               ` Dmitry Gutov
2020-04-28 19:20                                                                 ` Paul Eggert
2020-04-28 19:33                                                                   ` Dmitry Gutov
2020-04-28 20:09                                                                     ` Paul Eggert
2020-04-28 21:10                                                                       ` Dmitry Gutov
2020-04-28 23:10                                                                         ` Paul Eggert
2020-04-28 23:36                                                                           ` Dmitry Gutov
2020-04-28 23:53                                                                             ` Paul Eggert
2020-04-28 23:57                                                                               ` Dmitry Gutov
2020-04-28 23:53                                                                           ` Dmitry Gutov
2020-04-29  0:04                                                                             ` Paul Eggert
2020-04-29  0:14                                                                               ` Dmitry Gutov
2020-04-29  0:55                                                                           ` Drew Adams
2020-04-29  1:03                                                                             ` Dmitry Gutov
2020-04-29  1:15                                                                               ` Drew Adams
2020-04-29  1:27                                                                                 ` Michael Heerdegen
2020-04-29  1:38                                                                             ` Paul Eggert
2020-04-29  4:36                                                                               ` Drew Adams
2020-04-29 16:18                                                                                 ` Paul Eggert
2020-05-01  2:47                                                                                   ` Richard Stallman
2020-05-01  6:23                                                                                     ` Eli Zaretskii
2020-05-01  3:13                                                                               ` Dmitry Gutov
2020-05-01  5:15                                                                                 ` Drew Adams
2020-05-01 21:40                                                                                 ` Paul Eggert
2020-05-01 22:05                                                                                   ` Drew Adams
2020-05-01 22:28                                                                                     ` Paul Eggert
2020-05-02  1:07                                                                                   ` Dmitry Gutov
2020-05-02  6:28                                                                                     ` Paul Eggert
2020-05-02 15:42                                                                                       ` Dmitry Gutov
2020-05-02 19:35                                                                                         ` Paul Eggert
2020-05-03  1:30                                                                                           ` Dmitry Gutov
2020-05-03  7:40                                                                                             ` Paul Eggert
2020-05-03 16:44                                                                                               ` Dmitry Gutov
2020-05-03 20:48                                                                                                 ` Paul Eggert
2020-05-03 22:17                                                                                                   ` Dmitry Gutov
2020-05-03 22:18                                                                                                   ` Dmitry Gutov
2020-05-03 22:39                                                                                                     ` Paul Eggert
2020-05-03 22:53                                                                                                       ` Dmitry Gutov
2020-05-03 23:10                                                                                                         ` Paul Eggert
2020-05-04 10:16                                                                                                           ` Dmitry Gutov
2020-05-04 17:52                                                                                                             ` Paul Eggert
2020-05-05  1:39                                                                                                               ` Dmitry Gutov
2020-05-05  6:09                                                                                                                 ` Paul Eggert
2020-05-05 12:38                                                                                                                   ` Dmitry Gutov
2020-05-09  6:10                                                                                                                     ` Paul Eggert
2020-05-10  3:13                                                                                                                       ` Dmitry Gutov
2020-05-10 13:34                                                                                                                         ` Dmitry Gutov
2020-05-10 17:29                                                                                                                         ` Paul Eggert
2020-05-11  0:00                                                                                                                           ` Michael Heerdegen
2020-05-11  0:26                                                                                                                             ` Dmitry Gutov
2020-05-11  1:47                                                                                                                             ` Drew Adams
2020-05-11  1:54                                                                                                                               ` Dmitry Gutov
2020-05-11  2:33                                                                                                                                 ` Drew Adams
2020-05-11  2:56                                                                                                                               ` Michael Heerdegen
2020-05-11  4:21                                                                                                                                 ` Drew Adams
2020-05-11  4:51                                                                                                                                   ` Michael Heerdegen
2020-05-11  6:28                                                                                                                                     ` Paul Eggert
2020-05-11 13:57                                                                                                                                       ` Noam Postavsky
2020-05-11 22:36                                                                                                                                         ` Michael Heerdegen
2020-05-11 22:30                                                                                                                                       ` Michael Heerdegen
2020-05-12  3:20                                                                                                                                       ` Richard Stallman
2020-05-12  4:24                                                                                                                                         ` Michael Heerdegen
2020-05-13  3:57                                                                                                                                           ` Richard Stallman
2020-05-13  5:05                                                                                                                                             ` Michael Heerdegen
2020-05-14  5:14                                                                                                                                               ` Richard Stallman
     [not found]                                                                                                                                       ` <05BEF593-F16A-4DEE-98BC-653221F1F9EE@acm.org>
2020-05-17  0:11                                                                                                                                         ` Paul Eggert
2020-05-17  9:43                                                                                                                                           ` Mattias Engdegård
2020-05-17 16:38                                                                                                                                             ` Paul Eggert
2020-05-11  1:53                                                                                                                             ` Paul Eggert
2020-05-11  3:18                                                                                                                               ` Michael Heerdegen
2020-05-11  0:44                                                                                                                           ` Dmitry Gutov
2020-05-11  1:57                                                                                                                             ` Paul Eggert
2020-05-12  1:59                                                                                                                               ` Dmitry Gutov
2020-05-17  1:28                                                                                                                                 ` Paul Eggert
2020-05-17  5:02                                                                                                                                   ` Michael Heerdegen
2020-05-17 16:34                                                                                                                                     ` Paul Eggert
2020-05-17 12:39                                                                                                                                   ` Dmitry Gutov
2020-05-17 16:21                                                                                                                                     ` Paul Eggert
2020-05-05 17:40                                                                                                                   ` Drew Adams
2020-05-05 18:49                                                                                                                     ` Dmitry Gutov
2020-05-05 19:26                                                                                                                       ` Drew Adams
2020-05-05 20:48                                                                                                               ` Kevin Vigouroux via Bug reports for GNU Emacs, the Swiss army knife of text editors
2020-05-09  5:57                                                                                                                 ` Paul Eggert
2020-04-28 21:18                                                                       ` Dmitry Gutov
2020-04-28 17:25                                                           ` Drew Adams
2020-04-28 17:47                                                             ` Paul Eggert
2020-04-29  0:32                                                               ` Michael Heerdegen
2020-04-29  1:40                                                                 ` Paul Eggert
2020-04-29  4:40                                                                   ` Michael Heerdegen
2020-04-29  8:01                                                                     ` Eli Zaretskii
2020-04-29 16:36                                                                 ` Paul Eggert
2020-04-24 17:18                               ` Drew Adams
2020-04-25  3:38                               ` Richard Stallman
2020-04-25 18:26                                 ` Paul Eggert
2020-04-25  2:22                           ` Paul Eggert
2020-04-25  6:00                             ` Andreas Schwab
2020-04-25 18:23                               ` Paul Eggert
2020-04-28 23:52                             ` Michael Heerdegen
2020-04-21  1:25               ` Michael Heerdegen
2020-04-21  2:20                 ` Paul Eggert
2020-04-20  6:02             ` Drew Adams
2020-04-19 20:45     ` Paul Eggert
2020-04-20 14:10       ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=A25E0620-7A8F-4856-86A5-045E370A7357@acm.org \
    --to=mattiase@acm.org \
    --cc=40671@debbugs.gnu.org \
    --cc=ke.vigouroux@laposte.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.