From patchwork Fri May 26 23:18:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 70177 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 642633858421 for ; Fri, 26 May 2023 23:18:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 642633858421 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685143121; bh=t+I+C4/+Uh+NuQB0rGFH0BCFcDHCAd/ydGCXDfX7Fqk=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=k6ZJPPGGfqJM/aZaeYBHCaMMjQFNuwaG46HruL32Pit3bDJlhlF1yr7DDKXkCrTr4 jA5Y0vq38UVlcgb5S+lEk83FT7S7l3rSiGksKYbjHv+dKvW+DABoi7l05MFdFuIfvE 2heLm4ZV5XBwwJIWieHAIIp73EuVjVbKphHRKtAM= 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 562033858C5F for ; Fri, 26 May 2023 23:18:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 562033858C5F Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-278-Dy3hmC_lMsm_NkSNzmVb9A-1; Fri, 26 May 2023 19:18:10 -0400 X-MC-Unique: Dy3hmC_lMsm_NkSNzmVb9A-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BDF79185A7B6 for ; Fri, 26 May 2023 23:18:09 +0000 (UTC) Received: from pdp-11.lan (unknown [10.22.17.241]) by smtp.corp.redhat.com (Postfix) with ESMTP id A1C5E20296C8; Fri, 26 May 2023 23:18:09 +0000 (UTC) To: Jason Merrill , GCC Patches Subject: [PATCH] c++: wrong error with static constexpr var in tmpl [PR109876] Date: Fri, 26 May 2023 19:18:05 -0400 Message-Id: <20230526231805.406578-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.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, 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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 base-commit: 8d6bd830f5f9c939e8565c0341a0c6c588834484 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. */ + && !is_really_empty_class (TREE_TYPE (expression), + /*ignore_vptr*/false)) + 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>(); +}