]> 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, 24 Mar 2023 18:51:24 +0000 (14:51 -0400)
commitbbf2424c57c2e13d1a972c4ef4e871c3119b9cb4
tree9814580fb3f609a1ab9359eefa560f76b5badd75
parent1f641d6aba284e0c277e6684cd6b2c73591cd14d
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.
gcc/cp/parser.cc
gcc/testsuite/g++.dg/lookup/name-clash11.C
gcc/testsuite/g++.dg/lookup/this2.C [new file with mode: 0644]