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 ESMTPS id 649A23858C50 for ; Mon, 2 May 2022 18:50:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 649A23858C50 Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-656-UAgZvLgHNbGgj8NYkk7gsg-1; Mon, 02 May 2022 14:50:33 -0400 X-MC-Unique: UAgZvLgHNbGgj8NYkk7gsg-1 Received: by mail-qv1-f72.google.com with SMTP id ke27-20020a056214301b00b0045a82079370so4051490qvb.20 for ; Mon, 02 May 2022 11:50:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=uCpb4Qk8i5SY/8aRxlk32ICPpq1SBX7zDb93XwUu7+A=; b=tOHEOIoYIbemdnw4lhoyeu5tVeNQ1I483MHPsZHvcbnrjEeq16i8v5/p6hw+wer7mn njLecRjJHtP4kcZW0KoJHJ9I+QNbKkROV6tAmUNlRgp8wYzVAcDYSGfemoSOCgEeUQyR 3dgFq2b0fF5EPe2y66vcIxvHHoDR7wOH/HAmyhI9W71jx7Hb9pDqQ+XbDYljwQZoykqO JWH+oZ2iRGd37Oj/LTV5DlRri4icKiC/PymztbN1yxsn4Oaz/oOB0R052v5eWSLgCf2e pVB3bsXOcqwBV3ppLBEBGHSfya2RrHgwq7AbBBjmkWeUwrYBZP0LEvZLeLb+ByhPXlr9 xgtQ== X-Gm-Message-State: AOAM530t7QLRMJi+a7S/La0KOu21RtH1yTHa0+flI1SWi0NUxdZpmYI5 Vkd51aA1wqJv5mcfLzuLNDQOVQ3UQBu1oOdbMoRBF4VwLHDw3eelnytt904bciOR2ngk6uS1vcq 1/UhTVVLFJh9z/G/lfUMltmL6IPILzyC/kcqXZiZx0w4HOUhFl876WzByjYjSkqLztrk= X-Received: by 2002:a05:622a:1d6:b0:2e2:db3:17c9 with SMTP id t22-20020a05622a01d600b002e20db317c9mr11637115qtw.665.1651517432154; Mon, 02 May 2022 11:50:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzm/StcnNQVa9mpg9PjFFvfqPQ4bHggNxgsWHm78o9exZqeMcl3Rq1Ef9CT5P+mSCj0jGMOVQ== X-Received: by 2002:a05:622a:1d6:b0:2e2:db3:17c9 with SMTP id t22-20020a05622a01d600b002e20db317c9mr11637097qtw.665.1651517431817; Mon, 02 May 2022 11:50:31 -0700 (PDT) Received: from localhost.localdomain (ool-18e40894.dyn.optonline.net. [24.228.8.148]) by smtp.gmail.com with ESMTPSA id m24-20020ac86898000000b002f39b99f686sm4596406qtq.32.2022.05.02.11.50.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 11:50:31 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: partial spec constraint checking context [PR105220] Date: Mon, 2 May 2022 14:50:29 -0400 Message-Id: <20220502185029.92137-1-ppalka@redhat.com> X-Mailer: git-send-email 2.36.0.44.g0f828332d5 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-14.6 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_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Mon, 02 May 2022 18:50:35 -0000 Currently when checking the constraints of a class template, we do so in the context of the template, not the specialized type. This is the best we can do for a primary template since the specialized type is valid only if the primary template's constraints are satisfied. But for a partial specialization, we can assume the specialized type is valid (as a consequence of constraints being checked only when necessary), so we arguably should check the constraints on a partial specialization more specifically in the context of the specialized type, not the template. This patch implements this by substituting and setting the access context appropriately in satisfy_declaration_constraints. Note that setting the access context in this case is somewhat redundant since the relevant caller most_specialized_partial_spec will already have set the access context to the specialiation, but this redundancy should be harmless. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk and perhaps 12.2 (after the branch is thawed)? PR c++/105220 gcc/cp/ChangeLog: * constraint.cc (satisfy_declaration_constraints): When checking the constraints of a partial template specialization, do so in the context of the specialized type not the template. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-partial-spec12.C: New test. --- gcc/cp/constraint.cc | 17 ++++++++++++++--- .../g++.dg/cpp2a/concepts-partial-spec12.C | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-partial-spec12.C diff --git a/gcc/cp/constraint.cc b/gcc/cp/constraint.cc index 94f6222b436..772f8532b47 100644 --- a/gcc/cp/constraint.cc +++ b/gcc/cp/constraint.cc @@ -3253,11 +3253,22 @@ satisfy_declaration_constraints (tree t, tree args, sat_info info) { if (!push_tinst_level (t, args)) return result; - tree pattern = DECL_TEMPLATE_RESULT (t); + tree ascope = DECL_TEMPLATE_RESULT (t); + if (CLASS_TYPE_P (TREE_TYPE (t)) + && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (t))) + { + gcc_checking_assert (t == most_general_template (t)); + /* When checking the constraints on a partial specialization, + do so in the context of the specialized type, not the template. + This substitution should always succeed since we shouldn't + be checking constraints thereof unless the specialized type + is valid. */ + ascope = tsubst (ascope, args, tf_none, info.in_decl); + } push_to_top_level (); - push_access_scope (pattern); + push_access_scope (ascope); result = satisfy_normalized_constraints (norm, args, info); - pop_access_scope (pattern); + pop_access_scope (ascope); pop_from_top_level (); pop_tinst_level (); } diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-partial-spec12.C b/gcc/testsuite/g++.dg/cpp2a/concepts-partial-spec12.C new file mode 100644 index 00000000000..641d456722d --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-partial-spec12.C @@ -0,0 +1,19 @@ +// PR c++/105220 +// { dg-do compile { target c++20 } } + +template +concept fooable = requires(T t) { t.foo(); }; + +template +struct A; // #1, incomplete + +template +struct A { }; // #2 + +struct B { +private: + friend struct A; + void foo(); +}; + +template struct A; // OK, B::foo() is accessible from #2 -- 2.36.0.44.g0f828332d5