public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/111352] New: Inconsistent constructor behavior associated with auto lambda
@ 2023-09-08 23:04 peter.fletcher at bandicootimaging dot com.au
  2023-09-08 23:07 ` [Bug c++/111352] " peter.fletcher at bandicootimaging dot com.au
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: peter.fletcher at bandicootimaging dot com.au @ 2023-09-08 23:04 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111352

            Bug ID: 111352
           Summary: Inconsistent constructor behavior associated with auto
                    lambda
           Product: gcc
           Version: 11.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: peter.fletcher at bandicootimaging dot com.au
  Target Milestone: ---

Created attachment 55861
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55861&action=edit
Output of gcc -v -save_temps

When a constructor involving an auto lambda is called in two different
contexts, one triggers a compilation error, one does not.

g++ test_bug3.cpp -o test_bug3 -Wall -Wextra
test_bug3.cpp: In function ‘int main(int, char**)’:
test_bug3.cpp:41:27: error: ‘auto’ parameter not permitted in this context
   41 |     AA aa(Constructor([&](auto& p) { p.x = 1; }));
      |                           ^~~~
Compilation exited abnormally with code 1 at Sat Sep  9 08:50:05

template <typename L> struct Constructor
{
    const L& lambda;

    Constructor(const L& lambda) :
        lambda(lambda)
    {
    }

    template <typename T> operator T () const
    {
        T out;

        lambda(out);

        return out;
    }
};

struct A
{
    int x;
};

struct AA
{
    int x{0};

    AA()
    {
    }

    AA(A op) :
        x(op.x)
    {
    }
};

int main(int argc, char* argv[])
{
    AA aa(Constructor([&](auto& p) { p.x = 1; }));
    AA bb = AA(Constructor([&](auto& p) { p.x = 1; }));

    return aa.x+bb.x+argc+argv[0][0];
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/111352] Inconsistent constructor behavior associated with auto lambda
  2023-09-08 23:04 [Bug c++/111352] New: Inconsistent constructor behavior associated with auto lambda peter.fletcher at bandicootimaging dot com.au
@ 2023-09-08 23:07 ` peter.fletcher at bandicootimaging dot com.au
  2023-09-08 23:12 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: peter.fletcher at bandicootimaging dot com.au @ 2023-09-08 23:07 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111352

Peter Fletcher <peter.fletcher at bandicootimaging dot com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |peter.fletcher@bandicootima
                   |                            |ging.com.au

--- Comment #1 from Peter Fletcher <peter.fletcher at bandicootimaging dot com.au> ---
Created attachment 55862
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55862&action=edit
Source file

Source code

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/111352] Inconsistent constructor behavior associated with auto lambda
  2023-09-08 23:04 [Bug c++/111352] New: Inconsistent constructor behavior associated with auto lambda peter.fletcher at bandicootimaging dot com.au
  2023-09-08 23:07 ` [Bug c++/111352] " peter.fletcher at bandicootimaging dot com.au
@ 2023-09-08 23:12 ` pinskia at gcc dot gnu.org
  2023-09-09  5:53 ` pinskia at gcc dot gnu.org
  2023-09-09  5:55 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-08 23:12 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111352

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
99% sure this is because we start parsing as a function declaration and then
see it is not but we errored out already.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/111352] Inconsistent constructor behavior associated with auto lambda
  2023-09-08 23:04 [Bug c++/111352] New: Inconsistent constructor behavior associated with auto lambda peter.fletcher at bandicootimaging dot com.au
  2023-09-08 23:07 ` [Bug c++/111352] " peter.fletcher at bandicootimaging dot com.au
  2023-09-08 23:12 ` pinskia at gcc dot gnu.org
@ 2023-09-09  5:53 ` pinskia at gcc dot gnu.org
  2023-09-09  5:55 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-09  5:53 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111352

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2023-09-09
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
           Keywords|                            |rejects-valid

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
You can also use this as a workaround:
```
AA aa{Constructor([](auto& p) { p.x = 1; })};
```

Confirmed.
The problem is GCC starts parsing as a function declaration for aa that takes a
type of arrays of function pointers that has a function argument named
Constructor and then noticed the `{` and decides whoops we need to back up. But
it is too late as GCC already error'd for the auto too early on.

Basically GCC errors out on auto too soon (during the parsing) rather
afterwards ...

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/111352] Inconsistent constructor behavior associated with auto lambda
  2023-09-08 23:04 [Bug c++/111352] New: Inconsistent constructor behavior associated with auto lambda peter.fletcher at bandicootimaging dot com.au
                   ` (2 preceding siblings ...)
  2023-09-09  5:53 ` pinskia at gcc dot gnu.org
@ 2023-09-09  5:55 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-09  5:55 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111352

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
also see https://en.wikipedia.org/wiki/Most_vexing_parse for more explanation
on why this mistake in GCC happened.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-09-09  5:55 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-08 23:04 [Bug c++/111352] New: Inconsistent constructor behavior associated with auto lambda peter.fletcher at bandicootimaging dot com.au
2023-09-08 23:07 ` [Bug c++/111352] " peter.fletcher at bandicootimaging dot com.au
2023-09-08 23:12 ` pinskia at gcc dot gnu.org
2023-09-09  5:53 ` pinskia at gcc dot gnu.org
2023-09-09  5:55 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).