https://gcc.gnu.org/g:477a1ff545a5e0d2b49f117fae49797d5405c4f9
commit r16-7898-g477a1ff545a5e0d2b49f117fae49797d5405c4f9 Author: Jakub Jelinek <[email protected]> Date: Wed Mar 4 19:22:29 2026 +0100 c++: Find annotations in DECL_ATTRIBUTES (TYPE_NAME (r)) for type aliases On Wed, Feb 25, 2026 at 08:50:40PM +0100, Jakub Jelinek wrote: > > Sounds like the maybe_strip_typedefs is wrong, since reflection in general > > tries to preserve aliases. > > Actually the maybe_strip_typedefs call is correct, that is for the type > argument (so when it is std::meta::annotations_with_type) and the standard > says that dealias should be used > - https://eel.is/c++draft/meta.reflection#annotation-6.2 > But we probably shouldn't use TYPE_ATTRIBUTES but DECL_ATTRIBUTES (TYPE_NAME (r)) > if r is a type alias. > I'll test a patch for that separately. Here it is. 2026-03-04 Jakub Jelinek <[email protected]> PR c++/123866 * reflect.cc (eval_annotations_of): For type aliases look for annotations in DECL_ATTRIBUTES (TYPE_NAME (r)). * g++.dg/reflect/annotations11.C: New test. Diff: --- gcc/cp/reflect.cc | 7 ++++++- gcc/testsuite/g++.dg/reflect/annotations11.C | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/gcc/cp/reflect.cc b/gcc/cp/reflect.cc index 5d4cc16c67aa..6d15db8ae7da 100644 --- a/gcc/cp/reflect.cc +++ b/gcc/cp/reflect.cc @@ -3799,7 +3799,12 @@ eval_annotations_of (location_t loc, const constexpr_ctx *ctx, tree r, } } else if (TYPE_P (r)) - r = TYPE_ATTRIBUTES (r); + { + if (typedef_variant_p (r)) + r = DECL_ATTRIBUTES (TYPE_NAME (r)); + else + r = TYPE_ATTRIBUTES (r); + } else if (DECL_P (r)) r = DECL_ATTRIBUTES (r); else diff --git a/gcc/testsuite/g++.dg/reflect/annotations11.C b/gcc/testsuite/g++.dg/reflect/annotations11.C new file mode 100644 index 000000000000..6ae5e2620b85 --- /dev/null +++ b/gcc/testsuite/g++.dg/reflect/annotations11.C @@ -0,0 +1,21 @@ +// PR c++/123866 +// { dg-do compile { target c++26 } } +// { dg-additional-options "-freflection" } + +#include <meta> + +struct [[=1, =2]] A {}; +using B [[=3, =4, =5]] = A; +typedef A C [[=6, =7, =8, =9]]; +using D [[=10, =11, =12, =13, =14]] = const int; +typedef volatile int E [[=15, =16, =17, =18, =19, =20]]; + +static_assert (annotations_of (^^A).size () == 2); +static_assert (annotations_of (^^B).size () == 3); +static_assert (annotations_of (dealias (^^B)).size () == 2); +static_assert (annotations_of (^^C).size () == 4); +static_assert (annotations_of (dealias (^^C)).size () == 2); +static_assert (annotations_of (^^D).size () == 5); +static_assert (annotations_of (dealias (^^D)).size () == 0); +static_assert (annotations_of (^^E).size () == 6); +static_assert (annotations_of (dealias (^^E)).size () == 0);
