From patchwork Mon Mar 21 20:46:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1607915 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=x4QBVs3T; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KMmrv5p6wz9s0r for ; Tue, 22 Mar 2022 07:47:27 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 540F93945C15 for ; Mon, 21 Mar 2022 20:47:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 540F93945C15 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1647895645; bh=FqWupKTTBmvRq/PWEQdHGftkILMI+QnBazS8JJqp4zc=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=x4QBVs3T/QDIFcf9OAPBEbx1V05qvjXRIOqEo6PrYBnbHHImWFMDzyEY+ivh3Q6Qy 1lbxkLB9MGOUFikPeCYLzaHDU8Gp0bKF9v242l6pKIFzxB+LgCekSZa57NJ5/LjUoF 3MMNSzD+NySZqUIWoIwwGmAxW7kMCbDR8BNDm09o= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 441BD389EC53 for ; Mon, 21 Mar 2022 20:46:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 441BD389EC53 Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-614-H4yf3K2YMbyHqnNzjETuFA-1; Mon, 21 Mar 2022 16:46:18 -0400 X-MC-Unique: H4yf3K2YMbyHqnNzjETuFA-1 Received: by mail-qv1-f70.google.com with SMTP id x16-20020a0ce250000000b00432ec6eaf85so12391362qvl.15 for ; Mon, 21 Mar 2022 13:46:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=FqWupKTTBmvRq/PWEQdHGftkILMI+QnBazS8JJqp4zc=; b=TPaEu4CAkgOrLrnNrlQOA4ApjNhMBF7/XQ69jyqOPE2Y5kLr87dyWMLzkqeeGE3jWd gOP3HZy2cNCXAvfznrnU8IB0VgAyEARsoXqqCkCNqNZHRNYO/sF8Ynpl3BHXCJvkvMKE rIgpmHnmq+zZMQ5aZMMCv0V8SMG8L7kG+7SUzV5MDfXm2xWDzgZJVZmNcgsBNSp7NrXV lesAhMwr0sQoelwLneEAkqPqG4GP8RR6zab1OV/WLdpbkmU0wlFVE8ZCmQIsgfeUBEjY hdMrXWPx4QyBHJFfRF4MRys0V0zFvIuE7pATRkdgs7klQ4GZWMCJspLY9sfYLIjxApUP k9pA== X-Gm-Message-State: AOAM530RTd5EiclWuJwBXM2Yone3xC8G6IEbec5aLfiZI53QQvXdsrFi vmPE2blRD4HRHnGKiGdsVcgkOCRso7dUr1LyQ0YTjq0wKM2O1w9YFdycbCyo33DRANBU35ZvGqp fYCg9CvZQ8odx8BGXkvviLN4+h7HIpLA8vC15B9TpH81J6Jc3Q8ey1eV4JW7N8odg6g== X-Received: by 2002:a05:6214:29c8:b0:440:fac2:a6c with SMTP id gh8-20020a05621429c800b00440fac20a6cmr13226508qvb.96.1647895577593; Mon, 21 Mar 2022 13:46:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwos0jeoN5zG+p7aCtnIgRn17XZhAtau5jj/rOEjlAcQqoHSuiqDgWmTKKx1MVAVBLQONSaDg== X-Received: by 2002:a05:6214:29c8:b0:440:fac2:a6c with SMTP id gh8-20020a05621429c800b00440fac20a6cmr13226482qvb.96.1647895577076; Mon, 21 Mar 2022 13:46:17 -0700 (PDT) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id d126-20020a37b484000000b0067e60283d08sm4919554qkf.40.2022.03.21.13.46.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Mar 2022 13:46:16 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: designated init and aggregate members [PR102337] Date: Mon, 21 Mar 2022 16:46:14 -0400 Message-Id: <20220321204614.2409000-1-jason@redhat.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Our C++20 designated initializer handling was broken with members of class type; we would find the relevant member and then try to find a member of the member with the same name. Or we would sometimes ignore the designator entirely. The former problem is fixed by the change to reshape_init_class, the latter by the change to reshape_init_r. Tested x86_64-pc-linux-gnu, applying to trunk. PR c++/103337 PR c++/102740 PR c++/103299 PR c++/102538 gcc/cp/ChangeLog: * decl.cc (reshape_init_class): Avoid looking for designator after we found it. (reshape_init_r): Keep looking for designator. gcc/testsuite/ChangeLog: * g++.dg/ext/flexary3.C: Remove one error. * g++.dg/parse/pr43765.C: Likewise. * g++.dg/cpp2a/desig22.C: New test. * g++.dg/cpp2a/desig23.C: New test. * g++.dg/cpp2a/desig24.C: New test. * g++.dg/cpp2a/desig25.C: New test. --- gcc/cp/decl.cc | 47 +++++++++++++++++++++++++--- gcc/testsuite/g++.dg/cpp2a/desig22.C | 11 +++++++ gcc/testsuite/g++.dg/cpp2a/desig23.C | 20 ++++++++++++ gcc/testsuite/g++.dg/cpp2a/desig24.C | 11 +++++++ gcc/testsuite/g++.dg/cpp2a/desig25.C | 13 ++++++++ gcc/testsuite/g++.dg/ext/flexary3.C | 2 +- gcc/testsuite/g++.dg/parse/pr43765.C | 6 ++-- 7 files changed, 101 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/desig22.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/desig23.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/desig24.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/desig25.C base-commit: 70b8f43695b0e6fabc760d247ac83f354092b21d prerequisite-patch-id: d89344aafac30ce0b8645945beb2d94b7ddb3515 diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 375385e0013..34d9dad9fb0 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -6598,8 +6598,9 @@ reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p, { tree field_init; constructor_elt *old_cur = d->cur; + bool direct_desig = false; - /* Handle designated initializers, as an extension. */ + /* Handle C++20 designated initializers. */ if (d->cur->index) { if (d->cur->index == error_mark_node) @@ -6617,7 +6618,10 @@ reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p, } } else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE) - field = get_class_binding (type, d->cur->index); + { + field = get_class_binding (type, d->cur->index); + direct_desig = true; + } else { if (complain & tf_error) @@ -6669,6 +6673,7 @@ reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p, break; gcc_assert (aafield); field = aafield; + direct_desig = false; } } @@ -6683,9 +6688,32 @@ reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p, assumed to correspond to no elements of the initializer list. */ goto continue_; - field_init = reshape_init_r (TREE_TYPE (field), d, - /*first_initializer_p=*/NULL_TREE, - complain); + if (direct_desig) + { + /* The designated field F is initialized from this one element: + Temporarily clear the designator so a recursive reshape_init_class + doesn't try to find it again in F, and adjust d->end so we don't + try to use the next initializer to initialize another member of F. + + Note that we don't want these changes if we found the designator + inside an anon aggr above; we leave them alone to implement: + + "If the element is an anonymous union member and the initializer + list is a brace-enclosed designated- initializer-list, the element + is initialized by the designated-initializer-list { D }, where D + is the designated- initializer-clause naming a member of the + anonymous union member." */ + auto end_ = make_temp_override (d->end, d->cur + 1); + auto idx_ = make_temp_override (d->cur->index, NULL_TREE); + field_init = reshape_init_r (TREE_TYPE (field), d, + /*first_initializer_p=*/NULL_TREE, + complain); + } + else + field_init = reshape_init_r (TREE_TYPE (field), d, + /*first_initializer_p=*/NULL_TREE, + complain); + if (field_init == error_mark_node) return error_mark_node; @@ -6941,6 +6969,15 @@ reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p, to handle initialization of arrays and similar. */ else if (COMPOUND_LITERAL_P (stripped_init)) gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init)); + /* If we have an unresolved designator, we need to find the member it + designates within TYPE, so proceed to the routines below. For + FIELD_DECL or INTEGER_CST designators, we're already initializing + the designated element. */ + else if (d->cur->index + && TREE_CODE (d->cur->index) == IDENTIFIER_NODE) + /* Brace elision with designators is only permitted for anonymous + aggregates. */ + gcc_checking_assert (ANON_AGGR_TYPE_P (type)); /* A CONSTRUCTOR of the target's type is a previously digested initializer. */ else if (same_type_ignoring_top_level_qualifiers_p (type, init_type)) diff --git a/gcc/testsuite/g++.dg/cpp2a/desig22.C b/gcc/testsuite/g++.dg/cpp2a/desig22.C new file mode 100644 index 00000000000..ba083f8e3d5 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/desig22.C @@ -0,0 +1,11 @@ +// PR c++/103337 +// { dg-do compile { target c++20 } } + +struct op_t { + struct put_t { + int x; + } put; +}; + +op_t x{0}; // OK +op_t y{.put=0}; // bogus error: 'op_t::put_t' has no non-static data member named 'put' diff --git a/gcc/testsuite/g++.dg/cpp2a/desig23.C b/gcc/testsuite/g++.dg/cpp2a/desig23.C new file mode 100644 index 00000000000..4354e644f6a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/desig23.C @@ -0,0 +1,20 @@ +// PR c++/102740 +// { dg-do compile { target c++20 } } +// { dg-additional-options -Wmissing-braces } + +typedef struct { + union { + struct { + const void* content; + } put; + }; +} op_t; + +op_t f(const char* alias) { + return op_t{ + .put = + { + .content = alias, + }, + }; // { dg-warning "missing braces" } +} diff --git a/gcc/testsuite/g++.dg/cpp2a/desig24.C b/gcc/testsuite/g++.dg/cpp2a/desig24.C new file mode 100644 index 00000000000..219cc9c3b8e --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/desig24.C @@ -0,0 +1,11 @@ +// PR c++/103299 +// { dg-do compile { target c++20 } } + +struct foo { + union { + int fp1{}; + char fp2; + }; +}; + +static_assert(foo{.fp2={}}.fp2 == 0); diff --git a/gcc/testsuite/g++.dg/cpp2a/desig25.C b/gcc/testsuite/g++.dg/cpp2a/desig25.C new file mode 100644 index 00000000000..9da958c29e9 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/desig25.C @@ -0,0 +1,13 @@ +// PR c++/102538 +// { dg-do run { target c++20 } } + +struct X { union { char r8[8]; int r32[2]; }; }; +struct Y { X v[1]; }; +Y x = { { { .r32 = { 5, 6 } } } }; + +int +main () +{ + if (x.v[0].r32[0] != 5 || x.v[0].r32[1] != 6) + __builtin_abort (); +} diff --git a/gcc/testsuite/g++.dg/ext/flexary3.C b/gcc/testsuite/g++.dg/ext/flexary3.C index 34b17254f8c..8344b42dd16 100644 --- a/gcc/testsuite/g++.dg/ext/flexary3.C +++ b/gcc/testsuite/g++.dg/ext/flexary3.C @@ -16,7 +16,7 @@ struct s { int main() { - struct s s = { .c = 0 }; // { dg-error "initializer" } + struct s s = { .c = 0 }; // { dg-error "non-static initialization of a flexible array member" "" { target *-*-* } .-1 } return 0; } diff --git a/gcc/testsuite/g++.dg/parse/pr43765.C b/gcc/testsuite/g++.dg/parse/pr43765.C index 5e602204007..aa099a4d20b 100644 --- a/gcc/testsuite/g++.dg/parse/pr43765.C +++ b/gcc/testsuite/g++.dg/parse/pr43765.C @@ -12,6 +12,6 @@ SomeType vals[] = { { 0, values : temp, }, // { dg-error "either all initializer clauses should be designated or none of them should be" "" { target c++2a } } 0 - }; // { dg-error "GNU-style designated initializer for an array|cannot convert" } -// (note the error above is on the wrong line) - // { dg-error "initialization of flexible array member in a nested context" "" { target *-*-* } .-2 } + }; +// (note the error below is on the wrong line) +// { dg-error "initialization of flexible array member in a nested context" "" { target *-*-* } .-2 }