[PATCH] c++, v2: Call check_trait_type for CPTK_STRUCTURED_BINDING_SIZE [PR127766]

Jakub Jelinek jakub@redhat.com
Fri Oct 9 15:39:45 GMT 2026


On Fri, Oct 09, 2026 at 11:00:40AM -0400, Marek Polacek wrote:
> >      case CPTK_STRUCTURED_BINDING_SIZE:
> > +      if (!check_trait_type (type1, /* kind = */ 4, complain))
> > +	return error_mark_node;
> >        return finish_structured_binding_size (loc, type1, complain);
> 
> This doesn't seem to fix the tsubst_expr part where we also call
> finish_structured_binding_size:
> 
> template <typename T> struct B { T a, b, c; };
> template <typename T>
> constexpr auto f () { return __builtin_structured_binding_size (B<T>); }
> static_assert (f<int> () == 3, "");
> 
> Maybe the completion should go to cp_decomp_size?

Or call check_trait_type also in pt.cc.  Though it is static in
semantics.cc.  Though finish_structured_binding_size is semantics.cc,
so perhaps I should call check_trait_type in finish_structured_binding_type
instead?
So far lightly tested...

2026-10-09  Jakub Jelinek  <jakub@redhat.com>

	PR c++/127766
	* semantics.cc (finish_structured_binding_size): Call check_trait_type
	with kind 4.

	* g++.dg/ext/builtin-structured-binding-size2.C: Expect different
	diagnostics.
	* g++.dg/ext/builtin-structured-binding-size7.C: New test.

--- a/gcc/cp/semantics.cc	2026-10-09 10:03:42.076300173 +0200
+++ b/gcc/cp/semantics.cc	2026-10-09 17:32:36.606505355 +0200
@@ -14578,6 +14578,8 @@ tree
 finish_structured_binding_size (location_t loc, tree type,
 				tsubst_flags_t complain)
 {
+  if (!check_trait_type (type, /* kind = */ 4, complain))
+    return error_mark_node;
   if (TYPE_REF_P (type))
     {
       if (complain & tf_error)
--- a/gcc/testsuite/g++.dg/ext/builtin-structured-binding-size2.C	2026-10-09 10:03:42.096299900 +0200
+++ b/gcc/testsuite/g++.dg/ext/builtin-structured-binding-size2.C	2026-10-09 17:32:03.450092528 +0200
@@ -21,7 +21,7 @@ template <>
 struct std::tuple_size <C> { static constexpr double value = 42; };
 template <>
 struct std::tuple_size <D> { static constexpr int value = -1; };
-int a = __builtin_structured_binding_size (A);			// { dg-error "structured binding refers to incomplete class type 'A'" }
+int a = __builtin_structured_binding_size (A);			// { dg-error "invalid use of incomplete type 'struct A'" }
 int b = __builtin_structured_binding_size (A &);		// { dg-error "'__builtin_structured_binding_size' argument 'A\\\&' is a reference" }
 int c = __builtin_structured_binding_size (B[]);		// { dg-error "cannot decompose array of unknown bound 'B \\\[\\\]'" }
 int d = __builtin_structured_binding_size (C);			// { dg-error "'std::tuple_size<C>::value' is not an integral constant expression" }
--- a/gcc/testsuite/g++.dg/ext/builtin-structured-binding-size7.C	2026-10-09 17:32:03.450277119 +0200
+++ b/gcc/testsuite/g++.dg/ext/builtin-structured-binding-size7.C	2026-10-09 17:37:37.900424320 +0200
@@ -0,0 +1,10 @@
+// PR c++/127766
+// { dg-do compile { target c++11 } }
+
+template <typename T> struct A { T a, b; };
+static_assert (__builtin_structured_binding_size (A <int>) == 2, "");
+
+template <typename T> struct B { T a, b, c; };
+template <typename T>
+constexpr int foo () { return __builtin_structured_binding_size (B<T>); }
+static_assert (foo <int> () == 3, "");


	Jakub



More information about the Gcc-patches mailing list