From: "Mattias Engdegård" <mattias.engdegard@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: philipk@posteo.net, 69387@debbugs.gnu.org, monnier@iro.umontreal.ca
Subject: bug#69387: 30.0.50; A string shouldn't be both a docstring and a return value
Date: Wed, 6 Mar 2024 12:44:19 +0100 [thread overview]
Message-ID: <01974A21-25DA-4445-BDE7-A022AF1D1BF8@gmail.com> (raw)
In-Reply-To: <86r0golmdg.fsf@gnu.org>
[-- Attachment #1: Type: text/plain, Size: 320 bytes --]
5 mars 2024 kl. 16.38 skrev Eli Zaretskii <eliz@gnu.org>:
> Thanks, but please always remember to end the heading lines in NEWS
> with a period.
Thank you, corrected in the patch.
I'm rowing back on the warning; it's probably more annoying than detecting actual errors, so it's been dropped from the patch.
[-- Attachment #2: 0001-Use-single-string-literal-as-return-value-not-doc-st.patch --]
[-- Type: application/octet-stream, Size: 5121 bytes --]
From dd2aeed849d4f3c72ab37f942700f8f79f1439c2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mattias=20Engdeg=C3=A5rd?= <mattiase@acm.org>
Date: Wed, 6 Mar 2024 12:03:06 +0100
Subject: [PATCH] Use single string literal as return value, not doc string
A function or macro body consisting of a single string literal now only
uses it as a return value. Previously, it was used as a doc string as
well which was never what the programmer intended and had some
inconvenient consequences (bug#69387).
* lisp/emacs-lisp/bytecomp.el (byte-compile-lambda):
Don't use a lone string literal as doc string.
* test/lisp/emacs-lisp/bytecomp-resources/warn-wide-docstring-defun.el
(foo): Update docstring warning test.
* doc/lispref/functions.texi (Function Documentation): Update manual.
* etc/NEWS: Announce the change.
---
doc/lispref/functions.texi | 13 ++++++-------
etc/NEWS | 16 ++++++++++++++++
lisp/emacs-lisp/bytecomp.el | 9 ++++-----
.../warn-wide-docstring-defun.el | 3 ++-
4 files changed, 28 insertions(+), 13 deletions(-)
diff --git a/doc/lispref/functions.texi b/doc/lispref/functions.texi
index 344b3ff3a50..ff635fc54b2 100644
--- a/doc/lispref/functions.texi
+++ b/doc/lispref/functions.texi
@@ -498,13 +498,12 @@ Function Documentation
nice in the source code will look ugly when displayed by the help
commands.
- You may wonder how the documentation string could be optional, since
-there are required components of the function that follow it (the body).
-Since evaluation of a string returns that string, without any side effects,
-it has no effect if it is not the last form in the body. Thus, in
-practice, there is no confusion between the first form of the body and the
-documentation string; if the only body form is a string then it serves both
-as the return value and as the documentation.
+ A documentation string must always be followed by at least one Lisp
+expression; otherwise, it is not a documentation string at all but the
+single expression of the body and used as the return value.
+When there is no meaningful value to return from a function, it is
+standard practice to return @code{nil} by adding it after the
+documentation string.
The last line of the documentation string can specify calling
conventions different from the actual function arguments. Write
diff --git a/etc/NEWS b/etc/NEWS
index fd957fdb115..73faeb20126 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1805,6 +1805,22 @@ Tree-sitter conditionally sets 'forward-sexp-function' for major modes
that have defined 'sexp' in 'treesit-thing-settings' to enable
sexp-related motion commands.
++++
+** Returned strings are never docstrings.
+Functions and macros whose bodies consist of a single string literal now
+only return that string; it is not used as a docstring. Example:
+
+ (defun sing-a-song ()
+ "Sing a song.")
+
+The above function returns the string '"Sing a song."' but has no
+docstring. Previously, that string was used as both a docstring and
+return value, which was never what the programmer wanted. If you want
+the string to be a docstring, add an explicit return value.
+
+This change applies to 'defun', 'defsubst', 'defmacro' and 'lambda'
+forms; other defining forms such as 'cl-defun' already worked this way.
+
** New or changed byte-compilation warnings
---
diff --git a/lisp/emacs-lisp/bytecomp.el b/lisp/emacs-lisp/bytecomp.el
index c3355eedd75..cf0e6d600dd 100644
--- a/lisp/emacs-lisp/bytecomp.el
+++ b/lisp/emacs-lisp/bytecomp.el
@@ -3061,12 +3061,11 @@ byte-compile-lambda
(append (if (not lexical-binding) arglistvars)
byte-compile-bound-variables))
(body (cdr (cdr fun)))
- (doc (if (stringp (car body))
+ ;; Treat a final string literal as a value, not a doc string.
+ (doc (if (and (cdr body) (stringp (car body)))
(prog1 (car body)
- ;; Discard the doc string from the body
- ;; unless it is the last element of the body.
- (if (cdr body)
- (setq body (cdr body))))))
+ ;; Discard the doc string from the body.
+ (setq body (cdr body)))))
(int (assq 'interactive body))
command-modes)
(when lexical-binding
diff --git a/test/lisp/emacs-lisp/bytecomp-resources/warn-wide-docstring-defun.el b/test/lisp/emacs-lisp/bytecomp-resources/warn-wide-docstring-defun.el
index 94b0e80c979..571f7f6f095 100644
--- a/test/lisp/emacs-lisp/bytecomp-resources/warn-wide-docstring-defun.el
+++ b/test/lisp/emacs-lisp/bytecomp-resources/warn-wide-docstring-defun.el
@@ -1,3 +1,4 @@
;;; -*- lexical-binding: t -*-
(defun foo ()
- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
+ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+ nil)
--
2.32.0 (Apple Git-132)
next prev parent reply other threads:[~2024-03-06 11:44 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-25 16:29 bug#69387: 30.0.50; A string shouldn't be both a docstring and a return value Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-02-25 17:13 ` Eli Zaretskii
2024-02-25 18:23 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-02-25 17:33 ` Eshel Yaron via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-02-26 14:47 ` Philip Kaludercic
2024-02-26 17:15 ` Mattias Engdegård
2024-02-26 17:44 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-02-26 18:04 ` Mattias Engdegård
2024-02-26 18:17 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-02-26 19:06 ` Mattias Engdegård
2024-03-04 14:28 ` Mattias Engdegård
2024-03-04 14:46 ` Eli Zaretskii
2024-03-05 13:16 ` Mattias Engdegård
2024-03-05 15:38 ` Eli Zaretskii
2024-03-06 11:44 ` Mattias Engdegård [this message]
2024-03-07 14:06 ` Mattias Engdegård
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=01974A21-25DA-4445-BDE7-A022AF1D1BF8@gmail.com \
--to=mattias.engdegard@gmail.com \
--cc=69387@debbugs.gnu.org \
--cc=eliz@gnu.org \
--cc=monnier@iro.umontreal.ca \
--cc=philipk@posteo.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.