public inbox for gcc-cvs@sourceware.org help / color / mirror / Atom feed
From: Jason Merrill <jason@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-6779] c++: designator for base class member [PR101405] Date: Thu, 20 Jan 2022 20:24:35 +0000 (GMT) [thread overview] Message-ID: <20220120202435.7AD6D3857C77@sourceware.org> (raw) https://gcc.gnu.org/g:cc01cd9397fe99e707bcc5b98bb33d807ba610d7 commit r12-6779-gcc01cd9397fe99e707bcc5b98bb33d807ba610d7 Author: Jason Merrill <jason@redhat.com> Date: Thu Jan 20 09:18:45 2022 -0500 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. Diff: --- gcc/cp/decl.cc | 14 ++++++++++---- gcc/testsuite/g++.dg/cpp2a/desig20.C | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 1cbe9a34be0..8e5421848d1 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -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) diff --git a/gcc/testsuite/g++.dg/cpp2a/desig20.C b/gcc/testsuite/g++.dg/cpp2a/desig20.C new file mode 100644 index 00000000000..0ceda7ccabd --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/desig20.C @@ -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" } +}
reply other threads:[~2022-01-20 20:24 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20220120202435.7AD6D3857C77@sourceware.org \ --to=jason@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).