From patchwork Fri Apr 12 13:47:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1923148 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Ztn62ME1; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VGHv51Gfrz1yYL for ; Fri, 12 Apr 2024 23:47:45 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 61BAC3858420 for ; Fri, 12 Apr 2024 13:47:43 +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.129.124]) by sourceware.org (Postfix) with ESMTPS id 1AC493858D38 for ; Fri, 12 Apr 2024 13:47:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1AC493858D38 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 1AC493858D38 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712929640; cv=none; b=vmM1qI7MEVgyD6QJdtN1bBP+tnpPRlkZuWDE6S2iSM4X7facSCSpG7BE7HjAnuQ/kV4Iez1cTNqc6xSsd+5hgrd07wdYoM5xRRylOCFVIyAXjPhyUR3swidPXzGgDnaCrXihfFGYXDH0Kifaj9xzuAV1q80BJdvGZdA08Cx6veo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712929640; c=relaxed/simple; bh=UqZZD0mpoO5gpabpNf7oCr1KG7ZD4p55+2M7J5Ie1/E=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=JEspYkGAQKdToiFsQcWyR1TSafjKVeHSdWDu0wRrYQ9OlqUN9Qv5DF9vq5ZkUseDtMukZ96HSxjwsGsOj5Eb0aJS06vbwrni07QYi8b4w+kKUFC8jEFajXk8vqb59kHnpkBmT0JRjRN9JNG7uUhdEaU9+DrPABIIV/OM//wBkcw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712929636; 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=8AhfxZ9Ou46BkovmEPSUafGOLpzR9p64m7fWlDsAalU=; b=Ztn62ME1D1dH3m5B4vND3T1WuqsI/gNv3kDvQ5ZZQz0N+sAqGyP38SHMndrWFaqloEXY4p 2BC5F2DSPtIn0U+1oAfwPE3Evh/2lMJassdUPo91Hj19XiN13kR56WCh3G9kHr7Kd4PxX3 RIvTQM50fDeiZE/3ybKMluOxKESqdog= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-317-0xYpyQ1ONMO5rnpE8qgC0w-1; Fri, 12 Apr 2024 09:47:15 -0400 X-MC-Unique: 0xYpyQ1ONMO5rnpE8qgC0w-1 Received: by mail-qt1-f200.google.com with SMTP id d75a77b69052e-431750e74b7so12297811cf.0 for ; Fri, 12 Apr 2024 06:47:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712929634; x=1713534434; 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=8AhfxZ9Ou46BkovmEPSUafGOLpzR9p64m7fWlDsAalU=; b=i5fdCquQWlL4QdO466o0/5WkLfANP7wmKcP1EVwFkQeLQ0rOp6ysajaS/wBQ5rKMKo cZiy9G51lDcwHc+G86cifwxM49wF+XrRlIIpuVMRaPpJcj0X1S7tEh31eTEVl+9fm5g/ 7hdz3NYnbMKe6Gzwdy+JI4fs4t0LAkJN3cX5XBYyfg95YeZqix8ozD7erDpgJXdIREZ4 5aAX/RP0OJdXj0KOftJVkAT9Xbui668C3ftpbkoljoaE07lqYN1AI9zVqs+SeN6RDrVn lGOXgR9c8MfQ+lsTXMnNHmxEmKXzTgQO+x+Ov99cNlandbksAQjskWsbDgiDjsTrLSPe JrXA== X-Gm-Message-State: AOJu0YxACnfe8v+QPjSN8Aj45JYQvD8LB2t5+cPMn/LlMFMgK7uueKej CO5NVFsGKtAb9F9tU3Z+6dCdUpVik+Ul7Hi1qH1MmvAv3KzGn1oz0vbGc3fzbKCNj6pqFsU6rI7 9uIvfClV24DWElxlp4bHHe4G5bWSXAyLIfiuCxsQlwKCd0MEIUVTw9HM292fjfYdgmkeL6U55I1 z3mSbDsR8D0afxZ3Rj4GMwQZRLTjVoPzCxBOvA X-Received: by 2002:a05:622a:1991:b0:436:6340:2dcb with SMTP id u17-20020a05622a199100b0043663402dcbmr2888596qtc.50.1712929633960; Fri, 12 Apr 2024 06:47:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEpk+AKzIznMz1AVV0OeU+mjrtI01zWTwvlJ8gswgao8uNiNMom/9ESb1UYx21e9a3HddgecQ== X-Received: by 2002:a05:622a:1991:b0:436:6340:2dcb with SMTP id u17-20020a05622a199100b0043663402dcbmr2888574qtc.50.1712929633557; Fri, 12 Apr 2024 06:47:13 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id i9-20020ac860c9000000b004343d542598sm2228285qtm.62.2024.04.12.06.47.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Apr 2024 06:47:13 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: templated substitution into lambda-expr, cont [PR114393] Date: Fri, 12 Apr 2024 09:47:11 -0400 Message-ID: <20240412134711.3308389-1-ppalka@redhat.com> X-Mailer: git-send-email 2.44.0.568.g436d4e5b14 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.9 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, RCVD_IN_SORBS_WEB, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? -- >8 -- The original PR114393 testcase is unfortunately still not accepted after r14-9938-g081c1e93d56d35 due to return type deduction confusion when a lambda-expr is used as a default template argument. The below reduced testcase demonstrates the bug. Here, when forming the dependent specialization b_v we substitute the default argument of F, a lambda-expr, with _Descriptor=U. (In this case in_template_context is true since we're in the context of the template c_v so we don't defer.) This substitution in turn lowers the level of its auto return type from 2 to 1. So later, when instantiating c_v we incorrectly replace the auto with the template argument at level=0, index=0, i.e. int, instead of going through do_auto_deduction which would yield char. One way to fix this would be to use a level-less auto to represent a deduced return type of a lambda, but that might be too invasive of a change at this stage. Another way would be to pass tf_partial during dependent substitution into a default template argument from coerce_template_parms so that the levels of deeper parameters don't get lowered, but that wouldn't do the right thing currently due to the tf_partial early exit in the LAMBDA_EXPR case of tsubst_expr. Another way, and the approach that this patch takes, is to just defer all dependent substitution into a lambda-expr, building upon the logic added in r14-9938-g081c1e93d56d35. This seems like the right thing to do in light of the way we build up LAMBDA_EXPR_REGEN_INFO which should consist only of the concrete template arguments used to regenerate the lambda. PR c++/114393 gcc/cp/ChangeLog: * pt.cc (tsubst_lambda_expr): Also defer all dependent substitution into a lambda-expr. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/lambda-targ2a.C: New test. --- gcc/cp/pt.cc | 9 +++++++-- gcc/testsuite/g++.dg/cpp2a/lambda-targ2a.C | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/lambda-targ2a.C diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index ec259ee0fbf..bbd35417f8d 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -19622,11 +19622,16 @@ tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl) in_decl = oldfn; args = add_extra_args (LAMBDA_EXPR_EXTRA_ARGS (t), args, complain, in_decl); - if (processing_template_decl && !in_template_context) + if (processing_template_decl + && (!in_template_context || any_dependent_template_arguments_p (args))) { /* Defer templated substitution into a lambda-expr if we lost the necessary template context. This may happen for a lambda-expr - used as a default template argument. */ + used as a default template argument. + + Defer dependent substitution as well so that we don't lower the + level of a deduced return type or any other auto or template + parameter. */ t = copy_node (t); LAMBDA_EXPR_EXTRA_ARGS (t) = NULL_TREE; LAMBDA_EXPR_EXTRA_ARGS (t) = build_extra_args (t, args, complain); diff --git a/gcc/testsuite/g++.dg/cpp2a/lambda-targ2a.C b/gcc/testsuite/g++.dg/cpp2a/lambda-targ2a.C new file mode 100644 index 00000000000..7136ce79872 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/lambda-targ2a.C @@ -0,0 +1,14 @@ +// PR c++/114393 +// { dg-do compile { target c++20 } } + +template struct c1 {}; + +template +inline constexpr auto b_v = F; + +template +inline constexpr auto c_v = b_v; + +auto f = c_v; +using type = decltype(f()); +using type = char;