C++ PATCH for c++/84421, wrong error with type-dependent constexpr if

Message ID CADzB+2=vPSnqOJc0aBxgXk+JBGG_WFKK=-=5CPLrifm=R3vshA@mail.gmail.com
State New
Headers show
Series
  • C++ PATCH for c++/84421, wrong error with type-dependent constexpr if
Related show

Commit Message

Jason Merrill Feb. 16, 2018, 4:43 p.m.
We can't judge whether the condition is a constant-expression until
we've converted it to bool; if it's type-dependent, let's leave that
checking for later.

Tested x86_64-pc-linux-gnu, applying to trunk.
commit b1fa979536b793b6bb1b9262e65ba6c753d5802b
Author: Jason Merrill <jason@redhat.com>
Date:   Fri Feb 16 10:53:53 2018 -0500

            PR c++/84421 - type-dependent if constexpr
    
            * semantics.c (finish_if_stmt_cond): Check
            type_dependent_expression_p.

Patch

diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index f0cee68e46f..35569d0cb0d 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -731,6 +731,7 @@  finish_if_stmt_cond (tree cond, tree if_stmt)
 {
   cond = maybe_convert_cond (cond);
   if (IF_STMT_CONSTEXPR_P (if_stmt)
+      && !type_dependent_expression_p (cond)
       && require_constant_expression (cond)
       && !value_dependent_expression_p (cond))
     {
diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-if14.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-if14.C
new file mode 100644
index 00000000000..f6cc39a0c2f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-if14.C
@@ -0,0 +1,14 @@ 
+// PR c++/84421
+// { dg-options -std=c++17 }
+
+struct A{
+  constexpr operator bool() const { return true; }
+};
+
+int main(){
+  auto f = [](auto v){
+    if constexpr(v){}
+  };
+  A a;
+  f(a);
+}