]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: constantness of call to function pointer [PR111703]
authorPatrick Palka <ppalka@redhat.com>
Thu, 16 Nov 2023 14:32:07 +0000 (09:32 -0500)
committerPatrick Palka <ppalka@redhat.com>
Thu, 16 Nov 2023 14:32:07 +0000 (09:32 -0500)
potential_constant_expression for CALL_EXPR tests FUNCTION_POINTER_TYPE_P
on the callee rather than on the type of the callee, which means we
always pass want_rval=any when recursing and so may fail to identify a
non-constant function pointer callee as such.  Fixing this turns out to
further work around PR111703.

PR c++/111703
PR c++/107939

gcc/cp/ChangeLog:

* constexpr.cc (potential_constant_expression_1) <case CALL_EXPR>:
Fix FUNCTION_POINTER_TYPE_P test.

gcc/testsuite/ChangeLog:

* g++.dg/cpp2a/concepts-fn8.C: Extend test.
* g++.dg/diagnostic/constexpr4.C: New test.

gcc/cp/constexpr.cc
gcc/testsuite/g++.dg/cpp2a/concepts-fn8.C
gcc/testsuite/g++.dg/diagnostic/constexpr4.C [new file with mode: 0644]

index 8a6b210144afac1b35ba61f9c94ced5e216a68c9..344107d494b4218c22959f95735aa459b64fc530 100644 (file)
@@ -9547,7 +9547,12 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
          }
        else if (fun)
           {
-           if (RECUR (fun, FUNCTION_POINTER_TYPE_P (fun) ? rval : any))
+           if (TREE_TYPE (fun)
+               && FUNCTION_POINTER_TYPE_P (TREE_TYPE (fun)))
+             want_rval = rval;
+           else
+             want_rval = any;
+           if (RECUR (fun, want_rval))
              /* Might end up being a constant function pointer.  But it
                 could also be a function object with constexpr op(), so
                 we pass 'any' so that the underlying VAR_DECL is deemed
index 3f63a5b28d70c9e06443fc3cf8dfce7aa1217302..c63d26c931d9e3e7dbdca4b0ac74a715a98d0c25 100644 (file)
@@ -15,10 +15,12 @@ struct P {
 };
 
 void (*f)(P);
+P (*h)(P);
 
 template<class T>
 constexpr bool g() {
   P x;
   f(x); // { dg-bogus "from here" }
+  f(h(x)); // { dg-bogus "from here" }
   return true;
 }
diff --git a/gcc/testsuite/g++.dg/diagnostic/constexpr4.C b/gcc/testsuite/g++.dg/diagnostic/constexpr4.C
new file mode 100644 (file)
index 0000000..f37c01c
--- /dev/null
@@ -0,0 +1,9 @@
+// Verify we diagnose a call to a non-constant function pointer ahead of time.
+// { dg-do compile { target c++11 } }
+
+bool (*f)(int);
+
+template<int N>
+void g() {
+  static_assert(f(N), ""); // { dg-error "non-constant|'f' is not usable" }
+}