From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1888) id 7CBB1385770C; Fri, 28 Apr 2023 22:13:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7CBB1385770C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1682720010; bh=+79BU3XQ/O4FjxW0fJeDEwIg9j3UXHxu0k76P0+CuvU=; h=From:To:Subject:Date:From; b=ipyJIufiRq0oMPz5lapVFXn8D5TTGdS1cf6Ajrzh3qzeWnXO3s+rOgHBeww4+Xlw6 1O9gkCEgxyWpv8RK0Aog4xLOQ1kSvBCgBXPX2g5FXvhVfdtg2aOkSQPK+ycaLDQQAw W1BrL7LQwDEajSuvRImLerVlPLO0Gis2oYjAkMN4= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Patrick Palka To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-9493] c++: outer 'this' leaking into local class [PR106969] X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 73e86b6766cc92aa8c18cc987bf95929c4ea0672 X-Git-Newrev: 458bda5432d352469e258f1c0e4c2a37c853575a Message-Id: <20230428221330.7CBB1385770C@sourceware.org> Date: Fri, 28 Apr 2023 22:13:30 +0000 (GMT) List-Id: https://gcc.gnu.org/g:458bda5432d352469e258f1c0e4c2a37c853575a commit r12-9493-g458bda5432d352469e258f1c0e4c2a37c853575a Author: Patrick Palka 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) Diff: --- gcc/cp/parser.cc | 13 +++++++++---- gcc/testsuite/g++.dg/lookup/name-clash11.C | 2 +- gcc/testsuite/g++.dg/lookup/this2.C | 22 ++++++++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index e5211d8075e..d2c3fc1d0b9 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -25877,6 +25877,11 @@ cp_parser_class_specifier_1 (cp_parser* parser) saved_in_unbraced_linkage_specification_p = parser->in_unbraced_linkage_specification_p; parser->in_unbraced_linkage_specification_p = false; + /* 'this' from an enclosing non-static member function is unavailable. */ + tree saved_ccp = current_class_ptr; + tree saved_ccr = current_class_ref; + current_class_ptr = NULL_TREE; + current_class_ref = NULL_TREE; /* Start the class. */ if (nested_name_specifier_p) @@ -26095,8 +26100,6 @@ cp_parser_class_specifier_1 (cp_parser* parser) /* If there are noexcept-specifiers that have not yet been processed, take care of them now. Do this before processing NSDMIs as they may depend on noexcept-specifiers already having been processed. */ - tree save_ccp = current_class_ptr; - tree save_ccr = current_class_ref; FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl) { tree ctx = DECL_CONTEXT (decl); @@ -26172,8 +26175,8 @@ cp_parser_class_specifier_1 (cp_parser* parser) cp_parser_late_parsing_nsdmi (parser, decl); } vec_safe_truncate (unparsed_nsdmis, 0); - current_class_ptr = save_ccp; - current_class_ref = save_ccr; + current_class_ptr = NULL_TREE; + current_class_ref = NULL_TREE; if (pushed_scope) pop_scope (pushed_scope); @@ -26205,6 +26208,8 @@ cp_parser_class_specifier_1 (cp_parser* parser) = saved_num_template_parameter_lists; parser->in_unbraced_linkage_specification_p = saved_in_unbraced_linkage_specification_p; + current_class_ptr = saved_ccp; + current_class_ref = saved_ccr; return type; } diff --git a/gcc/testsuite/g++.dg/lookup/name-clash11.C b/gcc/testsuite/g++.dg/lookup/name-clash11.C index bc63645e8d3..2ae9a65264d 100644 --- a/gcc/testsuite/g++.dg/lookup/name-clash11.C +++ b/gcc/testsuite/g++.dg/lookup/name-clash11.C @@ -7,7 +7,7 @@ # define ASSERT(e) static_assert (e, #e) #else # define ASSERT(e) \ - do { struct S { bool: !!(e); } asrt; (void)&asrt; } while (0) + do { typedef int asrt[bool(e) ? 1 : -1]; } while (0) #endif diff --git a/gcc/testsuite/g++.dg/lookup/this2.C b/gcc/testsuite/g++.dg/lookup/this2.C new file mode 100644 index 00000000000..064c146e937 --- /dev/null +++ b/gcc/testsuite/g++.dg/lookup/this2.C @@ -0,0 +1,22 @@ +// PR c++/106969 +// { dg-do compile { target c++11 } } + +struct Context +{ + void + action() const + { + struct Foo + { + int wrapped; + decltype(&wrapped) get() { return &wrapped; } + } t; + + *t.get()= 42; // OK, get() returns int* not const int* + + struct Bar + { + using type = decltype(this); // { dg-error "invalid use of 'this'" } + }; + } +};