public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/106969] New: member function constness incorrectly propagates to local class member function return type deduction
@ 2022-09-19 17:13 enolan at alumni dot cmu.edu
  2022-09-19 17:30 ` [Bug c++/106969] [12/13 Regression] " pinskia at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: enolan at alumni dot cmu.edu @ 2022-09-19 17:13 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106969
           Summary: member function constness incorrectly propagates to
                    local class member function return type deduction
           Product: gcc
           Version: 12.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: enolan at alumni dot cmu.edu
  Target Milestone: ---

When using decltype in the return type of a member function of a local class
defined inside of a const member function, the type of 'this' seems to be
deduced as a pointer to const, when it should be a pointer to non-const.

Compiler explorer link: https://godbolt.org/z/9KKbrzq1e

Minimal reproduction:

struct Context
{
    void
    action() const
    {
        struct
        {
            int wrapped;
            decltype( & wrapped ) get() { return &wrapped; }
        } t;

        *t.get()= 42;
    }
};

Note that if the local class member function definition is changed to:

            auto get() -> decltype( & wrapped ) { return &wrapped; }

Then the bug no longer appears.

> the exact version of gcc

gcc version 12.2.1 20220819 (Red Hat 12.2.1-2) (GCC)

> the system type

Fedora release 38 (Rawhide)

> the options given when GCC was configured/built

Configured with: ../configure --enable-bootstrap
--enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared
--enable-threads=posix --enable-checking=release --enable-multilib
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions
--enable-gnu-unique-object --enable-linker-build-id
--with-gcc-major-version-only --enable-libstdcxx-backtrace
--with-linker-hash-style=gnu --enable-plugin --enable-initfini-array
--with-isl=/builddir/build/BUILD/gcc-12.2.1-20220819/obj-x86_64-redhat-linux/isl-install
--enable-offload-targets=nvptx-none --without-cuda-driver
--enable-offload-defaulted --enable-gnu-indirect-function --enable-cet
--with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
--with-build-config=bootstrap-lto --enable-link-serialization=1

> the complete command line that triggers the bug

g++ poc.cpp

> the compiler output

poc.cpp: In member function ‘void Context::action() const’:
poc.cpp:12:17: error: assignment of read-only location ‘* t.Context::action()
const::<unnamed struct>::get()’
   12 |         *t.get()= 42;
      |         ~~~~~~~~^~~~

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

* [Bug c++/106969] [12/13 Regression] member function constness incorrectly propagates to local class member function return type deduction
  2022-09-19 17:13 [Bug c++/106969] New: member function constness incorrectly propagates to local class member function return type deduction enolan at alumni dot cmu.edu
@ 2022-09-19 17:30 ` pinskia at gcc dot gnu.org
  2022-09-20 14:33 ` ppalka at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-09-19 17:30 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
            Summary|member function constness   |[12/13 Regression] member
                   |incorrectly propagates to   |function constness
                   |local class member function |incorrectly propagates to
                   |return type deduction       |local class member function
                   |                            |return type deduction
      Known to fail|                            |12.2.0
   Last reconfirmed|                            |2022-09-19
           Keywords|                            |needs-bisection,
                   |                            |rejects-valid
             Status|UNCONFIRMED                 |NEW
   Target Milestone|---                         |12.3
      Known to work|                            |12.1.0

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.

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

* [Bug c++/106969] [12/13 Regression] member function constness incorrectly propagates to local class member function return type deduction
  2022-09-19 17:13 [Bug c++/106969] New: member function constness incorrectly propagates to local class member function return type deduction enolan at alumni dot cmu.edu
  2022-09-19 17:30 ` [Bug c++/106969] [12/13 Regression] " pinskia at gcc dot gnu.org
@ 2022-09-20 14:33 ` ppalka at gcc dot gnu.org
  2022-10-18  8:43 ` rguenth at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: ppalka at gcc dot gnu.org @ 2022-09-20 14:33 UTC (permalink / raw)
  To: gcc-bugs

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

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
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=105637
                 CC|                            |ppalka at gcc dot gnu.org
             Status|NEW                         |ASSIGNED
           Keywords|needs-bisection             |

--- Comment #2 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Started with r13-984-g44a5bd6d933d86 which has also been backported to the 12
branch.

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

* [Bug c++/106969] [12/13 Regression] member function constness incorrectly propagates to local class member function return type deduction
  2022-09-19 17:13 [Bug c++/106969] New: member function constness incorrectly propagates to local class member function return type deduction enolan at alumni dot cmu.edu
  2022-09-19 17:30 ` [Bug c++/106969] [12/13 Regression] " pinskia at gcc dot gnu.org
  2022-09-20 14:33 ` ppalka at gcc dot gnu.org
@ 2022-10-18  8:43 ` rguenth at gcc dot gnu.org
  2023-03-24 18:51 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-10-18  8:43 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P2

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

* [Bug c++/106969] [12/13 Regression] member function constness incorrectly propagates to local class member function return type deduction
  2022-09-19 17:13 [Bug c++/106969] New: member function constness incorrectly propagates to local class member function return type deduction enolan at alumni dot cmu.edu
                   ` (2 preceding siblings ...)
  2022-10-18  8:43 ` rguenth at gcc dot gnu.org
@ 2023-03-24 18:51 ` cvs-commit at gcc dot gnu.org
  2023-03-24 18:52 ` [Bug c++/106969] [12 " ppalka at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-03-24 18:51 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 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:bbf2424c57c2e13d1a972c4ef4e871c3119b9cb4

commit r13-6856-gbbf2424c57c2e13d1a972c4ef4e871c3119b9cb4
Author: Patrick Palka <ppalka@redhat.com>
Date:   Fri Mar 24 14:51:24 2023 -0400

    c++: outer 'this' leaking into local class [PR106969]

    Here when resolving the implicit object for '&wrapped' within the
    local class Foo, we expect to obtain a dummy object of type Foo& since
    there's no 'this' available in this context.  And yet at this point
    current_class_ref still corresponds to the outer class Context (and is
    const), which confuses maybe_dummy_object into propagating the cv-quals
    of current_class_ref and returning an object of type const Foo&.  Thus
    decltype(&wrapped) wrongly yields const int* instead of int*.

    The problem ultimately seems to be that the 'this' from the enclosing
    class appears available for use when parsing the local class, but 'this'
    shouldn't persist across classes like that.  This patch fixes this by
    clearing current_class_ptr/ref before parsing a class definition.

    After this change, for the test name-clash11.C in C++98 mode we would
    now complain about an invalid use of 'this' in e.g.

      ASSERT (sizeof (this->A) == 16);

    due to the way the test defines the ASSERT macro via a local class.
    This patch redefines the macro using a local typedef instead.

            PR c++/106969

    gcc/cp/ChangeLog:

            * parser.cc (cp_parser_class_specifier): Clear current_class_ptr
            and current_class_ref sooner, before parsing a class definition.

    gcc/testsuite/ChangeLog:

            * g++.dg/lookup/name-clash11.C: Fix ASSERT macro definition in
            C++98 mode.
            * g++.dg/lookup/this2.C: New test.

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

* [Bug c++/106969] [12 Regression] member function constness incorrectly propagates to local class member function return type deduction
  2022-09-19 17:13 [Bug c++/106969] New: member function constness incorrectly propagates to local class member function return type deduction enolan at alumni dot cmu.edu
                   ` (3 preceding siblings ...)
  2023-03-24 18:51 ` cvs-commit at gcc dot gnu.org
@ 2023-03-24 18:52 ` ppalka at gcc dot gnu.org
  2023-04-28 22:13 ` cvs-commit at gcc dot gnu.org
  2023-04-28 22:15 ` ppalka at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: ppalka at gcc dot gnu.org @ 2023-03-24 18:52 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[12/13 Regression] member   |[12 Regression] member
                   |function constness          |function constness
                   |incorrectly propagates to   |incorrectly propagates to
                   |local class member function |local class member function
                   |return type deduction       |return type deduction

--- Comment #4 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Fixed for GCC 13 so far

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

* [Bug c++/106969] [12 Regression] member function constness incorrectly propagates to local class member function return type deduction
  2022-09-19 17:13 [Bug c++/106969] New: member function constness incorrectly propagates to local class member function return type deduction enolan at alumni dot cmu.edu
                   ` (4 preceding siblings ...)
  2023-03-24 18:52 ` [Bug c++/106969] [12 " ppalka at gcc dot gnu.org
@ 2023-04-28 22:13 ` cvs-commit at gcc dot gnu.org
  2023-04-28 22:15 ` ppalka at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-04-28 22:13 UTC (permalink / raw)
  To: gcc-bugs

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

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

https://gcc.gnu.org/g:458bda5432d352469e258f1c0e4c2a37c853575a

commit r12-9493-g458bda5432d352469e258f1c0e4c2a37c853575a
Author: Patrick Palka <ppalka@redhat.com>
Date:   Fri Mar 24 14:51:24 2023 -0400

    c++: outer 'this' leaking into local class [PR106969]

    Here when resolving the implicit object for '&wrapped' within the
    local class Foo, we expect to obtain a dummy object of type Foo& since
    there's no 'this' available in this context.  And yet at this point
    current_class_ref still corresponds to the outer class Context (and is
    const), which confuses maybe_dummy_object into propagating the cv-quals
    of current_class_ref and returning an object of type const Foo&.  Thus
    decltype(&wrapped) wrongly yields const int* instead of int*.

    The problem ultimately seems to be that the 'this' from the enclosing
    class appears available for use when parsing the local class, but 'this'
    shouldn't persist across classes like that.  This patch fixes this by
    clearing current_class_ptr/ref before parsing a class definition.

    After this change, for the test name-clash11.C in C++98 mode we would
    now complain about an invalid use of 'this' in e.g.

      ASSERT (sizeof (this->A) == 16);

    due to the way the test defines the ASSERT macro via a local class.
    This patch redefines the macro using a local typedef instead.

            PR c++/106969

    gcc/cp/ChangeLog:

            * parser.cc (cp_parser_class_specifier): Clear current_class_ptr
            and current_class_ref sooner, before parsing a class definition.

    gcc/testsuite/ChangeLog:

            * g++.dg/lookup/name-clash11.C: Fix ASSERT macro definition in
            C++98 mode.
            * g++.dg/lookup/this2.C: New test.

    (cherry picked from commit bbf2424c57c2e13d1a972c4ef4e871c3119b9cb4)

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

* [Bug c++/106969] [12 Regression] member function constness incorrectly propagates to local class member function return type deduction
  2022-09-19 17:13 [Bug c++/106969] New: member function constness incorrectly propagates to local class member function return type deduction enolan at alumni dot cmu.edu
                   ` (5 preceding siblings ...)
  2023-04-28 22:13 ` cvs-commit at gcc dot gnu.org
@ 2023-04-28 22:15 ` ppalka at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: ppalka at gcc dot gnu.org @ 2023-04-28 22:15 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #6 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Fixed for GCC 12.3+, thanks for the bug report.

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

end of thread, other threads:[~2023-04-28 22:15 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-19 17:13 [Bug c++/106969] New: member function constness incorrectly propagates to local class member function return type deduction enolan at alumni dot cmu.edu
2022-09-19 17:30 ` [Bug c++/106969] [12/13 Regression] " pinskia at gcc dot gnu.org
2022-09-20 14:33 ` ppalka at gcc dot gnu.org
2022-10-18  8:43 ` rguenth at gcc dot gnu.org
2023-03-24 18:51 ` cvs-commit at gcc dot gnu.org
2023-03-24 18:52 ` [Bug c++/106969] [12 " ppalka at gcc dot gnu.org
2023-04-28 22:13 ` cvs-commit at gcc dot gnu.org
2023-04-28 22:15 ` 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).