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: 1238 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp343976wrt; Thu, 15 Sep 2022 08:59:12 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6X2GKufQhCY8UN9rVEGg9443YlCWeCm+SDcV43hKHmU4P3+JtbtId5KRKMRMCBLPbQkSgi X-Received: by 2002:a17:907:1c96:b0:780:602c:677c with SMTP id nb22-20020a1709071c9600b00780602c677cmr448420ejc.736.1663257552730; Thu, 15 Sep 2022 08:59:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663257552; cv=none; d=google.com; s=arc-20160816; b=La+dQ9Q4rIATKVJB50Xwv0BACiJFbRcK3GdzfM2u5M32rpJrBMlUBRDHGObkl+is8U o+nkRkNDTHBtLIVNPd8hS7mcMs+xaUbb0kTZmq7K+WBOnTZKHypjusfDv7IoJAb3FiFo OqAKcqcsqvXzYKl4ycvo9v84U4FdzCu1Od8JHTbkOjdquFjdctIn9rIlyd/e9SQnhz+0 xytJ8NlqzWSE60c2l2DcJBesshGbNzncfFMJsAbnzGeWXKlQYw0vbcJJyOWPjsGsn5SO D+ghK0WnAC68WyStrBVILYWSOrzcyEH+Ry2je6yUOIjQSLkkUN8AAWecbIUnAiNGT4NO iOGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=vDJtb2iB2GeWCaY06TB7jgXZz6tfIFphYPDFk1Wc7s0=; b=0i5/g6fEu9vHq3Iah9o7KfjGiqIczsH9KJCnAST0kLXyBHQKYVW4hXajU/OWI2yRNs DSIXSPAsnyjd/hZ6LEEQL3x9eqb07Ejyzqi4NgO9j9G65vuBYWx7SGr/0+zEKpM6oPWO Q3KBQ+2hTAI0/Sfzbq6zBDyWrfDeNqGSR3mxyOpH4BMiNzY1+8nbaOcoeYClyHMM2tM6 iKTxy98aPvG+S3wgxKIPPVYh0o+Ma0gzb4uXtws1i+oIDNR4+ItyctGxO7q5aenv9806 1LbfP97T2cLrlIgWO405SMfvfJCnPCTMdw5LXM8gDIfTCsIKC1+ke8mCExr/5tYU9Not b0BA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=v7nh23MP; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id bt20-20020a170906b15400b007763cc7093csi12068977ejb.884.2022.09.15.08.59.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Sep 2022 08:59:12 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=v7nh23MP; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6D37E3821FDF for ; Thu, 15 Sep 2022 15:59:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6D37E3821FDF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1663257551; bh=vDJtb2iB2GeWCaY06TB7jgXZz6tfIFphYPDFk1Wc7s0=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=v7nh23MPbe8xUUpcvNo+fVpHjeDnUDQczNImdhvXkG869Hbv1AIwuMZxzBc9WbY3P Zl1sIjxd1ur+oPNRKmSznAxEQp8X9XELE97ikudpdCcRB0okAAH5lHLi8wENzaztNX qdueGskm6713OBVyQYlCkH5tCLQDPHWmK70Pd2c4= 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-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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1744051951744736453?= X-GMAIL-MSGID: =?utf-8?q?1744051951744736453?= 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;