Subject: bug#74349: 30.0.92; Visiting a file under c-ts-mode loads cc-mode

From: Eli Zaretskii <[email-masked]>
Date: Sat, 23 Nov 2024 14:32:14 +0200

> Cc: [email-masked]
> From: Stefan Monnier <[email-masked]>
> Date: Thu, 14 Nov 2024 11:21:49 -0500
> 
> >> 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.

I think you should install this on master, thanks.

> 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).

Alan, any comments?