public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Marek Polacek <polacek@redhat.com>
To: Jason Merrill <jason@redhat.com>, GCC Patches <gcc-patches@gcc.gnu.org>
Subject: [PATCH] c++: wrong error with static constexpr var in tmpl [PR109876]
Date: Fri, 26 May 2023 19:18:05 -0400	[thread overview]
Message-ID: <20230526231805.406578-1-polacek@redhat.com> (raw)

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 <jason@redhat.com>

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

	PR c++/109876

gcc/cp/ChangeLog:

	* pt.cc (value_dependent_expression_p) <case VAR_DECL>: 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 <class> 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<int> gnum{2};
+
+template <int> struct Array {};
+template <int> void g()
+{
+  static constexpr std::initializer_list<int> num{2};
+  static_assert(num.size(), "");
+  Array<num.size()> ctx;
+
+  constexpr Array<1> num1{};
+}
+
+template <int N>
+struct Foo
+{
+  static constexpr std::initializer_list<int> num = { 1, 2 };
+  static_assert(num.size(), "");
+  Array<num.size()> 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 <const Foo&> struct X {};
+
+void g()
+{
+  static constexpr Foo foo;
+  X<foo> x;
+}
+
+template<int>
+void f()
+{
+  static constexpr Foo foo;
+  X<foo> x;
+}
+
+void
+h ()
+{
+  f<0>();
+  f<1>();
+}

base-commit: 8d6bd830f5f9c939e8565c0341a0c6c588834484
-- 
2.40.1


             reply	other threads:[~2023-05-26 23:18 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-26 23:18 Marek Polacek [this message]
2023-05-27  1:47 ` Jason Merrill
2023-07-13 18:54   ` [PATCH v2] " Marek Polacek
2023-07-14 17:07     ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230526231805.406578-1-polacek@redhat.com \
    --to=polacek@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).