]> git.ipfire.org Git - people/ms/gcc.git/commitdiff
c++: ICE with constexpr lambda [PR107280]
authorMarek Polacek <polacek@redhat.com>
Fri, 10 Mar 2023 15:14:20 +0000 (10:14 -0500)
committerMarek Polacek <polacek@redhat.com>
Wed, 15 Mar 2023 18:23:06 +0000 (14:23 -0400)
We crash here since r10-3661, the store_init_value hunk in particular.
Before, we called cp_fully_fold_init, so e.g.

  {.str=VIEW_CONVERT_EXPR<char[8]>("")}

was folded into

  {.str=""}

but now we don't fold and keep the VCE around, and it causes trouble in
cxx_eval_store_expression: in the !refs->is_empty () loop we descend on
.str's initializer but since it's wrapped in a VCE, we skip the STRING_CST
check and then crash on the CONSTRUCTOR_NO_CLEARING.

PR c++/107280

gcc/cp/ChangeLog:

* constexpr.cc (cxx_eval_store_expression): Strip location wrappers.

gcc/testsuite/ChangeLog:

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

gcc/cp/constexpr.cc
gcc/testsuite/g++.dg/cpp1z/constexpr-lambda28.C [new file with mode: 0644]

index 8683c00596aef0f9f040f95055b1476580129a22..abf6ee560c524664ed8d25cd1ae5dffdd46b1cfb 100644 (file)
@@ -6033,7 +6033,8 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
          *valp = build_constructor (type, NULL);
          CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
        }
-      else if (TREE_CODE (*valp) == STRING_CST)
+      else if (STRIP_ANY_LOCATION_WRAPPER (*valp),
+              TREE_CODE (*valp) == STRING_CST)
        {
          /* An array was initialized with a string constant, and now
             we're writing into one of its elements.  Explode the
diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-lambda28.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-lambda28.C
new file mode 100644 (file)
index 0000000..aafbfdd
--- /dev/null
@@ -0,0 +1,15 @@
+// PR c++/107280
+// { dg-do compile { target c++17 } }
+
+struct string {
+  char str[8] = "";
+};
+template <int, int> constexpr void
+test ()
+{
+  string str{};
+  auto append = [&](const char *s) { *str.str = *s; };
+  append("");
+}
+
+static_assert ((test<true, true>(), true), "");