GNU bug report logs - #62339
cc-mode fontifies variables incorrectly when const follows type

Previous Next

Package: emacs;

Reported by: Daniel Colascione <dancol <at> dancol.org>

Date: Tue, 21 Mar 2023 15:17:02 UTC

Severity: normal

Done: Alan Mackenzie <acm <at> muc.de>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 62339 in the body.
You can then email your comments to 62339 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#62339; Package emacs. (Tue, 21 Mar 2023 15:17:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Daniel Colascione <dancol <at> dancol.org>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Tue, 21 Mar 2023 15:17:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Daniel Colascione <dancol <at> dancol.org>
To: bug-gnu-emacs <at> gnu.org
Subject: cc-mode fontifies variables incorrectly when const follows type
Date: Tue, 21 Mar 2023 11:14:13 -0400
cc-mode incorrectly fontifies my_variable as a type in "MyType const
my_variable"




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#62339; Package emacs. (Tue, 21 Mar 2023 17:40:02 GMT) Full text and rfc822 format available.

Message #8 received at 62339 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Daniel Colascione <dancol <at> dancol.org>
Cc: 62339 <at> debbugs.gnu.org
Subject: Re: bug#62339: cc-mode fontifies variables incorrectly when const
 follows type
Date: Tue, 21 Mar 2023 19:39:57 +0200
> From: Daniel Colascione <dancol <at> dancol.org>
> Date: Tue, 21 Mar 2023 11:14:13 -0400
> 
> 
> cc-mode incorrectly fontifies my_variable as a type in "MyType const
> my_variable"

I cannot reproduce this, at least not in Emacs 29 and 30.  In which
version of Emacs do you see this?  If Emacs 29 or later, can you show
a minimal example source file where it happens?

Thanks.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#62339; Package emacs. (Wed, 22 Mar 2023 14:21:02 GMT) Full text and rfc822 format available.

Message #11 received at 62339 <at> debbugs.gnu.org (full text, mbox):

From: Daniel Colascione <dancol <at> dancol.org>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 62339 <at> debbugs.gnu.org
Subject: Re: bug#62339: cc-mode fontifies variables incorrectly when const
 follows type
Date: Wed, 22 Mar 2023 10:19:55 -0400
Eli Zaretskii <eliz <at> gnu.org> writes:

>> From: Daniel Colascione <dancol <at> dancol.org>
>> Date: Tue, 21 Mar 2023 11:14:13 -0400
>> 
>> 
>> cc-mode incorrectly fontifies my_variable as a type in "MyType const
>> my_variable"
>
> I cannot reproduce this, at least not in Emacs 29 and 30.  In which
> version of Emacs do you see this?  If Emacs 29 or later, can you show
> a minimal example source file where it happens?
>
> Thanks.

This problem reproduces for me on latest master with emacs -Q:

```
TEST(Foo, Bar) {
  NamedTemporaryDirectory const test_directory;
}
```




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#62339; Package emacs. (Wed, 22 Mar 2023 15:13:02 GMT) Full text and rfc822 format available.

Message #14 received at 62339 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Daniel Colascione <dancol <at> dancol.org>, Alan Mackenzie <acm <at> muc.de>
Cc: 62339 <at> debbugs.gnu.org
Subject: Re: bug#62339: cc-mode fontifies variables incorrectly when const
 follows type
Date: Wed, 22 Mar 2023 17:12:52 +0200
> From: Daniel Colascione <dancol <at> dancol.org>
> Cc: 62339 <at> debbugs.gnu.org
> Date: Wed, 22 Mar 2023 10:19:55 -0400
> 
> This problem reproduces for me on latest master with emacs -Q:
> 
> ```
> TEST(Foo, Bar) {
>   NamedTemporaryDirectory const test_directory;
> }
> ```

Thanks.  What I see with Emacs built from master is that
test_directory in the above example gets font-lock-type-face in
c++-mode (but not in c-mode).  With Emacs built from emacs-29, both
modes produce correct fontification.

Alan, can you please look into this?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#62339; Package emacs. (Wed, 22 Mar 2023 22:16:01 GMT) Full text and rfc822 format available.

Message #17 received at 62339 <at> debbugs.gnu.org (full text, mbox):

From: Alan Mackenzie <acm <at> muc.de>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 62339 <at> debbugs.gnu.org, acm <at> muc.de, Daniel Colascione <dancol <at> dancol.org>
Subject: Re: bug#62339: cc-mode fontifies variables incorrectly when const
 follows type
Date: Wed, 22 Mar 2023 22:14:12 +0000
Hello, Eli and Daniel.

Thanks for the bug report!

On Wed, Mar 22, 2023 at 17:12:52 +0200, Eli Zaretskii wrote:
> > From: Daniel Colascione <dancol <at> dancol.org>
> > Cc: 62339 <at> debbugs.gnu.org
> > Date: Wed, 22 Mar 2023 10:19:55 -0400

> > This problem reproduces for me on latest master with emacs -Q:

> > ```
> > TEST(Foo, Bar) {
> >   NamedTemporaryDirectory const test_directory;
> > }
> > ```

> Thanks.  What I see with Emacs built from master is that
> test_directory in the above example gets font-lock-type-face in
> c++-mode (but not in c-mode).  With Emacs built from emacs-29, both
> modes produce correct fontification.

> Alan, can you please look into this?

I think this is caused by a faulty fix of bug #59267, where "typeless" C
declarations like

    const foo;

, which are implicit int, were not being recognised.

In the current test case, in C Mode, NamedTemporaryDirectory ought to be
getting fontified as a type, but isn't.  In C++ Mode, test_directory
gets spuriously fontified as a type, as already noted.

Seeing as how the current bug affects only the master branch, not the
emacs-29 branch, I think I'm going to take a bit of time to fix this bug
in the hope of fixing it properly this time.

-- 
Alan Mackenzie (Nuremberg, Germany).




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#62339; Package emacs. (Wed, 22 Mar 2023 22:39:02 GMT) Full text and rfc822 format available.

Message #20 received at 62339 <at> debbugs.gnu.org (full text, mbox):

From: Daniel Colascione <dancol <at> dancol.org>
To: Alan Mackenzie <acm <at> muc.de>
Cc: 62339 <at> debbugs.gnu.org, Eli Zaretskii <eliz <at> gnu.org>
Subject: Re: bug#62339: cc-mode fontifies variables incorrectly when const
 follows type
Date: Wed, 22 Mar 2023 18:17:05 -0400
Alan Mackenzie <acm <at> muc.de> writes:

> Hello, Eli and Daniel.
>
> Thanks for the bug report!
>
> On Wed, Mar 22, 2023 at 17:12:52 +0200, Eli Zaretskii wrote:
>> > From: Daniel Colascione <dancol <at> dancol.org>
>> > Cc: 62339 <at> debbugs.gnu.org
>> > Date: Wed, 22 Mar 2023 10:19:55 -0400
>
>> > This problem reproduces for me on latest master with emacs -Q:
>
>> > ```
>> > TEST(Foo, Bar) {
>> >   NamedTemporaryDirectory const test_directory;
>> > }
>> > ```
>
>> Thanks.  What I see with Emacs built from master is that
>> test_directory in the above example gets font-lock-type-face in
>> c++-mode (but not in c-mode).  With Emacs built from emacs-29, both
>> modes produce correct fontification.
>
>> Alan, can you please look into this?
>
> I think this is caused by a faulty fix of bug #59267, where "typeless" C
> declarations like
>
>     const foo;
>
> , which are implicit int, were not being recognised.

Can we back out this fix in the meantime? These "typeless" declarations
can't be all that common.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#62339; Package emacs. (Thu, 23 Mar 2023 00:44:02 GMT) Full text and rfc822 format available.

Message #23 received at 62339 <at> debbugs.gnu.org (full text, mbox):

From: Po Lu <luangruo <at> yahoo.com>
To: Daniel Colascione <dancol <at> dancol.org>
Cc: 62339 <at> debbugs.gnu.org, Alan Mackenzie <acm <at> muc.de>,
 Eli Zaretskii <eliz <at> gnu.org>
Subject: Re: bug#62339: cc-mode fontifies variables incorrectly when const
 follows type
Date: Thu, 23 Mar 2023 08:42:48 +0800
Daniel Colascione <dancol <at> dancol.org> writes:

> Can we back out this fix in the meantime?

No.

> These "typeless" declarations can't be all that common.

I see them more or less every day.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#62339; Package emacs. (Thu, 23 Mar 2023 02:03:01 GMT) Full text and rfc822 format available.

Message #26 received at 62339 <at> debbugs.gnu.org (full text, mbox):

From: Daniel Colascione <dancol <at> dancol.org>
To: Po Lu <luangruo <at> yahoo.com>
Cc: 62339 <at> debbugs.gnu.org, Alan Mackenzie <acm <at> muc.de>,
 Eli Zaretskii <eliz <at> gnu.org>
Subject: Re: bug#62339: cc-mode fontifies variables incorrectly when const
 follows type
Date: Wed, 22 Mar 2023 22:02:22 -0400
[Message part 1 (text/plain, inline)]

On March 22, 2023 20:43:02 Po Lu <luangruo <at> yahoo.com> wrote:

> Daniel Colascione <dancol <at> dancol.org> writes:
>
>> Can we back out this fix in the meantime?
>
> No.

Yes, we can  We lived with the old behavior for decades. We can live with 
it a few weeks longer.

>
>
>> These "typeless" declarations can't be all that common.
>
> I see them more or less every day.

Give me a break. No, you don't. Practically nobody uses this obscure and 
deprecated C feature. If people did, we'd have addressed the problem years ago.
[Message part 2 (text/html, inline)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#62339; Package emacs. (Thu, 23 Mar 2023 05:46:01 GMT) Full text and rfc822 format available.

Message #29 received at 62339 <at> debbugs.gnu.org (full text, mbox):

From: Po Lu <luangruo <at> yahoo.com>
To: Daniel Colascione <dancol <at> dancol.org>
Cc: 62339 <at> debbugs.gnu.org, Alan Mackenzie <acm <at> muc.de>,
 Eli Zaretskii <eliz <at> gnu.org>
Subject: Re: bug#62339: cc-mode fontifies variables incorrectly when const
 follows type
Date: Thu, 23 Mar 2023 13:45:21 +0800
Daniel Colascione <dancol <at> dancol.org> writes:

> Yes, we can  We lived with the old behavior for decades. We can live with it a few weeks longer.

I reported this bug because I saw such code in use.

> Give me a break. No, you don't.

I do.  I *see* code making use of implicit int every day.

> Practically nobody uses this obscure and deprecated C feature. If
> people did, we'd have addressed the problem years ago.

Who does ``we'' refer to?

Put bluntly, you have no right to decide what C code I see or write, or
what C code Alan wants to support.  Declarations consisting of only a
storage class specifier are part of the 1989 C Standard.

This bug is no more important than bug#59267, and both will have to be
fixed and stay fixed.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#62339; Package emacs. (Thu, 23 Mar 2023 07:23:01 GMT) Full text and rfc822 format available.

Message #32 received at 62339 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Po Lu <luangruo <at> yahoo.com>
Cc: 62339 <at> debbugs.gnu.org, acm <at> muc.de, dancol <at> dancol.org
Subject: Re: bug#62339: cc-mode fontifies variables incorrectly when const
 follows type
Date: Thu, 23 Mar 2023 09:22:15 +0200
> From: Po Lu <luangruo <at> yahoo.com>
> Cc: Alan Mackenzie <acm <at> muc.de>,  <62339 <at> debbugs.gnu.org>,  Eli Zaretskii
>  <eliz <at> gnu.org>
> Date: Thu, 23 Mar 2023 13:45:21 +0800
> 
> Daniel Colascione <dancol <at> dancol.org> writes:
> 
> > Yes, we can  We lived with the old behavior for decades. We can live with it a few weeks longer.
> 
> I reported this bug because I saw such code in use.
> 
> > Give me a break. No, you don't.
> 
> I do.  I *see* code making use of implicit int every day.
> 
> > Practically nobody uses this obscure and deprecated C feature. If
> > people did, we'd have addressed the problem years ago.
> 
> Who does ``we'' refer to?
> 
> Put bluntly, you have no right to decide what C code I see or write, or
> what C code Alan wants to support.  Declarations consisting of only a
> storage class specifier are part of the 1989 C Standard.
> 
> This bug is no more important than bug#59267, and both will have to be
> fixed and stay fixed.

This argument is pointless.  Getting "blunt" (a.k.a. "rude") over that
is certainly uncalled for.  Anyone can make a local change in the
relevant Lisp code if what happens in the repository doesn't suit
him/her.  Making a local copy of whatever cc-*.el file which causes
this is very easy, and won't even cause any merge conflicts in the
future, if you want to avoid it.  Or make a local branch with that
change and periodically merge from master.  Or any other of the
possible solutions.

So please don't continue arguing whose use case is more important.
Alan will decide whether backing out the offending "fix" is
reasonable, and people who track the master branch should live with
that until a better solution is found.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#62339; Package emacs. (Thu, 23 Mar 2023 14:41:02 GMT) Full text and rfc822 format available.

Message #35 received at 62339 <at> debbugs.gnu.org (full text, mbox):

From: Alan Mackenzie <acm <at> muc.de>
To: Daniel Colascione <dancol <at> dancol.org>
Cc: 62339 <at> debbugs.gnu.org, Eli Zaretskii <eliz <at> gnu.org>
Subject: Re: bug#62339: cc-mode fontifies variables incorrectly when const
 follows type
Date: Thu, 23 Mar 2023 14:40:23 +0000
Hello, Daniel.

On Wed, Mar 22, 2023 at 18:17:05 -0400, Daniel Colascione wrote:

> Alan Mackenzie <acm <at> muc.de> writes:

> > Hello, Eli and Daniel.

> > Thanks for the bug report!

> > On Wed, Mar 22, 2023 at 17:12:52 +0200, Eli Zaretskii wrote:
> >> > From: Daniel Colascione <dancol <at> dancol.org>
> >> > Cc: 62339 <at> debbugs.gnu.org
> >> > Date: Wed, 22 Mar 2023 10:19:55 -0400

> >> > This problem reproduces for me on latest master with emacs -Q:

> >> > ```
> >> > TEST(Foo, Bar) {
> >> >   NamedTemporaryDirectory const test_directory;
> >> > }
> >> > ```

> >> Thanks.  What I see with Emacs built from master is that
> >> test_directory in the above example gets font-lock-type-face in
> >> c++-mode (but not in c-mode).  With Emacs built from emacs-29, both
> >> modes produce correct fontification.

> >> Alan, can you please look into this?

> > I think this is caused by a faulty fix of bug #59267, where "typeless" C
> > declarations like

> >     const foo;

> > , which are implicit int, were not being recognised.

> Can we back out this fix in the meantime? These "typeless" declarations
> can't be all that common.

It turned out that the bug was caused by a single missing line of code, a 
(c-forward-syntactic-ws) in c-forward-type, so the fix wasn't too
difficult.  I've taken the opportunity also to fix some minor other
innaccuracies in c-forward-type.

Please try the following patch, and either confirm to me that it appears
to fix the bug, or say what's still wrong.  Thanks!



diff -r 300ebf19cd62 cc-engine.el
--- a/cc-engine.el	Fri Feb 17 08:58:11 2023 +0000
+++ b/cc-engine.el	Thu Mar 23 14:34:08 2023 +0000
@@ -9143,7 +9143,7 @@
     (c-forward-syntactic-ws))
 
   (let ((start (point)) pos res name-res id-start id-end id-range
-	post-prefix-pos)
+	post-prefix-pos prefix-end-pos)
 
     ;; Skip leading type modifiers.  If any are found we know it's a
     ;; prefix of a type.
@@ -9153,6 +9153,7 @@
 	  (when (looking-at c-no-type-key)
 	    (setq res 'no-id)))
 	(goto-char (match-end 1))
+	(setq prefix-end-pos (point))
 	(setq pos (point))
 	(c-forward-syntactic-ws)
 	(or (eq res 'no-id)
@@ -9304,7 +9305,10 @@
 		  (not (looking-at c-type-decl-prefix-key)))))
       ;; A C specifier followed by an implicit int, e.g.
       ;; "register count;"
-      (goto-char id-start)
+      (goto-char prefix-end-pos)
+      (setq pos (point))
+      (unless stop-at-end
+	(c-forward-syntactic-ws))
       (setq res 'no-id))
 
      (name-res
@@ -9312,6 +9316,7 @@
 	     ;; A normal identifier.
 	     (goto-char id-end)
 	     (setq pos (point))
+	     (c-forward-syntactic-ws)
 	     (if (or res c-promote-possible-types)
 		 (progn
 		   (when (not (eq c-promote-possible-types 'just-one))
@@ -9319,7 +9324,9 @@
 		   (when (and c-record-type-identifiers id-range)
 		     (c-record-type-id id-range))
 		   (unless res
-		     (setq res 'found)))
+		     (setq res 'found))
+		   (when (eq res 'prefix)
+		     (setq res t)))
 	       (setq res (if (c-check-qualified-type id-start)
 			     ;; It's an identifier that has been used as
 			     ;; a type somewhere else.


-- 
Alan Mackenzie (Nuremberg, Germany).




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#62339; Package emacs. (Wed, 05 Apr 2023 15:54:02 GMT) Full text and rfc822 format available.

Message #38 received at 62339 <at> debbugs.gnu.org (full text, mbox):

From: Alan Mackenzie <acm <at> muc.de>
To: Daniel Colascione <dancol <at> dancol.org>
Cc: 62339 <at> debbugs.gnu.org, Eli Zaretskii <eliz <at> gnu.org>
Subject: Re: bug#62339: cc-mode fontifies variables incorrectly when const
 follows type
Date: Wed, 5 Apr 2023 15:53:47 +0000
Hello, Daniel.

On Thu, Mar 23, 2023 at 14:40:23 +0000, Alan Mackenzie wrote:
> On Wed, Mar 22, 2023 at 18:17:05 -0400, Daniel Colascione wrote:

> > >> > This problem reproduces for me on latest master with emacs -Q:

> > >> > ```
> > >> > TEST(Foo, Bar) {
> > >> >   NamedTemporaryDirectory const test_directory;
> > >> > }
> > >> > ```

> > >> Thanks.  What I see with Emacs built from master is that
> > >> test_directory in the above example gets font-lock-type-face in
> > >> c++-mode (but not in c-mode).  With Emacs built from emacs-29, both
> > >> modes produce correct fontification.

> > >> Alan, can you please look into this?

[ .... ]

> It turned out that the bug was caused by a single missing line of code, a 
> (c-forward-syntactic-ws) in c-forward-type, so the fix wasn't too
> difficult.  I've taken the opportunity also to fix some minor other
> innaccuracies in c-forward-type.

> Please try the following patch, and either confirm to me that it appears
> to fix the bug, or say what's still wrong.  Thanks!

Ping?

-- 
Alan Mackenzie (Nuremberg, Germany).




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#62339; Package emacs. (Thu, 06 Apr 2023 03:17:02 GMT) Full text and rfc822 format available.

Message #41 received at 62339 <at> debbugs.gnu.org (full text, mbox):

From: Daniel Colascione <dancol <at> dancol.org>
To: Alan Mackenzie <acm <at> muc.de>
Cc: 62339 <at> debbugs.gnu.org, Eli Zaretskii <eliz <at> gnu.org>
Subject: Re: bug#62339: cc-mode fontifies variables incorrectly when const
 follows type
Date: Wed, 05 Apr 2023 23:16:16 -0400
[Message part 1 (text/plain, inline)]

On April 5, 2023 11:53:54 Alan Mackenzie <acm <at> muc.de> wrote:

> Hello, Daniel.
>
> On Thu, Mar 23, 2023 at 14:40:23 +0000, Alan Mackenzie wrote:
>> On Wed, Mar 22, 2023 at 18:17:05 -0400, Daniel Colascione wrote:
>
>>>>>> This problem reproduces for me on latest master with emacs -Q:
>
>>>>>> ```
>>>>>> TEST(Foo, Bar) {
>>>>>> NamedTemporaryDirectory const test_directory;
>>>>>> }
>>>>>> ```
>
>>>>> Thanks.  What I see with Emacs built from master is that
>>>>> test_directory in the above example gets font-lock-type-face in
>>>>> c++-mode (but not in c-mode).  With Emacs built from emacs-29, both
>>>>> modes produce correct fontification.
>
>>>>> Alan, can you please look into this?
>
> [ .... ]
>
>> It turned out that the bug was caused by a single missing line of code, a
>> (c-forward-syntactic-ws) in c-forward-type, so the fix wasn't too
>> difficult.  I've taken the opportunity also to fix some minor other
>> innaccuracies in c-forward-type.
>
>> Please try the following patch, and either confirm to me that it appears
>> to fix the bug, or say what's still wrong.  Thanks!
>
> Ping?

Works for me. Thanks!


>
> --
> Alan Mackenzie (Nuremberg, Germany).

[Message part 2 (text/html, inline)]

Reply sent to Alan Mackenzie <acm <at> muc.de>:
You have taken responsibility. (Thu, 06 Apr 2023 09:28:02 GMT) Full text and rfc822 format available.

Notification sent to Daniel Colascione <dancol <at> dancol.org>:
bug acknowledged by developer. (Thu, 06 Apr 2023 09:28:02 GMT) Full text and rfc822 format available.

Message #46 received at 62339-done <at> debbugs.gnu.org (full text, mbox):

From: Alan Mackenzie <acm <at> muc.de>
To: Daniel Colascione <dancol <at> dancol.org>
Cc: 62339-done <at> debbugs.gnu.org, acm <at> muc.de, Eli Zaretskii <eliz <at> gnu.org>
Subject: Re: bug#62339: cc-mode fontifies variables incorrectly when const
 follows type
Date: Thu, 6 Apr 2023 09:26:53 +0000
Hello, Daniel.

On Wed, Apr 05, 2023 at 23:16:16 -0400, Daniel Colascione wrote:


> On April 5, 2023 11:53:54 Alan Mackenzie <acm <at> muc.de> wrote:

> > On Thu, Mar 23, 2023 at 14:40:23 +0000, Alan Mackenzie wrote:

[ .... ]

> >> Please try the following patch, and either confirm to me that it
> >> appears to fix the bug, or say what's still wrong.  Thanks!

> > Ping?

> Works for me. Thanks!

Thanks!  I've committed the patch and I'm closing the bug with this post.

-- 
Alan Mackenzie (Nuremberg, Germany).




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Thu, 04 May 2023 11:24:06 GMT) Full text and rfc822 format available.

This bug report was last modified 329 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.