]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: check DECL_INITIAL for constexpr
authorJason Merrill <jason@redhat.com>
Tue, 20 Sep 2022 21:12:29 +0000 (17:12 -0400)
committerJason Merrill <jason@redhat.com>
Thu, 29 Sep 2022 17:50:54 +0000 (13:50 -0400)
We were overlooking non-potentially-constant bits in variable initializer
because we didn't walk into DECL_INITIAL.

gcc/cp/ChangeLog:

* constexpr.cc (potential_constant_expression_1): Look into
DECL_INITIAL.  Use location wrappers.

gcc/testsuite/ChangeLog:

* g++.dg/cpp1y/constexpr-local4.C: Expect error sooner.
* g++.dg/cpp2a/consteval24.C: Likewise.
* g++.dg/cpp2a/consteval7.C: Likewise.
* g++.dg/cpp2a/inline-asm3.C: Likewise.

gcc/cp/constexpr.cc
gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C
gcc/testsuite/g++.dg/cpp2a/consteval24.C
gcc/testsuite/g++.dg/cpp2a/consteval7.C
gcc/testsuite/g++.dg/cpp2a/inline-asm3.C

index 10639876d9cfc45694f8cecd84213e9a5c8abd5c..ed41d7552696ab137db13098de4c46b7f56b5d40 100644 (file)
@@ -8892,7 +8892,10 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
       {
         tree from = TREE_OPERAND (t, 0);
        if (location_wrapper_p (t))
-         return (RECUR (from, want_rval));
+         {
+           iloc_sentinel ils = loc;
+           return (RECUR (from, want_rval));
+         }
        if (INDIRECT_TYPE_P (TREE_TYPE (t)))
          {
            STRIP_ANY_LOCATION_WRAPPER (from);
@@ -9348,7 +9351,7 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
                   (tmp, /*constexpr_context_p=*/true, flags))
            return false;
        }
-      return RECUR (tmp, want_rval);
+      return RECUR (DECL_INITIAL (tmp), want_rval);
 
     case TRY_FINALLY_EXPR:
       return (RECUR (TREE_OPERAND (t, 0), want_rval)
index bef6248857921f8e55c81717b36aa556f9880c09..647b5dcd7cd5a8beaec5077ad2eba900238f8d8a 100644 (file)
@@ -10,8 +10,8 @@ const A a = 42;
 
 constexpr int f()
 {
-  const int j = a.i;           // { dg-message "'a'" }
+  const int j = a.i;           // { dg-error "'a'" }
   return j;
 }
 
-static_assert (f() == 42,"");  // { dg-error "non-constant" }
+static_assert (f() == 42,"");  // { dg-error "" }
index 6d0c63c749bec63da68446bc6dfe8206dda56eec..6d7034c5515e322c5a4ac102cace4da80434d3cd 100644 (file)
@@ -27,4 +27,4 @@ bar ()
   return fn1 () + fn2 () + (s.*fn3) () + (s.*fn4) () + fn5 () + (s.*fn6) () + (s.*fn7) ();
 }
 
-auto a = bar ();
+auto a = bar ();               // { dg-error "bar" }
index 23f3d25a39e03f45980899c67e41714951459ef1..74996d31a82e7b0bedca15512b360039feecde6b 100644 (file)
@@ -10,4 +10,4 @@ consteval int qux () { S s = baz (); return s.b + s.c (); }
 consteval int quux () { constexpr S s = baz (); return s.b + s.c (); } // { dg-error "immediate evaluation returns address of immediate function 'consteval int foo\\(\\)'" }
 constexpr auto d = baz ();     // { dg-error "immediate evaluation returns address of immediate function 'consteval int foo\\(\\)'" }
 constexpr auto e = qux ();
-constexpr auto f = quux ();
+constexpr auto f = quux ();    // { dg-error "quux" }
index a7476b1d9d1a48fbbda4100fdf936088437d9307..a6f612e2447eceaba5a2a20f113ecddcf8337705 100644 (file)
@@ -9,4 +9,4 @@ foo ()
  return i;
 }
 
-constexpr int i = foo ();
+constexpr int i = foo ();      // { dg-error "foo" }