]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: outer 'this' leaking into local class [PR106969]
authorPatrick Palka <ppalka@redhat.com>
Fri, 24 Mar 2023 18:51:24 +0000 (14:51 -0400)
committerPatrick Palka <ppalka@redhat.com>
Fri, 28 Apr 2023 19:47:54 +0000 (15:47 -0400)
commit458bda5432d352469e258f1c0e4c2a37c853575a
treea5c87793dfd9451193631b21fa552d9fdc2ed796
parent73e86b6766cc92aa8c18cc987bf95929c4ea0672
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)
gcc/cp/parser.cc
gcc/testsuite/g++.dg/lookup/name-clash11.C
gcc/testsuite/g++.dg/lookup/this2.C [new file with mode: 0644]