https://gcc.gnu.org/g:f60570b26446781c0205981804f6aa4ff1708b12

commit r15-9226-gf60570b26446781c0205981804f6aa4ff1708b12
Author: Patrick Palka <ppa...@redhat.com>
Date:   Sat Apr 5 15:22:48 2025 -0400

    c++: harmless use of 'this' rejected despite P2280R4 [PR118249]
    
    Here the implicit use of 'this' in inner.size() template argument was
    being rejected despite P2280R4 relaxations, due to the special *this
    handling in the INDIRECT_REF case of potential_constant_expression_1.
    
    This handling was originally added by r196737 as part of fixing PR56481,
    and it seems obsolete especially in light of P2280R4.  There doesn't
    seem to be a good reason that we need to handle *this specially from
    other dereferences.
    
    This patch therefore removes this special handling.  As a side benefit
    we now correctly reject some *reinterpret_cast<...>(...) constructs
    earlier, via p_c_e_1 rather than via constexpr evaluation (because the
    removed STRIP_NOPS step meant we'd overlook such casts), which causes a
    couple of diagnostic changes (for the better).
    
            PR c++/118249
    
    gcc/cp/ChangeLog:
    
            * constexpr.cc (potential_constant_expression_1)
            <case INDIRECT_REF>: Remove obsolete *this handling.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp0x/constexpr-reinterpret2.C: Expect error at
            call site of the non-constexpr functions.
            * g++.dg/cpp23/constexpr-nonlit12.C: Likewise.
            * g++.dg/cpp0x/constexpr-ref14.C: New test.
    
    Reviewed-by: Jason Merrill <ja...@redhat.com>

Diff:
---
 gcc/cp/constexpr.cc                                 | 17 +----------------
 gcc/testsuite/g++.dg/cpp0x/constexpr-ref14.C        | 21 +++++++++++++++++++++
 gcc/testsuite/g++.dg/cpp0x/constexpr-reinterpret2.C |  4 ++--
 gcc/testsuite/g++.dg/cpp23/constexpr-nonlit12.C     |  2 +-
 4 files changed, 25 insertions(+), 19 deletions(-)

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 9a57f4865e0f..37ea65cb6550 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -10279,22 +10279,7 @@ potential_constant_expression_1 (tree t, bool 
want_rval, bool strict, bool now,
       return true;
 
     case INDIRECT_REF:
-      {
-        tree x = TREE_OPERAND (t, 0);
-        STRIP_NOPS (x);
-        if (is_this_parameter (x) && !is_capture_proxy (x))
-         {
-           if (now || !var_in_maybe_constexpr_fn (x))
-             {
-               if (flags & tf_error)
-                 constexpr_error (loc, fundef_p, "use of %<this%> in a "
-                                  "constant expression");
-               return false;
-             }
-           return true;
-         }
-       return RECUR (x, rval);
-      }
+      return RECUR (TREE_OPERAND (t, 0), rval);
 
     case STATEMENT_LIST:
       for (tree stmt : tsi_range (t))
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-ref14.C 
b/gcc/testsuite/g++.dg/cpp0x/constexpr-ref14.C
new file mode 100644
index 000000000000..f201afbd81f9
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-ref14.C
@@ -0,0 +1,21 @@
+// PR c++/118249
+// { dg-do compile { target c++11 } }
+
+template <int I>
+void f() { }
+
+template <int N>
+struct array {
+    constexpr int size() const { return N; }
+};
+
+extern array<10>& outer;
+
+struct C {
+    array<10> inner;
+
+    void g() {
+        f<outer.size()>(); // OK
+        f<inner.size()>(); // was error: use of 'this' in a constant expression
+    }
+};
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-reinterpret2.C 
b/gcc/testsuite/g++.dg/cpp0x/constexpr-reinterpret2.C
index 1bc2a8f30122..52328bc4ff55 100644
--- a/gcc/testsuite/g++.dg/cpp0x/constexpr-reinterpret2.C
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-reinterpret2.C
@@ -17,5 +17,5 @@ bar ()
   return ((void **) &s)[0];    // { dg-error "reinterpret_cast" }
 }
 
-constexpr auto x = foo ();
-constexpr auto y = bar ();
+constexpr auto x = foo (); // { dg-error "called in a constant expression" }
+constexpr auto y = bar (); // { dg-error "called in a constant expression" }
diff --git a/gcc/testsuite/g++.dg/cpp23/constexpr-nonlit12.C 
b/gcc/testsuite/g++.dg/cpp23/constexpr-nonlit12.C
index 8f003b801907..b104b16784fc 100644
--- a/gcc/testsuite/g++.dg/cpp23/constexpr-nonlit12.C
+++ b/gcc/testsuite/g++.dg/cpp23/constexpr-nonlit12.C
@@ -19,6 +19,6 @@ void
 g ()
 {
   constexpr int i = 42;
-  constexpr auto a1 = fn0 (&i);
+  constexpr auto a1 = fn0 (&i); // { dg-error "called in a constant 
expression" }
   constexpr auto a2 = fn1 (i); // { dg-error "called in a constant expression" 
}
 }

Reply via email to