From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1685143092; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=t+I+C4/+Uh+NuQB0rGFH0BCFcDHCAd/ydGCXDfX7Fqk=; b=CYyViF5l2mmP85Z0mI66BZdlA3Cg/oFyhZUMkUQntYfGxFwIzSM7MZf6hCwVdllfpz0BM3 /drMwzalquo9UaGHQzeNZSVG5sBdyak4ZQYjb8ohmkymkE0HALdNuiGQMI22j4H131QsAf sT8bSQE2RERnd7i1KqHuTIXSVpNTnU8= 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) From: Marek Polacek 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 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true 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 List-Id: 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. */ + && !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>(); +} base-commit: 8d6bd830f5f9c939e8565c0341a0c6c588834484 -- 2.40.1