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 [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 6F825385481C for ; Tue, 29 Jun 2021 18:45:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6F825385481C Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-481-gjOES5wYPdiMskQ-0OhHjA-1; Tue, 29 Jun 2021 14:45:23 -0400 X-MC-Unique: gjOES5wYPdiMskQ-0OhHjA-1 Received: by mail-qk1-f199.google.com with SMTP id v1-20020a372f010000b02903aa9be319adso21452705qkh.11 for ; Tue, 29 Jun 2021 11:45:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=/4uXKFmwWd9iBEG9EiIJ5nr49EhUtYSR/zJAWpNKIcg=; b=qc+cYBlKeuUhKbi3XyuYzLTpb/GNC7F4oZFt3/UzGO2JwHmImiUQjDiwNoRc4gZ507 xpYP5JdltDBFYn8l7tmN0vR9dtdJSqAQAf9Ud4tNeKP8KGu7OzZWuKZe7X/TL8oL66Ru ZFQZCVQ6bp7D7TdWjZoqwTs8v5NRMw8Qqeko+IzBbZFeRrC/aVsrVs87mKw7rCxdFPdJ hhRJoe1BbSxEOZ0v8zkShCeRlSjk4yjRMmXnN5UUGrRziHln0u6CGdFAo/dhaXggyd1u gTdE1J2FauLfXCGTGgpOIBhGLm9X9IPDVHUwRFw6sYiLQqSR6UBkqDUsiww8jV0kaUdI bpsA== X-Gm-Message-State: AOAM530mm2PdIbvk+4TVdpWRrfaR1sDZyJSP1D/l4CIpUpWVu+tD05lH ppth4ZjQo83IaRigYOaitfW4cVhTC9tIjXTy04tKsUXjRHp+zYjtaFm3TK8k/3xzOKr6pzJ6CnM Exo0phTwVnLfp2OkGMsULrKDwtoXfkmI7m+1ZmY8AIc4Q6nK4ZM1G7Hx/A+8Az0aWPw== X-Received: by 2002:ac8:474e:: with SMTP id k14mr20672594qtp.384.1624992322902; Tue, 29 Jun 2021 11:45:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyGMAOos7bjmq1PkxypeFCSjleiccZ5K3NICvkmIi43tNQFH3xPNRjmAFea4QWInrKgRaWFZQ== X-Received: by 2002:ac8:474e:: with SMTP id k14mr20672560qtp.384.1624992322588; Tue, 29 Jun 2021 11:45:22 -0700 (PDT) Received: from [192.168.1.148] (130-44-159-43.s11817.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id w185sm12606333qkd.30.2021.06.29.11.45.21 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 29 Jun 2021 11:45:21 -0700 (PDT) Subject: Re: [PATCH 1/2] c++: Fix push_access_scope and introduce RAII wrapper for it To: Patrick Palka , gcc-patches@gcc.gnu.org References: <20210629175723.1009233-1-ppalka@redhat.com> From: Jason Merrill Message-ID: Date: Tue, 29 Jun 2021 14:45:20 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.0 MIME-Version: 1.0 In-Reply-To: <20210629175723.1009233-1-ppalka@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-11.9 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_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_SBL, URIBL_SBL_A autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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-List-Received-Date: Tue, 29 Jun 2021 18:45:32 -0000 On 6/29/21 1:57 PM, Patrick Palka wrote: > When push_access_scope is passed a TYPE_DECL for a class type (which > can happen during e.g. satisfaction), we undesirably push only the > enclosing context of the class instead of the class itself. This causes > us to mishandle e.g. testcase below due to us not entering the scope of > A before checking its constraints. > > This patch adjusts push_access_scope accordingly, and introduces an > RAII wrapper for it. We also make use of this wrapper right away by > replacing the only use of push_nested_class_guard with this new wrapper, > which means we can remove this old wrapper (whose functionality is > basically subsumed by the new wrapper). > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for > trunk? > > gcc/cp/ChangeLog: > > * constraint.cc (get_normalized_constraints_from_decl): Use > push_access_scope_guard instead of push_nested_class_guard. > * cp-tree.h (struct push_nested_class_guard): Replace with ... > (struct push_access_scope_guard): ... this. > * pt.c (push_access_scope): When the argument corresponds to > a class type, push the class instead of its context. > (pop_access_scope): Adjust accordingly. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp2a/concepts-access2.C: New test. > --- > gcc/cp/constraint.cc | 7 +----- > gcc/cp/cp-tree.h | 23 +++++++++++-------- > gcc/cp/pt.c | 9 +++++++- > gcc/testsuite/g++.dg/cpp2a/concepts-access2.C | 13 +++++++++++ > 4 files changed, 35 insertions(+), 17 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-access2.C > > diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc > index 6df3ca6ce32..99d3ccc6998 100644 > --- a/gcc/cp/constraint.cc > +++ b/gcc/cp/constraint.cc > @@ -926,12 +926,7 @@ get_normalized_constraints_from_decl (tree d, bool diag = false) > tree norm = NULL_TREE; > if (tree ci = get_constraints (decl)) > { > - push_nested_class_guard pncs (DECL_CONTEXT (d)); > - > - temp_override ovr (current_function_decl); > - if (TREE_CODE (decl) == FUNCTION_DECL) > - current_function_decl = decl; > - > + push_access_scope_guard pas (decl); > norm = get_normalized_constraints_from_info (ci, tmpl, diag); > } > > diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h > index 6f713719589..58da7460001 100644 > --- a/gcc/cp/cp-tree.h > +++ b/gcc/cp/cp-tree.h > @@ -8463,21 +8463,24 @@ is_constrained_auto (const_tree t) > return is_auto (t) && PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t); > } > > -/* RAII class to push/pop class scope T; if T is not a class, do nothing. */ > +/* RAII class to push/pop the access scope for T. */ > > -struct push_nested_class_guard > +struct push_access_scope_guard > { > - bool push; > - push_nested_class_guard (tree t) > - : push (t && CLASS_TYPE_P (t)) > + tree decl; > + push_access_scope_guard (tree t) > + : decl (t) > { > - if (push) > - push_nested_class (t); > + if (VAR_OR_FUNCTION_DECL_P (decl) > + || TREE_CODE (decl) == TYPE_DECL) > + push_access_scope (decl); > + else > + decl = NULL_TREE; > } > - ~push_nested_class_guard () > + ~push_access_scope_guard () > { > - if (push) > - pop_nested_class (); > + if (decl) > + pop_access_scope (decl); > } > }; > > diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c > index f2039e09cd7..bd8b17ca047 100644 > --- a/gcc/cp/pt.c > +++ b/gcc/cp/pt.c > @@ -224,7 +224,7 @@ static void instantiate_body (tree pattern, tree args, tree d, bool nested); > /* Make the current scope suitable for access checking when we are > processing T. T can be FUNCTION_DECL for instantiated function > template, VAR_DECL for static member variable, or TYPE_DECL for > - alias template (needed by instantiate_decl). */ > + for a class or alias template (needed by instantiate_decl). */ > > void > push_access_scope (tree t) > @@ -234,6 +234,10 @@ push_access_scope (tree t) > > if (DECL_FRIEND_CONTEXT (t)) > push_nested_class (DECL_FRIEND_CONTEXT (t)); > + else if (TREE_CODE (t) == TYPE_DECL > + && CLASS_TYPE_P (TREE_TYPE (t)) > + && DECL_ORIGINAL_TYPE (t) == NULL_TREE) I suspect DECL_IMPLICIT_TYPEDEF_P is a better test for this case. > + push_nested_class (TREE_TYPE (t)); > else if (DECL_CLASS_SCOPE_P (t)) > push_nested_class (DECL_CONTEXT (t)); > else if (deduction_guide_p (t) && DECL_ARTIFICIAL (t)) > @@ -260,6 +264,9 @@ pop_access_scope (tree t) > current_function_decl = saved_access_scope->pop(); > > if (DECL_FRIEND_CONTEXT (t) > + || (TREE_CODE (t) == TYPE_DECL > + && CLASS_TYPE_P (TREE_TYPE (t)) > + && DECL_ORIGINAL_TYPE (t) == NULL_TREE) > || DECL_CLASS_SCOPE_P (t) > || (deduction_guide_p (t) && DECL_ARTIFICIAL (t))) > pop_nested_class (); > diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-access2.C b/gcc/testsuite/g++.dg/cpp2a/concepts-access2.C > new file mode 100644 > index 00000000000..8ddcad236e3 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-access2.C > @@ -0,0 +1,13 @@ > +// { dg-do compile { target concepts } } > + > +template requires T::value struct A { }; > +template requires T::value struct B { }; // { dg-error "private" } > + > +struct S { > +private: > + static constexpr bool value = true; > + template requires T::value friend struct A; > +}; > + > +A x; > +B y; // { dg-error "constraint" } >