From patchwork Thu Jul 13 18:54:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 120086 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a6b2:0:b0:3e4:2afc:c1 with SMTP id c18csp2023372vqm; Thu, 13 Jul 2023 11:55:16 -0700 (PDT) X-Google-Smtp-Source: APBJJlGGXwWDqjf7CfOULWrlmuanaymb0oHumG5jzPjyRE9xzh63anDuB6XR/P4OpJcd60CZo5BK X-Received: by 2002:a17:906:b2cb:b0:993:e94e:7234 with SMTP id cf11-20020a170906b2cb00b00993e94e7234mr2150238ejb.77.1689274516684; Thu, 13 Jul 2023 11:55:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689274516; cv=none; d=google.com; s=arc-20160816; b=fZ0wZuoq8FEjI0YMnFDP4DQLartC6YIWkN45YGhAQGFdZM5FwK6g5Q3z+GTyIoC4R9 A30/DRnlT0rAM/Mk6Gz+KD49u3ivB4CBXJCGiLIGZDnVAPRK7LGnEHgfY++OeDTEJSEj bjf8NNX7o81RX9uEisEb6ZrJ3fc0IJ3yr2ulBZW9q6rDSwg9vaqEN4DJQScs1waxX2P9 HXb/BdYK1rq6AHdwdkdQYX6y+H2zIoTnF/LstYJF51p5tAzXcEsDOfAZjc6KzJHq6Uo+ ZKY3MsiFa6js0OkxiYTdOSoQx8nwSGOcLQxKG3mob65d9/D5tadlQA2zJkn6FY/u+Bp0 hcKQ== 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-disposition:user-agent:in-reply-to:mime-version:references :message-id:subject:cc:to:date:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=wc4p/5SFkM6kN3z/UfZAnCULyslUQfgvfgS73Wvnuas=; fh=wxPOwoedVnlKkUHnA8s/1n6trrPau9zkHRZNu2smGsY=; b=wzSXF6f7RcAb+yfRgpRpGQMjeBrq43pEVwIK1SBvuNAq43JBQ5pAh/0syplmuZNVLM rZv/WjWm8ws0R5iVKHEyrYvy6WX/wrL6gHxVPcochvhu26xqx97JLzzmk8j22xtAlvUi 9tUffJVZoIuqJtKKaSG/qCidHUinDKroWPDwh1kcXQI8cAKU/CcY8VTFvU13lVndn7my lmioStX/3trwIEAHqp057yTX+2uXQLttqvVBVji6ayF6cVS6+APna/A1cp/dbWI7JQMN GrbAcx1r9bwVfWi4bh2hehF/pkMioZlZUa6U4kudkBhrAHu85NqZ9fHt6IEjMj4c4LFb T4QA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=X4derGXF; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id gs15-20020a170906f18f00b00993150e5326si6859167ejb.626.2023.07.13.11.55.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jul 2023 11:55:16 -0700 (PDT) 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=X4derGXF; 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 4A7A8385843E for ; Thu, 13 Jul 2023 18:55:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4A7A8385843E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1689274515; bh=wc4p/5SFkM6kN3z/UfZAnCULyslUQfgvfgS73Wvnuas=; h=Date:To:Cc:Subject:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=X4derGXF5milvC7upQ5uxLa08KCCuf6jsoRKkiFj7qqi3kfSgWtWx6gbJ6XFV3QOU lK1PDrhFQwSkT2y9+B5fuoe2IZLc92jwr4Jgu14ldsn+vh7mhitbZPuax58Jg/A4L7 v5AJazYLntWjEzBPBFKfqZIDt7vU/wPm9UURdysA= 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 DBEE83858C60 for ; Thu, 13 Jul 2023 18:54:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DBEE83858C60 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-83-I5Ay1jnANxSOyVBuKsSchQ-1; Thu, 13 Jul 2023 14:54:29 -0400 X-MC-Unique: I5Ay1jnANxSOyVBuKsSchQ-1 Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-7659b44990eso103786985a.1 for ; Thu, 13 Jul 2023 11:54:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689274468; x=1691866468; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wc4p/5SFkM6kN3z/UfZAnCULyslUQfgvfgS73Wvnuas=; b=DG7hUo9MKG5MD/YIBVYGOUWZg4jPgV1oM9VGD6yjDmnz4ghBtKaj+cvVeqMQmedL2p iURtdrLK1W9iq/UIQd6z5r08eY0kCtvolZ+qcgFXT+WYRKI7oUK45vulH4pdgoAZsVzm fdUQO8T3L1ecOLBBcA4tYuBUr/qPl8cigkpN5R9xgp5vWQHs5/jXzMxnpMMF8jtivFxj hmCgYZlS5y+rwaUdIcqKQwZEZK5MAn/8ml9JXaj0oYZG39OwOSr6/aA6AWbiP+kx04WC bLTHfLNtd1jbScltxtptEnH/D/ozv9wIhI8xAbJPxPVsfcgerJmQu41uqU7VMnx1wF0P o66g== X-Gm-Message-State: ABy/qLbEyul/CFiRvgbzIPw8RfrLfukYXYFptKJg8cjesAMr13x/+Csb LdQrRU9mWBz4TEowihDiNYuhD5PsMv/z6LlzIo84IFk8rX3sWBx7pSZdm4remlK2grcnI6GmVrN TAW4jrrUmBi94zrSOoUeyTNDduA== X-Received: by 2002:ae9:de03:0:b0:765:8347:e92a with SMTP id s3-20020ae9de03000000b007658347e92amr1751670qkf.56.1689274468248; Thu, 13 Jul 2023 11:54:28 -0700 (PDT) X-Received: by 2002:ae9:de03:0:b0:765:8347:e92a with SMTP id s3-20020ae9de03000000b007658347e92amr1751653qkf.56.1689274467764; Thu, 13 Jul 2023 11:54:27 -0700 (PDT) Received: from redhat.com (2603-7000-9500-34a5-0000-0000-0000-1db4.res6.spectrum.com. [2603:7000:9500:34a5::1db4]) by smtp.gmail.com with ESMTPSA id f8-20020a05620a15a800b007671b599cf5sm3125019qkk.40.2023.07.13.11.54.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jul 2023 11:54:27 -0700 (PDT) Date: Thu, 13 Jul 2023 14:54:25 -0400 To: Jason Merrill Cc: GCC Patches Subject: [PATCH v2] c++: wrong error with static constexpr var in tmpl [PR109876] Message-ID: References: <20230526231805.406578-1-polacek@redhat.com> <460bf6aa-a336-7ac2-0434-c8dcfd8d0b87@redhat.com> MIME-Version: 1.0 In-Reply-To: <460bf6aa-a336-7ac2-0434-c8dcfd8d0b87@redhat.com> User-Agent: Mutt/2.2.9 (2022-11-12) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-12.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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1767000650859500890 X-GMAIL-MSGID: 1771332715579237264 On Fri, May 26, 2023 at 09:47:10PM -0400, Jason Merrill wrote: > On 5/26/23 19:18, Marek Polacek wrote: > > Since r8-509, we'll no longer create a static temporary var for > > the initializer '{ 1, 2 }' for num in the attached test because > > the code in finish_compound_literal is now guarded by > > '&& fcl_context == fcl_c99' but it's fcl_functional here. This > > causes us to reject num as non-constant when evaluating it in > > a template. > > > > Jason's idea was to treat num as value-dependent even though it > > actually isn't. This patch implements that suggestion. > > > > The is_really_empty_class check is sort of non-obvious but the > > comment should explain why I added it. > > > > Co-authored-by: Jason Merrill > > > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > > > PR c++/109876 > > > > gcc/cp/ChangeLog: > > > > * pt.cc (value_dependent_expression_p) : Treat a > > constexpr-declared non-constant variable as value-dependent. > > > > gcc/testsuite/ChangeLog: > > > > * g++.dg/cpp0x/constexpr-template12.C: New test. > > * g++.dg/cpp1z/constexpr-template1.C: New test. > > --- > > gcc/cp/pt.cc | 12 ++++++ > > .../g++.dg/cpp0x/constexpr-template12.C | 38 +++++++++++++++++++ > > .../g++.dg/cpp1z/constexpr-template1.C | 25 ++++++++++++ > > 3 files changed, 75 insertions(+) > > create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-template12.C > > create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-template1.C > > > > diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc > > index 7fb3e75bceb..38fd8070705 100644 > > --- a/gcc/cp/pt.cc > > +++ b/gcc/cp/pt.cc > > @@ -27969,6 +27969,18 @@ value_dependent_expression_p (tree expression) > > else if (TYPE_REF_P (TREE_TYPE (expression))) > > /* FIXME cp_finish_decl doesn't fold reference initializers. */ > > return true; > > + /* We have a constexpr variable and we're processing a template. When > > + there's lifetime extension involved (for which finish_compound_literal > > + used to create a temporary), we'll not be able to evaluate the > > + variable until instantiating, so pretend it's value-dependent. */ > > + else if (DECL_DECLARED_CONSTEXPR_P (expression) > > + && !TREE_CONSTANT (expression) > > + /* When there's nothing to initialize, we'll never mark the > > + VAR_DECL TREE_CONSTANT, therefore it would remain > > + value-dependent and we wouldn't instantiate. */ Sorry it's taken so long to get back to this. > Interesting. Can we change that (i.e. mark it TREE_CONSTANT) rather than > work around it here? I think we can. Maybe as in the below: -- >8 -- Since r8-509, we'll no longer create a static temporary var for the initializer '{ 1, 2 }' for num in the attached test because the code in finish_compound_literal is now guarded by '&& fcl_context == fcl_c99' but it's fcl_functional here. This causes us to reject num as non-constant when evaluating it in a template. Jason's idea was to treat num as value-dependent even though it actually isn't. This patch implements that suggestion. We weren't marking objects whose type is an empty class type constant. This patch changes that so that v_d_e_p doesn't need to check is_really_empty_class. Co-authored-by: Jason Merrill Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? PR c++/109876 gcc/cp/ChangeLog: * decl.cc (cp_finish_decl): Set TREE_CONSTANT when initializing an object of empty class type. * pt.cc (value_dependent_expression_p) : Treat a constexpr-declared non-constant variable as value-dependent. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/constexpr-template12.C: New test. * g++.dg/cpp1z/constexpr-template1.C: New test. * g++.dg/cpp1z/constexpr-template2.C: New test. --- gcc/cp/decl.cc | 13 +++++-- gcc/cp/pt.cc | 7 ++++ .../g++.dg/cpp0x/constexpr-template12.C | 38 +++++++++++++++++++ .../g++.dg/cpp1z/constexpr-template1.C | 25 ++++++++++++ .../g++.dg/cpp1z/constexpr-template2.C | 25 ++++++++++++ 5 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-template12.C create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-template1.C create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-template2.C base-commit: 834f34947b20b18696487fc5d2bccab5ea720351 diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 60f107d50c4..792ab330dd0 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -8200,7 +8200,6 @@ void cp_finish_decl (tree decl, tree init, bool init_const_expr_p, tree asmspec_tree, int flags) { - tree type; vec *cleanups = NULL; const char *asmspec = NULL; int was_readonly = 0; @@ -8220,7 +8219,7 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p, /* Parameters are handled by store_parm_decls, not cp_finish_decl. */ gcc_assert (TREE_CODE (decl) != PARM_DECL); - type = TREE_TYPE (decl); + tree type = TREE_TYPE (decl); if (type == error_mark_node) return; @@ -8410,7 +8409,7 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p, if (decl_maybe_constant_var_p (decl) /* FIXME setting TREE_CONSTANT on refs breaks the back end. */ && !TYPE_REF_P (type)) - TREE_CONSTANT (decl) = 1; + TREE_CONSTANT (decl) = true; } /* This is handled mostly by gimplify.cc, but we have to deal with not warning about int x = x; as it is a GCC extension to turn off @@ -8421,6 +8420,14 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p, && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self)) suppress_warning (decl, OPT_Winit_self); } + else if (VAR_P (decl) + && COMPLETE_TYPE_P (type) + && !TYPE_REF_P (type) + && !dependent_type_p (type) + && is_really_empty_class (type, /*ignore_vptr*/false)) + /* We have no initializer but there's nothing to initialize anyway. + Treat DECL as constant due to c++/109876. */ + TREE_CONSTANT (decl) = true; if (flag_openmp && TREE_CODE (decl) == FUNCTION_DECL diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index fa15b75b9c5..255d18b9539 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -27983,6 +27983,13 @@ value_dependent_expression_p (tree expression) else if (TYPE_REF_P (TREE_TYPE (expression))) /* FIXME cp_finish_decl doesn't fold reference initializers. */ return true; + /* We have a constexpr variable and we're processing a template. When + there's lifetime extension involved (for which finish_compound_literal + used to create a temporary), we'll not be able to evaluate the + variable until instantiating, so pretend it's value-dependent. */ + else if (DECL_DECLARED_CONSTEXPR_P (expression) + && !TREE_CONSTANT (expression)) + return true; return false; case DYNAMIC_CAST_EXPR: diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-template12.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-template12.C new file mode 100644 index 00000000000..a9e065320c8 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-template12.C @@ -0,0 +1,38 @@ +// PR c++/109876 +// { dg-do compile { target c++11 } } + +using size_t = decltype(sizeof 0); + +namespace std { +template struct initializer_list { + const int *_M_array; + size_t _M_len; + constexpr size_t size() const { return _M_len; } +}; +} // namespace std + +constexpr std::initializer_list gnum{2}; + +template struct Array {}; +template void g() +{ + static constexpr std::initializer_list num{2}; + static_assert(num.size(), ""); + Array ctx; + + constexpr Array<1> num1{}; +} + +template +struct Foo +{ + static constexpr std::initializer_list num = { 1, 2 }; + static_assert(num.size(), ""); + Array ctx; +}; + +void +f (Foo<5>) +{ + g<0>(); +} diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-template1.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-template1.C new file mode 100644 index 00000000000..58be046fd36 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-template1.C @@ -0,0 +1,25 @@ +// PR c++/109876 +// { dg-do compile { target c++17 } } + +struct Foo {}; +template struct X {}; + +void g() +{ + static constexpr Foo foo; + X x; +} + +template +void f() +{ + static constexpr Foo foo; + X x; +} + +void +h () +{ + f<0>(); + f<1>(); +} diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-template2.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-template2.C new file mode 100644 index 00000000000..5a9a96ba60a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-template2.C @@ -0,0 +1,25 @@ +// PR c++/109876 +// { dg-do compile { target c++17 } } + +struct Foo {}; +template struct X {}; + +void g() +{ + static constexpr Foo foo{}; + X x; +} + +template +void f() +{ + static constexpr Foo foo{}; + X x; +} + +void +h () +{ + f<0>(); + f<1>(); +}