mirror of
git://gcc.gnu.org/git/gcc.git
synced 2025-03-18 09:00:34 +08:00
c++: designator for base class member [PR101405]
A C++20 designator must name a direct non-static member of the class; in this case it names a member of a base class, and we should give an error instead of crashing. PR c++/101405 gcc/cp/ChangeLog: * decl.cc (reshape_init_class): Reject designator for a member of another class. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/desig20.C: New test.
This commit is contained in:
parent
30b38394b4
commit
cc01cd9397
@ -6569,16 +6569,22 @@ reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p,
|
||||
tree ictx = DECL_CONTEXT (field);
|
||||
if (!same_type_ignoring_top_level_qualifiers_p (ictx, type))
|
||||
{
|
||||
gcc_assert (ANON_AGGR_TYPE_P (ictx));
|
||||
/* Find the anon aggr that is a direct member of TYPE. */
|
||||
while (true)
|
||||
while (ANON_AGGR_TYPE_P (ictx))
|
||||
{
|
||||
tree cctx = TYPE_CONTEXT (ictx);
|
||||
if (same_type_ignoring_top_level_qualifiers_p (cctx, type))
|
||||
break;
|
||||
goto found;
|
||||
ictx = cctx;
|
||||
}
|
||||
/* And then the TYPE member with that anon aggr type. */
|
||||
|
||||
/* Not found, e.g. FIELD is a member of a base class. */
|
||||
if (complain & tf_error)
|
||||
error ("%qD is not a direct member of %qT", field, type);
|
||||
return error_mark_node;
|
||||
|
||||
found:
|
||||
/* Now find the TYPE member with that anon aggr type. */
|
||||
tree aafield = TYPE_FIELDS (type);
|
||||
for (; aafield; aafield = TREE_CHAIN (aafield))
|
||||
if (TREE_TYPE (aafield) == ictx)
|
||||
|
20
gcc/testsuite/g++.dg/cpp2a/desig20.C
Normal file
20
gcc/testsuite/g++.dg/cpp2a/desig20.C
Normal file
@ -0,0 +1,20 @@
|
||||
// PR c++/101405
|
||||
// { dg-do compile { target c++20 } }
|
||||
|
||||
struct A {
|
||||
int const a = 1;
|
||||
int const b = 2;
|
||||
};
|
||||
|
||||
struct B : A {
|
||||
using A::a;
|
||||
using A::b;
|
||||
int const c = 3;
|
||||
int const d = 4;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
[[maybe_unused]] B b =
|
||||
{ .a = 10, .d = 42 }; // { dg-error "not a direct member" }
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user