https://gcc.gnu.org/g:eca81a88ed151269a7a1aa7c37c4036995ded6e4

commit r17-5820-geca81a88ed151269a7a1aa7c37c4036995ded6e4
Author: Marek Polacek <[email protected]>
Date:   Thu Oct 8 16:35:18 2026 -0400

    c++/reflection: detect more invalid splice-exprs [PR125631]
    
    [expr.prim.splice]/2 doesn't actually allow templates in
    a splice-expression of the form splice-specifier, but we are not
    currently detecting that in check_splice_expr, which can mean
    that we crash if the user attempts to use such a splice.
    
    Also, [expr.prim.splice]/4 allows only function and variable templates
    in the template splice-specialization-specifier form.
    
    As an extension, we allow function templates, and assume that the
    user forgot to type 'template'.
    
    mangle3.C was testing invalid cases so replace them with something
    valid.
    
    This also fixes c++/127802, missing missing_template_diag for dependent
    splices.
    
            PR c++/127802
            PR c++/125631
    
    gcc/cp/ChangeLog:
    
            * cp-tree.h (missing_template_diag): Declare.
            * parser.cc (missing_template_diag): No longer static.
            (cp_parser_splice_expression): Don't call missing_template_diag
            here, but call it...
            * reflect.cc (check_splice_expr): ...here.  Reject (non-function)
            templates.  When template arguments are present, reject
            templates other than function and variable templates.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/reflect/crash6.C: Adjust dg-error.
            * g++.dg/reflect/mangle3.C: Remove some invalid tests and
            replace them with valid tests.
            * g++.dg/reflect/member22.C: Adjust dg-error.  Add more tests.
            * g++.dg/reflect/splice15.C: Add dg-warning.
            * g++.dg/reflect/splice18.C: New test.
            * g++.dg/reflect/diag7.C: New test.
            * g++.dg/reflect/error15.C: New test.
    
    Reviewed-by: Jason Merrill <[email protected]>

Diff:
---
 gcc/cp/cp-tree.h                        |   3 +
 gcc/cp/parser.cc                        |  14 +----
 gcc/cp/reflect.cc                       |  38 ++++++++++++
 gcc/testsuite/g++.dg/reflect/crash6.C   |   4 +-
 gcc/testsuite/g++.dg/reflect/diag7.C    |  27 +++++++++
 gcc/testsuite/g++.dg/reflect/error15.C  | 100 ++++++++++++++++++++++++++++++++
 gcc/testsuite/g++.dg/reflect/mangle3.C  |  13 +++--
 gcc/testsuite/g++.dg/reflect/member22.C |   7 ++-
 gcc/testsuite/g++.dg/reflect/splice15.C |   1 +
 gcc/testsuite/g++.dg/reflect/splice18.C |  27 +++++++++
 10 files changed, 211 insertions(+), 23 deletions(-)

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 73fd3e8aa7a0..f87d14006404 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -2439,6 +2439,9 @@ struct GTY(()) language_function {
 
 /* In parser.cc.  */
 extern tree cp_literal_operator_id (const char *);
+extern void missing_template_diag (location_t,
+                                  diagnostics::kind
+                                  = diagnostics::kind::warning);
 
 #define NON_ERROR(NODE) ((NODE) == error_mark_node ? NULL_TREE : (NODE))
 
diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index b30d3e7f6755..28eb38cd7803 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -279,8 +279,6 @@ static tree omp_start_variant_function
 static int omp_finish_variant_function
   (cp_parser *, tree, tree, tree, bool, bool);
 static void maybe_start_implicit_template (cp_parser *parser);
-static void missing_template_diag
-  (location_t, diagnostics::kind = diagnostics::kind::warning);
 
 /* Manifest constants.  */
 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
@@ -6440,16 +6438,6 @@ cp_parser_splice_expression (cp_parser *parser, bool 
template_p,
                          /*complain=*/true))
     return error_mark_node;
 
-  if (!template_p
-      /* No 'template' but there were template arguments?  */
-      && (targs_p
-         /* No 'template' but the splice-specifier designates a function
-            template?  */
-         || reflection_function_template_p (t))
-      && warning_enabled_at (loc, OPT_Wmissing_template_keyword))
-    /* Were 'template' present, this would be valid code, so keep going.  */
-    missing_template_diag (loc, diagnostics::kind::pedwarn);
-
   cp_unevaluated u;
 
   /* When doing foo.[: bar :], cp_parser_postfix_dot_deref_expression wants
@@ -7299,7 +7287,7 @@ cp_parser_primary_expression (cp_parser *parser,
 /* Complain about missing template keyword when naming a dependent
    member template.  */
 
-static void
+void
 missing_template_diag (location_t loc,
                       diagnostics::kind diag_kind/*=kind::warning*/)
 {
diff --git a/gcc/cp/reflect.cc b/gcc/cp/reflect.cc
index 7dcd56c4f564..9cc200523ab7 100644
--- a/gcc/cp/reflect.cc
+++ b/gcc/cp/reflect.cc
@@ -9375,6 +9375,33 @@ check_splice_expr (location_t loc, location_t start_loc, 
tree t,
       return false;
     }
 
+  /* [expr.prim.splice]/2 doesn't allow for the splice-expression of the form
+     splice-specifier to designate a template.  But we accept a function
+     template here as an extension, because it would be valid if the 'template'
+     keyword were present.  */
+  if (!template_p
+      && !targs_p
+      && TREE_CODE (t) == TEMPLATE_DECL
+      && !DECL_FUNCTION_TEMPLATE_P (t))
+    {
+      if (complain_p)
+       error_at (loc, "cannot use template %qE in a splice expression", t);
+      return false;
+    }
+
+  /* [expr.prim.splice]/4 allows only function and variable templates in the
+     template splice-specialization-specifier form.  */
+  if (targs_p
+      && TREE_CODE (t) == TEMPLATE_DECL
+      && !DECL_FUNCTION_TEMPLATE_P (t)
+      && !variable_template_p (t))
+    {
+      if (complain_p)
+       error_at (loc, "expected a reflection of a function or variable "
+                 "template");
+      return false;
+    }
+
   /* [expr.prim.splice]/2: "The expression is ill-formed if S [the construct
      designated by splice-specifier] is
      -- a local entity such that there is a lambda scope that intervenes
@@ -9467,6 +9494,17 @@ check_splice_expr (location_t loc, location_t start_loc, 
tree t,
                           || dependent_splice_p (t));
     }
 
+  if (complain_p
+      && !template_p
+      /* No 'template' but there were template arguments?  */
+      && (targs_p
+         /* No 'template' but the splice-specifier designates a function
+            template?  */
+         || reflection_function_template_p (t))
+      && warning_enabled_at (loc, OPT_Wmissing_template_keyword))
+    /* Were 'template' present, this would be valid code, so keep going.  */
+    missing_template_diag (loc, diagnostics::kind::pedwarn);
+
   return true;
 }
 
diff --git a/gcc/testsuite/g++.dg/reflect/crash6.C 
b/gcc/testsuite/g++.dg/reflect/crash6.C
index a455a61cd65b..4dd2f5fc7b87 100644
--- a/gcc/testsuite/g++.dg/reflect/crash6.C
+++ b/gcc/testsuite/g++.dg/reflect/crash6.C
@@ -10,8 +10,8 @@ template<info R>
 void
 f ()
 {
-  [:R:] r; // { dg-error "expected" }
-  [:R:]<int> r; // { dg-error "expected" }
+  [:R:] r; // { dg-error "expected|cannot use template" }
+  [:R:]<int> r; // { dg-error "expected|cannot use template" }
 }
 
 void
diff --git a/gcc/testsuite/g++.dg/reflect/diag7.C 
b/gcc/testsuite/g++.dg/reflect/diag7.C
new file mode 100644
index 000000000000..3c55be991a50
--- /dev/null
+++ b/gcc/testsuite/g++.dg/reflect/diag7.C
@@ -0,0 +1,27 @@
+// PR c++/127802
+// { dg-do compile { target c++26 } }
+// { dg-additional-options "-freflection" }
+
+template<typename T> int fn (T) { return 0; }
+
+int i = [:^^fn:](0); // { dg-error "expected .template. keyword before 
dependent template name" }
+
+template<auto R>
+int
+f ()
+{
+  return [:R:](0);  // { dg-error "expected .template. keyword before 
dependent template name" }
+}
+
+template<auto>
+void
+uninst ()
+{
+  [:^^fn:]<int>;  // { dg-error "expected .template. keyword before dependent 
template name" }
+}
+
+void
+g ()
+{
+  f<^^fn> ();
+}
diff --git a/gcc/testsuite/g++.dg/reflect/error15.C 
b/gcc/testsuite/g++.dg/reflect/error15.C
new file mode 100644
index 000000000000..a42282b99a66
--- /dev/null
+++ b/gcc/testsuite/g++.dg/reflect/error15.C
@@ -0,0 +1,100 @@
+// PR c++/125631
+// { dg-do compile { target c++26 } }
+// { dg-additional-options "-freflection" }
+
+#include <tuple>
+
+template<auto Templ, class Tuple>
+const auto
+make_from_tuple (Tuple&& t)
+{
+  return [:Templ:]{}; // { dg-error "expected|cannot use template .std::tuple. 
in a splice expression" }
+}
+auto p = make_from_tuple<^^std::tuple>(std::tuple());
+
+template<auto N> int tv;
+template<auto N> concept C = requires { true; };
+template<typename T> struct S { };
+template<typename T>
+using A = S<T>;
+template<typename> using I = int;
+
+auto a1 = [:^^tv:];   // { dg-error "cannot use template .tv<N>. in a splice 
expression" }
+auto a2 = [:^^C:];    // { dg-error "cannot use template .C. in a splice 
expression" }
+auto a3 = [:^^S:];    // { dg-error "cannot use template .S. in a splice 
expression" }
+auto a4 = [:^^A:];    // { dg-error "cannot use template .A. in a splice 
expression" }
+auto a5 = [:^^I:];    // { dg-error "cannot use template .I. in a splice 
expression" }
+auto a6 = [:^^C:]<int>;        // { dg-error "expected a reflection of a 
function or variable template" }
+auto a7 = template [:^^C:]<int>;    // { dg-error "expected a reflection of a 
function or variable template" }
+
+template<auto R>
+void
+g1 ()
+{
+  auto a = [:R:];  // { dg-error "cannot use template .tv<N>. in a splice 
expression" }
+}
+
+template<auto R>
+void
+g2 ()
+{
+  auto a = [:R:];  // { dg-error "cannot use template .C. in a splice 
expression" }
+}
+
+template<auto R>
+void
+g3 ()
+{
+  auto a = [:R:];  // { dg-error "cannot use template .S. in a splice 
expression" }
+}
+
+template<auto R>
+void
+g4 ()
+{
+  auto a = [:R:];  // { dg-error "cannot use template .A. in a splice 
expression" }
+}
+
+template<auto R>
+void
+g5 ()
+{
+  auto a = [:R:];  // { dg-error "cannot use template .I. in a splice 
expression" }
+}
+
+template<template<class> class TT>
+void
+g6 ()
+{
+  auto x = [:^^TT:];  // { dg-error "cannot use template .S. in a splice 
expression" }
+}
+
+template<auto R>
+void
+g7 ()
+{
+  &[:R:];  // { dg-error "cannot use template .S. in a splice expression" }
+}
+
+template<auto R> auto g8 () -> decltype([:R:]);        // { dg-error "cannot 
use template .tv<N>. in a splice expression" }
+
+template<auto R>
+void
+g9 ()
+{
+  auto a = template [:R:]<int>;  // { dg-error "expected a reflection of a 
function or variable template" }
+}
+
+void
+h ()
+{
+  g1<^^tv>();
+  g2<^^C>();
+  g3<^^S>();
+  g4<^^A>();
+  g5<^^I>();
+  g6<S>();
+  g7<^^S>();
+  g8<^^tv>(); // { dg-error "no matching function" }
+  g9<^^C>();
+}
diff --git a/gcc/testsuite/g++.dg/reflect/mangle3.C 
b/gcc/testsuite/g++.dg/reflect/mangle3.C
index 390f249fb0c9..97336886155d 100644
--- a/gcc/testsuite/g++.dg/reflect/mangle3.C
+++ b/gcc/testsuite/g++.dg/reflect/mangle3.C
@@ -137,6 +137,9 @@ constexpr auto f27 (typename [:R:]::Alias x) { return x; }
 template<info M>
 constexpr auto f28 () -> decltype(S{}.[:M:].i) { return 42; }
 
+template<info R>
+void f29 () { }
+
 void
 g (int p)
 {
@@ -160,18 +163,12 @@ g (int p)
 // { dg-final { scan-assembler "_Z2f4ILDmsb2a1EE4TClsIXszDST_EEEv" } }
   f4<^^A>();
 // { dg-final { scan-assembler "_Z2f4ILDmen4Enum1AEE4TClsIXszDST_EEEv" } }
-  f4<^^TVar>();
-// { dg-final { scan-assembler "_Z2f4ILDmvt4TVarEE4TClsIXszDST_EEEv" } }
   f5<^^i>();
 // { dg-final { scan-assembler "_Z2f5ILDmvr1iEEDtDST_EEv" } }
   f5<^^a1>();
 // { dg-final { scan-assembler "_Z2f5ILDmsb2a1EEDtDST_EEv" } }
   f5<^^A>();
 // { dg-final { scan-assembler "_Z2f5ILDmen4Enum1AEEDtDST_EEv" } }
-  f5<^^TVar>();
-// { dg-final { scan-assembler "_Z2f5ILDmvt4TVarEEDtDST_EEv" } }
-  f5<^^Concept>();
-// { dg-final { scan-assembler "_Z2f5ILDmco7ConceptEEDtDST_EEv" } }
   f5<^^p>();
 // { dg-final { scan-assembler "_Z2f5ILDmvrZ1giE1pEEDtDST_EEv" } }
   f5<std::meta::reflect_constant (42)>();
@@ -239,4 +236,8 @@ g (int p)
 // { dg-final { scan-assembler "_Z3f27ILDmgsEEDaNDST_E5AliasE" } }
   f28<bases_of (^^S, ctx)[0]>();
 // { dg-final { scan-assembler "_Z3f28ILDmba_1SEEDtdtdttlS0_EDST_E1iEv" } }
+  f29<^^TVar>();
+// { dg-final { scan-assembler "_Z3f29ILDmvt4TVarEEvv" } }
+  f29<^^Concept>();
+// { dg-final { scan-assembler "_Z3f29ILDmco7ConceptEEvv" } }
 }
diff --git a/gcc/testsuite/g++.dg/reflect/member22.C 
b/gcc/testsuite/g++.dg/reflect/member22.C
index 8267e1846700..43f391d42ddc 100644
--- a/gcc/testsuite/g++.dg/reflect/member22.C
+++ b/gcc/testsuite/g++.dg/reflect/member22.C
@@ -7,6 +7,8 @@ struct S {
   struct tt {};
   template<typename T>
   using tu = tt<T>;
+  template<typename T>
+  static const int vt = 5;
 };
 int x;
 void f() {}
@@ -17,7 +19,7 @@ struct tt {};
 template<typename>
 void tf() {}
 
-template<typename>
+template<typename T>
 int tv = 5;
 
 template<typename T>
@@ -30,8 +32,9 @@ int main() {
   s.[:^^f:];           // { dg-error "not a member of .S." }
   s.[:^^tt:];          // { dg-error "invalid class member access of type 
template .tt." }
   s.template [:^^tf:]; // { dg-error "not a member of .S." }
-  s.[:^^tv:];          // { dg-error "not a member of .S." }
+  s.[:^^tv:];          // { dg-error "cannot use template" }
   s.[:^^tu:];          // { dg-error "invalid class member access of type 
template .tu." }
   s.[:^^S::tt:];       // { dg-error "invalid class member access of type 
template .S::tt." }
   s.[:^^S::tu:];       // { dg-error "invalid class member access of type 
template .S::tu." }
+  s.[:^^S::vt:];       // { dg-error "cannot use template .S::vt<T>. in a 
splice expression" }
 }
diff --git a/gcc/testsuite/g++.dg/reflect/splice15.C 
b/gcc/testsuite/g++.dg/reflect/splice15.C
index f72e3f83373b..ec1e02169a10 100644
--- a/gcc/testsuite/g++.dg/reflect/splice15.C
+++ b/gcc/testsuite/g++.dg/reflect/splice15.C
@@ -22,6 +22,7 @@ template <auto r> void (C::*tp2)(int) = &template[:r:];
 template <auto r> void (C::*tp3)(int) = template[:r:]; // { dg-error "cannot 
implicitly reference a class member" }
 /* tp4 and tp5 intentionally omitted as they are not applicable in a template 
context */
 template <auto r> void (C::*tp6)(int) = &[:r:];
+// { dg-warning "expected .template. keyword" "" { target *-*-* } .-1 }
 template <auto r> void (C::*tp7)(int) = [:r:]; // { dg-error "cannot 
implicitly reference a class member" }
 template <auto r> void (C::*tp0n)(int) = &template[:r:]<int>; // { dg-error 
"no matches converting function" }
 template <auto r> void (C::*tp1n)(int) = template[:r:]<int>; // { dg-error 
"cannot implicitly reference a class member" }
diff --git a/gcc/testsuite/g++.dg/reflect/splice18.C 
b/gcc/testsuite/g++.dg/reflect/splice18.C
new file mode 100644
index 000000000000..4cbe5cfa220e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/reflect/splice18.C
@@ -0,0 +1,27 @@
+// PR c++/125631
+// { dg-do compile { target c++26 } }
+// { dg-additional-options "-freflection" }
+
+template<auto N> int tv = N;
+struct S { template<int N> static constexpr int vt = N; };
+
+int v1 = template [:^^tv:]<0>;
+constexpr int v2 = template [:^^S::vt:]<1>;
+static_assert (v2 == 1);
+
+template<auto R>
+int
+g ()
+{
+  return template [:R:]<2>;
+}
+
+template<auto R>
+constexpr int
+h ()
+{
+  return template [:R:]<3>;
+}
+
+int v3 = g<^^tv>();
+static_assert (h<^^S::vt>() == 3);

Reply via email to