From patchwork Fri Dec 16 16:45:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 34026 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:e747:0:0:0:0:0 with SMTP id c7csp1074240wrn; Fri, 16 Dec 2022 08:46:35 -0800 (PST) X-Google-Smtp-Source: AA0mqf59mH34pe1+V/72SQAsNdgkXqW7c8BxU5t0swJnaIy0/y1xkKmnajzXMz9Yw5D+kVtmyqxd X-Received: by 2002:a17:907:7704:b0:7c1:79ce:a431 with SMTP id kw4-20020a170907770400b007c179cea431mr18966908ejc.25.1671209195320; Fri, 16 Dec 2022 08:46:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671209195; cv=none; d=google.com; s=arc-20160816; b=fA7JX/WGfVkHHF8Co0SqqOqakxQXd+tLoLz3iSFykSatj65WYFScVnupjPEQFMhPPp JmOjAp2gwYHygDj1eQkGjFuUEhMdSm/vuD4G3GjrJYP7kIHehAG0/kWig8nKPBqzDfCH WN1Q7wZu8SrAGgZ7iI86IgDJ4S27aMldanH+r/yUn6XU6pfcvrrNGza3eA7Un1Ws04Bf ctobe0it2MbO9QoGFvU7FAe0CW0/iCG7JV0oZ37PRPuPu50RPwsV7uIA4/0bG10795u6 OFNcI7vpX26ziq0klv7pU/HwMYmc71WgmyemDjnhD/gZC+ei1J3C+cP/FHOjEjqN4Dha vnag== 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:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=P5Gi83cMJBRoBZirDT9bD4tmhxh6HtnWZmUVEMfrZXM=; b=equjIQwYhzZ3aDUzLxqgR70e4jCwede1wy5aSf/LPsAhZl2VyvLvLzy+sJw+D5z1I0 KG5G73Iw2AOACXDS/0GPE1EQ70nMYKpjNErKaA8C5cQV2oVZmm7qWYYDPMsSDIKxyjk4 ztRjM6sQNsTpJO0paBXSalKTpMxFC6c0qA/YJUpdL9ZbPs2fJAVPwT4uECz/z+6ZySK3 6QkQxOx6CnBG2zShw2iZWVo3zWQMUxng2ozt+ODUIOof+RLeIPxNuP712eTU9ZFMubo+ R+Zy2HI3h0O+A+wTRqjWQDWj8UK2GLBqaIDJBsmJTAjoL8Iy4Y1bEDNUTOMcSK6T03B9 6o5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=FY4CdpRs; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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. [8.43.85.97]) by mx.google.com with ESMTPS id cw4-20020a170906c78400b007807e1f3d9dsi2311906ejb.842.2022.12.16.08.46.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Dec 2022 08:46:35 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=FY4CdpRs; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 4AC1A38500A6 for ; Fri, 16 Dec 2022 16:46:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4AC1A38500A6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1671209194; bh=P5Gi83cMJBRoBZirDT9bD4tmhxh6HtnWZmUVEMfrZXM=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=FY4CdpRsC+E+JaMqQZUTS/JmrrAaOlARFYdJbpyL8l1Uure/bDu+pcQShg50Extw2 +YF18AwTmjL1HfUJ/8zHkYL3Fgcx+RbupeSXy1/S/y616CrYMNMzHtaV+X077st7pJ 2lZ3UpBmzhZBKLJx54E18gFCb9J2nIQ6hMracnz0= 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 EEAA438500A6 for ; Fri, 16 Dec 2022 16:45:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org EEAA438500A6 Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-622-f9CLqzahO-OkVXzF8CztZg-1; Fri, 16 Dec 2022 11:45:30 -0500 X-MC-Unique: f9CLqzahO-OkVXzF8CztZg-1 Received: by mail-qv1-f72.google.com with SMTP id s9-20020a0cf789000000b004d544bd8258so1751578qvn.16 for ; Fri, 16 Dec 2022 08:45:30 -0800 (PST) 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:message-id :reply-to; bh=P5Gi83cMJBRoBZirDT9bD4tmhxh6HtnWZmUVEMfrZXM=; b=w9a2r9F61lHmNNoA5eLTwcboeSSub4XYOwJbdV8p7ON2oXxo/PIF3GREgaHaM79Gh6 YsziJvq7cotcUc2DTLTUjZG3vm8cDASXCFcKBbXUCDOhea4qMD31wBrm5552wkIODWeE NXVaMZo93tibyoB6B0xyVHY4aeGw6NscybsH9+W57NNC5KdaoZHvfKCixfnYA+lm+CbM e+8aTznGP0wO9HiiT79X+baJ7l9rPemKNcbXlfC7n6SJCvjEIqPypLkrWK8wk2a22LEC 576u9bYseaLvojinoGCSCQFOzZAs7to+Pc9iUXMMnLQ65RZ8xA5paXvHJzWJOeRKGjCJ Hp5w== X-Gm-Message-State: ANoB5pnhaAP4io5MJh5zoUC4f29YcbSP5maVlIaa8hbgZ7wBon0VkcSK NChZMkVYbccal2uArbYKmerCWcYuzOkF9yC+xoFdwlMFCPsq/0bchii0VnIWZBDmGheAvpu38kf KkYccHJwumC4+ybtyvpMZWQkd2/uiioLnzLN5cSDU8Rt1E/csW3cCWeWJKScEZRfLHyk= X-Received: by 2002:a05:622a:1e18:b0:3a7:e619:61a with SMTP id br24-20020a05622a1e1800b003a7e619061amr41854568qtb.37.1671209129662; Fri, 16 Dec 2022 08:45:29 -0800 (PST) X-Received: by 2002:a05:622a:1e18:b0:3a7:e619:61a with SMTP id br24-20020a05622a1e1800b003a7e619061amr41854534qtb.37.1671209129245; Fri, 16 Dec 2022 08:45:29 -0800 (PST) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id m9-20020a05620a290900b006fa8299b4d5sm1847600qkp.100.2022.12.16.08.45.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Dec 2022 08:45:28 -0800 (PST) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: empty captured var as template argument [PR107437] Date: Fri, 16 Dec 2022 11:45:26 -0500 Message-Id: <20221216164526.224772-1-ppalka@redhat.com> X-Mailer: git-send-email 2.39.0.56.g57e2c6ebbe 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_H2, 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?1752389852914719655?= X-GMAIL-MSGID: =?utf-8?q?1752389852914719655?= Here we're rejecting the use of the captured 't' (of empty type) as a template argument ultimately because convert_nontype_argument checks potentiality using is_constant_expression which returns false for captured variables since want_rval=false. But in this case an lvalue-to-rvalue conversion of the argument is implied, so I believe we should be checking is_rvalue_constant_expression. This patch defines an rvalue version of is_nondep_const_expr and uses it in convert_nontype_argument when the target type is a non-reference. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look right? Alternatively, since 'non_dep' in convert_nontype_argument controls only whether we should call instantiate_non_dependent_expr, it seems weird to me that we care about potentiality at all. So I experimented with using instantiation_dependent_expression_p here instead, and that seemed to work well and even fixed the dg-ice'd test cpp1z/constexpr-lambda26.C. In r12-7564-gec0f53a3a542e7 we made a similar change to decltype. PR c++/107437 gcc/cp/ChangeLog: * constexpr.cc (is_nondependent_rvalue_constant_expression): Define. * cp-tree.h (is_nondependent_rvalue_constant_expression): Declare. * pt.cc (convert_nontype_argument): Use it instead of the non-rvalue version when converting to a non-reference type. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/lambda-generic-107437.C: New test. --- gcc/cp/constexpr.cc | 10 +++++++++ gcc/cp/cp-tree.h | 1 + gcc/cp/pt.cc | 4 +++- .../g++.dg/cpp1y/lambda-generic-107437.C | 22 +++++++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp1y/lambda-generic-107437.C diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index e43d92864f5..77e0e261cca 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -10139,6 +10139,16 @@ is_nondependent_constant_expression (tree t) && !instantiation_dependent_expression_p (t)); } +/* As above, but expect an rvalue. */ + +bool +is_nondependent_rvalue_constant_expression (tree t) +{ + return (!type_unknown_p (t) + && is_rvalue_constant_expression (t) + && !instantiation_dependent_expression_p (t)); +} + /* Returns true if T is a potential static initializer expression that is not instantiation-dependent. */ diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 0d6c234b3b0..2d4c5cc3ebd 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -8471,6 +8471,7 @@ extern bool potential_constant_expression (tree); extern bool is_constant_expression (tree); extern bool is_rvalue_constant_expression (tree); extern bool is_nondependent_constant_expression (tree); +extern bool is_nondependent_rvalue_constant_expression (tree); extern bool is_nondependent_static_init_expression (tree); extern bool is_static_init_expression (tree); extern bool is_std_allocator (tree); diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index bc566ab702b..87caea5f202 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -7318,7 +7318,9 @@ convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain) && has_value_dependent_address (expr)) /* If we want the address and it's value-dependent, don't fold. */; else if (processing_template_decl - && is_nondependent_constant_expression (expr)) + && (TYPE_REF_P (type) + ? is_nondependent_constant_expression (expr) + : is_nondependent_rvalue_constant_expression (expr))) non_dep = true; if (error_operand_p (expr)) return error_mark_node; diff --git a/gcc/testsuite/g++.dg/cpp1y/lambda-generic-107437.C b/gcc/testsuite/g++.dg/cpp1y/lambda-generic-107437.C new file mode 100644 index 00000000000..934f863659d --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/lambda-generic-107437.C @@ -0,0 +1,22 @@ +// PR c++/107437 +// { dg-do compile { target c++11 } } + +struct integral_constant { + constexpr operator int() const { return 42; } +}; + +template +struct A { + static constexpr int value = N; +}; + +template +void f(T t) { + [=](auto) { + A a; // { dg-bogus "constant" } + return a.value; + }(0); +} + +template void f(integral_constant); +