We need to call instantiate_non_dependent_expr before
cxx_constant_value in a template.

Tested x86_64-pc-linux-gnu, applying to trunk and 7.
commit 1645e51aeab6cea4e7206cb6a3520eaf383e47f6
Author: Jason Merrill <ja...@redhat.com>
Date:   Mon Jun 19 15:47:47 2017 -0400

            PR c++/80562 - ICE with constexpr if.
    
            * semantics.c (finish_if_stmt_cond): Call
            instantiate_non_dependent_expr.

diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index 5b5ec54..5fe772a 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -733,7 +733,10 @@ finish_if_stmt_cond (tree cond, tree if_stmt)
   if (IF_STMT_CONSTEXPR_P (if_stmt)
       && require_potential_rvalue_constant_expression (cond)
       && !value_dependent_expression_p (cond))
-    cond = cxx_constant_value (cond, NULL_TREE);
+    {
+      cond = instantiate_non_dependent_expr (cond);
+      cond = cxx_constant_value (cond, NULL_TREE);
+    }
   finish_cond (&IF_COND (if_stmt), cond);
   add_stmt (if_stmt);
   THEN_CLAUSE (if_stmt) = push_stmt_list ();
diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-if12.C 
b/gcc/testsuite/g++.dg/cpp1z/constexpr-if12.C
new file mode 100644
index 0000000..1ed2c30
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-if12.C
@@ -0,0 +1,14 @@
+// PR c++/80562
+// { dg-options -std=c++1z }
+
+struct T {
+  constexpr auto foo() { return false; }
+};
+
+template <class MustBeTemplate>
+constexpr auto bf(T t) {
+    if constexpr(t.foo()) {
+        return false;
+    }
+    return true;
+}

Reply via email to