diff mbox series

C++ PATCH for c++/84559, ICE with constexpr VLA

Message ID CADzB+2nqdwSKRu4BuinJqZvKY9pbXVsmrtXZgpvKA1oUAzoLeA@mail.gmail.com
State New
Headers show
Series C++ PATCH for c++/84559, ICE with constexpr VLA | expand

Commit Message

Jason Merrill Feb. 26, 2018, 9:58 p.m. UTC
Clang rejects this testcase saying that the VLA is a non-literal type,
but I think we don't want that; we want to allow VLAs in a constexpr
function, where the length can be constant during constexpr
evaluation.

In this case, however, we are dealing with a constexpr variable rather
than a variable in a constexpr function, so we want to reject the VLA.

Tested x86_64-pc-linux-gnu, applying to trunk.
commit d63c30c86d45831f20c4dc35f4ffaac2cfdd2588
Author: Jason Merrill <jason@redhat.com>
Date:   Mon Feb 26 15:32:32 2018 -0500

            PR c++/84559 - ICE with constexpr VLA.
    
            * constexpr.c (ensure_literal_type_for_constexpr_object): Check
            for constexpr variable with VLA type.
diff mbox series

Patch

diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c
index 26d0d099a05..92b13219215 100644
--- a/gcc/cp/constexpr.c
+++ b/gcc/cp/constexpr.c
@@ -112,6 +112,13 @@  ensure_literal_type_for_constexpr_object (tree decl)
 	      cp_function_chain->invalid_constexpr = true;
 	    }
 	}
+      else if (DECL_DECLARED_CONSTEXPR_P (decl)
+	       && variably_modified_type_p (type, NULL_TREE))
+	{
+	  error ("%<constexpr%> variable %qD has variably-modified type %qT",
+		 decl, type);
+	  decl = error_mark_node;
+	}
     }
   return decl;
 }
diff --git a/gcc/testsuite/g++.dg/ext/constexpr-vla5.C b/gcc/testsuite/g++.dg/ext/constexpr-vla5.C
new file mode 100644
index 00000000000..565d40ab077
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ext/constexpr-vla5.C
@@ -0,0 +1,7 @@ 
+// PR c++/84559
+// { dg-do compile { target c++11 } }
+
+void foo(int i)
+{
+  constexpr char x[i] = "";	// { dg-error "" }
+}