https://gcc.gnu.org/g:d941833a7f8d44ad2c445b756156d4f61dc79ab5
commit r16-7260-gd941833a7f8d44ad2c445b756156d4f61dc79ab5 Author: Marek Polacek <[email protected]> Date: Mon Feb 2 18:09:08 2026 -0500 c++/reflection: refactor compare_reflections 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. Reviewed-by: Jason Merrill <[email protected]> Diff: --- 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 5b6ad8be14b6..bc69bdad73b6 100644 --- a/gcc/cp/reflect.cc +++ b/gcc/cp/reflect.cc @@ -8230,6 +8230,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); @@ -8243,27 +8246,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 2f386e16b9cc..761f7d8ff3dc 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:
