From patchwork Thu Sep 5 14:54:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 97177 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 07F243865C29 for ; Thu, 5 Sep 2024 14:55:25 +0000 (GMT) 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 ESMTP id 847A13858294 for ; Thu, 5 Sep 2024 14:54:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 847A13858294 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 847A13858294 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725548095; cv=none; b=DwU6LGG8H4TOknLWA2o+YiXQ+j1tuGmF4UJk4kLTd9Iq1j+hA6JwjK8U+/YJyi2BbOBR1YawnlNonPl1KWNXvePFiM+MIs6vg4gOyJhGZ72AQkRCaHHw5K7Klz9IYmH7GyHZ/P6cO+nGLIHtFNOeIcv3ByEUOy4iEiXvVKyA3+8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725548095; c=relaxed/simple; bh=5mFDtfacULoA8QyUNGYZHNnyLodh2oom18BuAPuo26o=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=n+Cn+1C6a0eiJK6Mhd+qx3PhY3gAazbENASbWNW/mPWIAxxsPCss3okmVsakFgCRMiNxeFjCe3Bt0xTuF+eye6SlPvmVykV/XNcKhUxHnClnEpBsUY+JZ8waNLnTNu5NDSpKtjTsYG4S5q2F2NJIteWyXi6lIwJig/S66dV1c18= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725548093; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=w5DDftNyK91wxrRHowqYt9PyT0B6XyYpdT1iWESgTus=; b=BvsMlFSk+OABQVvzRaP1glRWtndopMqmQoCC0WoVlFGE7gJb0uPOk2hcAgQa0jStMAz8gb ZYgtPxeuD6oG0BHCUXr9zAQ4HVgPBPxFslblrWKBhEucl3s9OYmXGicl9UZNkuOLlu4PO9 Lg3DUPhH0Cno/oDluz4WuuRPsK9c+pA= 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_256_GCM_SHA384) id us-mta-14-rNywPdQbPTaiOGYqfJjMJQ-1; Thu, 05 Sep 2024 10:54:52 -0400 X-MC-Unique: rNywPdQbPTaiOGYqfJjMJQ-1 Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-7a979aaaa6dso9748885a.3 for ; Thu, 05 Sep 2024 07:54:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725548091; x=1726152891; 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=w5DDftNyK91wxrRHowqYt9PyT0B6XyYpdT1iWESgTus=; b=hWeMi0AfIFL8S/8SMoi5AfnL/3vpGUZHRaRUn+3qoFxpGB6+r4THTPcJVT3dJNcQ2J U1SFjkKdqWb6rE71gQHhSDkFQ4gzgaGmh461/txUF9K6twmwoLuyWKL7cSixZNvUc5H/ PNKw+B8oMJ4zoVVZnJD5o5UZI88OjgEAXI3LH00xjRCgXlmiBmD8EIUeI4MzimhSnCPJ 49d5LtZPjsq2J9kz1J+y9PvOPz29ARfz9w6M6FJSsETay+neZ7wOs2kFTf0QhQEzsqjR Xsh65SFIihaPXXU8nzKxPKqXWDqjh5Qi5SyQm6d9bCjLoNxgsujPnSDhGwNpIgdKqedY un8A== X-Gm-Message-State: AOJu0YywW8HMUra1RYmphP64+D9+nfkXmGZpNIxY2YFxVtf6k3c/I9JV 40ACcMGuPxCGjxCxsae++SLr/ed/Y4lOKn32OZf47rapag+fJzx5Zor1jIi5HMCSbkbd9g3tkVy 6rE1YuXL+XM0gs65penYnR7NnmXb8zcVYHobnjZSL3OjhubLK5swfv7dsaXBkqfHqXDTjJgoELv ERCNb1GZchLSo61dc7E24dOmDLCaoNpQKWT1hD X-Received: by 2002:a05:620a:17a8:b0:7a7:f94b:7458 with SMTP id af79cd13be357-7a81303cda5mr1278025185a.2.1725548090869; Thu, 05 Sep 2024 07:54:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHDLbOAgofT/1a128lKrOVmtM9WLWWuMsXcGfnUHJMU0OSzkA0sJTCkkUv7kK6rMeIFt7XfTQ== X-Received: by 2002:a05:620a:17a8:b0:7a7:f94b:7458 with SMTP id af79cd13be357-7a81303cda5mr1278022985a.2.1725548090440; Thu, 05 Sep 2024 07:54:50 -0700 (PDT) Received: from localhost.localdomain (ool-18bb2a2e.dyn.optonline.net. [24.187.42.46]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7a98ef4db48sm80834685a.68.2024.09.05.07.54.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Sep 2024 07:54:50 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: template depth of lambda in default targ [PR116567] Date: Thu, 5 Sep 2024 10:54:47 -0400 Message-ID: <20240905145447.4154053-1-ppalka@redhat.com> X-Mailer: git-send-email 2.46.0.519.g2e7b89e038 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.5 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_H4, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk/14? -- >8 -- A lambda within a default template argument used in some template-id may have a smaller template depth than the context of the template-id. For example, the lambda in v1's default template argument has template depth 1, and in v2's has template depth 2, but the template-ids v1<0> and v2<0> which uses these default arguments appear in a depth 3 template context. So add_extra_args will ultimately return args with depth 3 -- too many args for the lambda, leading to a bogus substitution. This patch fixes this by trimming the result of add_extra_args to match the template depth of the lambda. A new LAMBDA_EXPR_TEMPLATE_DEPTH field is added that tracks the template-ness of a lambda; PR c++/116567 gcc/cp/ChangeLog: * pt.cc (tsubst_lambda_expr): For a deferred-substitution lambda, trim the augmented template arguments to match the template depth of the lambda. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/lambda-targ7.C: New test. --- gcc/cp/pt.cc | 11 +++++++++ gcc/testsuite/g++.dg/cpp2a/lambda-targ7.C | 30 +++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp2a/lambda-targ7.C diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 747e627f547..c49a26b4f5e 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -19699,6 +19699,17 @@ tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl) LAMBDA_EXPR_EXTRA_ARGS (t) = build_extra_args (t, args, complain); return t; } + if (LAMBDA_EXPR_EXTRA_ARGS (t)) + { + /* If we deferred substitution into this lambda, then it's probably from + a context (e.g. default template argument context) which may have fewer + levels than the current context it's embedded in. Adjust the result of + add_extra_args accordingly. */ + tree ctx_parms = DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (oldfn)); + if (generic_lambda_fn_p (oldfn)) + ctx_parms = TREE_CHAIN (ctx_parms); + args = get_innermost_template_args (args, TMPL_PARMS_DEPTH (ctx_parms)); + } tree r = build_lambda_expr (); diff --git a/gcc/testsuite/g++.dg/cpp2a/lambda-targ7.C b/gcc/testsuite/g++.dg/cpp2a/lambda-targ7.C new file mode 100644 index 00000000000..c5c0525908e --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/lambda-targ7.C @@ -0,0 +1,30 @@ +// PR c++/116567 +// { dg-do compile { target c++20 } } + +template +bool v1 = true; + +template +bool v1g = true; + +template +struct A { + template + static inline bool v2 = true; + + template + static inline bool v2g = true; + + template + struct B { + template + static void f() { + v1<0> && v1g<0>; + v2<0> && v2g<0>; + } + }; +}; + +auto main() -> int { + A::B::f(); +}