From 118cd6baeda97b91c667bb5933a56a72f8c0dc84 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Sat, 24 Feb 2018 13:10:44 +0000 Subject: [PATCH] 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 --- gcc/cp/ChangeLog | 10 ++++++++ gcc/cp/constexpr.c | 24 ++++++++++++++++---- gcc/cp/cp-tree.h | 1 + gcc/cp/typeck2.c | 5 ++-- gcc/testsuite/ChangeLog | 5 ++++ gcc/testsuite/g++.dg/cpp1z/constexpr-83692.C | 21 +++++++++++++++++ 6 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-83692.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 58710a91787..3d4c946ad16 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,13 @@ +2018-02-24 Marek Polacek + + 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 PR c++/70468 - ICE with constructor delegation via typedef. diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 47ff90cb055..26d0d099a05 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 9038d677b2d..04c7b7ce3a9 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 899d60e8535..153b46cca77 100644 --- a/gcc/cp/typeck2.c +++ b/gcc/cp/typeck2.c @@ -830,9 +830,10 @@ store_init_value (tree decl, tree init, vec** 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 2ec3d405800..1a0bcd3a602 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2018-02-24 Marek Polacek + + PR c++/83692 + * g++.dg/cpp1z/constexpr-83692.C: New test. + 2018-02-23 Steven G. Kargl 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 00000000000..f6b61eeab85 --- /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{};