]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR c++/89336 (internal compiler error when compiling a constexpr function)
authorJakub Jelinek <jakub@redhat.com>
Wed, 20 Feb 2019 21:16:27 +0000 (22:16 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Wed, 20 Feb 2019 21:16:27 +0000 (22:16 +0100)
PR c++/89336
* constexpr.c (cxx_eval_store_expression): Diagnose changing of active
union member for -std=c++17 and earlier.

* g++.dg/cpp1y/constexpr-89336-3.C: New test.

From-SVN: r269052

gcc/cp/ChangeLog
gcc/cp/constexpr.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp1y/constexpr-89336-3.C [new file with mode: 0644]

index dc0d4a2081441c6df635fefde6fba13e649a4301..fabcb1a5b4c191a5834dbdb5571ef1ce1fdf502d 100644 (file)
@@ -1,3 +1,9 @@
+2019-02-20  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/89336
+       * constexpr.c (cxx_eval_store_expression): Diagnose changing of active
+       union member for -std=c++17 and earlier.
+
 2019-02-19  Jason Merrill  <jason@redhat.com>
 
        PR c++/87513 - 'sorry' mangling PMF template-id.
index d413c6b9b275b299045664a62467e37b039449ed..a9aa7d500412db394c32df9fe1b7ba1bef51b603 100644 (file)
@@ -3813,8 +3813,20 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
 
          if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp)
              && CONSTRUCTOR_ELT (*valp, 0)->index != index)
-           /* Changing active member.  */
-           vec_safe_truncate (CONSTRUCTOR_ELTS (*valp), 0);
+           {
+             if (cxx_dialect < cxx2a)
+               {
+                 if (!ctx->quiet)
+                   error_at (cp_expr_loc_or_loc (t, input_location),
+                             "change of the active member of a union "
+                             "from %qD to %qD",
+                             CONSTRUCTOR_ELT (*valp, 0)->index,
+                             index);
+                 *non_constant_p = true;
+               }
+             /* Changing active member.  */
+             vec_safe_truncate (CONSTRUCTOR_ELTS (*valp), 0);
+           }
 
          for (idx = 0;
               vec_safe_iterate (CONSTRUCTOR_ELTS (*valp), idx, &cep);
index 9d344ba7c2d8bf880db60c3c910eacfc4eb5a6f0..6be764f6ddbb8dba80cba4e2a8904e0bb40b4374 100644 (file)
@@ -1,3 +1,8 @@
+2019-02-20  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/89336
+       * g++.dg/cpp1y/constexpr-89336-3.C: New test.
+
 2019-02-20  David Malcolm  <dmalcolm@redhat.com>
 
        PR c/89410
diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-89336-3.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-89336-3.C
new file mode 100644 (file)
index 0000000..9d370dd
--- /dev/null
@@ -0,0 +1,46 @@
+// PR c++/89336
+// { dg-do compile { target c++14 } }
+
+constexpr int
+foo ()
+{
+  union U { int a; long b; };
+  union V { union U u; short v; };
+  V w {};
+  w.u.a = w.v = w.u.b = 5L;            // { dg-error "change of the active member of a union from" "" { target c++17_down } }
+  return w.u.a;
+}
+
+static_assert (foo () == 5, "");       // { dg-error "non-constant condition for static assertion" "" { target c++17_down } }
+                                       // { dg-message "expansion of" "" { target c++17_down } .-1 }
+
+constexpr int
+bar ()
+{
+  union U { int a[5]; long b; };
+  union V { union U u; short v; };
+  V w {};
+  w.v = 5;
+  w.u.a[3] = w.u.a[1] = w.v;           // { dg-error "change of the active member of a union from" "" { target c++17_down } }
+  return w.u.a[1] + w.u.a[3];
+}
+
+static_assert (bar () == 10, "");      // { dg-error "non-constant condition for static assertion" "" { target c++17_down } }
+                                       // { dg-message "expansion of" "" { target c++17_down } .-1 }
+
+struct Z { int x, y; };
+
+constexpr Z
+baz ()
+{
+  union W { Z a; long long w; };
+  W w {};
+  w.a = { 5, 0 };
+  w.a = { (int) (w.w = 17LL + w.a.x), 2 };     // { dg-error "change of the active member of a union from" "" { target c++17_down } }
+  return w.a;
+}
+
+static_assert (baz ().x == 22, "");    // { dg-error "non-constant condition for static assertion" "" { target c++17_down } }
+                                       // { dg-message "expansion of" "" { target c++17_down } .-1 }
+static_assert (baz ().y == 2, "");     // { dg-error "non-constant condition for static assertion" "" { target c++17_down } }
+                                       // { dg-message "expansion of" "" { target c++17_down } .-1 }