re PR c++/84854 (ICE: unexpected expression in cxx_eval_constant_expression, at cp...
authorMarek Polacek <polacek@redhat.com>
Thu, 22 Mar 2018 08:08:07 +0000 (08:08 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Thu, 22 Mar 2018 08:08:07 +0000 (08:08 +0000)
PR c++/84854
* semantics.c (finish_if_stmt_cond): Check if the type of the condition
is boolean.

* g++.dg/cpp1z/constexpr-if15.C: New test.
* g++.dg/cpp1z/constexpr-if16.C: New test.

From-SVN: r258756

gcc/cp/ChangeLog
gcc/cp/semantics.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp1z/constexpr-if15.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1z/constexpr-if16.C [new file with mode: 0644]

index 44de2fb5b37bd3cad5cdc544cc3a8baec971f108..f9e7a57ec03dffb235c7916a267f2e1a23cfc058 100644 (file)
@@ -1,3 +1,9 @@
+2018-03-22  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/84854
+       * semantics.c (finish_if_stmt_cond): Check if the type of the condition
+       is boolean.
+
 2018-03-21  Jason Merrill  <jason@redhat.com>
 
        PR c++/81311 - wrong C++17 overload resolution.
index 4b9fc27743621a0b1f88afb3c777bc1ceaa22ffe..97fa57ae94e54b12faec1e984ba46ac73e127e46 100644 (file)
@@ -733,7 +733,10 @@ finish_if_stmt_cond (tree cond, tree if_stmt)
   if (IF_STMT_CONSTEXPR_P (if_stmt)
       && !type_dependent_expression_p (cond)
       && require_constant_expression (cond)
-      && !value_dependent_expression_p (cond))
+      && !value_dependent_expression_p (cond)
+      /* Wait until instantiation time, since only then COND has been
+        converted to bool.  */
+      && TREE_TYPE (cond) == boolean_type_node)
     {
       cond = instantiate_non_dependent_expr (cond);
       cond = cxx_constant_value (cond, NULL_TREE);
index cc4cbbc857f433b6be1cd2ac9450321ab96f1abe..00b804c701893c07153f7d2231f43e162004d1d1 100644 (file)
@@ -1,3 +1,9 @@
+2018-03-22  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/84854
+       * g++.dg/cpp1z/constexpr-if15.C: New test.
+       * g++.dg/cpp1z/constexpr-if16.C: New test.
+
 2018-03-21  Joseph Myers  <joseph@codesourcery.com>
 
        * gcc.dg/builtin-tgmath-3.c: New test.
diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-if15.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-if15.C
new file mode 100644 (file)
index 0000000..9a9053c
--- /dev/null
@@ -0,0 +1,11 @@
+// PR c++/84854
+// { dg-options -std=c++17 }
+
+constexpr int foo () { return 1; }
+constexpr int foo (int) { return 2; }
+
+template <typename>
+void a()
+{
+  if constexpr(foo) { };
+}
diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-if16.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-if16.C
new file mode 100644 (file)
index 0000000..31a1497
--- /dev/null
@@ -0,0 +1,20 @@
+// { dg-options -std=c++17 }
+
+struct A
+{
+  constexpr operator bool () { return true; }
+  int i;
+};
+
+A a;
+
+template <class T> void f()
+{
+  constexpr bool b = a;
+  if constexpr (a) { }
+}
+
+int main()
+{
+  f<int>();
+}