From patchwork Fri Jun 9 15:19:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 70828 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 AE1B4385660E for ; Fri, 9 Jun 2023 15:19:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AE1B4385660E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1686323991; bh=WQcacS6Tt73+JhT1yA6rGU5XJ8FnO8B7nh8U/w68QwQ=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=wPO7soe/Hn6lW640C95uL+NOCp9gny4JFxhzAFTQwM22MSn7d9SQ4oww4tkniU/oJ XUapgaPC9Ac3Ru+rK/t/e9hd+y6lfpLqxo480EdxnJNUOBostLi+tdlHh3p2RPD/+9 9XyvEdQ1dzCL6vvuieAtyElfC7kiFjOAUulCMBdU= 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 5D1583858D3C for ; Fri, 9 Jun 2023 15:19:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5D1583858D3C Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-630-sUKKh_z8OSOMAeOKWKJDSg-1; Fri, 09 Jun 2023 11:19:12 -0400 X-MC-Unique: sUKKh_z8OSOMAeOKWKJDSg-1 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-625fd27e074so24380426d6.3 for ; Fri, 09 Jun 2023 08:19:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686323952; x=1688915952; 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=WQcacS6Tt73+JhT1yA6rGU5XJ8FnO8B7nh8U/w68QwQ=; b=W0YBZ2gUXeB3ItXtqG04Qcmdrcc6Q7l4F9a+XT2WTShM3Uw9x5taX/4EGpWEfr+oda gCjnRS1uIn+6RpaoQ14lVa/4Aon92uRciwspzJurh+jueGpFtycouW6C7hh0D9DfmbVt EnPb4FhOJoobKfwoshqGlFSBEHGh4WWgnKK7SzlzGG+Co7xX1XdNKEC5YplG9yzaqOv6 nJQLCyi5zfOvGsDXB0D8pb+qvtXJs7LTPyKuzE/xYflrIRcTjfU5jD9Y7N6K0iDtE2cx ih9e2WBPfySxns59wNjzNi7cnzJpGVsRBgffU3tBKPEO/mPP24Hb+svIW7DU8+ZdCxSK uH3g== X-Gm-Message-State: AC+VfDwTqVIsZb+RzfauTEPIAYeogL9g0Y/JkfW8RKufVEk7gG5zLaVc R3ADFea0fIYbJw9fdJr07i6zATmBGLTSbfryKvXd6H33TEC+KuqwVtXEnyG3Vt/Km5DvjjpwMfq k9xa/z5X6vLgpZKQOBnhWaGISGMJAKcXJ8Y9Q7wOzwADyq1iYDnoYONZURljyh1qMilEyTpecTa M= X-Received: by 2002:a05:6214:21a7:b0:626:1adb:e10b with SMTP id t7-20020a05621421a700b006261adbe10bmr1984866qvc.13.1686323951633; Fri, 09 Jun 2023 08:19:11 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ41nH9lonkewlHiAIS7daz8+hmONPvcklpNF7q18zIcFX82Pdzk1GTqDB00MR8WWVVbP/3Exg== X-Received: by 2002:a05:6214:21a7:b0:626:1adb:e10b with SMTP id t7-20020a05621421a700b006261adbe10bmr1984839qvc.13.1686323951273; Fri, 09 Jun 2023 08:19:11 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id r17-20020a0ccc11000000b0061b7c0ff41dsm1196822qvk.34.2023.06.09.08.19.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Jun 2023 08:19:10 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: diagnostic ICE b/c of empty TPARMS_PRIMARY_TEMPLATE [PR109655] Date: Fri, 9 Jun 2023 11:19:09 -0400 Message-ID: <20230609151909.3628123-1-ppalka@redhat.com> X-Mailer: git-send-email 2.41.0.rc1.10.g9e49351c30 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_H5, RCVD_IN_MSPIKE_WL, 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" When defining a previously declared class template, we neglect to set TPARMS_PRIMARY_TEMPLATE for the in-scope template parameters, which the class members go on to inherit, and so the members' DECL_TEMPLATE_PARMS will have empty TPARMS_PRIMARY_TEMPLATE at those levels as well. This causes us to crash when diagnosing a constraint mismatch for an out-of-line declaration of a member of a constrained class template. This patch fixes this by walking the context to get at the corresponding primary template instead. I spent a while trying to get us to set TPARMS_PRIMARY_TEMPLATE for templated class definitions that are redeclarations, but it proved to be hairy in particular for partial specializations and nested templates. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk/13? PR c++/109655 gcc/cp/ChangeLog: * pt.cc (push_template_decl): Handle TPARMS_PRIMARY_TEMPLATE being empty when diagnosing a constraint mismatch for an enclosing template scope. Don't bother checking constraints if DECL_PARMS and SCOPE_PARMS are the same. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-class6.C: New test. * g++.dg/cpp2a/concepts-class6a.C: New test. --- gcc/cp/pt.cc | 19 +++++++-- gcc/testsuite/g++.dg/cpp2a/concepts-class6.C | 30 ++++++++++++++ gcc/testsuite/g++.dg/cpp2a/concepts-class6a.C | 40 +++++++++++++++++++ 3 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-class6.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-class6a.C diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 17bf4d24151..f913b248345 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -6155,12 +6155,25 @@ push_template_decl (tree decl, bool is_friend) decl_parms = TREE_CHAIN (decl_parms); scope_parms = TREE_CHAIN (scope_parms); } - while (decl_parms) + while (decl_parms && decl_parms != scope_parms) { if (!template_requirements_equivalent_p (decl_parms, scope_parms)) { - error ("redeclaration of %qD with different constraints", - TPARMS_PRIMARY_TEMPLATE (TREE_VALUE (decl_parms))); + tree td = TPARMS_PRIMARY_TEMPLATE (TREE_VALUE (decl_parms)); + if (!td) + { + /* FIXME: TPARMS_PRIMARY_TEMPLATE doesn't always get + set for enclosing template scopes. Work around + this by walking the context to obtain the relevant + (primary) template whose constraints we mismatch. */ + int level = TMPL_PARMS_DEPTH (decl_parms); + td = TYPE_TI_TEMPLATE (ctx); + while (!PRIMARY_TEMPLATE_P (td) + || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)) + != level)) + td = TYPE_TI_TEMPLATE (DECL_CONTEXT (td)); + } + error ("redeclaration of %qD with different constraints", td); break; } decl_parms = TREE_CHAIN (decl_parms); diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-class6.C b/gcc/testsuite/g++.dg/cpp2a/concepts-class6.C new file mode 100644 index 00000000000..dcef6a2c9d4 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-class6.C @@ -0,0 +1,30 @@ +// PR c++/109655 +// { dg-do compile { target c++20 } } + +class C { + template + requires true + friend class D; + + template + requires true + class E; +}; + +template +requires true +class D { + void f(); +}; + +template +void D::f() { } // { dg-error "class D' with different constraints" } + +template +requires true +class C::E { + void f(); +}; + +template +void C::E::f() { } // { dg-error "class C::E' with different constraints" } diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-class6a.C b/gcc/testsuite/g++.dg/cpp2a/concepts-class6a.C new file mode 100644 index 00000000000..751d13cdf6c --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-class6a.C @@ -0,0 +1,40 @@ +// PR c++/109655 +// { dg-do compile { target c++20 } } + +template +requires true +class C { + class D; + + template + requires (!!true) + class E; +}; + +template +requires true +class C::D { + void f(); +}; + +template // missing "requires true" +void C::D::f() { } // { dg-error "class C' with different constraints" } + +template +requires true +template +requires (!!true) +class C::E { + void f(); + void g(); +}; + +template +requires true +template +void C::E::f() { } // { dg-error "class C::E' with different constraints" } + +template +template +requires (!!true) +void C::E::g() { } // { dg-error "class C' with different constraints" }