]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
PR c++/89599
authorjakub <jakub@138bc75d-0d04-0410-961f-82ee72b054a4>
Fri, 8 Mar 2019 07:43:58 +0000 (07:43 +0000)
committerjakub <jakub@138bc75d-0d04-0410-961f-82ee72b054a4>
Fri, 8 Mar 2019 07:43:58 +0000 (07:43 +0000)
* constexpr.c (potential_constant_expression_1): Reject
REINTERPRET_CAST_P NOP_EXPRs.

* g++.dg/ubsan/vptr-4.C: Adjust expected diagnostics.
* g++.dg/parse/array-size2.C: Likewise.
* g++.dg/cpp0x/constexpr-89599.C: New test.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@269482 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/cp/ChangeLog
gcc/cp/constexpr.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp0x/constexpr-89599.C [new file with mode: 0644]
gcc/testsuite/g++.dg/parse/array-size2.C
gcc/testsuite/g++.dg/ubsan/vptr-4.C

index 9bdb4936053f3ead93e0d95a4bb62a3d45f7cd4c..c6e023ff6630bf0703718442ae95b059a6f1be49 100644 (file)
@@ -1,5 +1,9 @@
 2019-03-08  Jakub Jelinek  <jakub@redhat.com>
 
+       PR c++/89599
+       * constexpr.c (potential_constant_expression_1): Reject
+       REINTERPRET_CAST_P NOP_EXPRs.
+
        PR c++/89622
        * call.c (joust): Call print_z_candidate only if pedwarn returned
        true.
index 1c3c7252807164ba808abe8411131587730f03b7..783d1fcb496f68c41c3c078876bdec12b07ca440 100644 (file)
@@ -5961,6 +5961,13 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
       return true;
 
     case NOP_EXPR:
+      if (REINTERPRET_CAST_P (t))
+       {
+         if (flags & tf_error)
+           error_at (loc, "a reinterpret_cast is not a constant expression");
+         return false;
+       }
+      /* FALLTHRU */
     case CONVERT_EXPR:
     case VIEW_CONVERT_EXPR:
       /* -- a reinterpret_cast.  FIXME not implemented, and this rule
index 777c1b29937a9a5e0f6fe5c192c778a253c04ad0..5db4e375b277df6522d9d2c8801e70e1f5d26445 100644 (file)
@@ -1,5 +1,10 @@
 2019-03-08  Jakub Jelinek  <jakub@redhat.com>
 
+       PR c++/89599
+       * g++.dg/ubsan/vptr-4.C: Adjust expected diagnostics.
+       * g++.dg/parse/array-size2.C: Likewise.
+       * g++.dg/cpp0x/constexpr-89599.C: New test.
+
        PR c++/89622
        * g++.dg/warn/pr89622.C: New test.
 
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-89599.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-89599.C
new file mode 100644 (file)
index 0000000..07760a3
--- /dev/null
@@ -0,0 +1,6 @@
+// PR c++/89599
+// { dg-do compile { target c++11 } }
+
+void foo (int x) {}
+constexpr void *arr[2] = { (void*) &foo, (void *) foo };// { dg-error "a reinterpret_cast is not a constant expression" }
+constexpr void *ptr = (void *) &foo;                   // { dg-error "a reinterpret_cast is not a constant expression" }
index e58fe266e77154cfad8089ada733955de9662874..c4a69df3b0160e514dc8bf56a42218492fbb83cf 100644 (file)
@@ -15,6 +15,8 @@ void
 foo (void)
 {
   char g[(char *) &((struct S *) 0)->b - (char *) 0]; // { dg-error "40:size of array .g. is not an integral constant-expression" }
+                                                     // { dg-error "narrowing conversion" "" { target c++11 } .-1 }
+                                                     // { dg-message "expression has a constant value but is not a C.. constant-expression" "" { target c++11 } .-2 }
   char h[(__SIZE_TYPE__) &((struct S *) 8)->b];              // { dg-error "10:size of array .h. is not an integral constant-expression" }
   bar (g, h);
 }
index cf638e9b35f9d9225c2c10408a0f35db6652ec09..764f599950162ce92f110d6241da273bba299c21 100644 (file)
@@ -19,7 +19,7 @@ struct T : S {
 };
 
 constexpr T t;
-constexpr const T *p = t.foo ();       // { dg-message "expansion of" }
+constexpr const T *p = t.foo ();       // { dg-error "called in a constant expression" }
 
 template <typename U>
 struct V {
@@ -39,17 +39,16 @@ struct W : V<U> {
 };
 
 constexpr W<int> w;
-constexpr const W<int> *s = w.foo ();  // { dg-error "is not a constant expression" }
-// { dg-message "expansion of" "" { target *-*-* } .-1 }
+constexpr const W<int> *s = w.foo ();  // { dg-error "called in a constant expression" }
 
 template <typename U>
 int foo (void)
 {
   static constexpr T t;
-  static constexpr const T *p = t.foo ();      // { dg-message "expansion of" }
+  static constexpr const T *p = t.foo ();      // { dg-error "called in a constant expression" }
   static constexpr W<U> w;
-  static constexpr const W<U> *s = w.foo ();   // { dg-error "is not a constant expression" }
-  return t.b + w.b;                            // { dg-message "expansion of" "" { target *-*-* } .-1 }
+  static constexpr const W<U> *s = w.foo ();   // { dg-error "called in a constant expression" }
+  return t.b + w.b;
 }
 
 int x = foo <char> ();