[gcc r16-7991] c++: Use complete_type on first annotations_of argument if it is a type [PR124399]

Jakub Jelinek jakub@gcc.gnu.org
Tue Mar 10 20:27:57 GMT 2026


https://gcc.gnu.org/g:6d88d6e66e85400ed58bb7e9ad6690f249e319af

commit r16-7991-g6d88d6e66e85400ed58bb7e9ad6690f249e319af
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Tue Mar 10 21:24:33 2026 +0100

    c++: Use complete_type on first annotations_of argument if it is a type [PR124399]
    
    The following testcases FAIL (annotations12.C is directly from
    https://eel.is/c++draft/meta.reflection#annotation-4 ) because it is
    called on a reflection of class template instantiation which has been
    just looked up but not yet completed, and in that case it doesn't
    have any attributes instantiated.
    Only complete_type instantiates those.  Unlike the second argument
    of annotations_of_with_type where the standard requires is_complete_type,
    in this case it doesn't, so it is fine if one uses
    struct S;
    static_assert (annotations_of (^^S).size () == 0);
    but I'm afraid we need to instantiate the argument to get the annotations.
    
    2026-03-10  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/124399
            * reflect.cc (eval_annotations_of): Call complete_type on r if it
            is a type.
    
            * g++.dg/reflect/annotations3.C: Uncomment 3 test lines.
            * g++.dg/reflect/annotations12.C: New test.
            * g++.dg/reflect/constant_of5.C: Enable some commented out
            or #if 0ed out tests.
            * g++.dg/reflect/constant_of6.C: Remove dg-error directive.
            * g++.dg/reflect/extract6.C: Remove 2 dg-error directives.

Diff:
---
 gcc/cp/reflect.cc                            |  1 +
 gcc/testsuite/g++.dg/reflect/annotations12.C | 13 +++++++++++++
 gcc/testsuite/g++.dg/reflect/annotations3.C  |  6 +++---
 gcc/testsuite/g++.dg/reflect/constant_of5.C  |  6 +-----
 gcc/testsuite/g++.dg/reflect/constant_of6.C  |  1 -
 gcc/testsuite/g++.dg/reflect/extract6.C      |  3 +--
 6 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/gcc/cp/reflect.cc b/gcc/cp/reflect.cc
index e36548e6c386..172b0e3bc597 100644
--- a/gcc/cp/reflect.cc
+++ b/gcc/cp/reflect.cc
@@ -3800,6 +3800,7 @@ eval_annotations_of (location_t loc, const constexpr_ctx *ctx, tree r,
     }
   else if (TYPE_P (r))
     {
+      complete_type (r);
       if (typedef_variant_p (r))
 	r = DECL_ATTRIBUTES (TYPE_NAME (r));
       else
diff --git a/gcc/testsuite/g++.dg/reflect/annotations12.C b/gcc/testsuite/g++.dg/reflect/annotations12.C
new file mode 100644
index 000000000000..7fb41618e62c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/reflect/annotations12.C
@@ -0,0 +1,13 @@
+// PR c++/124399
+// { dg-do compile { target c++26 } }
+// { dg-options "-freflection" }
+
+#include <meta>
+
+template <class T>
+  struct [[=42]] D { };
+
+constexpr std::meta::info a1 = annotations_of (^^D <int>)[0];
+constexpr std::meta::info a2 = annotations_of (^^D <char>)[0];
+static_assert (a1 != a2);
+static_assert (constant_of (a1) == constant_of (a2));
diff --git a/gcc/testsuite/g++.dg/reflect/annotations3.C b/gcc/testsuite/g++.dg/reflect/annotations3.C
index 1f80d7a18512..95fecc4121d8 100644
--- a/gcc/testsuite/g++.dg/reflect/annotations3.C
+++ b/gcc/testsuite/g++.dg/reflect/annotations3.C
@@ -41,9 +41,9 @@ static_assert (type_of (a0) == ^^int);
 template <class T>
 struct [[=42]] D {};
 
-//constexpr std::meta::info a1 = annotations_of (^^D<int>)[0];
-//constexpr std::meta::info a2 = annotations_of (^^D<char>)[0];
-//static_assert (is_annotation (a1) && is_annotation (a2));
+constexpr std::meta::info a1 = annotations_of (^^D<int>)[0];
+constexpr std::meta::info a2 = annotations_of (^^D<char>)[0];
+static_assert (is_annotation (a1) && is_annotation (a2));
 
 [[=1, =2L, =3.0, =4U, =5U, =6L, =7U]] int y;
 static_assert (annotations_of (^^y).size () == 7);
diff --git a/gcc/testsuite/g++.dg/reflect/constant_of5.C b/gcc/testsuite/g++.dg/reflect/constant_of5.C
index 07187e915b02..ac21b5a78c76 100644
--- a/gcc/testsuite/g++.dg/reflect/constant_of5.C
+++ b/gcc/testsuite/g++.dg/reflect/constant_of5.C
@@ -23,15 +23,11 @@ static_assert (type_of (constant_of (annotations_of (^^S)[0])) == ^^int);
 static_assert (type_of (constant_of (annotations_of (^^S)[1])) == ^^int);
 static_assert (type_of (constant_of (annotations_of (^^S)[2])) == ^^int);
 static_assert (type_of (constant_of (annotations_of (^^S)[3])) == ^^float);
-#if 0
 template <typename> struct [[=5, =5, =6, =3.0f]] TCls {};
-// TODO Should work but we don't propagate the annotations correctly to the
-// instantiation.  See also the TODO in annotations3.C.
 static_assert (type_of (constant_of (annotations_of (^^TCls<int>)[0])) == ^^int);
 static_assert (type_of (constant_of (annotations_of (^^TCls<int>)[1])) == ^^int);
 static_assert (type_of (constant_of (annotations_of (^^TCls<int>)[2])) == ^^int);
 static_assert (type_of (constant_of (annotations_of (^^TCls<int>)[3])) == ^^float);
-#endif
 template <typename> [[=5, =5, =6, =3.0f]] void TFn();
 static_assert (type_of (constant_of (annotations_of (^^TFn<int>)[0])) == ^^int);
 static_assert (type_of (constant_of (annotations_of (^^TFn<int>)[1])) == ^^int);
@@ -57,7 +53,7 @@ template<info R>
 struct [[=[:constant_of (annotations_of (R)[0]):]]] Y {};
 
 static_assert (extract<int>(annotations_of (^^bar<^^::fn>)[0]) == 1);
-//static_assert (extract<int>(annotations_of (^^Y<^^::S>)[0]) == 3);
+static_assert (extract<int>(annotations_of (^^Y<^^::S>)[0]) == 3);
 
 struct [[=42, =42.0f]] S1;
 struct [[=42, =40]] S2;
diff --git a/gcc/testsuite/g++.dg/reflect/constant_of6.C b/gcc/testsuite/g++.dg/reflect/constant_of6.C
index 6db96c0cab1c..0b7474f0599c 100644
--- a/gcc/testsuite/g++.dg/reflect/constant_of6.C
+++ b/gcc/testsuite/g++.dg/reflect/constant_of6.C
@@ -17,4 +17,3 @@ struct [[=[:constant_of (annotations_of (R)[0]):]]] Y {};
 
 constexpr auto y = constant_of (annotations_of (^^bar<^^::fn>)[0]);
 constexpr auto z = constant_of (annotations_of (^^Y<^^::S>)[0]);
-// { dg-error "call to non-.constexpr." "" { target *-*-* } 0 }
diff --git a/gcc/testsuite/g++.dg/reflect/extract6.C b/gcc/testsuite/g++.dg/reflect/extract6.C
index 15c46b812081..95ecdc071ce9 100644
--- a/gcc/testsuite/g++.dg/reflect/extract6.C
+++ b/gcc/testsuite/g++.dg/reflect/extract6.C
@@ -15,6 +15,5 @@ template<info R>
 struct [[=[:constant_of (annotations_of (R)[0]):]]] TCls {};
 
 static_assert (extract<int>(annotations_of (^^TFn<^^::fn>)[0]) == 1);
-static_assert (extract<int>(annotations_of (^^TCls<^^::S>)[0]) == 3); // { dg-error "non-constant" }
+static_assert (extract<int>(annotations_of (^^TCls<^^::S>)[0]) == 3);
 static_assert (extract<int>(annotations_of (^^fn)[0]) == 1);
-// { dg-error "call to non-.constexpr." "" { target *-*-* } 0 }


More information about the Gcc-cvs mailing list