Commit 118cd6ba by Marek Polacek Committed by Marek Polacek

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
parent 2d98a9c1
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> 2018-02-22 Jason Merrill <jason@redhat.com>
PR c++/70468 - ICE with constructor delegation via typedef. PR c++/70468 - ICE with constructor delegation via typedef.
......
...@@ -5123,8 +5123,8 @@ fold_non_dependent_expr (tree t) ...@@ -5123,8 +5123,8 @@ fold_non_dependent_expr (tree t)
/* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
than wrapped in a TARGET_EXPR. */ than wrapped in a TARGET_EXPR. */
tree static tree
maybe_constant_init (tree t, tree decl) maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant)
{ {
if (!t) if (!t)
return t; return t;
...@@ -5139,10 +5139,10 @@ maybe_constant_init (tree t, tree decl) ...@@ -5139,10 +5139,10 @@ maybe_constant_init (tree t, tree decl)
t = TARGET_EXPR_INITIAL (t); t = TARGET_EXPR_INITIAL (t);
if (!is_nondependent_static_init_expression (t)) if (!is_nondependent_static_init_expression (t))
/* Don't try to evaluate it. */; /* Don't try to evaluate it. */;
else if (CONSTANT_CLASS_P (t)) else if (CONSTANT_CLASS_P (t) && allow_non_constant)
/* No evaluation needed. */; /* No evaluation needed. */;
else 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) if (TREE_CODE (t) == TARGET_EXPR)
{ {
tree init = TARGET_EXPR_INITIAL (t); tree init = TARGET_EXPR_INITIAL (t);
...@@ -5152,6 +5152,22 @@ maybe_constant_init (tree t, tree decl) ...@@ -5152,6 +5152,22 @@ maybe_constant_init (tree t, tree decl)
return t; 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 #if 0
/* FIXME see ADDR_EXPR section in potential_constant_expression_1. */ /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
/* Return true if the object referred to by REF has automatic or thread /* Return true if the object referred to by REF has automatic or thread
......
...@@ -7411,6 +7411,7 @@ extern bool require_potential_constant_expression (tree); ...@@ -7411,6 +7411,7 @@ extern bool require_potential_constant_expression (tree);
extern bool require_constant_expression (tree); extern bool require_constant_expression (tree);
extern bool require_potential_rvalue_constant_expression (tree); extern bool require_potential_rvalue_constant_expression (tree);
extern tree cxx_constant_value (tree, tree = NULL_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_value (tree, tree = NULL_TREE);
extern tree maybe_constant_init (tree, tree = NULL_TREE); extern tree maybe_constant_init (tree, tree = NULL_TREE);
extern tree fold_non_dependent_expr (tree); extern tree fold_non_dependent_expr (tree);
......
...@@ -830,9 +830,10 @@ store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags) ...@@ -830,9 +830,10 @@ store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags)
if (!require_constant_expression (value)) if (!require_constant_expression (value))
value = error_mark_node; value = error_mark_node;
else 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)) if (TREE_CODE (value) == CONSTRUCTOR && cp_has_mutable_p (type))
/* Poison this CONSTRUCTOR so it can't be copied to another /* Poison this CONSTRUCTOR so it can't be copied to another
constexpr variable. */ constexpr variable. */
......
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> 2018-02-23 Steven G. Kargl <kargl@gcc.gnu.org>
PR fortran/84346 PR fortran/84346
......
// 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{};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment