From patchwork Wed May 25 17:24:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 54384 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 DF7413856DCB for ; Wed, 25 May 2022 17:25:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DF7413856DCB DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1653499525; bh=z2FRtOxv53Co5DpAcIu/rUBqQ076jQ8c5vM/d0fq8mE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=xnHI/EDCXbwv0aL4Mtlml/d1UXfIjkrepCt/rKUnwueQlcA4eDNHP2JAMZXrDG2Lx oiQbORfS7Ljq/gjh+5TbqqYH1kvYwskti4RAwguGMrcS+uXbVWUrEjR0jBzHwXwaPv NiiDzPzpyoKv9B0YLY0WMq65Psyinh/BnhfjSJro= 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 74088382F98C for ; Wed, 25 May 2022 17:24:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 74088382F98C Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-278-gyE-25MEMs2FdsA7JED7tw-1; Wed, 25 May 2022 13:24:15 -0400 X-MC-Unique: gyE-25MEMs2FdsA7JED7tw-1 Received: by mail-qk1-f198.google.com with SMTP id i2-20020a05620a144200b006a3a8651de1so5398724qkl.14 for ; Wed, 25 May 2022 10:24:14 -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=z2FRtOxv53Co5DpAcIu/rUBqQ076jQ8c5vM/d0fq8mE=; b=zaappS05diSMxCHfbhmLwEIUifR4HuSzrdnisYEGiMYwTeWMLNZ9I8+q1GR5XHU4oD jo8K5XutGB53c+NkbiqBg2VZnvzNgOyTGl7oetq74QhW0MabzQYAlO/Q6/nLlzKs5ZPZ cGPqdXMHKRj7PiEhERZlnDnFaTHg883KBf9VzTuF9L/GQaawtXobur9DFZZLTMqUeIAr 5QenxHXMh6MwQnuyKOB+eVba02RXbIgqNN6yFft2sXmwdqb5UqF5Ojhu59pY7aEGgjU6 +7b2oEGNgOVpEdZIzbqHLtE7XMeqzufSjy6mYUsN/v67g0gONTGVRzxzQR5ppuXgfqfS VLzw== X-Gm-Message-State: AOAM531pjL2680xECnW3GsdVCaYMTXeZIeMJZ1UZWLZUegnFpP6VCgLA v3dP/PPxoaDsI5gPpdxux7tNJNFTeCu3efIGqvj1mrNNt3uDof6A8Q9kqRyMdw2Bp21LZQx94GP rbsUXdhLNMMDzWtsbltwCy26I3gXx0OOEclMinRc43t4kHZxnzHADohPzYLMNosoaB4s= X-Received: by 2002:a05:620a:4116:b0:6a5:891d:bfb1 with SMTP id j22-20020a05620a411600b006a5891dbfb1mr4331649qko.664.1653499454026; Wed, 25 May 2022 10:24:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyFsB4FXJw6eokIb/WrdOzAoigu+lTnBLPeK6ZmZvyMu2/iscFmfLJjSUIjUTkH0A6BuNF7uA== X-Received: by 2002:a05:620a:4116:b0:6a5:891d:bfb1 with SMTP id j22-20020a05620a411600b006a5891dbfb1mr4331624qko.664.1653499453651; Wed, 25 May 2022 10:24:13 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id y187-20020a3796c4000000b006a3750e8b25sm1638569qkd.82.2022.05.25.10.24.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 May 2022 10:24:13 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: constrained partial spec forward decl [PR96363] Date: Wed, 25 May 2022 13:24:11 -0400 Message-Id: <20220525172411.1922336-1-ppalka@redhat.com> X-Mailer: git-send-email 2.36.1.163.g7a3eb28697 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.2 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, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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 during cp_parser_single_declaration for #2, we were calling associate_classtype_constraints for TPL (the primary template type) before maybe_process_partial_specialization could get a chance to notice that we're in fact declaring a distinct constrained partial spec and not redeclaring the primary template. This caused us to emit a bogus error about differing constraints b/t the primary template the current constraints at #2. This patch fixes this by moving the call to associate_classtype_constraints after the call to shadow_tag (which calls maybe_process_partial_specialization) and adjusting shadow_tag to use the return value of m_p_p_s. Moreover, if we later try to define a constrained partial specialization that's been declared earlier (as in the third testcase), then maybe_new_partial_specialization correctly notices that it's a redeclaration and returns NULL_TREE. But we need to also update TYPE to point to the constrained class type in this case (it'll otherwise continue to point to the primary template type, eventually leading to a bogus error). Bootstrapped and regtested on x86_64-pc-linux-gnu, also tested against cmcstl and range-v3, does this look OK for trunk? Since it should only affect concepts code, I wonder about backporting this for 12.2? PR c++/96363 gcc/cp/ChangeLog: * decl.cc (shadow_tag): Use the return value of maybe_process_partial_specialization. * parser.cc (cp_parser_single_declaration): Call shadow_tag before associate_classtype_constraints. * pt.cc (maybe_new_partial_specialization): Change return type to bool. Take 'type' argument by mutable reference. Set 'type' to point to the correct constrained specialization when appropriate. (maybe_process_partial_specialization): Adjust accordingly. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-partial-spec12.C: New test. * g++.dg/cpp2a/concepts-partial-spec13.C: New test. --- gcc/cp/decl.cc | 3 +- gcc/cp/parser.cc | 12 +++--- gcc/cp/pt.cc | 38 ++++++++++--------- .../g++.dg/cpp2a/concepts-partial-spec12.C | 10 +++++ .../g++.dg/cpp2a/concepts-partial-spec12a.C | 14 +++++++ .../g++.dg/cpp2a/concepts-partial-spec13.C | 16 ++++++++ 6 files changed, 69 insertions(+), 24 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-partial-spec12.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-partial-spec12a.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-partial-spec13.C diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 381259cb9cf..c7caa12f061 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -5464,7 +5464,8 @@ shadow_tag (cp_decl_specifier_seq *declspecs) if (!t) return NULL_TREE; - if (maybe_process_partial_specialization (t) == error_mark_node) + t = maybe_process_partial_specialization (t); + if (t == error_mark_node) return NULL_TREE; /* This is where the variables in an anonymous union are diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index 868b8610d60..d9e78e1f4cc 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -31811,12 +31811,6 @@ cp_parser_single_declaration (cp_parser* parser, if (cp_parser_declares_only_class_p (parser) || (declares_class_or_enum & 2)) { - /* If this is a declaration, but not a definition, associate - any constraints with the type declaration. Constraints - are associated with definitions in cp_parser_class_specifier. */ - if (declares_class_or_enum == 1) - associate_classtype_constraints (decl_specifiers.type); - decl = shadow_tag (&decl_specifiers); /* In this case: @@ -31838,6 +31832,12 @@ cp_parser_single_declaration (cp_parser* parser, else decl = error_mark_node; + /* If this is a declaration, but not a definition, associate + any constraints with the type declaration. Constraints + are associated with definitions in cp_parser_class_specifier. */ + if (declares_class_or_enum == 1) + associate_classtype_constraints (TREE_TYPE (decl)); + /* Perform access checks for template parameters. */ cp_parser_perform_template_parameter_access_checks (checks); diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index b45a29926d2..7de9b11bd12 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -874,12 +874,12 @@ check_explicit_instantiation_namespace (tree spec) spec, current_namespace, ns); } -/* Returns the type of a template specialization only if that - specialization needs to be defined. Otherwise (e.g., if the type has - already been defined), the function returns NULL_TREE. */ +/* Returns true if TYPE is a new partial specialization that needs to be + set up. This may also modify TYPE to point to the correct (new or + existing) constrained partial specialization in any case. */ -static tree -maybe_new_partial_specialization (tree type) +static bool +maybe_new_partial_specialization (tree& type) { /* An implicit instantiation of an incomplete type implies the definition of a new class template. @@ -893,7 +893,7 @@ maybe_new_partial_specialization (tree type) Here, S is an implicit instantiation of S whose type is incomplete. */ if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type)) - return type; + return true; /* It can also be the case that TYPE is a completed specialization. Continuing the previous example, suppose we also declare: @@ -919,11 +919,11 @@ maybe_new_partial_specialization (tree type) /* If there are no template parameters, this cannot be a new partial template specialization? */ if (!current_template_parms) - return NULL_TREE; + return false; /* The injected-class-name is not a new partial specialization. */ if (DECL_SELF_REFERENCE_P (TYPE_NAME (type))) - return NULL_TREE; + return false; /* If the constraints are not the same as those of the primary then, we can probably create a new specialization. */ @@ -933,7 +933,7 @@ maybe_new_partial_specialization (tree type) { tree main_constr = get_constraints (tmpl); if (equivalent_constraints (type_constr, main_constr)) - return NULL_TREE; + return false; } /* Also, if there's a pre-existing specialization with matching @@ -946,7 +946,10 @@ maybe_new_partial_specialization (tree type) tree spec_constr = get_constraints (spec_tmpl); if (comp_template_args (args, spec_args) && equivalent_constraints (type_constr, spec_constr)) - return NULL_TREE; + { + type = TREE_TYPE (spec_tmpl); + return false; + } specs = TREE_CHAIN (specs); } @@ -971,10 +974,11 @@ maybe_new_partial_specialization (tree type) set_instantiating_module (d); DECL_MODULE_EXPORT_P (d) = DECL_MODULE_EXPORT_P (tmpl); - return t; + type = t; + return true; } - return NULL_TREE; + return false; } /* The TYPE is being declared. If it is a template type, that means it @@ -1030,16 +1034,16 @@ maybe_process_partial_specialization (tree type) Make sure that `C' and `C' are implicit instantiations. */ - if (tree t = maybe_new_partial_specialization (type)) + if (maybe_new_partial_specialization (type)) { - if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t)) + if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type)) && !at_namespace_scope_p ()) return error_mark_node; - SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t); - DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location; + SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type); + DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location; if (processing_template_decl) { - tree decl = push_template_decl (TYPE_MAIN_DECL (t)); + tree decl = push_template_decl (TYPE_MAIN_DECL (type)); if (decl == error_mark_node) return error_mark_node; return TREE_TYPE (decl); 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..7868092af2b --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-partial-spec12.C @@ -0,0 +1,10 @@ +// PR c++/96363 +// { dg-do compile { target c++20 } } + +template class TPL; + +template requires true class TPL; // #1 +template requires false class TPL; // #2 error here + +template requires true class TPL; // #1 +template requires false class TPL; // #2 error here diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-partial-spec12a.C b/gcc/testsuite/g++.dg/cpp2a/concepts-partial-spec12a.C new file mode 100644 index 00000000000..18e67f70944 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-partial-spec12a.C @@ -0,0 +1,14 @@ +// PR c++/96363 +// { dg-do compile { target c++20 } } +// A version of concepts-partial-spec12.C where the primary template is +// constrained. + +template concept C = true; + +template class TPL; + +template requires true class TPL; // #1 +template requires false class TPL; // #2 error here + +template requires true class TPL; // #1 +template requires false class TPL; // #2 error here diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-partial-spec13.C b/gcc/testsuite/g++.dg/cpp2a/concepts-partial-spec13.C new file mode 100644 index 00000000000..78f6906b1ab --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-partial-spec13.C @@ -0,0 +1,16 @@ +// PR c++/99501 +// { dg-do compile { target c++20 } } + +template struct X{}; + +template requires requires{V.a;} struct X; +template requires requires{V.b;} struct X; + +template requires requires{V.a;} struct X { static const bool v = false; }; +template requires requires{V.b;} struct X { static const bool v = true; }; + +struct A {int a; }; +static_assert(!X::v); + +struct B { int b; }; +static_assert(X::v);