aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorSimon Martin <simon@nasilyan.com>2024-12-03 14:30:43 +0100
committerSimon Martin <simon@nasilyan.com>2024-12-04 11:34:07 +0100
commit72a2380a306a1c3883cb7e4f99253522bc265af0 (patch)
tree73187fcb4421d1e27712137c40f10bed94665b70 /gcc
parent205591919214cb5610e9c3b2394f05a3cfaa7f68 (diff)
downloadgcc-72a2380a306a1c3883cb7e4f99253522bc265af0.zip
gcc-72a2380a306a1c3883cb7e4f99253522bc265af0.tar.gz
gcc-72a2380a306a1c3883cb7e4f99253522bc265af0.tar.bz2
c++: Don't reject pointer to virtual method during constant evaluation [PR117615]
We currently reject the following valid code: === cut here === struct Base { virtual void doit (int v) const {} }; struct Derived : Base { void doit (int v) const {} }; using fn_t = void (Base::*)(int) const; struct Helper { fn_t mFn; constexpr Helper (auto && fn) : mFn(static_cast<fn_t>(fn)) {} }; void foo () { constexpr Helper h (&Derived::doit); } === cut here === The problem is that since r6-4014-gdcdbc004d531b4, &Derived::doit is represented with an expression with type pointer to method and using an INTEGER_CST (here 1), and that cxx_eval_constant_expression rejects any such expression with a non-null INTEGER_CST. This patch uses the same strategy as r12-4491-gf45610a45236e9 (fix for PR c++/102786), and simply lets such expressions go through. PR c++/117615 gcc/cp/ChangeLog: * constexpr.cc (cxx_eval_constant_expression): Don't reject INTEGER_CSTs with type POINTER_TYPE to METHOD_TYPE. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/constexpr-virtual22.C: New test.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/cp/constexpr.cc6
-rw-r--r--gcc/testsuite/g++.dg/cpp2a/constexpr-virtual22.C22
2 files changed, 28 insertions, 0 deletions
diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 5a87fa4..c1d5401 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -8275,6 +8275,12 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
return t;
}
}
+ else if (TYPE_PTR_P (type)
+ && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
+ /* INTEGER_CST with pointer-to-method type is only used
+ for a virtual method in a pointer to member function.
+ Don't reject those. */
+ ;
else
{
/* This detects for example:
diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-virtual22.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-virtual22.C
new file mode 100644
index 0000000..89330bf
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-virtual22.C
@@ -0,0 +1,22 @@
+// PR c++/117615
+// { dg-do "compile" { target c++20 } }
+
+struct Base {
+ virtual void doit (int v) const {}
+};
+
+struct Derived : Base {
+ void doit (int v) const {}
+};
+
+using fn_t = void (Base::*)(int) const;
+
+struct Helper {
+ fn_t mFn;
+ constexpr Helper (auto && fn) : mFn(static_cast<fn_t>(fn)) {}
+};
+
+void foo () {
+ constexpr Helper h (&Derived::doit);
+ constexpr Helper h2 (&Base::doit);
+}