Subject: bug#48837: Interaction between completion-moide and cc-mode new in 27.2

From: Alan Mackenzie <[email-masked]>
Date: Sun, 20 Jun 2021 18:43:33 +0000

Hello, John.

Thanks for taking the trouble to report this bug, and thanks even more
for cutting the test case down to an easy to work with minimum.

On Fri, Jun 04, 2021 at 16:13:47 -0400, John C. Ruttenberg wrote:
> This bug occurs in 27.2 but not in 26.3

> In a clean emacs "emacs -q --no-site-file":
> 1. Enable dynamic completion "(dynamic-completion-mode)"
> 2. Open a c++ source file
> 3. Text should be:

> foo() {
>   std::cerr <
>   int i = static_cast<unsigned>(z);
> }

>    4. Position the cursor directly after the "<" and type "< " (two
> characters "<" " "
>    5. I get this error message: "Scan error: containing expression ends
> prematurely, 22, 22"

The cause of this was that the "<" had wrongly been marked as an opening
angle bracket.  A backward `scan-sexps' Lisp instruction bumped into
this barrier of an angle bracket and gave the error.

The solution is to correct the reason for the spurious marking.  That
was that "static_cast" (along with three other newish ..._cast keywords)
weren't being properly handled.  This should be corrected in the
supplied patch (which actually fixes a bit more).

Would you please apply the following patch then byte-compile CC Mode in
its entirety (as a Lisp macro has been changed).  CC Mode is in
..../emacs/lisp/progmodes/cc-*.el

Then please load the new CC Mode into your Emacs 27.2, test it on your
real source code, and either confirm that the bug is fixed, or tell me
where it is still going wrong.  If you want any help with the patching
or byte compiling, feel free to send me private email.  Thanks!

diff -r 92a4592886a1 cc-engine.el
--- a/cc-engine.el	Sun Apr 25 17:26:38 2021 +0000
+++ b/cc-engine.el	Sun Jun 20 18:23:16 2021 +0000
@@ -6965,8 +6965,10 @@
 	(c-go-list-forward))
       (when (equal (c-get-char-property (1- (point)) 'syntax-table)
 		   c->-as-paren-syntax) ; should always be true.
-	(c-unmark-<->-as-paren (1- (point))))
-      (c-unmark-<->-as-paren pos))))
+	(c-unmark-<->-as-paren (1- (point)))
+	(c-truncate-lit-pos-cache (1- (point))))
+      (c-unmark-<->-as-paren pos)
+      (c-truncate-lit-pos-cache pos))))
 
 (defun c-clear->-pair-props (&optional pos)
   ;; POS (default point) is at a > character.  If it is marked with
@@ -6982,8 +6984,10 @@
 	(c-go-up-list-backward))
       (when (equal (c-get-char-property (point) 'syntax-table)
 			c-<-as-paren-syntax) ; should always be true.
-	(c-unmark-<->-as-paren (point)))
-      (c-unmark-<->-as-paren pos))))
+	(c-unmark-<->-as-paren (point))
+	(c-truncate-lit-pos-cache (point)))
+      (c-unmark-<->-as-paren pos)
+      (c-truncate-lit-pos-cache pos))))
 
 (defun c-clear-<>-pair-props (&optional pos)
   ;; POS (default point) is at a < or > character.  If it has an
@@ -7016,7 +7020,8 @@
 		 (equal (c-get-char-property (1- (point)) 'syntax-table)
 			c->-as-paren-syntax)) ; should always be true.
 	(c-unmark-<->-as-paren (1- (point)))
-	(c-unmark-<->-as-paren pos))
+	(c-unmark-<->-as-paren pos)
+	(c-truncate-lit-pos-cache pos))
       t)))
 
 (defun c-clear->-pair-props-if-match-before (lim &optional pos)
@@ -7037,6 +7042,7 @@
 		 (equal (c-get-char-property (point) 'syntax-table)
 			c-<-as-paren-syntax)) ; should always be true.
 	(c-unmark-<->-as-paren (point))
+	(c-truncate-lit-pos-cache (point))
 	(c-unmark-<->-as-paren pos))
       t)))
 
@@ -8078,13 +8084,14 @@
 	;; bracket arglist.  It's propagated through the return value
 	;; on successful completion.
 	(c-record-found-types c-record-found-types)
+	(syntax-table-prop-on-< (c-get-char-property (point) 'syntax-table))
 	;; List that collects the positions after the argument
 	;; separating ',' in the arglist.
 	arg-start-pos)
     ;; If the '<' has paren open syntax then we've marked it as an angle
     ;; bracket arglist before, so skip to the end.
     (if (and (not c-parse-and-markup-<>-arglists)
-	     (c-get-char-property (point) 'syntax-table))
+	     syntax-table-prop-on-<)
 
 	(progn
 	  (forward-char)
@@ -8167,8 +8174,20 @@
 			(c-put-c-type-property (1- (car arg-start-pos))
 					       'c-<>-arg-sep)
 			(setq arg-start-pos (cdr arg-start-pos)))
+		      (when (and (not syntax-table-prop-on-<)
+				 (c-get-char-property (1- (point))
+						      'syntax-table))
+			;; Clear the now spuriously matching < of its
+			;; syntax-table property.  This could happen on
+			;; inserting "_cast" into "static <" with C-y.
+			(save-excursion
+			  (and (c-go-list-backward)
+			       (eq (char-after) ?<)
+			       (c-truncate-lit-pos-cache (point))
+			       (c-unmark-<->-as-paren (point)))))
 		      (c-mark-<-as-paren start)
-		      (c-mark->-as-paren (1- (point))))
+		      (c-mark->-as-paren (1- (point)))
+		      (c-truncate-lit-pos-cache start))
 		    (setq res t)
 		    nil))		; Exit the loop.
 
diff -r 92a4592886a1 cc-langs.el
--- a/cc-langs.el	Sun Apr 25 17:26:38 2021 +0000
+++ b/cc-langs.el	Sun Jun 20 18:23:16 2021 +0000
@@ -2730,7 +2730,8 @@
 `c-recognize-<>-arglists' for details.  That language constant is
 assumed to be set if this isn't nil."
   t    nil
-  c++  '("template")
+  c++  '("template" "const_cast" "dynamic_cast" "reinterpret_cast"
+	 "static_cast")
   idl  '("fixed" "string" "wstring"))
 
 (c-lang-defconst c-<>-sexp-kwds

> In GNU Emacs 27.2 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.20)
>  of 2021-06-02 built on catb
> Windowing system distributor 'AT&T Laboratories Cambridge', version
> 11.0.3332
> System Description: Ubuntu 20.04.2 LTS

[ .... ]

-- 
Alan Mackenzie (Nuremberg, Germany).