From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 1565A3858D1E for ; Fri, 24 Mar 2023 16:27:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1565A3858D1E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679675251; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WgsITKHbxNqmfMl8TMjmN114b9+pbIAOJxvMDDIFhV0=; b=Yelk+aImyf8It1BhUTLOxQexGcHG70WNt+GlikF5A0h3XjirIR2V1Dyk3eCkf3v+rwdTns /45Hi8LUvVUQQNg1WDKlp5fInIOEbSvpQUkhjh1Z9EyTPVnvtSEsB62ugvuV9RAhOz0GTY 0mOO9RDtBbGWt9NwOF62VSLDehPkrSc= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-635-34uKKUtiPo6u1fzjXg2iOw-1; Fri, 24 Mar 2023 12:27:30 -0400 X-MC-Unique: 34uKKUtiPo6u1fzjXg2iOw-1 Received: by mail-qk1-f198.google.com with SMTP id dw26-20020a05620a601a00b0074300c772c0so1028294qkb.11 for ; Fri, 24 Mar 2023 09:27:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679675250; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=WgsITKHbxNqmfMl8TMjmN114b9+pbIAOJxvMDDIFhV0=; b=OgCNMI3hshMYVGVLoIy8NP+nAM8pcOa6LN4hMtnYYysBnkSJvmfpgu1yEqopzW/l0G VU6g99aGrym/895zKi3rK1wtMcBlcLd1AttZJ12/N5qGDrRiZMaQGmJisVI70NpzACpb ATmaLQZO3pZl4Ka+Oy3BrUkQy9gITiAcyVFzbvQsQV7z5oN54DAZUDvFHyN4ZCS0weFH XTbSXtCxzArJVDtEI+mNLNJ/oybzdnsk1Rb+Xx6M2aInhDdvqyiaXUnZw+LN8iwzX7Z9 x6+AVDRMxtFuTa4YLw1c3bBDTleKsT6nWdJMY5DKnXRgzyyg8bC/KfVmOIfi2S3h1j7F NkMg== X-Gm-Message-State: AAQBX9fX7YBfR5Y8eIORfnv/2EuonKDeBEehiP+TMymD/LT7gQ4sLl8x otEq83e48UKfXuol+vzBsy5jpdrUwVlCD0/HdOZnjak7nyWyswYHHWczPga19F/qkMsOGcQ+ozx /yO154Htp72ezQ6KiSQ== X-Received: by 2002:ad4:5d6f:0:b0:5b5:9c2:8c29 with SMTP id fn15-20020ad45d6f000000b005b509c28c29mr5769969qvb.12.1679675249993; Fri, 24 Mar 2023 09:27:29 -0700 (PDT) X-Google-Smtp-Source: AKy350ZzC4g696zqWmHhsmSBG/SipP2+01+/Y1g2QEZMKyfEz0w+dSjp+cLEebAwm18p40D3B8Ihxw== X-Received: by 2002:ad4:5d6f:0:b0:5b5:9c2:8c29 with SMTP id fn15-20020ad45d6f000000b005b509c28c29mr5769935qvb.12.1679675249666; Fri, 24 Mar 2023 09:27:29 -0700 (PDT) Received: from [192.168.1.108] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id de9-20020ad45849000000b005dd8b9345e2sm800656qvb.122.2023.03.24.09.27.28 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 24 Mar 2023 09:27:29 -0700 (PDT) Message-ID: <1451dd9f-b54c-a113-250a-80ff2d1d5f2a@redhat.com> Date: Fri, 24 Mar 2023 12:27:27 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.0 Subject: Re: [PATCH] c++: outer 'this' leaking into local class [PR106969] To: Patrick Palka , gcc-patches@gcc.gnu.org References: <20230323150055.2694558-1-ppalka@redhat.com> From: Jason Merrill In-Reply-To: <20230323150055.2694558-1-ppalka@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 3/23/23 11:00, Patrick Palka wrote: > 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 leak across classes like that. This patch fixes this by > clearing current_class_ptr/ref when parsing a class definition. > > After this change, for name-clash11.C in C++98 mode we would now > complain about an invalid use of 'this' for e.g. > > ASSERT (sizeof (this->A) == 16); > > due to the way the ASSERT macro is defined using a local class. This > patch redefines it using a local typedef instead. > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for > trunk/12? OK. > PR c++/106969 > > gcc/cp/ChangeLog: > > * parser.cc (cp_parser_class_specifier): Clear current_class_ptr > and current_class_ref when parsing a class definition. > > gcc/testsuite/ChangeLog: > > * g++.dg/lookup/name-clash11.C: New test. > * g++.dg/lookup/this2.C: New test. > --- > 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(-) > create mode 100644 gcc/testsuite/g++.dg/lookup/this2.C > > diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc > index a277003ea58..be9c77b415e 100644 > --- a/gcc/cp/parser.cc > +++ b/gcc/cp/parser.cc > @@ -26151,6 +26151,11 @@ cp_parser_class_specifier (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 unvailable. */ > + 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) > @@ -26369,8 +26374,6 @@ cp_parser_class_specifier (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); > @@ -26496,8 +26499,8 @@ cp_parser_class_specifier (cp_parser* parser) > } > vec_safe_truncate (unparsed_contracts, 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); > > @@ -26529,6 +26532,8 @@ cp_parser_class_specifier (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..1450c563d92 > --- /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'" } > + }; > + } > +};