When an if-stmt is determined to be non-constant because both of its
branches are non-constant, we issue a somewhat generic error which,
since the error also points to the 'if' token, misleadingly suggests
the condition is at fault:
constexpr-105050.C:8:3: error: expression ‘<statement>’ is not a constant expression
8 | if (p != q && *p < 0)
| ^~
This patch clarifies the error message to instead read:
constexpr-105050.C:8:3: error: neither branch of ‘if’ is a constant expression
8 | if (p != q && *p < 0)
| ^~
PR c++/105050
gcc/cp/ChangeLog:
* constexpr.cc (potential_constant_expression_1) <case IF_STMT>:
Clarify error message when a if-stmt is non-constant because its
branches are non-constant.
gcc/testsuite/ChangeLog:
* g++.dg/cpp1y/constexpr-105050.C: New test.
}
}
if (flags & tf_error)
- error_at (loc, "expression %qE is not a constant expression", t);
+ {
+ if (TREE_CODE (t) == IF_STMT)
+ error_at (loc, "neither branch of %<if%> is a constant expression");
+ else
+ error_at (loc, "expression %qE is not a constant expression", t);
+ }
return false;
case VEC_INIT_EXPR:
--- /dev/null
+// PR c++/105050
+// { dg-do compile { target c++14 } }
+
+void g();
+void h();
+
+constexpr void f(int* p, int* q) {
+ if (p != q && *p < 0) // { dg-error "neither branch of 'if' is a constant expression" }
+ g();
+ else
+ h();
+}