From patchwork Tue Mar 14 16:41:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 66383 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 536783858418 for ; Tue, 14 Mar 2023 16:42:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 536783858418 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678812146; bh=vILeZ1/eqZvtQ0qPsHaoUgcg8q8MBjGpbQUuCORPlao=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=F+M4FhqZUxcPxx/BCreahi6RlHGqbpUI/SqEdNIAosvYyKC5uKMCGotUMSNxrOxs2 qSAse7GOJju1EOyfu1Ufg2zR5rbkwFl7tIQ74AwY4LyrnQPmGUqi1yI0ZeUUF7zAJ4 6X6x4P+DklKHAmT8chVyRIZSfSwkBz9P7+P9Fdf0= 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 55DE73858C33 for ; Tue, 14 Mar 2023 16:41:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 55DE73858C33 Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-187-qxk9GL1uPhuG0pvVoENn7A-1; Tue, 14 Mar 2023 12:41:50 -0400 X-MC-Unique: qxk9GL1uPhuG0pvVoENn7A-1 Received: by mail-qk1-f200.google.com with SMTP id u6-20020a05620a120600b00745721af155so4048695qkj.10 for ; Tue, 14 Mar 2023 09:41:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678812109; 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=vILeZ1/eqZvtQ0qPsHaoUgcg8q8MBjGpbQUuCORPlao=; b=m/GPsOMpCi8lF35U+jnzjdwjPlXQii8iYEVr8gT1JDSOT0SbhTjmETh39Pzzuq/fCr xjgPeP13lS/lVdRTN4M7SgsoSrIdabxlN07voW6h6v6hILChynvJmPukRAdlvgLrgj3Z SpVK01sKz6Wa4j8hBTadePNj2fYxCdBVSquJiJlKDk1EMCLbuSRXrP5uN4p5BG1cPfJx BmeoiRCCJ5T0Sf5048L7nCQUrQ9PqB8nOR9JnvmQtq5A1TRn8Anz/fTKqOUzs+tXVjmf r518thsoXe4vtr8v20CojGh5OKUZqFfkIEZUZN9KkRyAG+E3Adq/3hlFAMZ8Sdy70bds 4Nuw== X-Gm-Message-State: AO0yUKW8XnRY9TiCCJNoALdMrTl+OyTNBhcuX6i63qcA0Q3Ov1OAAZRZ SOLaI9Hrf4MQBHJRTJh99QjBSsuOBsD8aNXSskSH9Prr0jVkFl1uBkJe0pZU89J7kHx7lrW7m4/ MpWd11Z6/ZFc3x8l4jAdnFVAJoandtG3zV6Ps7aerMFtpNLnk25d3pmUWu9koQwhkQYFQDD6EFJ g= X-Received: by 2002:ac8:7c4e:0:b0:3b9:c352:640e with SMTP id o14-20020ac87c4e000000b003b9c352640emr72389928qtv.46.1678812108866; Tue, 14 Mar 2023 09:41:48 -0700 (PDT) X-Google-Smtp-Source: AK7set9MCnKSS04LjO1oJCAjLU5y9Vpy9Z1XFrREWWhwMZM5+jdG/6gu2+IhWPO3kSowcjqaFaVGSw== X-Received: by 2002:ac8:7c4e:0:b0:3b9:c352:640e with SMTP id o14-20020ac87c4e000000b003b9c352640emr72389854qtv.46.1678812108288; Tue, 14 Mar 2023 09:41:48 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id a20-20020aed2794000000b003b9a573aec6sm2089207qtd.70.2023.03.14.09.41.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Mar 2023 09:41:47 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH 1/2] c++: constrained template friend class matching [PR96830] Date: Tue, 14 Mar 2023 12:41:45 -0400 Message-Id: <20230314164146.1470993-1-ppalka@redhat.com> X-Mailer: git-send-email 2.40.0 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Here when instantiating a constrained template friend naming an already declared class template, we erroneously pass the existing template's constraints instead of the friend declaration's constraints to redeclare_class_template, which causes the constraint comparison check therein to always be vacuously true, and we fail to diagnose legitimate constraint mismatches. Bootstrapped and regtested on x86_64-pc-linxu-gnu, does this look OK for trunk/12? PR c++/96830 gcc/cp/ChangeLog: * pt.cc (redeclare_class_template): Add missing "of" in constraint mismatch diagnostic. (tsubst_friend_class): For an already declared class template, substitute and pass the friend declaration's constraints to redeclare_class_template instead of passing the existing template's constraints. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-friend14.C: New test. --- gcc/cp/pt.cc | 8 ++++-- .../g++.dg/cpp2a/concepts-friend14.C | 26 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-friend14.C diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 85136df1730..053cc52b285 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -6377,7 +6377,7 @@ redeclare_class_template (tree type, tree parms, tree cons) if (!cp_tree_equal (req1, req2)) { auto_diagnostic_group d; - error_at (input_location, "redeclaration %q#D with different " + error_at (input_location, "redeclaration of %q#D with different " "constraints", tmpl); inform (DECL_SOURCE_LOCATION (tmpl), "original declaration appeared here"); @@ -11503,7 +11503,11 @@ tsubst_friend_class (tree friend_tmpl, tree args) tf_warning_or_error); location_t saved_input_location = input_location; input_location = DECL_SOURCE_LOCATION (friend_tmpl); - tree cons = get_constraints (tmpl); + tree cons = get_constraints (friend_tmpl); + ++processing_template_decl; + cons = tsubst_constraint_info (cons, args, tf_warning_or_error, + DECL_FRIEND_CONTEXT (friend_tmpl)); + --processing_template_decl; redeclare_class_template (TREE_TYPE (tmpl), parms, cons); input_location = saved_input_location; } diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-friend14.C b/gcc/testsuite/g++.dg/cpp2a/concepts-friend14.C new file mode 100644 index 00000000000..f46c7e4bdfb --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-friend14.C @@ -0,0 +1,26 @@ +// PR c++/96830 +// { dg-do compile { target c++20 } } + +template requires true +struct A { + template friend struct A; // { dg-error "different constraints" } + template requires (!!true) friend struct A; // { dg-error "different constraints" } +}; + +template +struct B { + template requires true friend struct A; + template requires true friend struct B; // { dg-error "different constraints" } +}; + +template concept C = true; + +template +struct D { + template friend struct D; // { dg-error "different constraints" } + template friend struct D; +}; + +template struct A; +template struct B; +template struct D; From patchwork Tue Mar 14 16:41:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 66384 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F263F3854830 for ; Tue, 14 Mar 2023 16:42:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F263F3854830 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678812147; bh=/Z2g7KScDIv6P+O0uKj4H0kIo36tez5pEuKKPxe7qPU=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=XzwJvHetrF/H9FqLzQiw40UPShT7A84PvpqEcJMrcTEGEYtGkut/L4/K7SdNWYELp aE18PIG9pgwbNKh6Zjfl4dMSUGiLNTZoUGQ6MyNe/m3DUPW9afQiTs9lCZguVBLR1I NSwm1B8ZnMbf6MaL5Td6F7aR+mqG3jYuujGlEaW8= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id DA08A385842E for ; Tue, 14 Mar 2023 16:41:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DA08A385842E Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-638-wbbdlTPeOUeIFstrUCX9MA-1; Tue, 14 Mar 2023 12:41:51 -0400 X-MC-Unique: wbbdlTPeOUeIFstrUCX9MA-1 Received: by mail-qk1-f199.google.com with SMTP id eb13-20020a05620a480d00b00742be8a1a17so8997737qkb.18 for ; Tue, 14 Mar 2023 09:41:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678812110; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/Z2g7KScDIv6P+O0uKj4H0kIo36tez5pEuKKPxe7qPU=; b=ZpUgXUl9BJ/XT/O9yc61i+epEt0QrVp3vtTHMBVJykANX2DNCfe4YH7qj42ef+AHiK G618TQcZrsS0K65g4WcRNe8zHJB/1voTJXKM2UW2aABKdGM6P5G1QqKqEx6Xdalx4pyi 5oNwP3oMLFgSPAsoXEKf6Nq7mx3lYaPgEnHOJ/YrtH2PxO+mdIfzewkyroBX4yMn4b7b A6C/W77U4Mu4NI7vUQEXd0uz3ftVO3bwvPwcygK7BSD8seqQ70pzuyFRZskPmK7v8Gk9 ImdZ6/VgdefT3nRqgpr9U6sZaWatawU0VSB7Vglhz6+il/GW3JcFjbn3ZXYGFgdgalAA O86w== X-Gm-Message-State: AO0yUKVoxIn34PSt0jQDzKkq0aK7f872HPLL2McUY0J8TqrPObqEdVJo wQOMw4rRcSxnE8NdUPz3DT3Ro4owCOlHpe+LhaIDec7Q4r/yCIQG7SvGpjThK3qZ6AXCHd9ILma k4cpcQQEgvloUDqE2hseqFHnAL+R6v7hfcVueDglJXur/XyOVcs0dRAsXLpPstAxSU5r79MqmeA g= X-Received: by 2002:ac8:7d4c:0:b0:3bf:d196:5e27 with SMTP id h12-20020ac87d4c000000b003bfd1965e27mr66872994qtb.20.1678812110453; Tue, 14 Mar 2023 09:41:50 -0700 (PDT) X-Google-Smtp-Source: AK7set87TvEsPy7jKpIUll+wtMA2nVdvrRtv1hpYTC+sNa99bswAg7HKdmmahWCKT4i9vYDjCYHstQ== X-Received: by 2002:ac8:7d4c:0:b0:3bf:d196:5e27 with SMTP id h12-20020ac87d4c000000b003bfd1965e27mr66872958qtb.20.1678812110074; Tue, 14 Mar 2023 09:41:50 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id a20-20020aed2794000000b003b9a573aec6sm2089207qtd.70.2023.03.14.09.41.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Mar 2023 09:41:49 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH 2/2] c++: redeclaring member of constrained class template [PR96830] Date: Tue, 14 Mar 2023 12:41:46 -0400 Message-Id: <20230314164146.1470993-2-ppalka@redhat.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230314164146.1470993-1-ppalka@redhat.com> References: <20230314164146.1470993-1-ppalka@redhat.com> 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" When redeclaring a member of a constrained class template, we need to repeat the class's constraints, but it turns out we don't verify anywhere that the repeated constraints match the class's constraints. A safe place to do so seems to be in push_template_decl, nearby a similar consistency check for the template parameter list length of a redeclaration. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk/12? PR c++/96830 gcc/cp/ChangeLog: * pt.cc (push_inline_template_parms_recursive): Set TEMPLATE_PARMS_CONSTRAINTS. (push_template_decl): For a redeclaration, verify constraints for each enclosing template scope match those of the original template declaratation. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-class5.C: New test. * g++.dg/cpp2a/concepts-class5a.C: New test. --- gcc/cp/pt.cc | 26 +++++++++++++ gcc/testsuite/g++.dg/cpp2a/concepts-class5.C | 37 ++++++++++++++++++ gcc/testsuite/g++.dg/cpp2a/concepts-class5a.C | 38 +++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-class5.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-class5a.C diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 053cc52b285..370a4f70f08 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -471,6 +471,8 @@ push_inline_template_parms_recursive (tree parmlist, int levels) current_template_parms = tree_cons (size_int (current_template_depth + 1), parms, current_template_parms); + TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) + = TEMPLATE_PARMS_CONSTRAINTS (parmlist); TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1; begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec, @@ -6134,6 +6136,30 @@ push_template_decl (tree decl, bool is_friend) DECL_INTERFACE_KNOWN (decl) = 1; return error_mark_node; } + + /* Check that the constraints for each enclosing template scope are + consistent with the original template declaration. */ + if (flag_concepts) + { + tree decl_parms = DECL_TEMPLATE_PARMS (tmpl); + tree scope_parms = current_template_parms; + if (PRIMARY_TEMPLATE_P (tmpl)) + { + decl_parms = TREE_CHAIN (decl_parms); + scope_parms = TREE_CHAIN (scope_parms); + } + while (decl_parms) + { + if (!template_requirements_equivalent_p (decl_parms, scope_parms)) + { + error ("redeclaration of %qD with different constraints", + TPARMS_PRIMARY_TEMPLATE (TREE_VALUE (decl_parms))); + break; + } + decl_parms = TREE_CHAIN (decl_parms); + scope_parms = TREE_CHAIN (scope_parms); + } + } } gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl); diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-class5.C b/gcc/testsuite/g++.dg/cpp2a/concepts-class5.C new file mode 100644 index 00000000000..5d893d9f6a7 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-class5.C @@ -0,0 +1,37 @@ +// PR c++/96830 +// { dg-do compile { target c++20 } } + +template concept C = true; + +template requires true +struct A { + void f(); + template void g(); + + struct B; + template struct C; + + static int D; + template static int E; +}; + +template +void A::f() { } // { dg-error "different constraints" } + +template requires true +template +void A::g() { } // { dg-error "different constraints" } + +template requires (!!true) +struct A::B { }; // { dg-error "different constraints" } + +template requires true +template +struct A::C { }; // { dg-error "different constraints" } + +template +int A::D = 0; // { dg-error "different constraints" } + +template requires true +template +int A::E = 0; // { dg-error "different constraints" } diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-class5a.C b/gcc/testsuite/g++.dg/cpp2a/concepts-class5a.C new file mode 100644 index 00000000000..a6a3684c258 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-class5a.C @@ -0,0 +1,38 @@ +// PR c++/96830 +// A valid version of concepts-class5.C. +// { dg-do compile { target c++20 } } + +template concept C = true; + +template requires true +struct A { + void f(); + template void g(); + + struct B; + template struct C; + + static int D; + template static int E; +}; + +template requires true +void A::f() { } + +template requires true +template +void A::g() { } + +template requires true +struct A::B { }; + +template requires true +template +struct A::C { }; + +template requires true +int A::D = 0; + +template requires true +template +int A::E = 0;