From patchwork Fri Aug 25 16:44:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 136928 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a7d1:0:b0:3f2:4152:657d with SMTP id p17csp1924107vqm; Fri, 25 Aug 2023 09:45:46 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFTKi2m5QjzvrcvXduB96Z0jzg3k1Txj4OaPA0N44mhDAgJHfswSuMdKLiplMgMDmAOcpFY X-Received: by 2002:a05:6512:2fc:b0:4f9:7aee:8dc5 with SMTP id m28-20020a05651202fc00b004f97aee8dc5mr13813197lfq.19.1692981946045; Fri, 25 Aug 2023 09:45:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692981946; cv=none; d=google.com; s=arc-20160816; b=LWz23GAROmtJEqbytZzRZZ2hpe/GLphAr1Q+webL1kj4tJrqWmUkIRIMf6gJWGRMnw 2iJc3c5YgNFvMmvWXRRsEyBQ/rPLutDPYtZBdPOLpoZZLZ6sFOO73bWmXChWQPcVCAxn to0nf6mM/uZpHFm+0UGn4YfAAMCchx1jXYckTIieExUWZBQgb6/ztljpPo6kckZCdqPI h9VWqDdGQ28FgHGBVrbj1Dvk+0O+E2Gssl3PofbHtmSDWpCmwxj8gibYDqPTOHaZM5Wk tOkJNymHyK/VP1+zU+Yyw45VRXkJPWArEH9iX0pAxcXA4FrlmbeeFHtiG/E7h8TsnlyW eDDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=PtPX9mc+sTbvtkXyuxff3+LtZiSNHRs0e7ZODc+8kVY=; fh=fpCc4VkYXmQrurtjjuGMl3oKQaOAjGwXgTAjHxumZ4s=; b=yU2ZaV4kMEimpxLzUtOsjD7IUTWt/uPJAAQ/AfgVx4vt9ysQQXhhmeX1ZkD0WrGMqn f5FbyD3IRJxBIP2bW8OOBq8N7Ujm+5h7D6RKGUdfflESgNyRPqqw/34sGbgO8wxjJYYK da0eqpxD/fwTomCcZztCmp2GREMKlogSLhLAPhA14B/QcJoC7/WCMJlJGypEqoJE1lTC IRY4hej5AK6sFJGwnwdRUFb/K6LNag40UGEQTFvO9MtP9mYVLC71cSj59CrLqhEDNvdJ RDcZ0CoyYaK5ek9uWfBe0Zv3rc1WqYSdWSRCtdMb1wE6w/ujLabV0EwD1jyzFKL7qzvY wgEg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="C+7B/4GE"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id d10-20020a056402516a00b0052a3cc19f64si1359968ede.390.2023.08.25.09.45.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Aug 2023 09:45:46 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="C+7B/4GE"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BFB14385828D for ; Fri, 25 Aug 2023 16:45:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BFB14385828D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692981944; bh=PtPX9mc+sTbvtkXyuxff3+LtZiSNHRs0e7ZODc+8kVY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=C+7B/4GEwscD2+ABD2uAxyipa4iNKqQFc4k/786BtV7/9w5RZBQdVlUjB2fvYYg35 WPkA7FIPZbT5wcBpyE68QgBidiPMGK/1Aiy78x7UVas3K1Nom/vr7iHISvmOjmK6/1 z3ea8sgYBWMqXHksJa72wtgh3wx9JHx/2uspFQ0Q= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id EC3333858C53 for ; Fri, 25 Aug 2023 16:45:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EC3333858C53 Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-503-PTlA9QiLO_-8zL_TJ3jo3A-1; Fri, 25 Aug 2023 12:44:59 -0400 X-MC-Unique: PTlA9QiLO_-8zL_TJ3jo3A-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E116D3C0D841 for ; Fri, 25 Aug 2023 16:44:58 +0000 (UTC) Received: from pdp-11.lan (unknown [10.22.17.3]) by smtp.corp.redhat.com (Postfix) with ESMTP id BC39640C6F4C; Fri, 25 Aug 2023 16:44:58 +0000 (UTC) To: Jason Merrill , GCC Patches Subject: [PATCH] c++: CWG 2359, wrong copy-init with designated init [PR91319] Date: Fri, 25 Aug 2023 12:44:47 -0400 Message-ID: <20230825164447.480720-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1775220237137549043 X-GMAIL-MSGID: 1775220237137549043 Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- This CWG clarifies that designated initializer support direct-initialization. Just be careful what Note 2 in [dcl.init.aggr]/4.2 says: "If the initialization is by designated-initializer-clause, its form determines whether copy-initialization or direct-initialization is performed." Hence this patch sets CONSTRUCTOR_IS_DIRECT_INIT only when we are dealing with ".x{}", but not ".x = {}". PR c++/91319 gcc/cp/ChangeLog: * parser.cc (cp_parser_initializer_list): Set CONSTRUCTOR_IS_DIRECT_INIT when the designated initializer is of the .x{} form. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/desig30.C: New test. --- gcc/cp/parser.cc | 6 ++++++ gcc/testsuite/g++.dg/cpp2a/desig30.C | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp2a/desig30.C base-commit: 54cc21eaf5f3eb7f7a508919a086f6c8bf5c4c17 diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index eeb22e44fb4..b3d5c65b469 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -25718,6 +25718,7 @@ cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p, tree designator; tree initializer; bool clause_non_constant_p; + bool direct_p = false; location_t loc = cp_lexer_peek_token (parser->lexer)->location; /* Handle the C++20 syntax, '. id ='. */ @@ -25740,6 +25741,8 @@ cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p, if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) /* Consume the `='. */ cp_lexer_consume_token (parser->lexer); + else + direct_p = true; } /* Also, if the next token is an identifier and the following one is a colon, we are looking at the GNU designated-initializer @@ -25817,6 +25820,9 @@ cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p, if (clause_non_constant_p && non_constant_p) *non_constant_p = true; + if (TREE_CODE (initializer) == CONSTRUCTOR) + CONSTRUCTOR_IS_DIRECT_INIT (initializer) |= direct_p; + /* If we have an ellipsis, this is an initializer pack expansion. */ if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)) diff --git a/gcc/testsuite/g++.dg/cpp2a/desig30.C b/gcc/testsuite/g++.dg/cpp2a/desig30.C new file mode 100644 index 00000000000..d9292df9de2 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/desig30.C @@ -0,0 +1,22 @@ +// PR c++/91319 +// { dg-do compile { target c++20 } } + +struct X { + explicit X() { } +}; + +struct Aggr { + X x; +}; + +Aggr +f () +{ + return Aggr{.x{}}; +} + +Aggr +f2 () +{ + return Aggr{.x = {}}; // { dg-error "explicit constructor" } +}