https://gcc.gnu.org/g:4e56ca07b9eeb9dd765893b34c147ca398a57f71

commit r17-1925-g4e56ca07b9eeb9dd765893b34c147ca398a57f71
Author: Marek Polacek <[email protected]>
Date:   Fri Jun 26 14:09:38 2026 -0400

    c++/reflection: consteval-only type in splice-spec [PR125900]
    
    In this test in the template for the
    
      std::println("{}", static_cast<int>(s.[:member:]));
    
    line provokes a bogus "consteval-only expressions are only allowed
    in a constant-evaluated context" error.  (Not the line before
    because there we have an OVERLOAD because the second argument's
    type isn't obvious, and so the whole thing is considered type-dep.)
    
    A splice-specifier is [: constant-expression :], and [expr.const.defns]:
    an expression or conversion is manifestly constant-evaluated if it is
    a constant-expression.
    
    So I think we shouldn't walk SPLICE_EXPRs in
    check_out_of_consteval_use_r.
    
            PR c++/125900
    
    gcc/cp/ChangeLog:
    
            * reflect.cc (check_out_of_consteval_use_r): Don't walk
            SPLICE_EXPR.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/reflect/expr18.C: New test.
    
    Reviewed-by: Jason Merrill <[email protected]>

Diff:
---
 gcc/cp/reflect.cc                     |  4 ++++
 gcc/testsuite/g++.dg/reflect/expr18.C | 25 +++++++++++++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/gcc/cp/reflect.cc b/gcc/cp/reflect.cc
index 3d6d24eb23ac..73e160d700f8 100644
--- a/gcc/cp/reflect.cc
+++ b/gcc/cp/reflect.cc
@@ -8760,6 +8760,10 @@ check_out_of_consteval_use_r (tree *tp, int 
*walk_subtrees, void *pset)
       || TREE_CODE (t) == DECL_EXPR
       /* Neither into USING_STMT.  */
       || TREE_CODE (t) == USING_STMT
+      /* The operand of a splice is a constant-expression, thus
+        manifestly constant-evaluated, so consteval-only types are permitted
+        here.  */
+      || TREE_CODE (t) == SPLICE_EXPR
       /* Blocks can appear in the TREE_VEC operand of OpenMP
         depend/affinity/map/to/from OMP_CLAUSEs when using iterators.  */
       || TREE_CODE (t) == BLOCK)
diff --git a/gcc/testsuite/g++.dg/reflect/expr18.C 
b/gcc/testsuite/g++.dg/reflect/expr18.C
new file mode 100644
index 000000000000..f2ceefc6f883
--- /dev/null
+++ b/gcc/testsuite/g++.dg/reflect/expr18.C
@@ -0,0 +1,25 @@
+// PR c++/125900
+// { dg-do compile { target c++26 } }
+// { dg-additional-options "-freflection" }
+
+#include <meta>
+#include <print>
+
+struct S {
+    float f;
+};
+
+int main() {
+    S s{.f = 1.5f};
+
+    constexpr auto member{^^S::f};
+    std::println("{}", s.[:member:]);
+    std::println("{}", static_cast<int>(s.[:member:]));
+
+    constexpr auto access_context{std::meta::access_context::current()};
+    template for (constexpr auto member :
+                  std::define_static_array(nonstatic_data_members_of(^^S, 
access_context))) {
+        std::println("{}", s.[:member:]);
+        std::println("{}", static_cast<int>(s.[:member:]));
+    }
+}

Reply via email to