https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127276

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2026-09-09
             Status|UNCONFIRMED                 |ASSIGNED

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Lightly tested fix:
2026-09-09  Jakub Jelinek  <[email protected]>

        PR c++/127276
        * pt.cc (tsubst_splice_expr): Call convert_from_reference if not
        member access and op isn't a type.

        * g++.dg/reflect/pr127276.C: New test.

--- a/gcc/cp/pt.cc      2026-09-08 22:18:28.000000000 +0200
+++ b/gcc/cp/pt.cc      2026-09-09 16:42:48.315435696 +0200
@@ -17173,6 +17173,11 @@ tsubst_splice_expr (tree t, tree args, t
      certain kind of entities.  */
   if (SPLICE_EXPR_MEMBER_ACCESS_P (t))
     gcc_assert (valid_splice_for_member_access_p (op,
/*decls_only_p=*/false));
+  else if (!TYPE_P (op)
+          && (TREE_TYPE (t) == NULL_TREE || !TYPE_REF_P (TREE_TYPE (t))))
+    /* If the original type was a reference, we'll be wrapped in
+       the appropriate INDIRECT_REF.  */
+    op = convert_from_reference (op);

   return op;
 }
--- a/gcc/testsuite/g++.dg/reflect/pr127276.C   2026-09-09 16:50:57.263203336
+0200
+++ b/gcc/testsuite/g++.dg/reflect/pr127276.C   2026-09-09 16:50:38.326445643
+0200
@@ -0,0 +1,34 @@
+// PR c++/127276
+// { dg-do compile { target c++26 } }
+// { dg-additional-options "-freflection" }
+
+struct A {
+  constexpr int &operator[] (int x) { return a[x]; };
+  int a[2];
+};
+
+constexpr int
+foo (A &x, int y)
+{
+  return [: ^^x :][ [: ^^y :] ];
+}
+
+template <typename T>
+constexpr int
+bar (T &x, int y)
+{
+  return [: ^^x :][ [: ^^y :] ];
+}
+
+constexpr bool
+baz ()
+{
+  A a = { 2, 3 };
+  if (foo (a, 0) != 2 || foo (a, 1) != 3)
+    return false;
+  if (bar (a, 0) != 2 || bar (a, 1) != 3)
+    return false;
+  return true;
+}
+
+static_assert (baz ());

Reply via email to