Subject: 28.0.50; inserting too many lines into a fresh cpp file breaks the buffer

From: Paul Nelson <[email-masked]>
Date: Thu, 29 Apr 2021 10:04:36 +0200

Inserting a sufficiently large chunk of text into a fresh cpp file
breaks syntax highlighting and causes the buffer to enter a
semi-permanent broken state.  Moreover, the offending function
"c-guess-basic-syntax" seems to break edebug instrumentation.

In more detail:

Start from emacs -Q.  Insert sufficiently many lines of C++ code in a
temporary buffer.  A slightly contrived example:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cfloat>
#include <vector>
#include <cmath>
#include <set>
#include <list>
#include <map>
#include <stack>
#include <queue>
#include <iostream>
#include <sstream>
#include <fstream>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cfloat>
#include <vector>
#include <cmath>
#include <set>
#include <list>
#include <map>
#include <stack>

Copy these lines to the kill ring.  Create a new file "test.cpp", and
paste what was copied.

The first error is that syntax highlighting stops after a certain
number of lines (in the above example, just before the final line).

The same problem arises if such lines are inserted via abbrev-mode.
This is the intended use case.

No problem if such lines are copy/pasted in small enough chunks.

The problem *appears* to go away if one cuts and then pastes the
offending lines, but the buffer remains in a broken state.  For
instance, attempting to insert a couple newlines followed by
  int main(
gives the following error:

Debugger entered--Lisp error: (wrong-type-argument number-or-marker-p nil)
  c-guess-basic-syntax()
  c-indent-line()
  indent-according-to-mode()
  c-electric-paren(nil)
  funcall-interactively(c-electric-paren nil)
  command-execute(c-electric-paren)

Incidentally, "C-u C-M-x" applied to c-guess-basic-syntax gives a long and
complicated backtrace that starts with:

Debugger entered--Lisp error: (invalid-read-syntax "Expected" "lambda")
  edebug-syntax-error("Expected" "lambda")

---

In GNU Emacs 28.0.50 (build 2, x86_64-apple-darwin19.6.0, NS appkit-1894.60
Version 10.15.7 (Build 19H524))
 of 2021-04-26 built on Pauls-MBP-2
Repository revision: 9d34fd8b33c55768190d41239931120e3fc9717f
Repository branch: master
Windowing system distributor 'Apple', version 10.3.1894
System Description:  Mac OS X 10.15.7

Configured using:
 'configure --with-native-compilation'

Configured features:
ACL DBUS GMP GNUTLS JPEG LCMS2 LIBXML2 MODULES NATIVE_COMP NOTIFY KQUEUE
NS PDUMPER PNG THREADS TIFF TOOLKIT_SCROLL_BARS XIM ZLIB

Important settings:
  value of $LC_CTYPE: UTF-8
  value of $LANG: en_CH.UTF-8
  locale-coding-system: utf-8-unix

Major mode: C++//l

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t
  abbrev-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message rmc puny dired dired-loaddefs
rfc822 mml mml-sec epa derived epg epg-config gnus-util rmail
rmail-loaddefs auth-source eieio eieio-core eieio-loaddefs
password-cache json map text-property-search time-date mm-decode
mm-bodies mm-encode mail-parse rfc2231 mailabbrev gmm-utils mailheader
sendmail rfc2047 rfc2045 ietf-drums mm-util mail-prsvr mail-utils novice
comp comp-cstr warnings subr-x rx cl-seq cl-macs cl-extra help-mode seq
byte-opt gv bytecomp byte-compile cconv cc-mode cc-fonts cc-guess
cc-menus cc-cmds cc-styles cc-align cc-engine cc-vars cc-defs
cl-loaddefs cl-lib iso-transl tooltip eldoc electric uniquify ediff-hook
vc-hooks lisp-float-type mwheel term/ns-win ns-win ucs-normalize
mule-util term/common-win tool-bar dnd fontset image regexp-opt fringe
tabulated-list replace newcomment text-mode elisp-mode lisp-mode
prog-mode register page tab-bar menu-bar rfn-eshadow isearch easymenu
timer select scroll-bar mouse jit-lock font-lock syntax font-core
term/tty-colors frame minibuffer cl-generic cham georgian utf-8-lang
misc-lang vietnamese tibetan thai tai-viet lao korean japanese eucjp-ms
cp51932 hebrew greek romanian slovak czech european ethiopic indian
cyrillic chinese composite charscript charprop case-table epa-hook
jka-cmpr-hook help simple abbrev obarray cl-preloaded nadvice button
loaddefs faces cus-face macroexp files window text-properties overlay
sha1 md5 base64 format env code-pages mule custom widget
hashtable-print-readable backquote threads dbusbind kqueue cocoa ns
lcms2 multi-tty make-network-process nativecomp emacs)

Memory information:
((conses 16 126796 8572)
 (symbols 48 11546 0)
 (strings 32 31421 1656)
 (string-bytes 1 1165707)
 (vectors 16 18464)
 (vector-slots 8 333103 10770)
 (floats 8 30 33)
 (intervals 56 540 0)
 (buffers 992 14))