public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/100493] New: Lambda default copy capture that captures "this" cannot be used in both C++17 and C++20 modes
@ 2021-05-09 15:19 avi@cloudius-systems.com
  2021-07-21  9:26 ` [Bug c++/100493] " gcc at bmevers dot de
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: avi@cloudius-systems.com @ 2021-05-09 15:19 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 100493
           Summary: Lambda default copy capture that captures "this"
                    cannot be used in both C++17 and C++20 modes
           Product: gcc
           Version: 10.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: avi@cloudius-systems.com
  Target Milestone: ---

Consider the following masterpiece:



struct a {
    int b;
    void f(int x) {
        (void)[=] { (void)(b + x); };
    }
};


The C++20 compiler wants me to disambiguate the [=] capture to mean either [=,
this] or [=, *this]:

<source>:6:15: warning: implicit capture of 'this' via '[=]' is deprecated in
C++20 [-Wdeprecated]
    6 |         (void)[=] { (void)(b + x); };
      |               ^
<source>:6:15: note: add explicit 'this' or '*this' capture

Humoring it, I add ", this" to the capture list:


struct a {
    int b;
    void f(int x) {
        (void)[=, this] { (void)(b + x); };
    }
};


This works in C++20 mode, but in C++17 mode, I get:

source>:6:19: warning: explicit by-copy capture of 'this' redundant with
by-copy capture default
    6 |         (void)[=, this] { (void)(b + x); };
      |                   ^~~~


These warnings are either always on, or enabled by -Wdeprecated. Given I want
-Wdeprecated for other deprecated features, it's impossible for me to write
similar code that works in both C++17 and C++20.

Perhaps the C++20 warning can be moved to a separate flag, to allow library
code that targets both C++17 and C++20.

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

* [Bug c++/100493] Lambda default copy capture that captures "this" cannot be used in both C++17 and C++20 modes
  2021-05-09 15:19 [Bug c++/100493] New: Lambda default copy capture that captures "this" cannot be used in both C++17 and C++20 modes avi@cloudius-systems.com
@ 2021-07-21  9:26 ` gcc at bmevers dot de
  2021-07-21 15:36 ` redi at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: gcc at bmevers dot de @ 2021-07-21  9:26 UTC (permalink / raw)
  To: gcc-bugs

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

Benno Evers <gcc at bmevers dot de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |gcc at bmevers dot de

--- Comment #1 from Benno Evers <gcc at bmevers dot de> ---
I can confirm this issue; working around it was the biggest single piece of
work we had to do when upgrading our codebase to C++20.

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

* [Bug c++/100493] Lambda default copy capture that captures "this" cannot be used in both C++17 and C++20 modes
  2021-05-09 15:19 [Bug c++/100493] New: Lambda default copy capture that captures "this" cannot be used in both C++17 and C++20 modes avi@cloudius-systems.com
  2021-07-21  9:26 ` [Bug c++/100493] " gcc at bmevers dot de
@ 2021-07-21 15:36 ` redi at gcc dot gnu.org
  2021-09-22 14:13 ` redbeard0531 at gmail dot com
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2021-07-21 15:36 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2021-07-21
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

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

* [Bug c++/100493] Lambda default copy capture that captures "this" cannot be used in both C++17 and C++20 modes
  2021-05-09 15:19 [Bug c++/100493] New: Lambda default copy capture that captures "this" cannot be used in both C++17 and C++20 modes avi@cloudius-systems.com
  2021-07-21  9:26 ` [Bug c++/100493] " gcc at bmevers dot de
  2021-07-21 15:36 ` redi at gcc dot gnu.org
@ 2021-09-22 14:13 ` redbeard0531 at gmail dot com
  2021-09-22 14:30 ` redbeard0531 at gmail dot com
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redbeard0531 at gmail dot com @ 2021-09-22 14:13 UTC (permalink / raw)
  To: gcc-bugs

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

Mathias Stearn <redbeard0531 at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |redbeard0531 at gmail dot com

--- Comment #2 from Mathias Stearn <redbeard0531 at gmail dot com> ---
This is making it harder for us to transition to -std=c++20, since we can't
have the same code be warning-clean in both versions. I really don't think the
warning for [=, this] is helpful given that it is the pattern that is now
recommended.

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

* [Bug c++/100493] Lambda default copy capture that captures "this" cannot be used in both C++17 and C++20 modes
  2021-05-09 15:19 [Bug c++/100493] New: Lambda default copy capture that captures "this" cannot be used in both C++17 and C++20 modes avi@cloudius-systems.com
                   ` (2 preceding siblings ...)
  2021-09-22 14:13 ` redbeard0531 at gmail dot com
@ 2021-09-22 14:30 ` redbeard0531 at gmail dot com
  2021-11-29 12:55 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: redbeard0531 at gmail dot com @ 2021-09-22 14:30 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Mathias Stearn <redbeard0531 at gmail dot com> ---
When reading https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82611, I noticed that
C++17 actually requires the warning on [=, this] from a conforming
implementation:
https://timsong-cpp.github.io/cppwp/n4659/expr.prim.lambda.capture#2. However,
given that this requirement was lifted in C++20, would it be possible to only
warn about that in -std=c++17 (or lower) with -pedantic? It seems
counterproductive to warn about it without -pedantic.

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

* [Bug c++/100493] Lambda default copy capture that captures "this" cannot be used in both C++17 and C++20 modes
  2021-05-09 15:19 [Bug c++/100493] New: Lambda default copy capture that captures "this" cannot be used in both C++17 and C++20 modes avi@cloudius-systems.com
                   ` (3 preceding siblings ...)
  2021-09-22 14:30 ` redbeard0531 at gmail dot com
@ 2021-11-29 12:55 ` cvs-commit at gcc dot gnu.org
  2021-11-29 13:01 ` ppalka at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-11-29 12:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>:

https://gcc.gnu.org/g:1420ff3efcff98df0e8c6f021a7ff24b5fc65043

commit r12-5578-g1420ff3efcff98df0e8c6f021a7ff24b5fc65043
Author: Patrick Palka <ppalka@redhat.com>
Date:   Mon Nov 29 07:52:47 2021 -0500

    c++: redundant explicit 'this' capture before C++20 [PR100493]

    As described in detail in the PR, in C++20 implicitly capturing 'this'
    via a '=' capture default is deprecated, and in C++17 adding an explicit
    'this' capture alongside a '=' capture default is diagnosed as redundant
    (and is strictly speaking ill-formed).  This means it's impossible to
    write, in a forward-compatible way, a C++17 lambda that has a '=' capture
    default and that also captures 'this' (implicitly or explicitly):

      [=] { this; }      // #1 deprecated in C++20, OK in C++17
                         // GCC issues a -Wdeprecated warning in C++20 mode

      [=, this] { }      // #2 ill-formed in C++17, OK in C++20
                         // GCC issues an unconditional warning in C++17 mode

    This patch resolves this dilemma by downgrading the warning for #2 into
    a -pedantic one.  In passing, move it into the -Wc++20-extensions class
    of warnings and adjust its wording accordingly.

            PR c++/100493

    gcc/cp/ChangeLog:

            * parser.c (cp_parser_lambda_introducer): In C++17, don't
            diagnose a redundant 'this' capture alongside a by-copy
            capture default unless -pedantic.  Move the diagnostic into
            -Wc++20-extensions and adjust wording accordingly.

    gcc/testsuite/ChangeLog:

            * g++.dg/cpp1z/lambda-this1.C: Adjust expected diagnostics.
            * g++.dg/cpp1z/lambda-this8.C: New test.
            * g++.dg/cpp2a/lambda-this3.C: Compile with -pedantic in C++17
            to continue to diagnose redundant 'this' captures.

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

* [Bug c++/100493] Lambda default copy capture that captures "this" cannot be used in both C++17 and C++20 modes
  2021-05-09 15:19 [Bug c++/100493] New: Lambda default copy capture that captures "this" cannot be used in both C++17 and C++20 modes avi@cloudius-systems.com
                   ` (4 preceding siblings ...)
  2021-11-29 12:55 ` cvs-commit at gcc dot gnu.org
@ 2021-11-29 13:01 ` ppalka at gcc dot gnu.org
  2021-12-15 19:55 ` cvs-commit at gcc dot gnu.org
  2021-12-15 19:58 ` ppalka at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: ppalka at gcc dot gnu.org @ 2021-11-29 13:01 UTC (permalink / raw)
  To: gcc-bugs

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

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |ppalka at gcc dot gnu.org
             Status|NEW                         |ASSIGNED
                 CC|                            |ppalka at gcc dot gnu.org
   Target Milestone|---                         |11.3

--- Comment #5 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Fixed on trunk so far by diagnosing [=, this] in C++17 only with -pedantic. 
This fix will also get backported to 11.3

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

* [Bug c++/100493] Lambda default copy capture that captures "this" cannot be used in both C++17 and C++20 modes
  2021-05-09 15:19 [Bug c++/100493] New: Lambda default copy capture that captures "this" cannot be used in both C++17 and C++20 modes avi@cloudius-systems.com
                   ` (5 preceding siblings ...)
  2021-11-29 13:01 ` ppalka at gcc dot gnu.org
@ 2021-12-15 19:55 ` cvs-commit at gcc dot gnu.org
  2021-12-15 19:58 ` ppalka at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-12-15 19:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-11 branch has been updated by Patrick Palka
<ppalka@gcc.gnu.org>:

https://gcc.gnu.org/g:d33f68865f6f71c28cfee5dfd91765e86d471ef1

commit r11-9391-gd33f68865f6f71c28cfee5dfd91765e86d471ef1
Author: Patrick Palka <ppalka@redhat.com>
Date:   Mon Nov 29 07:52:47 2021 -0500

    c++: redundant explicit 'this' capture before C++20 [PR100493]

    As described in detail in the PR, in C++20 implicitly capturing 'this'
    via a '=' capture default is deprecated, and in C++17 adding an explicit
    'this' capture alongside a '=' capture default is diagnosed as redundant
    (and is strictly speaking ill-formed).  This means it's impossible to
    write, in a forward-compatible way, a C++17 lambda that has a '=' capture
    default and that also captures 'this' (implicitly or explicitly):

      [=] { this; }      // #1 deprecated in C++20, OK in C++17
                         // GCC issues a -Wdeprecated warning in C++20 mode

      [=, this] { }      // #2 ill-formed in C++17, OK in C++20
                         // GCC issues an unconditional warning in C++17 mode

    This patch resolves this dilemma by downgrading the warning for #2 into
    a -pedantic one.  In passing, move it into the -Wc++20-extensions class
    of warnings and adjust its wording accordingly.

            PR c++/100493

    gcc/cp/ChangeLog:

            * parser.c (cp_parser_lambda_introducer): In C++17, don't
            diagnose a redundant 'this' capture alongside a by-copy
            capture default unless -pedantic.  Move the diagnostic into
            -Wc++20-extensions and adjust wording accordingly.

    gcc/testsuite/ChangeLog:

            * g++.dg/cpp1z/lambda-this1.C: Adjust expected diagnostics.
            * g++.dg/cpp1z/lambda-this8.C: New test.
            * g++.dg/cpp2a/lambda-this3.C: Compile with -pedantic in C++17
            to continue to diagnose redundant 'this' captures.

    (cherry picked from commit 1420ff3efcff98df0e8c6f021a7ff24b5fc65043)

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

* [Bug c++/100493] Lambda default copy capture that captures "this" cannot be used in both C++17 and C++20 modes
  2021-05-09 15:19 [Bug c++/100493] New: Lambda default copy capture that captures "this" cannot be used in both C++17 and C++20 modes avi@cloudius-systems.com
                   ` (6 preceding siblings ...)
  2021-12-15 19:55 ` cvs-commit at gcc dot gnu.org
@ 2021-12-15 19:58 ` ppalka at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: ppalka at gcc dot gnu.org @ 2021-12-15 19:58 UTC (permalink / raw)
  To: gcc-bugs

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

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

--- Comment #7 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Fixed for GCC 11.3/12.

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

end of thread, other threads:[~2021-12-15 19:58 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-09 15:19 [Bug c++/100493] New: Lambda default copy capture that captures "this" cannot be used in both C++17 and C++20 modes avi@cloudius-systems.com
2021-07-21  9:26 ` [Bug c++/100493] " gcc at bmevers dot de
2021-07-21 15:36 ` redi at gcc dot gnu.org
2021-09-22 14:13 ` redbeard0531 at gmail dot com
2021-09-22 14:30 ` redbeard0531 at gmail dot com
2021-11-29 12:55 ` cvs-commit at gcc dot gnu.org
2021-11-29 13:01 ` ppalka at gcc dot gnu.org
2021-12-15 19:55 ` cvs-commit at gcc dot gnu.org
2021-12-15 19:58 ` ppalka 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).