From patchwork Thu Mar 23 15:00:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 74097 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:604a:0:0:0:0:0 with SMTP id j10csp2960638wrt; Thu, 23 Mar 2023 08:01:44 -0700 (PDT) X-Google-Smtp-Source: AK7set9kuJC873Pk/QcXsId+SrcN79+ixDVlkWCwnwy91vGmaQeJiN7O2o9RvFVHlJvEx2z74tyO X-Received: by 2002:a17:906:5a47:b0:8f6:dc49:337f with SMTP id my7-20020a1709065a4700b008f6dc49337fmr10320453ejc.43.1679583704771; Thu, 23 Mar 2023 08:01:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679583704; cv=none; d=google.com; s=arc-20160816; b=Yxa/TmhsEzCmRh/6r9/VS4o9e0p6MGRSnX4j6S0ZEFVUMJEKfjPwhmlD2PIBbclOHO lmt3U/M6HKaB6AdewePzdF/Oa7+Dxt9uPB1cEnwgk2u47p34lEparVgdK/jKXPBf3VMX 8Gr/D1hMFQZTTDnKzllS2lyOpzQA5IQhwSWWqcma4bHVm9WQ5Fn3bETZKjO2lLb0e0mM DYjcqZUmKGONRLjlrw42cCUv+/86elvfF6+lN4jjYKzyDWloGaVcRKOGiG+rgyKa3T3o +oGVABSp/FNRjml8vwp6++Be52QuCMBYHlAlxlxKurTjGVkeePk5Z8XmjOb5QKpm+iF2 zkUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=bs9+CM/OqUKFsK0NMra+gGS0llv1bZ3yB2xHEZxKlJ4=; b=ljryQMeRfvpB6VX3r3+SUtswZY4LxOMztvgLOf+L65Or8TiKdB/52uyjZLbjlwITEq J1lciksvEHxYcAPXiio8moeCVISQayTjUOht/jugiVrBSDUMlYOzidfc14EV0ClUIIXm xssRAS1aJEzOBuNAgqwD3UT3y5yqiEzSRy3SU2DUnU3zamiGkeFucyd1XsmcRUFPZJ+K W01W0q7LB0WjTLf2Lofym0Wx5ZWanX9wYUTupyza0buXLVJeY+Qb86wUMcmYnAGbre+r igIHUteYsytPdu+wkfrZz3YnwIjUM1VtQTAsWAQGXFQaJcIxf357Fc6YTHfzM6nmKud6 gWaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=iUEJ9yL3; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id hu19-20020a170907a09300b0093d16dd337csi1259359ejc.290.2023.03.23.08.01.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 08:01:44 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=iUEJ9yL3; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BAA2838708AC for ; Thu, 23 Mar 2023 15:01:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BAA2838708AC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1679583703; bh=bs9+CM/OqUKFsK0NMra+gGS0llv1bZ3yB2xHEZxKlJ4=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=iUEJ9yL3o0C+cuR45ugQTWZ8KRRDdFc5GDnbafs6/9RT1E98cv+BaXJH9COEkgQGT VF4cwF5Mom0Crj8VFt09OILD+9+D6zv8oEc4ja3dq9thlMmpyjGdfTC63X73y9d6ZA B00K1QQC7YcoSc7SDfoqnRQ7NPxWQRWNO+14jnPY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 840173870899 for ; Thu, 23 Mar 2023 15:01:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 840173870899 Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-607-DXJr6zpiPhuWXrn_co7jJg-1; Thu, 23 Mar 2023 11:00:58 -0400 X-MC-Unique: DXJr6zpiPhuWXrn_co7jJg-1 Received: by mail-qt1-f198.google.com with SMTP id l2-20020ac87242000000b003bfecc6d046so13058728qtp.17 for ; Thu, 23 Mar 2023 08:00:58 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679583658; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=bs9+CM/OqUKFsK0NMra+gGS0llv1bZ3yB2xHEZxKlJ4=; b=qLyEF5QlS7CWX3qt50kTD8xVZ4ADNWvxhKxoRg2vLpdLnAymgd303a0uY2IJNzktSm ZS6crBJN8ph1r+ugwm+oezFsApgnkZB60ZmsUhfXx/havZ+3DeB3uV0rNVDh5kJIKUl+ sDtXPvlG4HtQFoF77nZtfFTCKuOZbIkyP1Nv59d4+s9ilB2KLhyRooOhIhg6kIoSAAy0 dl3D2gYT2Y6vcaU0Ym7q9fsFSs0qAfysGNApVbDDWhpAPgkIiPfwwytSBPckltzgOtr6 0og6/ikCi5CN5AIahAC6+WFful05ZsOwMS8BuBdL3HGfg5XbzUE7I8VowZj38ZJCdgdI q8DQ== X-Gm-Message-State: AO0yUKUwBR+36qlit7rRFvbmTh8JVP51xCsixAxPrVZaN4EipFnfL+wY nYpcbajhmdpp9aHuez9nnDcENLfRwRygFsxte8qWkkmL2hXUzi2r3MPN8iqpTklUGME/DBhfdfb GamdJ6SBiI+NdGRloFGu9abnAxxJUAZtnRj9r1y3Dml7Kf5G3U63hawy62IycjFk2NVVVgQICeu 0= X-Received: by 2002:a05:622a:11d2:b0:3df:50ef:faff with SMTP id n18-20020a05622a11d200b003df50effaffmr13205043qtk.58.1679583657918; Thu, 23 Mar 2023 08:00:57 -0700 (PDT) X-Received: by 2002:a05:622a:11d2:b0:3df:50ef:faff with SMTP id n18-20020a05622a11d200b003df50effaffmr13204980qtk.58.1679583657533; Thu, 23 Mar 2023 08:00:57 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id t197-20020a3746ce000000b0074280fc7bd8sm13391046qka.60.2023.03.23.08.00.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Mar 2023 08:00:57 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: outer 'this' leaking into local class [PR106969] Date: Thu, 23 Mar 2023 11:00:55 -0400 Message-Id: <20230323150055.2694558-1-ppalka@redhat.com> X-Mailer: git-send-email 2.40.0.130.g27d43aaaf5 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761171163113318587?= X-GMAIL-MSGID: =?utf-8?q?1761171163113318587?= 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? 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'" } + }; + } +};