GNU bug report logs - #72695
CC Mode 5.35.2 (C++//l); c++-mode misindents 'if constexpr' inside lambdas

Previous Next

Package: cc-mode;

Reported by: Arsen Arsenović <arsen <at> aarsen.me>

Date: Sun, 18 Aug 2024 09:52:02 UTC

Severity: normal

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

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: Arsen Arsenović <arsen <at> aarsen.me>
To: Alan Mackenzie <acm <at> muc.de>
Cc: 72695 <at> debbugs.gnu.org
Subject: bug#72695: CC Mode 5.35.2 (C++//l); c++-mode misindents 'if constexpr' inside lambdas
Date: Wed, 21 Aug 2024 22:40:02 +0200
[Message part 1 (text/plain, inline)]
Hello Alan,

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

> Hello, Arsen.
>
> Thanks for the bug report!

My pleasure.  Thanks for being as fast as always.

> On Sun, Aug 18, 2024 at 11:50:32 +0200, Arsen Arsenović via CC-Mode-help wrote:
>> Package: cc-mode
>
>> <#secure method=pgpmime mode=sign>
>> Package: cc-mode
>> Hi!
>
>> CC-mode appears to misindent the following C++ snippet:
>
>>   void f()
>>   {
>>     auto data_printer = [] <typename T>
>>       (const T& data)
>>       {
>>         if constexpr (true)
>>         {
>>           if constexpr (std::same_as<T, std::string>)
>>           std::println (" data: \"{}\"", data);
>>           else
>>             std::println (" data: {}", data);
>>         }
>>         else
>>           std::println ("");
>>       };
>   
>>     if constexpr (false)
>>       {
>>         foo ();
>>       }
>>   }
>
>> This example is supposed to be formatted in accordance to the default
>> GNU style, but the 'if constexpr' in the lambda expression are
>> misindented.
>
> Yes.
>
>> This might be the case because the inline-open of the lambda gets
>> mis-interpreted as statement-cont, so the statements in the body are
>> indented as brace-list-intro and brace-entry-open rather than
>> statement-block-intro and substatement-open.
>
> It's definitely something to do with the lambda function.  If I comment
> out Lines 3 and 4, the remainder of the function then indents correctly.
> As you remark, at the moment some lines are being wrongly analysed as
> brace-list constructs.
>
>> WRT lambda indentation generally, CC-mode appears to format lambdas in
>> GNU style so that their '{' aligns with their '[' (i.e. aligns their
>> compound-statement with their lambda-introducer per ISO parlance).
>> Maybe we should consider indenting them less. ....
>
> OK.  I'll fix the "brace-list" indentation bug first, then come back to
> this lambda indentation problem.

Thank you very much!

In the in-between time since I've sent this bug report with a feature
request snuck in, I've noticed the following in the GCC code:

  stattype = (cp_build_type_attribute_variant
	      (stattype, TYPE_ATTRIBUTES (optype)));

... which clued me into a different way to get the effect Lisp had, so I
figured out I can use the same trick for nested multi-line constructs:

    (std::visit
     ([] <typename T>
      (const T& data)
      {
        if constexpr (!std::same_as<T, std::monostate>)
          {
            if constexpr (std::same_as<T, std::string>)
              /* TODO(arsen): use {:?} here  */
              std::println (" data: \"{}\"", data);
            else
              std::println (" data: {}", data);
          }
        else
          std::println ("");
      }, tok.data));
  }

... which is an okay immitation of what Lisp does, and seems to provide
okay indentation here.  So maybe it's best not to avoid messing with
peoples styles ;) - please do not worry about the latter part of my
original message.

Thank you again!  Have a lovely evening.
-- 
Arsen Arsenović
[signature.asc (application/pgp-signature, inline)]

This bug report was last modified 294 days ago.

Previous Next


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