On Sun, Feb 08, 2026 at 11:04:14AM +0900, Jason Merrill wrote:
> On 2/6/26 6:38 PM, Jakub Jelinek wrote:
> > Hi!
> > 
> > We weren't adding the DECL_SELF_REFERENCE_P TYPE_DECL to TYPE_FIELDS of
> > eval_define_aggregate created aggregates, which resulted in ICE in
> > accessible_base_p which relies on DECL_SELF_REFERENCE_P TYPE_DECL to be
> > present in base classes of other classes.
> > 
> > Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for
> > trunk?
> > 
> > 2026-02-05  Jakub Jelinek  <[email protected]>
> > 
> >     PR c++/123984
> >     * reflect.cc (eval_define_aggregate): Set TYPE_BEING_DEFINED on type
> >     after pushclass and call build_self_reference before finish_struct.
> 
> A bit odd to have the self-ref last instead of first, this seems like an
> issue for members_of ordering?

It is pushed last anyway:
      /* All TYPE_DECLs go at the end of TYPE_FIELDS.  Ordinary fields
         go at the beginning.  The reason is that
         legacy_nonfn_member_lookup searches the list in order, and we
         want a field name to override a type name so that the "struct
         stat hack" will work.  In particular:

           struct S { enum E { }; static const int E = 5; int ary[S::E]; } s;
    
         is valid.  */

      if (TREE_CODE (decl) == TYPE_DECL)
        TYPE_FIELDS (current_class_type)
          = chainon (TYPE_FIELDS (current_class_type), decl);
      else
        {
          DECL_CHAIN (decl) = TYPE_FIELDS (current_class_type);
          TYPE_FIELDS (current_class_type) = decl;
        }
given that eval_define_aggregate only puts FIELD_DECLs into the
TYPE_FIELDS chain except for this.
But sure, I can move the build_self_reference (); call to right
after TYPE_BEING_DEFINED (type) = 1; and in that case change
tree fields = NULL_TREE; to tree fields = TYPE_FIELDS (type);
too if you prefer.  And in that case maybe also remove the
gcc_assert (!TYPE_FIELDS (type)).

> >     * g++.dg/reflect/define_aggregate6.C: New test.
> > 
> > --- gcc/cp/reflect.cc.jj    2026-02-05 15:19:21.963084934 +0100
> > +++ gcc/cp/reflect.cc       2026-02-05 17:40:03.901515578 +0100
> > @@ -6081,6 +6081,7 @@ eval_define_aggregate (location_t loc, c
> >       xref_basetypes (type, NULL_TREE);
> >     pushclass (type);
> >     gcc_assert (!TYPE_FIELDS (type));
> > +  TYPE_BEING_DEFINED (type) = 1;
> >     tree fields = NULL_TREE;
> >     for (int i = 0; i < TREE_VEC_LENGTH (rvec); ++i)
> >       {
> > @@ -6121,6 +6122,7 @@ eval_define_aggregate (location_t loc, c
> >         fields = f;
> >       }
> >     TYPE_FIELDS (type) = fields;
> > +  build_self_reference ();
> >     finish_struct (type, NULL_TREE);
> >     return get_reflection_raw (loc, orig_type);
> >   }

        Jakub

Reply via email to