all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Drew Adams <drew.adams@oracle.com>
To: 15122@debbugs.gnu.org
Subject: bug#15122: 24.3.50; [PATCH] byte-compiler warnings about destructive functions
Date: Sat, 17 Aug 2013 22:26:16 -0700 (PDT)	[thread overview]
Message-ID: <9ee2e7bb-6036-4e81-bb44-86d4ac74e7b8@default> (raw)

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

This StackOverflow entry suggested that the Emacs-Lisp byte compiler be
able to warn about the use of functions that are destructive, i.e., can
modify data structures in place:

http://stackoverflow.com/questions/17610046/elisp-destructive-operation-warning

Attached is a patch that provides this, at least a start.

It covers the basic functions that can perform in-place data structure 
modification: lists, strings, vectors and other arrays, symbol properties.
These are generally the functions identified as destructive or modifying
in the Elisp manual.

A comment in the code identifies some other possible candidates that
occurred to me while browsing the manual.  Modify the list of functions
as you see fit (variable `byte-compile-destructive-functions').

I'm no expert on some of the data structure implementations, and the
manual is sometimes not so clear about whether destructive modification
occurs.  And perhaps there are cases where although the modification is
destructive at an implementation level the user need not be concerned
about that level, so such functions should not be included.  Dunno.

Perhaps also something could be done in the future wrt recognition of
particular cases for `setf' modification.

In GNU Emacs 24.3.50.1 (i686-pc-mingw32)
 of 2013-08-07 on ODIEONE
Bzr revision: 113750 lekktu@gmail.com-20130808011911-0jzpc9xuncegg6x9
Windowing system distributor `Microsoft Corp.', version 6.1.7601
Configured using:
 `configure --prefix=/c/Devel/emacs/binary --enable-checking=yes,glyphs
 CFLAGS=-O0 -g3 LDFLAGS=-Lc:/Devel/emacs/lib
 CPPFLAGS=-Ic:/Devel/emacs/include'

[-- Attachment #2: bytecomp-2013-08-17a.patch --]
[-- Type: application/octet-stream, Size: 3045 bytes --]

diff -c bytecomp.el bytecomp-patched.el
*** bytecomp.el	Sat Aug 17 21:35:05 2013
--- bytecomp-patched.el	Sat Aug 17 22:11:45 2013
***************
*** 274,281 ****
  (defconst byte-compile-warning-types
    '(redefine callargs free-vars unresolved
  	     obsolete noruntime cl-functions interactive-only
! 	     make-local mapcar constants suspicious lexical)
    "The list of warning types used when `byte-compile-warnings' is t.")
  (defcustom byte-compile-warnings t
    "List of warnings that the byte-compiler should issue (t for all).
  
--- 274,282 ----
  (defconst byte-compile-warning-types
    '(redefine callargs free-vars unresolved
  	     obsolete noruntime cl-functions interactive-only
! 	     make-local mapcar constants suspicious lexical destructive)
    "The list of warning types used when `byte-compile-warnings' is t.")
+ 
  (defcustom byte-compile-warnings t
    "List of warnings that the byte-compiler should issue (t for all).
  
***************
*** 298,303 ****
--- 299,305 ----
    mapcar      mapcar called for effect.
    constants   let-binding of, or assignment to, constants/nonvariables.
    suspicious  constructs that usually don't do what the coder wanted.
+   destructive functions that can modify data structure in place.
  
  If the list begins with `not', then the remaining elements specify warnings to
  suppress.  For example, (not mapcar) will suppress warnings about mapcar."
***************
*** 353,358 ****
--- 355,370 ----
                    (t
                     (append byte-compile-warnings (list warning)))))))
  
+ ;; Possible candidates? `clrhash', `match-data' (with REUSE), `put', `puthash',
+ ;; `remhash', `set-char-table-extra-slot', `set-char-table-parent',
+ ;; `set-char-table-range', `setplist', `unintern', `write-region' (merging annotations)?
+ ;; What to do about `setf' with various generalized vars?
+ ;;
+ (defvar byte-compile-destructive-functions
+   '(aset clear-string delete delete-dups delq fillarray nbutlast nconc nreverse plist-put
+     ring-insert ring-insert-at-beginning ring-remove setcar setcdr sort store-substring)
+   "List of functions that can modify data structures in place.")
+ 
  (defvar byte-compile-interactive-only-functions
    '(beginning-of-buffer end-of-buffer replace-string replace-regexp
      insert-file insert-buffer insert-file-literally previous-line next-line
***************
*** 2925,2930 ****
--- 2937,2946 ----
               (handler (get fn 'byte-compile)))
          (when (macroexp--const-symbol-p fn)
            (byte-compile-warn "`%s' called as a function" fn))
+         (and (byte-compile-warning-enabled-p 'destructive)
+              (memq fn byte-compile-destructive-functions)
+              (byte-compile-warn "`%s' can modify data structure in place \
+ \(destructive)" fn))
          (and (byte-compile-warning-enabled-p 'interactive-only)
               (memq fn byte-compile-interactive-only-functions)
               (byte-compile-warn "`%s' used from Lisp code\n\

Diff finished.  Sat Aug 17 22:12:04 2013

             reply	other threads:[~2013-08-18  5:26 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-18  5:26 Drew Adams [this message]
2013-08-18 18:38 ` bug#15122: 24.3.50; [PATCH] byte-compiler warnings about destructive functions Richard Stallman
2013-08-19  2:59 ` Stefan Monnier
2013-08-19  5:07   ` Drew Adams
2013-08-20  5:07     ` Stefan Monnier
2013-08-20 14:07       ` Drew Adams
2013-08-20 14:39         ` Stefan Monnier
2013-08-20 15:15           ` Drew Adams
2013-08-21  1:00           ` Leo Liu
2013-08-21  4:24             ` Stefan Monnier
2013-08-21 16:54               ` Richard Stallman

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=9ee2e7bb-6036-4e81-bb44-86d4ac74e7b8@default \
    --to=drew.adams@oracle.com \
    --cc=15122@debbugs.gnu.org \
    /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.