* constexpr.c (fold_non_dependent_expr): Add complain parm.
* call.c, expr.c, init.c, pt.c, semantics.c, typeck.c, typeck2.c:
Pass it.
* call.c (build_cxx_call): Don't mess with builtins in a template.
* typeck2.c (store_init_value): If fold_non_dependent_expr didn't
produce a constant value, go back to the uninstantiated form.
From-SVN: r261972
2018-06-22 Jason Merrill <jason@redhat.com>
+ PR c++/86219 - ICE with erroneous initializer in template.
+ * constexpr.c (fold_non_dependent_expr): Add complain parm.
+ * call.c, expr.c, init.c, pt.c, semantics.c, typeck.c, typeck2.c:
+ Pass it.
+ * call.c (build_cxx_call): Don't mess with builtins in a template.
+ * typeck2.c (store_init_value): If fold_non_dependent_expr didn't
+ produce a constant value, go back to the uninstantiated form.
+
Avoid taking the address of something just because it's in parens.
* constexpr.c (same_type_ignoring_tlq_and_bounds_p): New.
(cxx_fold_indirect_ref): Use it.
}
else if (CP_INTEGRAL_TYPE_P (type))
{
- t = fold_non_dependent_expr (t);
+ t = fold_non_dependent_expr (t, tf_none);
STRIP_NOPS (t);
if (integer_zerop (t) && !TREE_OVERFLOW (t))
return true;
/* Check that arguments to builtin functions match the expectations. */
if (fndecl
+ && !processing_template_decl
&& DECL_BUILT_IN (fndecl)
&& DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
{
/* We need to take care that values to BUILT_IN_NORMAL
are reduced. */
for (i = 0; i < nargs; i++)
- argarray[i] = fold_non_dependent_expr (argarray[i]);
+ argarray[i] = maybe_constant_value (argarray[i]);
if (!check_builtin_function_arguments (EXPR_LOCATION (fn), vNULL, fndecl,
nargs, argarray))
/* Like maybe_constant_value but first fully instantiate the argument.
Note: this is equivalent to instantiate_non_dependent_expr_sfinae
- (t, tf_none) followed by maybe_constant_value but is more efficient,
- because calls instantiation_dependent_expression_p and
- potential_constant_expression at most once. */
+ (t, complain) followed by maybe_constant_value but is more efficient,
+ because it calls instantiation_dependent_expression_p and
+ potential_constant_expression at most once.
+
+ Callers should generally pass their active complain, or if they are in a
+ non-template, diagnosing context, they can use the default of
+ tf_warning_or_error. Callers that might be within a template context, don't
+ have a complain parameter, and aren't going to remember the result for long
+ (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
+ appropriately. */
tree
-fold_non_dependent_expr (tree t)
+fold_non_dependent_expr (tree t,
+ tsubst_flags_t complain /* = tf_warning_or_error */)
{
if (t == NULL_TREE)
return NULL_TREE;
if (is_nondependent_constant_expression (t))
{
processing_template_decl_sentinel s;
- t = instantiate_non_dependent_expr_internal (t, tf_none);
+ t = instantiate_non_dependent_expr_internal (t, complain);
if (type_unknown_p (t)
|| BRACE_ENCLOSED_INITIALIZER_P (t))
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);
+extern tree fold_non_dependent_expr (tree, tsubst_flags_t = tf_warning_or_error);
extern tree fold_simple (tree);
extern bool is_sub_constant_expr (tree);
extern bool reduced_constant_expression_p (tree);
/* It's not generally safe to fully fold inside of a template, so
call fold_non_dependent_expr instead. */
if (processing_template_decl)
- return fold_non_dependent_expr (x);
+ {
+ tree f = fold_non_dependent_expr (x, tf_none);
+ if (f == error_mark_node)
+ return x;
+ else
+ return f;
+ }
return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
}
/* Lots of logic below depends on whether we have a constant number of
elements, so go ahead and fold it now. */
- const_tree cst_outer_nelts = fold_non_dependent_expr (outer_nelts);
+ const_tree cst_outer_nelts = fold_non_dependent_expr (outer_nelts, complain);
/* If our base type is an array, then make sure we know how many elements
it has. */
/* Try to determine the constant value only for the purposes
of the diagnostic below but continue to use the original
value and handle const folding later. */
- const_tree cst_nelts = fold_non_dependent_expr (nelts);
+ const_tree cst_nelts = fold_non_dependent_expr (nelts, complain);
/* The expression in a noptr-new-declarator is erroneous if it's of
non-class type and its value before converting to std::size_t is
/* Don't instantiate the THEN_CLAUSE. */;
else
{
- bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
+ tree folded = fold_non_dependent_expr (tmp, complain);
+ bool inhibit = integer_zerop (folded);
if (inhibit)
++c_inhibit_evaluation_warnings;
RECUR (THEN_CLAUSE (t));
/* Don't instantiate the ELSE_CLAUSE. */;
else if (ELSE_CLAUSE (t))
{
- bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
+ tree folded = fold_non_dependent_expr (tmp, complain);
+ bool inhibit = integer_nonzerop (folded);
begin_else_clause (stmt);
if (inhibit)
++c_inhibit_evaluation_warnings;
{
tree cond = RECUR (TREE_OPERAND (t, 0));
cond = mark_rvalue_use (cond);
- tree folded_cond = fold_non_dependent_expr (cond);
+ tree folded_cond = fold_non_dependent_expr (cond, complain);
tree exp1, exp2;
if (TREE_CODE (folded_cond) == INTEGER_CST)
corresponding parameter is type-dependent. Make any necessary
adjustments based on whether arg is a reference. */
if (CONSTANT_CLASS_P (arg))
- parm = fold_non_dependent_expr (parm);
+ parm = fold_non_dependent_expr (parm, complain);
else if (REFERENCE_REF_P (arg))
{
tree sub = TREE_OPERAND (arg, 0);
/* Don't do this during concept expansion either and for
the same reason. */
&& !expanding_concept ())
- fold_non_dependent_expr (expr);
+ fold_non_dependent_expr (expr, tf_none);
STRIP_ANY_LOCATION_WRAPPER (expr);
/* Fold the expression and convert it to a boolean value. */
condition = perform_implicit_conversion_flags (boolean_type_node, condition,
complain, LOOKUP_NORMAL);
- condition = fold_non_dependent_expr (condition);
+ condition = fold_non_dependent_expr (condition, complain);
if (TREE_CODE (condition) == INTEGER_CST && !integer_zerop (condition))
/* Do nothing; the condition is satisfied. */
|| TREE_NO_WARNING (op))
return;
- tree cop = fold_non_dependent_expr (op);
+ tree cop = fold_non_dependent_expr (op, complain);
if (TREE_CODE (cop) == ADDR_EXPR
&& decl_with_nonnull_addr_p (TREE_OPERAND (cop, 0))
|| code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
{
enum tree_code tcode0 = code0, tcode1 = code1;
- tree cop1 = fold_non_dependent_expr (op1);
+ tree cop1 = fold_non_dependent_expr (op1, complain);
doing_div_or_mod = true;
warn_for_div_by_zero (location, cop1);
case TRUNC_MOD_EXPR:
case FLOOR_MOD_EXPR:
{
- tree cop1 = fold_non_dependent_expr (op1);
+ tree cop1 = fold_non_dependent_expr (op1, complain);
doing_div_or_mod = true;
warn_for_div_by_zero (location, cop1);
}
}
else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
{
- tree const_op1 = fold_non_dependent_expr (op1);
+ tree const_op1 = fold_non_dependent_expr (op1, complain);
if (TREE_CODE (const_op1) != INTEGER_CST)
const_op1 = op1;
result_type = type0;
}
else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
{
- tree const_op0 = fold_non_dependent_expr (op0);
+ tree const_op0 = fold_non_dependent_expr (op0, complain);
if (TREE_CODE (const_op0) != INTEGER_CST)
const_op0 = op0;
- tree const_op1 = fold_non_dependent_expr (op1);
+ tree const_op1 = fold_non_dependent_expr (op1, complain);
if (TREE_CODE (const_op1) != INTEGER_CST)
const_op1 = op1;
result_type = type0;
/* OP0 and/or OP1 might have side-effects. */
op0 = cp_save_expr (op0);
op1 = cp_save_expr (op1);
- op0 = fold_non_dependent_expr (op0);
- op1 = fold_non_dependent_expr (op1);
+ op0 = fold_non_dependent_expr (op0, complain);
+ op1 = fold_non_dependent_expr (op1, complain);
if (doing_div_or_mod
&& sanitize_flags_p (SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
{
if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl))
{
bool const_init;
+ tree oldval = value;
value = fold_non_dependent_expr (value);
if (DECL_DECLARED_CONSTEXPR_P (decl)
|| (DECL_IN_AGGR_P (decl)
/* FIXME setting TREE_CONSTANT on refs breaks the back end. */
if (!TYPE_REF_P (type))
TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
+ if (!const_init)
+ value = oldval;
}
value = cp_fully_fold (value);
return ok;
}
- init = fold_non_dependent_expr (init);
+ init = fold_non_dependent_expr (init, complain);
if (TREE_CODE (type) == INTEGER_TYPE
&& TREE_CODE (ftype) == REAL_TYPE)
init = TARGET_EXPR_INITIAL (init);
/* When we defer constant folding within a statement, we may want to
defer this folding as well. */
- tree t = fold_non_dependent_expr (init);
+ tree t = fold_non_dependent_expr (init, complain);
t = maybe_constant_init (t);
if (TREE_CONSTANT (t))
init = t;
--- /dev/null
+// PR c++/86219
+
+template <int a> struct t;
+template <int a>
+void f ()
+{
+ const int b = ""; // { dg-error "conversion" }
+ t<b>::c; // { dg-error "constant" }
+ // { dg-prune-output "template argument 1 is invalid" }
+}