Bootstrapped/regtested on ppc64le-pc-linux-gnu, ok for trunk?
-- >8 --
In <https://gcc.gnu.org/pipermail/gcc-patches/2026-January/705756.html>
Jason suggested using cp_tree_equal for all exprs in compare_reflections.
This patch does so. We just have to handle comparing annotations and
types specially, then we can use cp_tree_equal for the rest. It just
had to be taught not to crash on unequal NAMESPACE_DECLs.
gcc/cp/ChangeLog:
* reflect.cc (compare_reflections): Handle comparing annotations
and types specially, use cp_tree_equal for the rest.
* tree.cc (cp_tree_equal) <case NAMESPACE_DECL>: New.
---
gcc/cp/reflect.cc | 33 ++++++++++++++-------------------
gcc/cp/tree.cc | 1 +
2 files changed, 15 insertions(+), 19 deletions(-)
diff --git a/gcc/cp/reflect.cc b/gcc/cp/reflect.cc
index 24b8d744810..27470ca98df 100644
--- a/gcc/cp/reflect.cc
+++ b/gcc/cp/reflect.cc
@@ -8232,6 +8232,9 @@ compare_reflections (tree lhs, tree rhs)
// ??? Can we do something better?
lhs = maybe_get_first_fn (lhs);
rhs = maybe_get_first_fn (rhs);
+
+ /* First handle reflection-specific comparisons, then fall back to
+ cp_tree_equal. */
if (lkind == REFLECT_PARM)
{
lhs = maybe_update_function_parm (lhs);
@@ -8245,27 +8248,19 @@ compare_reflections (tree lhs, tree rhs)
&& tree_int_cst_equal (TREE_VEC_ELT (lhs, 3),
TREE_VEC_ELT (rhs, 3))
&& TREE_VEC_ELT (lhs, 4) == TREE_VEC_ELT (rhs, 4));
-
- if (lhs == rhs)
- return true;
-
- /* Some trees are not shared. */
- if (TREE_CODE (lhs) == TREE_CODE (rhs))
- switch (TREE_CODE (lhs))
- {
- case ARRAY_REF:
- case COMPONENT_REF:
- case REAL_CST:
- return cp_tree_equal (lhs, rhs);
- default:
- break;
- }
-
- if (TYPE_P (lhs) && TYPE_P (rhs))
- if (!typedef_variant_p (lhs) && !typedef_variant_p (rhs))
+ else if (lkind == REFLECT_ANNOTATION)
+ return lhs == rhs;
+ else if (TYPE_P (lhs) && TYPE_P (rhs))
+ {
+ /* Given "using A = int;", "^^int != ^^A" should hold. */
+ if (typedef_variant_p (lhs) != typedef_variant_p (rhs))
+ return false;
+ /* This is for comparing function types. E.g.,
+ auto fn() -> int; type_of(^^fn) == ^^auto()->int; */
return same_type_p (lhs, rhs);
+ }
- return false;
+ return cp_tree_equal (lhs, rhs);
}
/* Return true if T is a valid splice-type-specifier.
diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc
index 2f386e16b9c..761f7d8ff3d 100644
--- a/gcc/cp/tree.cc
+++ b/gcc/cp/tree.cc
@@ -4354,6 +4354,7 @@ cp_tree_equal (tree t1, tree t2)
case SSA_NAME:
case USING_DECL:
case DEFERRED_PARSE:
+ case NAMESPACE_DECL:
return false;
case BASELINK:
base-commit: f84ed64925e1a047a685397dc696de744ac79930
--
2.52.0