aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorMarek Polacek <polacek@redhat.com>2018-02-24 13:10:44 +0000
committerMarek Polacek <mpolacek@gcc.gnu.org>2018-02-24 13:10:44 +0000
commit118cd6baeda97b91c667bb5933a56a72f8c0dc84 (patch)
tree57b3ceeb33137e764e6de70a18d9ce21210db308 /gcc
parent2d98a9c1d52be9e77dfe163b83f27879f7bcda98 (diff)
downloadgcc-118cd6baeda97b91c667bb5933a56a72f8c0dc84.zip
gcc-118cd6baeda97b91c667bb5933a56a72f8c0dc84.tar.gz
gcc-118cd6baeda97b91c667bb5933a56a72f8c0dc84.tar.bz2
re PR c++/83692 (Rejects valid constexpr with unrelated code fixing problem)
PR c++/83692 * constexpr.c (maybe_constant_init_1): New function. (maybe_constant_init): Make it a wrapper around maybe_constant_init_1. (cxx_constant_init): New function. * cp-tree.h (cxx_constant_init): Declare. * typeck2.c (store_init_value): Call cxx_constant_init instead of cxx_constant_value. Move the maybe_constant_init call under an 'else'. * g++.dg/cpp1z/constexpr-83692.C: New test. From-SVN: r257961
Diffstat (limited to 'gcc')
-rw-r--r--gcc/cp/ChangeLog10
-rw-r--r--gcc/cp/constexpr.c24
-rw-r--r--gcc/cp/cp-tree.h1
-rw-r--r--gcc/cp/typeck2.c5
-rw-r--r--gcc/testsuite/ChangeLog5
-rw-r--r--gcc/testsuite/g++.dg/cpp1z/constexpr-83692.C21
6 files changed, 60 insertions, 6 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 58710a9..3d4c946 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,13 @@
+2018-02-24 Marek Polacek <polacek@redhat.com>
+
+ PR c++/83692
+ * constexpr.c (maybe_constant_init_1): New function.
+ (maybe_constant_init): Make it a wrapper around maybe_constant_init_1.
+ (cxx_constant_init): New function.
+ * cp-tree.h (cxx_constant_init): Declare.
+ * typeck2.c (store_init_value): Call cxx_constant_init instead of
+ cxx_constant_value. Move the maybe_constant_init call under an 'else'.
+
2018-02-22 Jason Merrill <jason@redhat.com>
PR c++/70468 - ICE with constructor delegation via typedef.
diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index 47ff90c..26d0d09 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -5123,8 +5123,8 @@ fold_non_dependent_expr (tree t)
/* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
than wrapped in a TARGET_EXPR. */
-tree
-maybe_constant_init (tree t, tree decl)
+static tree
+maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant)
{
if (!t)
return t;
@@ -5139,10 +5139,10 @@ maybe_constant_init (tree t, tree decl)
t = TARGET_EXPR_INITIAL (t);
if (!is_nondependent_static_init_expression (t))
/* Don't try to evaluate it. */;
- else if (CONSTANT_CLASS_P (t))
+ else if (CONSTANT_CLASS_P (t) && allow_non_constant)
/* No evaluation needed. */;
else
- t = cxx_eval_outermost_constant_expr (t, true, false, decl);
+ t = cxx_eval_outermost_constant_expr (t, allow_non_constant, false, decl);
if (TREE_CODE (t) == TARGET_EXPR)
{
tree init = TARGET_EXPR_INITIAL (t);
@@ -5152,6 +5152,22 @@ maybe_constant_init (tree t, tree decl)
return t;
}
+/* Wrapper for maybe_constant_init_1 which permits non constants. */
+
+tree
+maybe_constant_init (tree t, tree decl)
+{
+ return maybe_constant_init_1 (t, decl, true);
+}
+
+/* Wrapper for maybe_constant_init_1 which does not permit non constants. */
+
+tree
+cxx_constant_init (tree t, tree decl)
+{
+ return maybe_constant_init_1 (t, decl, false);
+}
+
#if 0
/* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
/* Return true if the object referred to by REF has automatic or thread
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 9038d67..04c7b7c 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -7411,6 +7411,7 @@ extern bool require_potential_constant_expression (tree);
extern bool require_constant_expression (tree);
extern bool require_potential_rvalue_constant_expression (tree);
extern tree cxx_constant_value (tree, tree = NULL_TREE);
+extern tree cxx_constant_init (tree, tree = NULL_TREE);
extern tree maybe_constant_value (tree, tree = NULL_TREE);
extern tree maybe_constant_init (tree, tree = NULL_TREE);
extern tree fold_non_dependent_expr (tree);
diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c
index 899d60e..153b46c 100644
--- a/gcc/cp/typeck2.c
+++ b/gcc/cp/typeck2.c
@@ -830,9 +830,10 @@ store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags)
if (!require_constant_expression (value))
value = error_mark_node;
else
- value = cxx_constant_value (value, decl);
+ value = cxx_constant_init (value, decl);
}
- value = maybe_constant_init (value, decl);
+ else
+ value = maybe_constant_init (value, decl);
if (TREE_CODE (value) == CONSTRUCTOR && cp_has_mutable_p (type))
/* Poison this CONSTRUCTOR so it can't be copied to another
constexpr variable. */
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 2ec3d40..1a0bcd3 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2018-02-24 Marek Polacek <polacek@redhat.com>
+
+ PR c++/83692
+ * g++.dg/cpp1z/constexpr-83692.C: New test.
+
2018-02-23 Steven G. Kargl <kargl@gcc.gnu.org>
PR fortran/84346
diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-83692.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-83692.C
new file mode 100644
index 0000000..f6b61ee
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-83692.C
@@ -0,0 +1,21 @@
+// PR c++/83692
+// { dg-options -std=c++17 }
+
+struct integer {
+ constexpr int value() const { return m_value; }
+ int m_value;
+};
+
+struct outer {
+ integer m_x{0};
+ constexpr outer()
+ {
+ if (m_x.value() != 0)
+ throw 0;
+ m_x.m_value = integer{1}.value();
+ if (m_x.value() != 1)
+ throw 0;
+ }
+};
+
+constexpr outer o{};