public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [pushed] c++: strict constexpr and local vars
@ 2022-05-25 15:08 Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2022-05-25 15:08 UTC (permalink / raw)
  To: gcc-patches

A change I was working on made constexpr_searcher.cc start to fail, and when
I looked at it I wondered why it had been accepted before.  This turned out
to be because we try to be more flexible about constant-evaluation of static
initializers, as allowed, but we were wrongly doing the same for non-static
initializers as well.

Tested x86_64-pc-linux-gnu, applying to trunk.

gcc/cp/ChangeLog:

	* constexpr.cc (maybe_constant_init_1): Only pass false for
	strict when initializing a variable of static duration.

libstdc++-v3/ChangeLog:

	* testsuite/20_util/function_objects/constexpr_searcher.cc: Add
	constexpr.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp1y/constexpr-local4.C: New test.
---
 gcc/cp/constexpr.cc                             | 12 +++++++++---
 gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C   | 17 +++++++++++++++++
 .../function_objects/constexpr_searcher.cc      |  4 ++--
 3 files changed, 28 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 388239ea8a8..1a70fda1dc5 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -8301,9 +8301,15 @@ maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
   else if (CONSTANT_CLASS_P (t) && allow_non_constant)
     /* No evaluation needed.  */;
   else
-    t = cxx_eval_outermost_constant_expr (t, allow_non_constant,
-					  /*strict*/false,
-					  manifestly_const_eval, false, decl);
+    {
+      /* [basic.start.static] allows constant-initialization of variables with
+	 static or thread storage duration even if it isn't required, but we
+	 shouldn't bend the rules the same way for automatic variables.  */
+      bool is_static = (decl && DECL_P (decl)
+			&& (TREE_STATIC (decl) || DECL_EXTERNAL (decl)));
+      t = cxx_eval_outermost_constant_expr (t, allow_non_constant, !is_static,
+					    manifestly_const_eval, false, decl);
+    }
   if (TREE_CODE (t) == TARGET_EXPR)
     {
       tree init = TARGET_EXPR_INITIAL (t);
diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C
new file mode 100644
index 00000000000..bef62488579
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C
@@ -0,0 +1,17 @@
+// { dg-do compile { target c++14 } }
+
+struct A
+{
+  int i;
+  constexpr A(int i): i(i) {};
+};
+
+const A a = 42;
+
+constexpr int f()
+{
+  const int j = a.i;		// { dg-message "'a'" }
+  return j;
+}
+
+static_assert (f() == 42,"");	// { dg-error "non-constant" }
diff --git a/libstdc++-v3/testsuite/20_util/function_objects/constexpr_searcher.cc b/libstdc++-v3/testsuite/20_util/function_objects/constexpr_searcher.cc
index 33012200cb0..17069694c1b 100644
--- a/libstdc++-v3/testsuite/20_util/function_objects/constexpr_searcher.cc
+++ b/libstdc++-v3/testsuite/20_util/function_objects/constexpr_searcher.cc
@@ -28,13 +28,13 @@
 
 #include <string_view>
 
-const std::string_view
+constexpr std::string_view
 patt = "World";
 
 constexpr std::string_view
 greet = "Hello, Humongous World of Wonder!!!";
 
-const std::wstring_view
+constexpr std::wstring_view
 wpatt = L"World";
 
 constexpr std::wstring_view

base-commit: 1b661f3f5e712c951e774b3b91fffe4dac734cc7
-- 
2.27.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [pushed] c++: strict constexpr and local vars
@ 2022-05-24 20:03 Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2022-05-24 20:03 UTC (permalink / raw)
  To: gcc-patches

A change I was working on made constexpr_searcher.cc start to fail, and when
I looked at it I wondered why it had been accepted before.  This turned out
to be because we try to be more flexible about constant-evaluation of static
initializers, as allowed, but we were wrongly doing the same for non-static
initializers as well.

Tested x86_64-pc-linux-gnu, applying to trunk.

gcc/cp/ChangeLog:

	* constexpr.cc (maybe_constant_init_1): Only pass false for
	strict when initializing a variable of static duration.

libstdc++-v3/ChangeLog:

	* testsuite/20_util/function_objects/constexpr_searcher.cc: Add
	constexpr.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp1y/constexpr-local4.C: New test.
---
 gcc/cp/constexpr.cc                             | 12 +++++++++---
 gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C   | 17 +++++++++++++++++
 .../function_objects/constexpr_searcher.cc      |  4 ++--
 3 files changed, 28 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 388239ea8a8..1a70fda1dc5 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -8301,9 +8301,15 @@ maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
   else if (CONSTANT_CLASS_P (t) && allow_non_constant)
     /* No evaluation needed.  */;
   else
-    t = cxx_eval_outermost_constant_expr (t, allow_non_constant,
-					  /*strict*/false,
-					  manifestly_const_eval, false, decl);
+    {
+      /* [basic.start.static] allows constant-initialization of variables with
+	 static or thread storage duration even if it isn't required, but we
+	 shouldn't bend the rules the same way for automatic variables.  */
+      bool is_static = (decl && DECL_P (decl)
+			&& (TREE_STATIC (decl) || DECL_EXTERNAL (decl)));
+      t = cxx_eval_outermost_constant_expr (t, allow_non_constant, !is_static,
+					    manifestly_const_eval, false, decl);
+    }
   if (TREE_CODE (t) == TARGET_EXPR)
     {
       tree init = TARGET_EXPR_INITIAL (t);
diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C
new file mode 100644
index 00000000000..bef62488579
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C
@@ -0,0 +1,17 @@
+// { dg-do compile { target c++14 } }
+
+struct A
+{
+  int i;
+  constexpr A(int i): i(i) {};
+};
+
+const A a = 42;
+
+constexpr int f()
+{
+  const int j = a.i;		// { dg-message "'a'" }
+  return j;
+}
+
+static_assert (f() == 42,"");	// { dg-error "non-constant" }
diff --git a/libstdc++-v3/testsuite/20_util/function_objects/constexpr_searcher.cc b/libstdc++-v3/testsuite/20_util/function_objects/constexpr_searcher.cc
index 33012200cb0..17069694c1b 100644
--- a/libstdc++-v3/testsuite/20_util/function_objects/constexpr_searcher.cc
+++ b/libstdc++-v3/testsuite/20_util/function_objects/constexpr_searcher.cc
@@ -28,13 +28,13 @@
 
 #include <string_view>
 
-const std::string_view
+constexpr std::string_view
 patt = "World";
 
 constexpr std::string_view
 greet = "Hello, Humongous World of Wonder!!!";
 
-const std::wstring_view
+constexpr std::wstring_view
 wpatt = L"World";
 
 constexpr std::wstring_view

base-commit: 1189c03859cefef4fc4fd44d57eb3d4d3348b562
-- 
2.27.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-05-25 15:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-25 15:08 [pushed] c++: strict constexpr and local vars Jason Merrill
  -- strict thread matches above, loose matches on Subject: below --
2022-05-24 20:03 Jason Merrill

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).