all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 74349@debbugs.gnu.org
Subject: bug#74349: 30.0.92; Visiting a file under c-ts-mode loads cc-mode
Date: Thu, 14 Nov 2024 11:21:49 -0500	[thread overview]
Message-ID: <jwvttc9stga.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <86ldxml3v5.fsf@gnu.org> (Eli Zaretskii's message of "Thu, 14 Nov 2024 08:35:26 +0200")

>> Could you clarify why you think it's a problem?
> cc-mode is large; loading it brings in many other cc-* files, which

That's indeed an inconvenient, but we usually consider it minor (several
modes seem happy to (require 'cc-mode) just to use one or two
variables from it such as syntax-tables).

> There's also the issue of the adverse consequences of loading cc-mode
> discussed in bug#74339.

Right, that's a self-inflicted problem because we did not follow our own
recommendation that loading a file should not affect the behavior
of Emacs.

> I hope those will be solved soon, but their current existence was the
> motivation for me to examine each and every reason for loading cc-mode
> when I didn't expect that to happen.

Fair enough.

>> It doesn't "funcall" it, but it does make sure the function is loaded:
>> 
>>               ;; If KEY is an extra parent it may remain not loaded
>>               ;; (hence with some of its mode-specific vars missing their
>>               ;; `safe-local-variable' property), leading to spurious
>>               ;; prompts about unsafe vars (bug#68246).
>>               (if (and (symbolp key) (autoloadp (indirect-function key)))
>>                   (ignore-errors (autoload-do-load (indirect-function key))))
>> 
>> > I didn't expect derived-mode-add-parents to cause c-mode be turned on,
>> 
>> It's not turned on: its file loaded.
>
> Why does it need to be loaded in this case?

As the comment explains: we load the mode to try and avoid spuriously
querying the user about "unsafe" settings.

> Is there some technical reason to do so in order to perform the
> settings of the variables defined by that mode?

Not to set the variable, but to check whether the setting is safe: If
your `.dir-locals.el` sets a variable FOO which is defined as
`safe-local-variable` in BASEMODE (e.g. `c-mode`) but you actually use
DERIVEDMODE (e.g. `c-ts-mode`) for that file and DERIVEDMODE does not
load BASEMODE, then we may have to load BASEMODE to discover that the
setting is safe.

The patch below should implement the refinement I suggested, where we
(auto)load BASENAME only in the case one of the vars doesn't yet have
a `safe-local-variable` property.

BTW, I notice that CC-mode's settings of `safe-local-variable` aren't
all autoloaded:

    % grep safe-local lisp/progmodes/cc*.el
    lisp/progmodes/cc-vars.el:;;;###autoload(put 'c-basic-offset 'safe-local-variable 'integerp)
    lisp/progmodes/cc-vars.el:(put 'c-syntactic-indentation 'safe-local-variable 'booleanp)
    lisp/progmodes/cc-vars.el:(put 'c-syntactic-indentation-in-macros 'safe-local-variable 'booleanp)
    lisp/progmodes/cc-vars.el:;;;###autoload(put 'c-backslash-column 'safe-local-variable 'integerp)
    lisp/progmodes/cc-vars.el:;;;###autoload (put 'c-font-lock-extra-types 'safe-local-variable #'c-string-list-p)
    lisp/progmodes/cc-vars.el:;;;###autoload (put 'c++-font-lock-extra-types 'safe-local-variable #'c-string-list-p)
    lisp/progmodes/cc-vars.el:;;;###autoload (put 'objc-font-lock-extra-types 'safe-local-variable #'c-string-list-p)
    lisp/progmodes/cc-vars.el:;;;###autoload (put 'java-font-lock-extra-types 'safe-local-variable #'c-string-list-p)
    lisp/progmodes/cc-vars.el:;;;###autoload (put 'idl-font-lock-extra-types 'safe-local-variable #'c-string-list-p)
    lisp/progmodes/cc-vars.el:;;;###autoload (put 'pike-font-lock-extra-types 'safe-local-variable #'c-string-list-p)
    lisp/progmodes/cc-vars.el:(put 'c-noise-macro-names 'safe-local-variable #'c-string-list-p)
    lisp/progmodes/cc-vars.el:(put 'c-noise-macro-with-parens-names 'safe-local-variable #'c-string-list-p)
    lisp/progmodes/cc-vars.el:(put 'c-macro-names-with-semicolon 'safe-local-variable
    lisp/progmodes/cc-vars.el:;;;###autoload(put 'c-file-style 'safe-local-variable 'string-or-null-p)
    %

Alan, I assume this "inconsistency" is accidental (I "fixed" it in the
patch below).


        Stefan


diff --git a/lisp/files.el b/lisp/files.el
index bffdaa288a5..1bc2372eeec 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -4484,6 +4484,21 @@ dir-locals-collect-mode-variables
 	;; Need a new cons in case we setcdr later.
 	(push (cons variable value) variables)))))
 
+(defun dir-locals--load-mode-if-needed (key alist)
+  ;; If KEY is an extra parent it may remain not loaded
+  ;; (hence with some of its mode-specific vars missing their
+  ;; `safe-local-variable' property), leading to spurious
+  ;; prompts about unsafe vars (bug#68246).
+ (when (and (symbolp key) (autoloadp (indirect-function key)))
+   (let ((unsafe nil))
+     (pcase-dolist (`(,var . ,_val) alist)
+       (unless (or (memq var '(mode eval))
+                   (get var 'safe-local-variable))
+        (setq unsafe t)))
+     (when unsafe
+       (ignore-errors
+        (autoload-do-load (indirect-function key)))))))
+
 (defun dir-locals-collect-variables (class-variables root variables
                                                      &optional predicate)
   "Collect entries from CLASS-VARIABLES into VARIABLES.
@@ -4514,15 +4514,9 @@
                   (funcall predicate key)
                 (or (not key)
                     (derived-mode-p key)))
-              ;; If KEY is an extra parent it may remain not loaded
-              ;; (hence with some of its mode-specific vars missing their
-              ;; `safe-local-variable' property), leading to spurious
-              ;; prompts about unsafe vars (bug#68246).
-              (if (and (symbolp key) (autoloadp (indirect-function key)))
-                  (ignore-errors (autoload-do-load (indirect-function key))))
               (let* ((alist (cdr entry))
                      (subdirs (assq 'subdirs alist)))
-                (if (or (not subdirs)
+                (when (or (not subdirs)
                         (progn
                           (setq alist (remq subdirs alist))
                           (cdr-safe subdirs))
@@ -4531,6 +4525,7 @@
                         ;; variables apply to this directory and N levels
                         ;; below it (0 == nil).
                         (equal root (expand-file-name default-directory)))
+                  (dir-locals--load-mode-if-needed key alist)
                     (setq variables (dir-locals-collect-mode-variables
                                      alist variables))))))))
       (error
diff --git a/lisp/progmodes/cc-vars.el b/lisp/progmodes/cc-vars.el
index f0e4c957ea5..305e324747d 100644
--- a/lisp/progmodes/cc-vars.el
+++ b/lisp/progmodes/cc-vars.el
@@ -530,7 +530,7 @@ c-syntactic-indentation
   :type 'boolean
   :group 'c)
 (make-variable-buffer-local 'c-syntactic-indentation)
-(put 'c-syntactic-indentation 'safe-local-variable 'booleanp)
+;;;###autoload(put 'c-syntactic-indentation 'safe-local-variable #'booleanp)
 
 (defcustom c-syntactic-indentation-in-macros t
   "Enable syntactic analysis inside macros.
@@ -550,7 +550,7 @@ c-syntactic-indentation-in-macros
   :type 'boolean
   :group 'c)
 
-(put 'c-syntactic-indentation-in-macros 'safe-local-variable 'booleanp)
+;;;###autoload(put 'c-syntactic-indentation-in-macros 'safe-local-variable #'booleanp)
 
 (defcustom c-defun-tactic 'go-outward
   "Whether functions are recognized inside, e.g., a class.
@@ -1732,7 +1732,7 @@ c-noise-macro-names
   :version "26.1"
   :type '(repeat :tag "List of names" string)
   :group 'c)
-(put 'c-noise-macro-names 'safe-local-variable #'c-string-list-p)
+;;;###autoload(put 'c-noise-macro-names 'safe-local-variable #'c-string-list-p)
 (make-variable-buffer-local 'c-noise-macro-names)
 
 (defcustom c-noise-macro-with-parens-names nil
@@ -1749,7 +1749,7 @@ c-noise-macro-with-parens-names
   :version "26.1"
   :type '(repeat :tag "List of names (possibly empty)" string)
   :group 'c)
-(put 'c-noise-macro-with-parens-names 'safe-local-variable #'c-string-list-p)
+;;;###autoload(put 'c-noise-macro-with-parens-names 'safe-local-variable #'c-string-list-p)
 (make-variable-buffer-local 'c-noise-macro-with-parens-names)
 
 (defun c-make-noise-macro-regexps ()
@@ -1798,8 +1798,7 @@ c-macro-names-with-semicolon
 `c-make-macros-with-semi-re' to set the necessary internal
 variables.")
 (make-variable-buffer-local 'c-macro-names-with-semicolon)
-(put 'c-macro-names-with-semicolon 'safe-local-variable
-     #'c-string-or-string-list-p)
+;;;###autoload(put 'c-macro-names-with-semicolon 'safe-local-variable #'c-string-or-string-list-p)
 
 (defun c-make-macro-with-semi-re ()
   ;; Convert `c-macro-names-with-semicolon' into the regexp






  reply	other threads:[~2024-11-14 16:21 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-13 19:24 bug#74349: 30.0.92; Visiting a file under c-ts-mode loads cc-mode Eli Zaretskii
2024-11-13 20:42 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-11-14  6:35   ` Eli Zaretskii
2024-11-14 16:21     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors [this message]
2024-11-23 12:32       ` Eli Zaretskii
2024-11-24  3:13         ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-12-07 12:20           ` 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=jwvttc9stga.fsf-monnier+emacs@gnu.org \
    --to=bug-gnu-emacs@gnu.org \
    --cc=74349@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /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.