Subject: bug#69431: 30.0.50; Strange fontificaion behavior

From: Andrea Corallo <[email-masked]>
Date: Sat, 06 Apr 2024 13:01:18 -0400

Okay after "some" debugging my theory is that it is problematic to setup
the fontification while the setup of another fontification is active on
the stack.

This is what is happening here because while we are setting up
fontification for 'test.org' native compilation activated
'emacs-lisp-compilation-mode' on the '*Async-native-compile-log*'
buffer.

If I remove the invocations of 'emacs-lisp-compilation-mode' from
'comp-run-async-workers' my org file gets fontified correctly.

Applying:

==============
modified   lisp/files.el
@@ -2828,6 +2828,13 @@ after-find-file

 (define-obsolete-function-alias 'report-errors 'with-demoted-errors "25.1")

+(defun k-variable-watcher (_symbol newval op _buffer)
+  (when (string= (buffer-name) "test.org")
+    (message "XXX %s" op)
+    (backtrace)))
+
+(add-variable-watcher 'font-lock-keywords #'k-variable-watcher)
+
 (defun normal-mode (&optional find-file)
   "Choose the major mode for this buffer automatically.
 Also sets up any specified local variables of the file or its directory.
==============

The backtrace I see in *Messages* looks like this in the non working
case (cold eln-cache):

==============
XXX set
  backtrace()
  k-variable-watcher(font-lock-keywords ((eval list (or outline-search-function (concat "^\\(?:" outline-regexp "\\).*" outline-heading-end-regexp)) 0 '(if outline-minor-mode (if outline-minor-mode-highlight (list 'face (outline-font-lock-face))) (outline-font-lock-face)) (when outline-minor-mode (pcase outline-minor-mode-highlight ('override t) ('append 'append))) t)) set #<buffer test.org>)
  font-lock-set-defaults()
  font-lock-mode-internal(t)
  font-lock-default-function(t)
  font-lock-mode()
  turn-on-font-lock()
  turn-on-font-lock-if-desired()
  global-font-lock-mode-enable-in-buffers()
  run-hooks(after-change-major-mode-hook)
  run-mode-hooks(emacs-lisp-compilation-mode-hook)
  emacs-lisp-compilation-mode()
  comp-run-async-workers()
  native--compile-async("/home/andcor03/emacs2/lisp/net/dbus.el" nil late)
  apply(native--compile-async ("/home/andcor03/emacs2/lisp/net/dbus.el" nil late))
  read-event(nil nil 0.001)
  dbus-call-method(:system "org.freedesktop.DBus" "/org/freedesktop/DBus" "org.freedesktop.DBus" "AddMatch" "type='signal',interface='org.freedesktop.DBus.Local',member='Disconnected',path='/org/freedesktop/DBus/Local'")
  dbus-register-signal(:system nil "/org/freedesktop/DBus/Local" "org.freedesktop.DBus.Local" "Disconnected" dbus-handle-bus-disconnect)
  dbus-init-bus(:system)
  dbus--init()
  byte-code("\300\301\302\"\210\302 \210\303\304!\207" [add-hook after-pdump-load-hook dbus--init provide dbus] 3)
  require(dbus)
  byte-code("\300\301!\210\300\302!\210\303\304\305\306\307DD\310\311\312\313\314&\7\207" [require gnus dbus custom-declare-variable gnus-dbus-close-on-sleep funcall function #f(compiled-function () #<bytecode -0x1df07492cba9682>) ("/home/andcor03/emacs2/lisp/gnus/gnus-dbus.elc" . 86) :group gnus-dbus :type boolean] 8)
  require(gnus-dbus)
  require(gnus-start)
  require(gnus-group)
  require(gnus-sum)
  require(ol-gnus)
  org-load-modules-maybe()
  org-mode()
  set-auto-mode-0(org-mode nil)
  set-auto-mode()
  normal-mode(t)
  after-find-file(nil t)
  find-file-noselect-1(#<buffer test.org> "~/test.org" nil nil "~/test.org" (14180533 64513))
  find-file-noselect("/home/andcor03/test.org")
  command-line-1(("-eval" "(setq native-comp-jit-compilation t)" "/home/andcor03/test.org"))
  command-line()
  normal-top-level()
==============

Why are we setting 'font-lock-keywords' on test.org if the last major
mode activation we have on the stack is for
'emacs-lisp-compilation-mode'?  IOW why the current buffer is test.org
if 'emacs-lisp-compilation-mode' is called wrapped by
'with-current-buffer' on '*Async-native-compile-log*'?

Also is the value of 'font-lock-keywords' we are setting on test.org the
expected one or are we mixing up things?

Thanks

  Andrea