On 1/23/26 12:40 AM, Marek Polacek wrote:
Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

OK.

-- >8 --
When extracting a function pointer, removing noexcept should be
allowed (but not the other way round):

   int fn (int) noexcept;
   constexpr auto a = extract<int (*)(int)>(^^fn);

but currently we reject this code -- I didn't realize that fnptr_conv_p
allows things that same_type_p doesn't allow, and in can_extract_* we
should check both.  And then we need to perform the actual conversion.

        PR c++/123620

gcc/cp/ChangeLog:

        * reflect.cc (can_extract_member_or_function_p): Also check
        fnptr_conv_p.
        (extract_member_or_function): Call perform_implicit_conversion.

gcc/testsuite/ChangeLog:

        * g++.dg/reflect/extract1.C: Test removing noexcept.
        * g++.dg/reflect/extract2.C: Adjust static_assert.
---
  gcc/cp/reflect.cc                       | 9 ++++++---
  gcc/testsuite/g++.dg/reflect/extract1.C | 7 ++-----
  gcc/testsuite/g++.dg/reflect/extract2.C | 4 ++--
  3 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/gcc/cp/reflect.cc b/gcc/cp/reflect.cc
index 9f979b5c0e7..bbbd6cb6e4f 100644
--- a/gcc/cp/reflect.cc
+++ b/gcc/cp/reflect.cc
@@ -7205,7 +7205,7 @@ can_extract_member_or_function_p (tree T, tree r, 
reflect_kind kind)
        tree F = TREE_TYPE (r);
        F = build_pointer_type (F);
        F = build_ptrmemfunc_type (F);
-      if (same_type_p (T, F))
+      if (same_type_p (T, F) || fnptr_conv_p (T, F))
        return true;
        return false;
      }
@@ -7213,7 +7213,7 @@ can_extract_member_or_function_p (tree T, tree r, 
reflect_kind kind)
      {
        tree F = TREE_TYPE (r);
        F = build_pointer_type (F);
-      if (same_type_p (T, F))
+      if (same_type_p (T, F) || fnptr_conv_p (T, F))
        return true;
        return false;
      }
@@ -7249,7 +7249,10 @@ extract_member_or_function (location_t loc, const 
constexpr_ctx *ctx,
const tsubst_flags_t complain = complain_flags (ctx);
    if (POINTER_TYPE_P (T))
-    return cp_build_addr_expr (r, complain);
+    {
+      r = cp_build_addr_expr (r, complain);
+      return perform_implicit_conversion (T, r, complain);
+    }
    else
      {
        if (!mark_used (r, complain))
diff --git a/gcc/testsuite/g++.dg/reflect/extract1.C 
b/gcc/testsuite/g++.dg/reflect/extract1.C
index 6eaf9ffe4b9..4f05fce40a5 100644
--- a/gcc/testsuite/g++.dg/reflect/extract1.C
+++ b/gcc/testsuite/g++.dg/reflect/extract1.C
@@ -171,13 +171,10 @@ constexpr auto a4 = extract<int (*)() noexcept>(^^B::fn4);
  constexpr auto a5 = extract<int (*)(B)>(^^B::fn5);
  constexpr auto a6 = extract<int (B::*)(int) &>(^^B::fn6);
  constexpr auto a7 = extract<int (B::*)(int) &&>(^^B::fn7);
-
  constexpr auto a8 = extract<int B::*>(^^B::m);
  constexpr auto a9 = extract<int const B::*>(^^B::m);
  constexpr auto a10 = extract<int* B::*>(^^B::p);
  constexpr auto a11 = extract<int* const B::*>(^^B::p);
  constexpr auto a12 = extract<int const* const B::*>(^^B::p);
-
-// FIXME removing noexcept should be allowed
-// constexpr auto a13 = extract<int (*)()>(^^B::fn4);
-// constexpr auto a14 = extract<int (B::*)()>(^^B::fn2);
+constexpr auto a13 = extract<int (*)()>(^^B::fn4);
+constexpr auto a14 = extract<int (B::*)()>(^^B::fn2);
diff --git a/gcc/testsuite/g++.dg/reflect/extract2.C 
b/gcc/testsuite/g++.dg/reflect/extract2.C
index edb5382ce39..9de23d1e931 100644
--- a/gcc/testsuite/g++.dg/reflect/extract2.C
+++ b/gcc/testsuite/g++.dg/reflect/extract2.C
@@ -130,11 +130,11 @@ struct C {
  };
  static_assert (!can_extract<void (C::*)()>(^^C::fn));
  static_assert (!can_extract<int (C::*)() noexcept>(^^C::fn));
-static_assert (!can_extract<int (C::*)()>(^^C::fn2));
+static_assert (can_extract<int (C::*)()>(^^C::fn2));
  static_assert (!can_extract<int (C::*)()>(^^C::fn3));
  static_assert (!can_extract<int (C::*)()>(^^C::fn4));
  static_assert (!can_extract<int (*)() noexcept>(^^C::fn3));
-static_assert (!can_extract<int (*)()>(^^C::fn4));
+static_assert (can_extract<int (*)()>(^^C::fn4));
  static_assert (!can_extract<int (*)()>(^^C::fn5));
  static_assert (!can_extract<int (C::*)(int) &&>(^^C::fn6));
  static_assert (!can_extract<int (C::*)(int) &>(^^C::fn7));

base-commit: 8f9034c840c2ff9063e04ba52520065c90ff56ad

Reply via email to