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.133.124]) by sourceware.org (Postfix) with ESMTP id 0579A385DC31 for ; Tue, 29 Jun 2021 19:38:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 0579A385DC31 Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-314-5Hb6lSHiMY2HMEApH9Q9Aw-1; Tue, 29 Jun 2021 15:38:25 -0400 X-MC-Unique: 5Hb6lSHiMY2HMEApH9Q9Aw-1 Received: by mail-qv1-f69.google.com with SMTP id r15-20020a0562140c4fb0290262f40bf4bcso21265197qvj.11 for ; Tue, 29 Jun 2021 12:38:25 -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; bh=V26MMQcKD+sN1bdiPHb1QM2o+KOCzIVo6pJABDS2488=; b=SpPeaRaJOokiY8fx7CgEVuKJg0e1f/XMq1EEtMMRLJbYvGPFo4iqz2oTP7EIlHGY8l i2ooKziNNRGe8n4fEGnunRor3gj47M1/4H/BbABLXl0aD3qCJMdXZLbcb4R0wK2A4Bhx WUA5TAE01OAgkpo3/szQhtyQuCobhDjNLVgVCOmMGEetUHmwSyd6GRo2DEeBoS/TtveQ g5CofB+p1ZN9zRxlCrjZUJij08UqbQnZLKxXTt99cK+rP44+aP+kRqLYpYkTCx2LOelr ad7Fmp6S720sTrFauLXD7s3aDvlOR976CymXlEOOzW5qTf6yYYKrqzOZbWLkX7Ewsf+X bnYw== X-Gm-Message-State: AOAM532nD+ccMucTpXLpC6oif3c2as2r31G6AI+/68Y88p1bbxmO5Yct aWyCXxzaKM5//+Z3Ns32HQEXftcrqAvv+vx4IVHEiZPwlFK7O2BhLVBaGpf/gfKVMWtQSk49P4B 9ROSjECZbboCwwWsepv0yxbdLISZaQixqvFPqmZj3zuMyUW1aipOqYiHkwcP3LqFOvw== X-Received: by 2002:ac8:6982:: with SMTP id o2mr2748801qtq.345.1624995504454; Tue, 29 Jun 2021 12:38:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyzdVHRY3xt2pbO+d0GHAsj5FiwD2IQHYVuCVS0noWJ3smesbramwuUNcP5f7bwuJiR7FFgDQ== X-Received: by 2002:ac8:6982:: with SMTP id o2mr2748781qtq.345.1624995504039; Tue, 29 Jun 2021 12:38:24 -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 s81sm12377467qka.82.2021.06.29.12.38.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 29 Jun 2021 12:38:22 -0700 (PDT) Subject: Re: [PATCH 2/2] c++: Extend PR96204 fix to variable templates To: Patrick Palka , gcc-patches@gcc.gnu.org References: <20210629175723.1009233-1-ppalka@redhat.com> <20210629175723.1009233-2-ppalka@redhat.com> From: Jason Merrill Message-ID: Date: Tue, 29 Jun 2021 15:38:21 -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-2-ppalka@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="------------B17CB2B61B8776870B0E4918" Content-Language: en-US X-Spam-Status: No, score=-13.2 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 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 19:38:28 -0000 This is a multi-part message in MIME format. --------------B17CB2B61B8776870B0E4918 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 6/29/21 1:57 PM, Patrick Palka wrote: > r12-1829 corrected the access scope during partial specialization > matching of class templates, but neglected the variable template case. > This patch moves the access scope adjustment to inside > most_specialized_partial_spec, so that all callers can benefit. > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for > trunk? > > PR c++/96204 > > gcc/cp/ChangeLog: > > * pt.c (instantiate_class_template_1): Remove call to > push_nested_class and pop_nested_class added by r12-1829. > (most_specialized_partial_spec): Use push_access_scope_guard > and deferring_access_check_sentinel. > > gcc/testsuite/ChangeLog: > > * g++.dg/template/access40b.C: New test. > --- > gcc/cp/pt.c | 12 +++++++---- > gcc/testsuite/g++.dg/template/access40b.C | 26 +++++++++++++++++++++++ > 2 files changed, 34 insertions(+), 4 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/template/access40b.C > > diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c > index bd8b17ca047..1e2e2ba5329 100644 > --- a/gcc/cp/pt.c > +++ b/gcc/cp/pt.c > @@ -11776,11 +11776,8 @@ instantiate_class_template_1 (tree type) > deferring_access_check_sentinel acs (dk_no_deferred); > > /* Determine what specialization of the original template to > - instantiate; do this relative to the scope of the class for > - sake of access checking. */ > - push_nested_class (type); > + instantiate. */ > t = most_specialized_partial_spec (type, tf_warning_or_error); > - pop_nested_class (); > if (t == error_mark_node) > return error_mark_node; > else if (t) > @@ -24989,26 +24986,33 @@ most_specialized_partial_spec (tree target, tsubst_flags_t complain) > tree outer_args = NULL_TREE; > tree tmpl, args; > > + tree decl; > if (TYPE_P (target)) > { > tree tinfo = CLASSTYPE_TEMPLATE_INFO (target); > tmpl = TI_TEMPLATE (tinfo); > args = TI_ARGS (tinfo); > + decl = TYPE_NAME (target); > } > else if (TREE_CODE (target) == TEMPLATE_ID_EXPR) > { > tmpl = TREE_OPERAND (target, 0); > args = TREE_OPERAND (target, 1); > + decl = DECL_TEMPLATE_RESULT (tmpl); Hmm, this won't get us the right scope; we get here for the result of finish_template_variable, where tmpl is the most general template and args are args for it. So in the below testcase, tmpl is outer::N: template struct outer { template static constexpr int f() { return N; }; template static const int N = f(); }; template template const int outer::N = 1; int i = outer::N; Oddly, I notice that we also get here for static data members of class templates that are not themselves templates, as in mem-partial1.C that I adapted the above from. Fixed by the attached patch. Since the type of the variable depends on the specialization, we can't actually get the decl before doing the resolution, but we should be able to push into the right enclosing class. Perhaps we should pass the partially instantiated template and its args to lookup_template_variable instead of the most general template and its args. > } > else if (VAR_P (target)) > { > tree tinfo = DECL_TEMPLATE_INFO (target); > tmpl = TI_TEMPLATE (tinfo); > args = TI_ARGS (tinfo); > + decl = target; > } > else > gcc_unreachable (); > > + push_access_scope_guard pas (decl); > + deferring_access_check_sentinel acs (dk_no_deferred); > + > tree main_tmpl = most_general_template (tmpl); > > /* For determining which partial specialization to use, only the > diff --git a/gcc/testsuite/g++.dg/template/access40b.C b/gcc/testsuite/g++.dg/template/access40b.C > new file mode 100644 > index 00000000000..040e3d18096 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/template/access40b.C > @@ -0,0 +1,26 @@ > +// PR c++/96204 > +// { dg-do compile { target c++14 } } > +// A variant of access40.C where has_type_member is a variable template instead > +// of a class template. > + > +template > +constexpr bool has_type_member = false; > + > +template > +constexpr bool has_type_member = true; > + > +struct Parent; > + > +struct Child { > +private: > + friend struct Parent; > + typedef void type; > +}; > + > +struct Parent { > + static void f() { > + // The partial specialization does not match despite Child::type > + // being accessible from the current scope. > + static_assert(!has_type_member, ""); > + } > +}; > --------------B17CB2B61B8776870B0E4918 Content-Type: text/x-patch; charset=UTF-8; name="0001-vartmp.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0001-vartmp.patch" >From b7b34f555b54f97a9d2315d6c6a552e27e2faa9c Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Tue, 29 Jun 2021 15:11:25 -0400 Subject: [PATCH] vartmp To: gcc-patches@gcc.gnu.org --- gcc/cp/pt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index f2039e09cd7..d2936c106ba 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -26003,7 +26003,7 @@ instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p) td = template_for_substitution (d); args = gen_args; - if (VAR_P (d)) + if (variable_template_specialization_p (d)) { /* Look up an explicit specialization, if any. */ tree tid = lookup_template_variable (gen_tmpl, gen_args); -- 2.27.0 --------------B17CB2B61B8776870B0E4918--