From patchwork Thu Sep 15 15:58:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 57669 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 730893821FC9 for ; Thu, 15 Sep 2022 15:58:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 730893821FC9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1663257537; bh=vDJtb2iB2GeWCaY06TB7jgXZz6tfIFphYPDFk1Wc7s0=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Sxl61fyKZLXP7u11KWdXrj7MLEf4qez1/N2008mD5X5T2NEl9hbkJHBA/DD/aORz8 iQjI1htRe/dXDH33HjqzHgI/afCJsBWezK0sG6sycJsx4sSOelL25P46D0ssPrgk+9 Ush+8B8K2FL6rA9Jm2cxVqjCHMfss0QE5J+0/Ahs= 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 19B14388B691 for ; Thu, 15 Sep 2022 15:58:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 19B14388B691 Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-547-ml-rvT9HNgCNKTwoCetWYw-1; Thu, 15 Sep 2022 11:58:26 -0400 X-MC-Unique: ml-rvT9HNgCNKTwoCetWYw-1 Received: by mail-qk1-f197.google.com with SMTP id bp17-20020a05620a459100b006ce7f4bb0b7so5314584qkb.5 for ; Thu, 15 Sep 2022 08:58:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date; bh=vDJtb2iB2GeWCaY06TB7jgXZz6tfIFphYPDFk1Wc7s0=; b=hi8/sbuKS/iPZv+mJdRCECuuFxNtzL/Dy3OC/DuoBGUSN1/YHPPS+pdZyRQ+OtCH8X S9pR+mm2LVQemdlZRgyTikDwRbFNkw/J0Vp8dIsGKNKMAooBev1sg44Yr4gjpahJAbnY yHKTebgxQ5EC7/bIlyIc/Aoq5PPdn6E5R0MohuuorVkuul1YUTaHKFNPD9YrvpAWNUVk FBOiHW/w12YudQhOVZzS46d6KvsNGKJjuib70LNPI+qUHY2IzEUUPmGKsICNakBbdYiU uZsGcEL/ofTsXGrNZD8VALCMJEkLP46ujDIuYrmW+TispuKiuahPdR3VsOm5k8IuugkU KgPQ== X-Gm-Message-State: ACrzQf3zg/T1VUJUmVcbTEezMkxmKMDOlD26e/hzO2krjEBce6YwiTzj qxEr3z7G2Qt1FinROJGYTAjB/mCtyFsQLqkRGVLOyPCTad2arjUh9MXX/JeSsjh+1sdrXQwt5E5 HorB8/l2TwieBPfFKwotO3CNcSxOgUnrFLjELjiB9ckY8dQ/wtjHpR4q9yUJzm/Oftg8= X-Received: by 2002:a05:622a:1102:b0:343:66b0:4a2 with SMTP id e2-20020a05622a110200b0034366b004a2mr461591qty.495.1663257505452; Thu, 15 Sep 2022 08:58:25 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5f0a0VoZpBh0f+uAncMFA8Vamdi4S0VzSe5Da5D/TpiWi6OxtBTYcxzrHC4TrpCbACjj91cA== X-Received: by 2002:a05:622a:1102:b0:343:66b0:4a2 with SMTP id e2-20020a05622a110200b0034366b004a2mr461565qty.495.1663257505087; Thu, 15 Sep 2022 08:58:25 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id do11-20020a05620a2b0b00b006bbb07ebd83sm4157254qkb.108.2022.09.15.08.58.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Sep 2022 08:58:24 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: constraint matching, TEMPLATE_ID_EXPR, current inst Date: Thu, 15 Sep 2022 11:58:22 -0400 Message-Id: <20220915155822.4021344-1-ppalka@redhat.com> X-Mailer: git-send-email 2.37.3.662.g36f8e7ed7d MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.4 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 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 we're crashing during constraint matching for the instantiated hidden friends due to two issues with dependent substitution into a TEMPLATE_ID_EXPR naming a template from the current instantiation (as performed from maybe_substitute_reqs_for for C<3> with T=T): * tsubst_copy substitutes into such a TEMPLATE_DECL by looking it up from the substituted class scope. But for this to not fail when the args are dependent, we need to pass entering_scope=true for the class scope substitution so that we obtain the primary template type A (which has TYPE_BINFO) instead of the implicit instantiation A (which doesn't). * lookup_and_finish_template_variable shouldn't instantiate a TEMPLATE_ID_EXPR that names a TEMPLATE_DECL which has more than one level of (unsubstituted) parameters (such as A::C). Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? gcc/cp/ChangeLog: * pt.cc (lookup_and_finish_template_variable): Don't instantiate if the template's scope is dependent. (tsubst_copy) : Pass entering_scope=true when substituting the class scope. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-friend10.C: New test. --- gcc/cp/pt.cc | 14 +++++++------ .../g++.dg/cpp2a/concepts-friend10.C | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-friend10.C diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index db4e808adec..bfcbe0b8670 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -10475,14 +10475,15 @@ tree lookup_and_finish_template_variable (tree templ, tree targs, tsubst_flags_t complain) { - templ = lookup_template_variable (templ, targs); - if (!any_dependent_template_arguments_p (targs)) + tree var = lookup_template_variable (templ, targs); + if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (templ)) == 1 + && !any_dependent_template_arguments_p (targs)) { - templ = finish_template_variable (templ, complain); - mark_used (templ); + var = finish_template_variable (var, complain); + mark_used (var); } - return convert_from_reference (templ); + return convert_from_reference (var); } /* If the set of template parameters PARMS contains a template parameter @@ -17282,7 +17283,8 @@ tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl) TEMPLATE_DECL with `D' as its DECL_CONTEXT. Now we have to substitute this with one having context `D'. */ - tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl); + tree context = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, + in_decl, /*entering_scope=*/true); return lookup_field (context, DECL_NAME(t), 0, false); } else diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-friend10.C b/gcc/testsuite/g++.dg/cpp2a/concepts-friend10.C new file mode 100644 index 00000000000..4b21a379f59 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-friend10.C @@ -0,0 +1,21 @@ +// { dg-do compile { target c++20 } } +// Verify we don't crash during constraint matching containing +// a TEMPLATE_ID_EXPR referring to a template from the current +// instantiation. + +template +struct A { + template static constexpr bool C = sizeof(T) > N; + friend constexpr void f(A) requires C<3> { } + friend constexpr void f(A) requires C<3> || true { } +}; + +template +struct A { + template static constexpr bool C = sizeof(T) > N; + friend constexpr void g(A) requires C<3> { } + friend constexpr void g(A) requires C<3> || true { } +}; + +template struct A; +template struct A;